2024-05-13 21:24:10 +00:00
|
|
|
{ pkgs, pkgsLinux, buildPackages, lib, callPackage, runCommand, stdenv
|
|
|
|
, substituteAll, testers }:
|
2024-05-02 00:46:19 +00:00
|
|
|
# Documentation is in doc/builders/testers.chapter.md
|
|
|
|
{
|
|
|
|
# See https://nixos.org/manual/nixpkgs/unstable/#tester-testBuildFailure
|
|
|
|
# or doc/builders/testers.chapter.md
|
2024-05-13 21:24:10 +00:00
|
|
|
testBuildFailure = drv:
|
|
|
|
drv.overrideAttrs (orig: {
|
|
|
|
builder = buildPackages.bash;
|
|
|
|
args = [
|
|
|
|
(substituteAll {
|
|
|
|
coreutils = buildPackages.coreutils;
|
|
|
|
src = ./expect-failure.sh;
|
|
|
|
})
|
|
|
|
orig.realBuilder or stdenv.shell
|
|
|
|
] ++ orig.args or [
|
|
|
|
"-e"
|
|
|
|
(orig.builder or ../../stdenv/generic/default-builder.sh)
|
|
|
|
];
|
|
|
|
});
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# See https://nixos.org/manual/nixpkgs/unstable/#tester-testEqualDerivation
|
|
|
|
# or doc/builders/testers.chapter.md
|
|
|
|
testEqualDerivation = callPackage ./test-equal-derivation.nix { };
|
|
|
|
|
|
|
|
# See https://nixos.org/manual/nixpkgs/unstable/#tester-testEqualContents
|
|
|
|
# or doc/builders/testers.chapter.md
|
2024-05-13 21:24:10 +00:00
|
|
|
testEqualContents = { assertion, actual, expected, }:
|
|
|
|
runCommand "equal-contents-${lib.strings.toLower assertion}" {
|
|
|
|
inherit assertion actual expected;
|
|
|
|
} ''
|
|
|
|
echo "Checking:"
|
|
|
|
echo "$assertion"
|
|
|
|
if ! diff -U5 -r "$actual" "$expected" --color=always
|
2024-05-02 00:46:19 +00:00
|
|
|
then
|
|
|
|
echo
|
2024-05-13 21:24:10 +00:00
|
|
|
echo 'Contents must be equal, but were not!'
|
2024-05-02 00:46:19 +00:00
|
|
|
echo
|
2024-05-13 21:24:10 +00:00
|
|
|
echo "+: expected, at $expected"
|
|
|
|
echo "-: unexpected, at $actual"
|
2024-05-02 00:46:19 +00:00
|
|
|
exit 1
|
|
|
|
else
|
2024-05-13 21:24:10 +00:00
|
|
|
find "$expected" -type f -executable > expected-executables | sort
|
|
|
|
find "$actual" -type f -executable > actual-executables | sort
|
|
|
|
if ! diff -U0 actual-executables expected-executables --color=always
|
|
|
|
then
|
|
|
|
echo
|
|
|
|
echo "Contents must be equal, but some files' executable bits don't match"
|
|
|
|
echo
|
|
|
|
echo "+: make this file executable in the actual contents"
|
|
|
|
echo "-: make this file non-executable in the actual contents"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "expected $expected and actual $actual match."
|
|
|
|
echo 'OK'
|
|
|
|
touch $out
|
|
|
|
fi
|
2024-05-02 00:46:19 +00:00
|
|
|
fi
|
2024-05-13 21:24:10 +00:00
|
|
|
'';
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# See https://nixos.org/manual/nixpkgs/unstable/#tester-testVersion
|
|
|
|
# or doc/builders/testers.chapter.md
|
2024-05-13 21:24:10 +00:00
|
|
|
testVersion = { package, command ?
|
|
|
|
"${package.meta.mainProgram or package.pname or package.name} --version"
|
|
|
|
, version ? package.version, }:
|
|
|
|
runCommand "${package.name}-test-version" {
|
|
|
|
nativeBuildInputs = [ package ];
|
|
|
|
meta.timeout = 60;
|
|
|
|
} ''
|
2024-05-02 00:46:19 +00:00
|
|
|
if output=$(${command} 2>&1); then
|
|
|
|
if grep -Fw -- "${version}" - <<< "$output"; then
|
|
|
|
touch $out
|
|
|
|
else
|
|
|
|
echo "Version string '${version}' not found!" >&2
|
|
|
|
echo "The output was:" >&2
|
|
|
|
echo "$output" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo -n ${lib.escapeShellArg command} >&2
|
|
|
|
echo " returned a non-zero exit code." >&2
|
|
|
|
echo "$output" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
# See doc/builders/testers.chapter.md or
|
|
|
|
# https://nixos.org/manual/nixpkgs/unstable/#tester-invalidateFetcherByDrvHash
|
|
|
|
invalidateFetcherByDrvHash = f: args:
|
|
|
|
let
|
|
|
|
drvPath = (f args).drvPath;
|
|
|
|
# It's safe to discard the context, because we don't access the path.
|
2024-05-13 21:24:10 +00:00
|
|
|
salt = builtins.unsafeDiscardStringContext
|
|
|
|
(lib.substring 0 12 (baseNameOf drvPath));
|
2024-05-02 00:46:19 +00:00
|
|
|
# New derivation incorporating the original drv hash in the name
|
2024-05-13 21:24:10 +00:00
|
|
|
salted =
|
|
|
|
f (args // { name = "${args.name or "source"}-salted-${salt}"; });
|
2024-05-02 00:46:19 +00:00
|
|
|
# Make sure we did change the derivation. If the fetcher ignores `name`,
|
|
|
|
# `invalidateFetcherByDrvHash` doesn't work.
|
2024-05-13 21:24:10 +00:00
|
|
|
checked = if salted.drvPath == drvPath 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."
|
|
|
|
else
|
|
|
|
salted;
|
2024-05-02 00:46:19 +00:00
|
|
|
in checked;
|
|
|
|
|
|
|
|
# See doc/builders/testers.chapter.md or
|
|
|
|
# https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest
|
2024-05-13 21:24:10 +00:00
|
|
|
runNixOSTest = let nixos = import ../../../nixos/lib { inherit lib; };
|
|
|
|
in testModule:
|
|
|
|
nixos.runTest {
|
|
|
|
_file = "pkgs.runNixOSTest implementation";
|
|
|
|
imports = [
|
|
|
|
(lib.setDefaultModuleLocation
|
|
|
|
"the argument that was passed to pkgs.runNixOSTest" testModule)
|
|
|
|
];
|
|
|
|
hostPkgs = pkgs;
|
|
|
|
node.pkgs = pkgsLinux;
|
|
|
|
};
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# See doc/builders/testers.chapter.md or
|
|
|
|
# https://nixos.org/manual/nixpkgs/unstable/#tester-invalidateFetcherByDrvHash
|
2024-05-13 21:24:10 +00:00
|
|
|
nixosTest = let
|
|
|
|
# The nixos/lib/testing-python.nix module, preapplied with arguments that
|
|
|
|
# make sense for this evaluation of Nixpkgs.
|
|
|
|
nixosTesting = (import ../../../nixos/lib/testing-python.nix {
|
|
|
|
inherit (stdenv.hostPlatform) system;
|
|
|
|
inherit pkgs;
|
|
|
|
extraConfigurations =
|
|
|
|
[ ({ lib, ... }: { config.nixpkgs.pkgs = lib.mkDefault pkgsLinux; }) ];
|
|
|
|
});
|
|
|
|
in test:
|
|
|
|
let
|
|
|
|
loadedTest = if builtins.typeOf test == "path" then import test else test;
|
|
|
|
calledTest = lib.toFunction loadedTest pkgs;
|
|
|
|
in nixosTesting.simpleTest calledTest;
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
hasPkgConfigModule = { moduleName, ... }@args:
|
|
|
|
lib.warn
|
|
|
|
"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."
|
|
|
|
(testers.hasPkgConfigModules (builtins.removeAttrs args [ "moduleName" ]
|
|
|
|
// {
|
2024-05-02 00:46:19 +00:00
|
|
|
moduleNames = [ moduleName ];
|
2024-05-13 21:24:10 +00:00
|
|
|
}));
|
2024-05-02 00:46:19 +00:00
|
|
|
hasPkgConfigModules = callPackage ./hasPkgConfigModules/tester.nix { };
|
|
|
|
|
|
|
|
testMetaPkgConfig = callPackage ./testMetaPkgConfig/tester.nix { };
|
|
|
|
}
|