Merge #307770: add optional version check in testers.hasPkgConfigModules

This commit is contained in:
nicoo 2024-05-03 20:18:56 +00:00 committed by GitHub
commit a817fdac5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 51 additions and 13 deletions

View file

@ -7,10 +7,11 @@ Nixpkgs provides a couple of facilities for working with this tool.
## Writing packages providing pkg-config modules {#pkg-config-writing-packages}
Packages should set `meta.pkgConfigModules` with the list of package config modules they provide.
They should also use `testers.testMetaPkgConfig` to check that the final built package matches that list.
They should also use `testers.hasPkgConfigModules` to check that the final built package matches that list,
and optionally check that the pkgconf modules' version metadata matches the derivation's.
Additionally, the [`validatePkgConfig` setup hook](https://nixos.org/manual/nixpkgs/stable/#validatepkgconfig), will do extra checks on to-be-installed pkg-config modules.
A good example of all these things is zlib:
A good example of all these things is miniz:
```nix
{ pkg-config, testers, ... }:
@ -20,11 +21,14 @@ stdenv.mkDerivation (finalAttrs: {
nativeBuildInputs = [ pkg-config validatePkgConfig ];
passthru.tests.pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage;
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
versionCheck = true;
};
meta = {
/* ... */
pkgConfigModules = [ "zlib" ];
pkgConfigModules = [ "miniz" ];
};
})
```

View file

@ -5,12 +5,14 @@
{ package,
moduleNames ? package.meta.pkgConfigModules,
testName ? "check-pkg-config-${lib.concatStringsSep "-" moduleNames}",
version ? package.version or null,
versionCheck ? false,
}:
runCommand testName {
nativeBuildInputs = [ pkg-config ];
buildInputs = [ package ];
inherit moduleNames;
inherit moduleNames version versionCheck;
meta = {
description = "Test whether ${package.name} exposes pkg-config modules ${lib.concatStringsSep ", " moduleNames}.";
}
@ -31,20 +33,38 @@ runCommand testName {
package.meta;
} ''
touch "$out"
notFound=0
versionMismatch=0
for moduleName in $moduleNames; do
echo "checking pkg-config module $moduleName in $buildInputs"
set +e
version="$($PKG_CONFIG --modversion $moduleName)"
moduleVersion="$($PKG_CONFIG --modversion $moduleName)"
r=$?
set -e
if [[ $r = 0 ]]; then
echo " pkg-config module $moduleName exists and has version $version"
if [[ "$moduleVersion" == "$version" ]]; then
echo " pkg-config module $moduleName exists and has version $moduleVersion"
else
echo " pkg-config module $moduleName exists and has version $moduleVersion when $version was expected"
((versionMismatch+=1))
fi
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
((notFound+=1))
fi
done
if [[ $notFound -eq 0 ]] && ([[ $versionMismatch -eq 0 ]] || [[ "$versionCheck" == false ]]); then
exit 0
fi
if [[ $notFound -ne 0 ]]; then
echo "$notFound modules not found"
echo "These modules were available in the input propagation closure:"
$PKG_CONFIG --list-all
fi
if [[ $versionMismatch -ne 0 ]]; then
echo "$versionMismatch version mismatches"
fi
exit 1
''

View file

@ -1,9 +1,20 @@
# cd nixpkgs
# nix-build -A tests.testers.hasPkgConfigModule
{ lib, testers, zlib, openssl, runCommand }:
# nix-build -A tests.testers.hasPkgConfigModules
{ lib, testers, miniz, zlib, openssl, runCommand }:
lib.recurseIntoAttrs {
miniz-versions-match = testers.hasPkgConfigModules {
package = miniz;
versionCheck = true;
};
miniz-versions-mismatch = testers.testBuildFailure (testers.hasPkgConfigModules {
package = miniz;
version = "1.2.3";
versionCheck = true;
});
zlib-has-zlib = testers.hasPkgConfigModules {
package = zlib;
moduleNames = [ "zlib" ];

View file

@ -28,7 +28,10 @@ stdenv.mkDerivation (finalAttrs: {
--replace-fail '=''${exec_prefix}//' '=/'
'';
passthru.tests.pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage;
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
versionCheck = true;
};
meta = with lib; {
description = "Single C source file zlib-replacement library";