testers.hasPkgConfigModule: Extract and add tests, docs

This commit is contained in:
Robert Hensing 2023-01-29 09:27:29 +01:00
parent f192e96d07
commit b6bec17eb9
6 changed files with 102 additions and 40 deletions

View file

@ -1,6 +1,19 @@
# Testers {#chap-testers}
This chapter describes several testing builders which are available in the <literal>testers</literal> namespace.
## `hasPkgConfigModule` {#tester-hasPkgConfigModule}
Checks whether a package exposes a certain `pkg-config` module.
Example:
```nix
passthru.tests.pkg-config = testers.hasPkgConfigModule {
package = finalAttrs.finalPackage;
moduleName = "libfoo";
}
```
## `testVersion` {#tester-testVersion}
Checks the command output contains the specified version

View file

@ -121,4 +121,6 @@
in
nixosTesting.simpleTest calledTest;
hasPkgConfigModule = callPackage ./hasPkgConfigModule/tester.nix { };
}

View file

@ -0,0 +1,47 @@
# Static arguments
{ runCommand, pkg-config }:
# Tester arguments
{ package,
moduleName,
testName ? "check-pkg-config-${moduleName}",
}:
runCommand testName {
nativeBuildInputs = [ pkg-config ];
buildInputs = [ package ];
inherit moduleName;
meta = {
description = "Test whether ${package.name} exposes pkg-config module ${moduleName}";
}
# Make sure licensing info etc is preserved, as this is a concern for e.g. cache.nixos.org,
# as hydra can't check this meta info in dependencies.
# The test itself is just Nixpkgs, with MIT license.
// builtins.intersectAttrs
{
available = throw "unused";
broken = throw "unused";
insecure = throw "unused";
license = throw "unused";
maintainers = throw "unused";
platforms = throw "unused";
unfree = throw "unused";
unsupported = throw "unused";
}
package.meta;
} ''
echo "checking pkg-config module $moduleName in $buildInputs"
set +e
version="$(pkg-config --modversion $moduleName)"
r=$?
set -e
if [[ $r = 0 ]]; then
echo " pkg-config module $moduleName exists and has version $version"
echo "$version" > $out
else
echo "These modules were available in the input propagation closure:"
pkg-config --list-all
echo " pkg-config module $moduleName was not found"
false
fi
''

View file

@ -0,0 +1,36 @@
# cd nixpkgs
# nix-build -A tests.testers.hasPkgConfigModule
{ lib, testers, zlib, runCommand }:
lib.recurseIntoAttrs {
zlib-has-zlib = testers.hasPkgConfigModule {
package = zlib;
moduleName = "zlib";
};
zlib-does-not-have-ylib = runCommand "zlib-does-not-have-ylib" {
failed = testers.testBuildFailure (
testers.hasPkgConfigModule {
package = zlib;
moduleName = "ylib";
}
);
} ''
echo 'it logs a relevant error message'
{
grep -F "pkg-config module ylib was not found" $failed/testBuildFailure.log
}
echo 'it logs which pkg-config modules are available, to be helpful'
{
# grep -v: the string zlib does also occur in a store path in an earlier message, which isn't particularly helpful
grep -v "checking pkg-config module" < $failed/testBuildFailure.log \
| grep -F "zlib"
}
# done
touch $out
'';
}

View file

@ -12,6 +12,8 @@ let
in
lib.recurseIntoAttrs {
hasPkgConfigModule = pkgs.callPackage ../hasPkgConfigModule/tests.nix { };
# Check that the wiring of nixosTest is correct.
# Correct operation of the NixOS test driver should be asserted elsewhere.
nixosTest-example = pkgs-with-overlay.testers.nixosTest ({ lib, pkgs, figlet, ... }: {

View file

@ -1,6 +1,6 @@
# cd nixpkgs
# nix-build -A tests.pkg-config.defaultPkgConfigPackages
{ lib, pkg-config, defaultPkgConfigPackages, runCommand }:
{ lib, pkg-config, defaultPkgConfigPackages, runCommand, testers }:
let
inherit (lib.strings) escapeNixIdentifier;
@ -39,45 +39,7 @@ let
else if pkg.meta.broken
then null
else makePkgConfigTest moduleName pkg;
else testers.hasPkgConfigModule { inherit moduleName; package = pkg; };
makePkgConfigTest = moduleName: pkg: runCommand "check-pkg-config-${moduleName}" {
nativeBuildInputs = [ pkg-config ];
buildInputs = [ pkg ];
inherit moduleName;
meta = {
description = "Test whether ${pkg.name} exposes pkg-config module ${moduleName}";
}
# Make sure licensing info etc is preserved, as this is a concern for e.g. cache.nixos.org,
# as hydra can't check this meta info in dependencies.
# The test itself is just Nixpkgs, with MIT license.
// builtins.intersectAttrs
{
available = throw "unused";
broken = throw "unused";
insecure = throw "unused";
license = throw "unused";
maintainers = throw "unused";
platforms = throw "unused";
unfree = throw "unused";
unsupported = throw "unused";
}
pkg.meta;
} ''
echo "checking pkg-config module $moduleName in $buildInputs"
set +e
version="$(pkg-config --modversion $moduleName)"
r=$?
set -e
if [[ $r = 0 ]]; then
echo " pkg-config module $moduleName exists and has version $version"
echo "$version" > $out
else
echo "These modules were available in the input propagation closure:"
pkg-config --list-all
echo " pkg-config module $moduleName was not found"
false
fi
'';
in
lib.recurseIntoAttrs allTests // { inherit tests-combined; }