Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ pkgs, buildPackages, lib, callPackage, runCommand, stdenv, substituteAll, }:
2# Documentation is in doc/builders/testers.chapter.md
3{
4 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testBuildFailure
5 # or doc/builders/testers.chapter.md
6 testBuildFailure = drv: drv.overrideAttrs (orig: {
7 builder = buildPackages.bash;
8 args = [
9 (substituteAll { coreutils = buildPackages.coreutils; src = ./expect-failure.sh; })
10 orig.realBuilder or stdenv.shell
11 ] ++ orig.args or ["-e" (orig.builder or ../../stdenv/generic/default-builder.sh)];
12 });
13
14 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testEqualDerivation
15 # or doc/builders/testers.chapter.md
16 testEqualDerivation = callPackage ./test-equal-derivation.nix { };
17
18 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testEqualContents
19 # or doc/builders/testers.chapter.md
20 testEqualContents = {
21 assertion,
22 actual,
23 expected,
24 }: runCommand "equal-contents-${lib.strings.toLower assertion}" {
25 inherit assertion actual expected;
26 } ''
27 echo "Checking:"
28 echo "$assertion"
29 if ! diff -U5 -r "$actual" "$expected" --color=always
30 then
31 echo
32 echo 'Contents must be equal, but were not!'
33 echo
34 echo "+: expected, at $expected"
35 echo "-: unexpected, at $actual"
36 exit 1
37 else
38 find "$expected" -type f -executable > expected-executables | sort
39 find "$actual" -type f -executable > actual-executables | sort
40 if ! diff -U0 actual-executables expected-executables --color=always
41 then
42 echo
43 echo "Contents must be equal, but some files' executable bits don't match"
44 echo
45 echo "+: make this file executable in the actual contents"
46 echo "-: make this file non-executable in the actual contents"
47 exit 1
48 else
49 echo "expected $expected and actual $actual match."
50 echo 'OK'
51 touch $out
52 fi
53 fi
54 '';
55
56 # See https://nixos.org/manual/nixpkgs/unstable/#tester-testVersion
57 # or doc/builders/testers.chapter.md
58 testVersion =
59 { package,
60 command ? "${package.meta.mainProgram or package.pname or package.name} --version",
61 version ? package.version,
62 }: runCommand "${package.name}-test-version" { nativeBuildInputs = [ package ]; meta.timeout = 60; } ''
63 if output=$(${command} 2>&1); then
64 if grep -Fw "${version}" - <<< "$output"; then
65 touch $out
66 else
67 echo "Version string '${version}' not found!" >&2
68 echo "The output was:" >&2
69 echo "$output" >&2
70 exit 1
71 fi
72 else
73 echo -n ${lib.escapeShellArg command} >&2
74 echo " returned a non-zero exit code." >&2
75 echo "$output" >&2
76 exit 1
77 fi
78 '';
79
80 # See doc/builders/testers.chapter.md or
81 # https://nixos.org/manual/nixpkgs/unstable/#tester-invalidateFetcherByDrvHash
82 invalidateFetcherByDrvHash = f: args:
83 let
84 drvPath = (f args).drvPath;
85 # It's safe to discard the context, because we don't access the path.
86 salt = builtins.unsafeDiscardStringContext (lib.substring 0 12 (baseNameOf drvPath));
87 # New derivation incorporating the original drv hash in the name
88 salted = f (args // { name = "${args.name or "source"}-salted-${salt}"; });
89 # Make sure we did change the derivation. If the fetcher ignores `name`,
90 # `invalidateFetcherByDrvHash` doesn't work.
91 checked =
92 if salted.drvPath == drvPath
93 then 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."
94 else salted;
95 in checked;
96
97 # See doc/builders/testers.chapter.md or
98 # https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest
99 runNixOSTest =
100 let nixos = import ../../../nixos/lib {};
101 in testModule:
102 nixos.runTest {
103 _file = "pkgs.runNixOSTest implementation";
104 imports = [
105 (lib.setDefaultModuleLocation "the argument that was passed to pkgs.runNixOSTest" testModule)
106 ];
107 hostPkgs = pkgs;
108 node.pkgs = pkgs;
109 };
110
111 # See doc/builders/testers.chapter.md or
112 # https://nixos.org/manual/nixpkgs/unstable/#tester-invalidateFetcherByDrvHash
113 nixosTest =
114 let
115 /* The nixos/lib/testing-python.nix module, preapplied with arguments that
116 * make sense for this evaluation of Nixpkgs.
117 */
118 nixosTesting =
119 (import ../../../nixos/lib/testing-python.nix {
120 inherit (stdenv.hostPlatform) system;
121 inherit pkgs;
122 extraConfigurations = [(
123 { lib, ... }: {
124 config.nixpkgs.pkgs = lib.mkDefault pkgs;
125 }
126 )];
127 });
128 in
129 test:
130 let
131 loadedTest = if builtins.typeOf test == "path"
132 then import test
133 else test;
134 calledTest = lib.toFunction loadedTest pkgs;
135 in
136 nixosTesting.simpleTest calledTest;
137
138 hasPkgConfigModule = callPackage ./hasPkgConfigModule/tester.nix { };
139
140 testMetaPkgConfig = callPackage ./testMetaPkgConfig/tester.nix { };
141}