Skip to content
Snippets Groups Projects
tests.nix 2.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • let
      fixArgs = a: builtins.removeAttrs a [
        # Make sure all the args tests.nix accepts but default.nix does not are
        # listed here so we don't try to forward them to default.nix
        "privatestorage"
        "hypothesisProfile"
        "collectCoverage"
        "testSuite"
        "trialArgs"
      ];
    in
    { privatestorage ? import ./. (fixArgs args)
    
    Tom Prince's avatar
    Tom Prince committed
    , hypothesisProfile ? null
    , collectCoverage ? false
    , testSuite ? null
    , trialArgs ? null
    
    # accept any other arguments to be passed on to default.nix
    
    Jean-Paul Calderone's avatar
    Jean-Paul Calderone committed
    , ...
    }@args:
    let
        inherit (privatestorage) pkgs mach-nix zkapauthorizer;
    
    Tom Prince's avatar
    Tom Prince committed
        inherit (pkgs) lib;
    
    Tom Prince's avatar
    Tom Prince committed
        hypothesisProfile' = if hypothesisProfile == null then "default" else hypothesisProfile;
    
        defaultTrialArgs = [ "--rterrors" ] ++ (lib.optional (! collectCoverage) "--jobs=$(($NIX_BUILD_CORES > 8 ? 8 : $NIX_BUILD_CORES))");
    
    Tom Prince's avatar
    Tom Prince committed
        trialArgs' = if trialArgs == null then defaultTrialArgs else trialArgs;
        extraTrialArgs = builtins.concatStringsSep " " trialArgs';
        testSuite' = if testSuite == null then "_zkapauthorizer" else testSuite;
    
    Tom Prince's avatar
    Tom Prince committed
        python = mach-nix.mkPython {
          inherit (zkapauthorizer.meta.mach-nix) python providers;
          requirements =
    
    Tom Prince's avatar
    Tom Prince committed
            builtins.readFile ./requirements/test.in;
    
    Tom Prince's avatar
    Tom Prince committed
          packagesExtra = [ zkapauthorizer ];
          _.hypothesis.postUnpack = "";
        };
    
    
        lint-python = mach-nix.mkPython {
    
          # Pin these narrowly so that lint rules only change when we specifically
          # want them to.
    
          requirements = ''
    
            isort == 5.10.1
            black == 21.12b0
            flake8 == 4.0.1
    
    Jean-Paul Calderone's avatar
    Jean-Paul Calderone committed
    
        tests = pkgs.runCommand "zkapauthorizer-tests" {
    
    Tom Prince's avatar
    Tom Prince committed
          passthru = {
            inherit python;
          };
        } ''
    
    Tom Prince's avatar
    Tom Prince committed
          mkdir -p $out
    
    
    Tom Prince's avatar
    Tom Prince committed
          pushd ${zkapauthorizer.src}
    
          ${lint-python}/bin/black --check src
          ${lint-python}/bin/isort --check src
    
          ${lint-python}/bin/flake8 src
    
    Tom Prince's avatar
    Tom Prince committed
          popd
    
    Tom Prince's avatar
    Tom Prince committed
          ZKAPAUTHORIZER_HYPOTHESIS_PROFILE=${hypothesisProfile'} ${python}/bin/python -m ${if collectCoverage
    
    Tom Prince's avatar
    Tom Prince committed
            then "coverage run --debug=config --rcfile=${zkapauthorizer.src}/.coveragerc --module"
            else ""
          } twisted.trial ${extraTrialArgs} ${testSuite'}
    
    Tom Prince's avatar
    Tom Prince committed
    
    
    Tom Prince's avatar
    Tom Prince committed
          ${lib.optionalString collectCoverage
    
    Tom Prince's avatar
    Tom Prince committed
            ''
              mkdir -p "$out/coverage"
              cp -v .coverage.* "$out/coverage"
            ''
          }
    
    Jean-Paul Calderone's avatar
    Jean-Paul Calderone committed
        '';
    in
    {
    
      inherit privatestorage lint-python tests;
    
    Jean-Paul Calderone's avatar
    Jean-Paul Calderone committed
    }