nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at devShellTools-shell 233 lines 7.2 kB view raw
1{ 2 lib, 3 buildPackages, 4 callPackage, 5 pkgs, 6 pkgsLinux, 7 8 diffoscopeMinimal, 9 runCommand, 10 runCommandWith, 11 stdenv, 12 stdenvNoCC, 13 replaceVars, 14 testers, 15}: 16# Documentation is in doc/build-helpers/testers.chapter.md 17{ 18 # See https://nixos.org/manual/nixpkgs/unstable/#tester-lycheeLinkCheck 19 # or doc/build-helpers/testers.chapter.md 20 inherit (callPackage ./lychee.nix { }) lycheeLinkCheck; 21 22 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testBuildFailure 23 # or doc/build-helpers/testers.chapter.md 24 testBuildFailure = 25 drv: 26 drv.overrideAttrs (orig: { 27 builder = buildPackages.bash; 28 args = [ 29 (replaceVars ./expect-failure.sh { 30 coreutils = buildPackages.coreutils; 31 vars = lib.toShellVars { 32 outputNames = (orig.outputs or [ "out" ]); 33 }; 34 }) 35 orig.realBuilder or stdenv.shell 36 ] 37 ++ orig.args or [ 38 "-e" 39 ../../stdenv/generic/source-stdenv.sh 40 (orig.builder or ../../stdenv/generic/default-builder.sh) 41 ]; 42 }); 43 44 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testBuildFailurePrime 45 # or doc/build-helpers/testers.chapter.md 46 testBuildFailure' = callPackage ./testBuildFailurePrime { }; 47 48 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testEqualDerivation 49 # or doc/build-helpers/testers.chapter.md 50 testEqualDerivation = callPackage ./test-equal-derivation.nix { }; 51 52 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testEqualContents 53 # or doc/build-helpers/testers.chapter.md 54 testEqualContents = 55 { 56 assertion, 57 actual, 58 expected, 59 }: 60 runCommand "equal-contents-${lib.strings.toLower assertion}" 61 { 62 inherit assertion actual expected; 63 nativeBuildInputs = [ diffoscopeMinimal ]; 64 } 65 '' 66 echo "Checking:" 67 printf '%s\n' "$assertion" 68 if ! diffoscope --no-progress --text-color=always --exclude-directory-metadata=no -- "$actual" "$expected" 69 then 70 echo 71 echo 'Contents must be equal, but were not!' 72 echo 73 echo "+: expected, at $expected" 74 echo "-: unexpected, at $actual" 75 false 76 else 77 echo "expected $expected and actual $actual match." 78 echo OK 79 touch -- "$out" 80 fi 81 ''; 82 83 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testEqualArrayOrMap 84 # or doc/build-helpers/testers.chapter.md 85 testEqualArrayOrMap = callPackage ./testEqualArrayOrMap { }; 86 87 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testVersion 88 # or doc/build-helpers/testers.chapter.md 89 testVersion = 90 { 91 package, 92 command ? "${package.meta.mainProgram or package.pname or package.name} --version", 93 version ? package.version, 94 }: 95 runCommand "${package.name}-test-version" 96 { 97 nativeBuildInputs = [ package ]; 98 meta.timeout = 60; 99 } 100 '' 101 if output=$(${command} 2>&1 | sed -e 's|${builtins.storeDir}/[^/ ]*/|{{storeDir}}/|g'); then 102 if grep -Fw -- "${version}" - <<< "$output"; then 103 touch $out 104 else 105 echo "Version string '${version}' not found!" >&2 106 echo "The output was:" >&2 107 echo "$output" >&2 108 exit 1 109 fi 110 else 111 echo -n ${lib.escapeShellArg command} >&2 112 echo " returned a non-zero exit code." >&2 113 echo "$output" >&2 114 exit 1 115 fi 116 ''; 117 118 # See https://nixos.org/manual/nixpkgs/unstable/#tester-invalidateFetcherByDrvHash 119 # or doc/build-helpers/testers.chapter.md 120 invalidateFetcherByDrvHash = 121 f: args: 122 let 123 drvPath = (f args).drvPath; 124 # It's safe to discard the context, because we don't access the path. 125 salt = builtins.unsafeDiscardStringContext (lib.substring 0 12 (baseNameOf drvPath)); 126 # New derivation incorporating the original drv hash in the name 127 salted = f (args // { name = "${args.name or "source"}-salted-${salt}"; }); 128 # Make sure we did change the derivation. If the fetcher ignores `name`, 129 # `invalidateFetcherByDrvHash` doesn't work. 130 checked = 131 if salted.drvPath == drvPath then 132 throw "invalidateFetcherByDrvHash: Adding the derivation hash to the fixed-output derivation name had no effect. Make sure the fetcher's name argument ends up in the derivation name. Otherwise, the fetcher will not be re-run when its implementation changes. This is important for testing." 133 else 134 salted; 135 in 136 checked; 137 138 # See https://nixos.org/manual/nixpkgs/unstable/#tester-runCommand 139 runCommand = testers.invalidateFetcherByDrvHash ( 140 { 141 hash ? pkgs.emptyFile.outputHash, 142 name, 143 script, 144 stdenv ? stdenvNoCC, 145 ... 146 }@args: 147 148 runCommandWith { 149 inherit name stdenv; 150 151 derivationArgs = { 152 outputHash = hash; 153 outputHashMode = "recursive"; 154 } 155 // lib.removeAttrs args [ 156 "hash" 157 "name" 158 "script" 159 "stdenv" 160 ]; 161 } script 162 ); 163 164 # See https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest 165 # or doc/build-helpers/testers.chapter.md 166 runNixOSTest = 167 let 168 nixos = import ../../../nixos/lib { 169 inherit lib; 170 }; 171 in 172 testModule: 173 nixos.runTest { 174 _file = "pkgs.runNixOSTest implementation"; 175 imports = [ 176 (lib.setDefaultModuleLocation "the argument that was passed to pkgs.runNixOSTest" testModule) 177 ]; 178 hostPkgs = pkgs; 179 node.pkgs = pkgsLinux; 180 }; 181 182 # See https://nixos.org/manual/nixpkgs/unstable/#tester-invalidateFetcherByDrvHash 183 # or doc/build-helpers/testers.chapter.md 184 nixosTest = 185 let 186 /* 187 The nixos/lib/testing-python.nix module, preapplied with arguments that 188 make sense for this evaluation of Nixpkgs. 189 */ 190 nixosTesting = ( 191 import ../../../nixos/lib/testing-python.nix { 192 inherit (stdenv.hostPlatform) system; 193 inherit pkgs; 194 extraConfigurations = [ 195 ( 196 { lib, ... }: 197 { 198 config.nixpkgs.pkgs = lib.mkDefault pkgsLinux; 199 } 200 ) 201 ]; 202 } 203 ); 204 in 205 test: 206 let 207 loadedTest = if builtins.typeOf test == "path" then import test else test; 208 calledTest = lib.toFunction loadedTest pkgs; 209 in 210 nixosTesting.simpleTest calledTest; 211 212 hasPkgConfigModule = 213 { moduleName, ... }@args: 214 lib.warn 215 "testers.hasPkgConfigModule has been deprecated in favor of testers.hasPkgConfigModules. It accepts a list of strings via the moduleNames argument instead of a single moduleName." 216 ( 217 testers.hasPkgConfigModules ( 218 builtins.removeAttrs args [ "moduleName" ] 219 // { 220 moduleNames = [ moduleName ]; 221 } 222 ) 223 ); 224 hasPkgConfigModules = callPackage ./hasPkgConfigModules/tester.nix { }; 225 226 hasCmakeConfigModules = callPackage ./hasCmakeConfigModules/tester.nix { }; 227 228 testMetaPkgConfig = callPackage ./testMetaPkgConfig/tester.nix { }; 229 230 shellcheck = callPackage ./shellcheck/tester.nix { }; 231 232 shfmt = callPackage ./shfmt { }; 233}