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

nicoo a817fdac af8edf6d

Changed files
+51 -13
doc
languages-frameworks
pkgs
build-support
testers
hasPkgConfigModules
development
libraries
miniz
+8 -4
doc/languages-frameworks/pkg-config.section.md
···
## 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, ... }:
···
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" ];
};
})
```
+26 -6
pkgs/build-support/testers/hasPkgConfigModules/tester.nix
···
{ 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}.";
}
···
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
''
+13 -2
pkgs/build-support/testers/hasPkgConfigModules/tests.nix
···
# 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;
+4 -1
pkgs/development/libraries/miniz/default.nix
···
--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";