about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-01-29 09:27:29 +0100
committerRobert Hensing <robert@roberthensing.nl>2023-01-30 00:35:34 +0100
commitb6bec17eb9b8f256151c396282ad76db255fff91 (patch)
tree0094313f2c03a372defbaaebc0a4acf02933a462
parentf192e96d0771a9c8fa8e8c73e6ef66af56a548f8 (diff)
testers.hasPkgConfigModule: Extract and add tests, docs
-rw-r--r--doc/builders/testers.chapter.md13
-rw-r--r--pkgs/build-support/testers/default.nix2
-rw-r--r--pkgs/build-support/testers/hasPkgConfigModule/tester.nix47
-rw-r--r--pkgs/build-support/testers/hasPkgConfigModule/tests.nix36
-rw-r--r--pkgs/build-support/testers/test/default.nix2
-rw-r--r--pkgs/top-level/pkg-config/test-defaultPkgConfigPackages.nix42
6 files changed, 102 insertions, 40 deletions
diff --git a/doc/builders/testers.chapter.md b/doc/builders/testers.chapter.md
index 3d91f096051ee..a0f0f97f9d53f 100644
--- a/doc/builders/testers.chapter.md
+++ b/doc/builders/testers.chapter.md
@@ -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
diff --git a/pkgs/build-support/testers/default.nix b/pkgs/build-support/testers/default.nix
index 6ab0ee843cb08..15694162edde3 100644
--- a/pkgs/build-support/testers/default.nix
+++ b/pkgs/build-support/testers/default.nix
@@ -121,4 +121,6 @@
         in
           nixosTesting.simpleTest calledTest;
 
+  hasPkgConfigModule = callPackage ./hasPkgConfigModule/tester.nix { };
+
 }
diff --git a/pkgs/build-support/testers/hasPkgConfigModule/tester.nix b/pkgs/build-support/testers/hasPkgConfigModule/tester.nix
new file mode 100644
index 0000000000000..c8342cdd5c3bf
--- /dev/null
+++ b/pkgs/build-support/testers/hasPkgConfigModule/tester.nix
@@ -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
+  ''
diff --git a/pkgs/build-support/testers/hasPkgConfigModule/tests.nix b/pkgs/build-support/testers/hasPkgConfigModule/tests.nix
new file mode 100644
index 0000000000000..8005c3f937095
--- /dev/null
+++ b/pkgs/build-support/testers/hasPkgConfigModule/tests.nix
@@ -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
+  '';
+
+}
diff --git a/pkgs/build-support/testers/test/default.nix b/pkgs/build-support/testers/test/default.nix
index 0a5381b2b738d..313c556737fb3 100644
--- a/pkgs/build-support/testers/test/default.nix
+++ b/pkgs/build-support/testers/test/default.nix
@@ -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, ... }: {
diff --git a/pkgs/top-level/pkg-config/test-defaultPkgConfigPackages.nix b/pkgs/top-level/pkg-config/test-defaultPkgConfigPackages.nix
index ba0e89438089e..37687117987d0 100644
--- a/pkgs/top-level/pkg-config/test-defaultPkgConfigPackages.nix
+++ b/pkgs/top-level/pkg-config/test-defaultPkgConfigPackages.nix
@@ -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; }