Skip to content

Commit

Permalink
testers.hasPkgConfigModules: allow checking multiple pkg-config mods
Browse files Browse the repository at this point in the history
This is very useful in conjunction with meta.pkgConfigModules, as the
new tester can use the list provided by this meta attribute as a default
value for moduleNames, making its usage in passthru.tests very
convenient.

For backwards compatibility, a shim under the old name is maintained
with a warning.
  • Loading branch information
sternenseemann committed Aug 8, 2023
1 parent 0d98427 commit feabc3d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 35 deletions.
24 changes: 19 additions & 5 deletions doc/builders/testers.chapter.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
# Testers {#chap-testers}
This chapter describes several testing builders which are available in the `testers` namespace.

## `hasPkgConfigModule` {#tester-hasPkgConfigModule}
## `hasPkgConfigModules` {#tester-hasPkgConfigModules}

Checks whether a package exposes a certain `pkg-config` module.
<!-- Old anchor name so links still work -->
[]{#tester-hasPkgConfigModule}
Checks whether a package exposes a given list of `pkg-config` modules.
If the `moduleNames` argument is omitted, `hasPkgConfigModules` will
use `meta.pkgConfigModules`.

Example:

```nix
passthru.tests.pkg-config = testers.hasPkgConfigModule {
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
moduleName = "libfoo";
}
moduleNames = [ "libfoo" ];
};
```

If the package in question has `meta.pkgConfigModules` set, it is even simpler:

```nix
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
};
meta.pkgConfigModules = [ "libfoo" ];
```

## `testVersion` {#tester-testVersion}
Expand Down
6 changes: 4 additions & 2 deletions pkgs/applications/networking/pjsip/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ stdenv.mkDerivation (finalAttrs: {
command = "pjsua --version";
};

passthru.tests.pkg-config = testers.hasPkgConfigModule {
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
moduleName = "libpjproject";
};

passthru.tests.python-pjsua2 = runCommand "python-pjsua2" { } ''
Expand All @@ -118,5 +117,8 @@ stdenv.mkDerivation (finalAttrs: {
maintainers = with maintainers; [ olynch ];
mainProgram = "pjsua";
platforms = platforms.linux ++ platforms.darwin;
pkgConfigModules = [
"libpjproject"
];
};
})
11 changes: 9 additions & 2 deletions pkgs/build-support/testers/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, buildPackages, lib, callPackage, runCommand, stdenv, substituteAll, }:
{ pkgs, buildPackages, lib, callPackage, runCommand, stdenv, substituteAll, testers }:
# Documentation is in doc/builders/testers.chapter.md
{
# See https://nixos.org/manual/nixpkgs/unstable/#tester-testBuildFailure
Expand Down Expand Up @@ -137,7 +137,14 @@
in
nixosTesting.simpleTest calledTest;

hasPkgConfigModule = callPackage ./hasPkgConfigModule/tester.nix { };
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" ] // {
moduleNames = [ moduleName ];
})
);
hasPkgConfigModules = callPackage ./hasPkgConfigModules/tester.nix { };

testMetaPkgConfig = callPackage ./testMetaPkgConfig/tester.nix { };
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Static arguments
{ runCommand, pkg-config }:
{ lib, runCommand, pkg-config }:

# Tester arguments
{ package,
moduleName,
testName ? "check-pkg-config-${moduleName}",
moduleNames ? package.meta.pkgConfigModules,
testName ? "check-pkg-config-${lib.concatStringsSep "-" moduleNames}",
}:

runCommand testName {
nativeBuildInputs = [ pkg-config ];
buildInputs = [ package ];
inherit moduleName;
inherit moduleNames;
meta = {
description = "Test whether ${package.name} exposes pkg-config module ${moduleName}";
description = "Test whether ${package.name} exposes pkg-config modules ${lib.concatStringsSep ", " moduleNames}.";
}
# 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.
Expand All @@ -30,18 +30,20 @@ runCommand testName {
}
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
for moduleName in $moduleNames; do
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"
printf '%s\t%s\n' "$moduleName" "$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
done
''
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
# cd nixpkgs
# nix-build -A tests.testers.hasPkgConfigModule
{ lib, testers, zlib, runCommand }:
{ lib, testers, zlib, openssl, runCommand }:

lib.recurseIntoAttrs {

zlib-has-zlib = testers.hasPkgConfigModule {
zlib-has-zlib = testers.hasPkgConfigModules {
package = zlib;
moduleName = "zlib";
moduleNames = [ "zlib" ];
};

zlib-has-meta-pkgConfigModules = testers.hasPkgConfigModules {
package = zlib;
};

openssl-has-openssl = testers.hasPkgConfigModules {
package = openssl;
moduleNames = [ "openssl" ];
};

openssl-has-all-meta-pkgConfigModules = testers.hasPkgConfigModules {
package = openssl;
};

zlib-does-not-have-ylib = runCommand "zlib-does-not-have-ylib" {
failed = testers.testBuildFailure (
testers.hasPkgConfigModule {
testers.hasPkgConfigModules {
package = zlib;
moduleName = "ylib";
moduleNames = [ "ylib" ];
}
);
} ''
Expand Down
2 changes: 1 addition & 1 deletion pkgs/build-support/testers/test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let

in
lib.recurseIntoAttrs {
hasPkgConfigModule = pkgs.callPackage ../hasPkgConfigModule/tests.nix { };
hasPkgConfigModules = pkgs.callPackage ../hasPkgConfigModules/tests.nix { };

runNixOSTest-example = pkgs-with-overlay.testers.runNixOSTest ({ lib, ... }: {
name = "runNixOSTest-test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let
else if pkg.meta.broken
then null

else testers.hasPkgConfigModule { inherit moduleName; package = pkg; };
else testers.hasPkgConfigModules { moduleNames = [ moduleName ]; package = pkg; };

in
lib.recurseIntoAttrs allTests // { inherit tests-combined; }

0 comments on commit feabc3d

Please sign in to comment.