nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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 postFailureMessage ? null,
60 checkMetadata ? true,
61 }:
62 runCommand "equal-contents-${lib.strings.toLower assertion}"
63 {
64 inherit
65 assertion
66 actual
67 expected
68 postFailureMessage
69 ;
70 excludeMetadata = if checkMetadata then "no" else "yes";
71 nativeBuildInputs = [ diffoscopeMinimal ];
72 }
73 ''
74 echo "Checking:"
75 printf '%s\n' "$assertion"
76 if ! diffoscope --no-progress --text-color=always --exclude-directory-metadata="$excludeMetadata" -- "$actual" "$expected"
77 then
78 echo
79 echo 'Contents must be equal, but were not!'
80 if [[ -n "''${postFailureMessage:-}" ]]; then
81 echo
82 echo "$postFailureMessage"
83 fi
84 echo
85 echo "+: expected, at $expected"
86 echo "-: unexpected, at $actual"
87 false
88 else
89 echo "expected $expected and actual $actual match."
90 echo OK
91 touch -- "$out"
92 fi
93 '';
94
95 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testEqualArrayOrMap
96 # or doc/build-helpers/testers.chapter.md
97 testEqualArrayOrMap = callPackage ./testEqualArrayOrMap { };
98
99 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testVersion
100 # or doc/build-helpers/testers.chapter.md
101 testVersion =
102 {
103 package,
104 command ? "${package.meta.mainProgram or package.pname or package.name} --version",
105 version ? package.version,
106 }:
107 runCommand "${package.name}-test-version"
108 {
109 nativeBuildInputs = [ package ];
110 meta.timeout = 60;
111 }
112 ''
113 if output=$(${command} 2>&1 | sed -e 's|${builtins.storeDir}/[^/ ]*/|{{storeDir}}/|g'); then
114 if grep -Fw -- "${version}" - <<< "$output"; then
115 touch $out
116 else
117 echo "Version string '${version}' not found!" >&2
118 echo "The output was:" >&2
119 echo "$output" >&2
120 exit 1
121 fi
122 else
123 echo -n ${lib.escapeShellArg command} >&2
124 echo " returned a non-zero exit code." >&2
125 echo "$output" >&2
126 exit 1
127 fi
128 '';
129
130 # See https://nixos.org/manual/nixpkgs/unstable/#tester-invalidateFetcherByDrvHash
131 # or doc/build-helpers/testers.chapter.md
132 invalidateFetcherByDrvHash =
133 f: args:
134 let
135 optionalFix = if lib.isFunction args then lib.id else lib.fix;
136 unsalted = f args;
137 drvPath = unsalted.drvPath;
138 # It's safe to discard the context, because we don't access the path.
139 salt = builtins.unsafeDiscardStringContext (lib.substring 0 12 (baseNameOf drvPath));
140 saltName = name: "${name}-salted-${salt}";
141 getSaltedNames =
142 args:
143 if args.pname or null != null then
144 { pname = saltName args.pname; }
145 else
146 { name = saltName args.name or "source"; };
147 # New derivation incorporating the original drv hash in the name
148 saltedByArgs = f (optionalFix (lib.extends (lib.toExtension getSaltedNames) (lib.toFunction args)));
149 saltedByOverrideAttrs = unsalted.overrideAttrs (previousAttrs: getSaltedNames previousAttrs);
150 saltedByOverrideAttrsForced = unsalted.overrideAttrs (previousAttrs: {
151 name = saltName unsalted.name;
152 });
153 # Make sure we did change the derivation.
154 # If the fetcher ignores `pname` and `name` and provide a broken `overrideAttrs`,
155 # `invalidateFetcherByDrvHash` doesn't work.
156 checked =
157 if saltedByArgs.drvPath != drvPath then
158 saltedByArgs
159 else if saltedByOverrideAttrs.drvPath != drvPath then
160 saltedByOverrideAttrs
161 else if saltedByOverrideAttrsForced.drvPath != drvPath then
162 saltedByOverrideAttrsForced
163 else
164 throw "invalidateFetcherByDrvHash: Neither adding pname/name to the fetcher args nor overriding with overrideAttrs change the result drvPath.";
165 in
166 checked;
167
168 # See https://nixos.org/manual/nixpkgs/unstable/#tester-runCommand
169 runCommand = testers.invalidateFetcherByDrvHash (
170 {
171 hash ? pkgs.emptyFile.outputHash,
172 name,
173 script,
174 stdenv ? stdenvNoCC,
175 ...
176 }@args:
177
178 runCommandWith {
179 inherit name stdenv;
180
181 derivationArgs = {
182 outputHash = hash;
183 outputHashMode = "recursive";
184 }
185 // lib.removeAttrs args [
186 "hash"
187 "name"
188 "script"
189 "stdenv"
190 ];
191 } script
192 );
193
194 # See https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest
195 # or doc/build-helpers/testers.chapter.md
196 runNixOSTest =
197 let
198 nixos = import ../../../nixos/lib {
199 inherit lib;
200 };
201 in
202 testModule:
203 nixos.runTest {
204 _file = "pkgs.runNixOSTest implementation";
205 imports = [
206 (lib.setDefaultModuleLocation "the argument that was passed to pkgs.runNixOSTest" testModule)
207 ];
208 hostPkgs = pkgs;
209 node.pkgs = pkgsLinux;
210 };
211
212 # See https://nixos.org/manual/nixpkgs/unstable/#tester-invalidateFetcherByDrvHash
213 # or doc/build-helpers/testers.chapter.md
214 nixosTest =
215 let
216 /*
217 The nixos/lib/testing-python.nix module, preapplied with arguments that
218 make sense for this evaluation of Nixpkgs.
219 */
220 nixosTesting = (
221 import ../../../nixos/lib/testing-python.nix {
222 inherit (stdenv.hostPlatform) system;
223 inherit pkgs;
224 extraConfigurations = [
225 (
226 { lib, ... }:
227 {
228 config.nixpkgs.pkgs = lib.mkDefault pkgsLinux;
229 }
230 )
231 ];
232 }
233 );
234 in
235 test:
236 let
237 loadedTest = if builtins.typeOf test == "path" then import test else test;
238 calledTest = lib.toFunction loadedTest pkgs;
239 in
240 nixosTesting.simpleTest calledTest;
241
242 hasPkgConfigModule =
243 { moduleName, ... }@args:
244 lib.warn
245 "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."
246 (
247 testers.hasPkgConfigModules (
248 removeAttrs args [ "moduleName" ]
249 // {
250 moduleNames = [ moduleName ];
251 }
252 )
253 );
254 hasPkgConfigModules = callPackage ./hasPkgConfigModules/tester.nix { };
255
256 hasCmakeConfigModules = callPackage ./hasCmakeConfigModules/tester.nix { };
257
258 testMetaPkgConfig = callPackage ./testMetaPkgConfig/tester.nix { };
259
260 shellcheck = callPackage ./shellcheck/tester.nix { };
261
262 shfmt = callPackage ./shfmt { };
263}