From b6bec17eb9b8f256151c396282ad76db255fff91 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 29 Jan 2023 09:27:29 +0100 Subject: [PATCH] testers.hasPkgConfigModule: Extract and add tests, docs --- doc/builders/testers.chapter.md | 13 +++++ pkgs/build-support/testers/default.nix | 2 + .../testers/hasPkgConfigModule/tester.nix | 47 +++++++++++++++++++ .../testers/hasPkgConfigModule/tests.nix | 36 ++++++++++++++ pkgs/build-support/testers/test/default.nix | 2 + .../test-defaultPkgConfigPackages.nix | 42 +---------------- 6 files changed, 102 insertions(+), 40 deletions(-) create mode 100644 pkgs/build-support/testers/hasPkgConfigModule/tester.nix create mode 100644 pkgs/build-support/testers/hasPkgConfigModule/tests.nix diff --git a/doc/builders/testers.chapter.md b/doc/builders/testers.chapter.md index 3d91f096051e..a0f0f97f9d53 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 testers 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 6ab0ee843cb0..15694162edde 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 000000000000..c8342cdd5c3b --- /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 000000000000..8005c3f93709 --- /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 0a5381b2b738..313c556737fb 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 ba0e89438089..37687117987d 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; }