From b68130fd2cb836f80ee6c8eb8840c39a12c50bef Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Thu, 6 May 2021 10:08:08 +0200 Subject: [PATCH] test-utilities: version test Extract 'version test' to a reusable test utility as discussed in https://github.com/NixOS/nixpkgs/pull/119636#issuecomment-826137021 and --- .../applications/graphics/ImageMagick/7.0.nix | 4 +++ pkgs/applications/misc/hello/default.nix | 10 +++++- .../networking/seaweedfs/default.nix | 8 ++--- .../science/logic/key/default.nix | 13 +++++--- pkgs/build-support/trivial-builders.nix | 33 +++++++++++++++++++ 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/pkgs/applications/graphics/ImageMagick/7.0.nix b/pkgs/applications/graphics/ImageMagick/7.0.nix index e0b75e7406428..5eb5b9ffcef91 100644 --- a/pkgs/applications/graphics/ImageMagick/7.0.nix +++ b/pkgs/applications/graphics/ImageMagick/7.0.nix @@ -2,6 +2,7 @@ , bzip2, zlib, libX11, libXext, libXt, fontconfig, freetype, ghostscript, libjpeg, djvulibre , lcms2, openexr, libpng, librsvg, libtiff, libxml2, openjpeg, libwebp, libheif , ApplicationServices +, testVersion, imagemagick }: let @@ -72,6 +73,9 @@ stdenv.mkDerivation rec { done ''; + passthru.tests.version = + testVersion { package = imagemagick; }; + meta = with lib; { homepage = "http://www.imagemagick.org/"; description = "A software suite to create, edit, compose, or convert bitmap images"; diff --git a/pkgs/applications/misc/hello/default.nix b/pkgs/applications/misc/hello/default.nix index a885b1643fe23..b446fe6223df1 100644 --- a/pkgs/applications/misc/hello/default.nix +++ b/pkgs/applications/misc/hello/default.nix @@ -1,4 +1,9 @@ -{ lib, stdenv, fetchurl }: +{ lib +, stdenv +, fetchurl +, testVersion +, hello +}: stdenv.mkDerivation rec { pname = "hello"; @@ -11,6 +16,9 @@ stdenv.mkDerivation rec { doCheck = true; + passthru.tests.version = + testVersion { package = hello; }; + meta = with lib; { description = "A program that produces a familiar, friendly greeting"; longDescription = '' diff --git a/pkgs/applications/networking/seaweedfs/default.nix b/pkgs/applications/networking/seaweedfs/default.nix index 61f318009f727..86a3e48ae8ff8 100644 --- a/pkgs/applications/networking/seaweedfs/default.nix +++ b/pkgs/applications/networking/seaweedfs/default.nix @@ -1,7 +1,7 @@ { lib , fetchFromGitHub , buildGoModule -, runCommand +, testVersion , seaweedfs }: @@ -20,10 +20,8 @@ buildGoModule rec { subPackages = [ "weed" ]; - passthru.tests.check-version = runCommand "weed-version" { meta.timeout = 3; } '' - ${seaweedfs}/bin/weed version | grep -Fw ${version} - touch $out - ''; + passthru.tests.version = + testVersion { package = seaweedfs; command = "weed version"; }; meta = with lib; { description = "Simple and highly scalable distributed file system"; diff --git a/pkgs/applications/science/logic/key/default.nix b/pkgs/applications/science/logic/key/default.nix index 531081beafaf3..e9b0cc7540b59 100644 --- a/pkgs/applications/science/logic/key/default.nix +++ b/pkgs/applications/science/logic/key/default.nix @@ -5,7 +5,7 @@ , ant , jre , makeWrapper -, runCommand +, testVersion , key }: @@ -51,10 +51,13 @@ in stdenv.mkDerivation rec { --add-flags "-cp $out/share/java/KeY.jar de.uka.ilkd.key.core.Main" ''; - passthru.tests.check-version = runCommand "key-help" {} '' - ${key}/bin/KeY --help | grep 2.5 # Wrong version in the code. On next version change to ${version} - touch $out - ''; + passthru.tests.version = + testVersion { + package = key; + command = "KeY --help"; + # Wrong '2.5' version in the code. On next version change to ${version} + version = "2.5"; + }; meta = with lib; { description = "Java formal verification tool"; diff --git a/pkgs/build-support/trivial-builders.nix b/pkgs/build-support/trivial-builders.nix index 4995efd9a4bfc..142a04f9a10c4 100644 --- a/pkgs/build-support/trivial-builders.nix +++ b/pkgs/build-support/trivial-builders.nix @@ -541,4 +541,37 @@ rec { phases = "unpackPhase patchPhase installPhase"; installPhase = "cp -R ./ $out"; }; + + /* Checks the command output contains the specified version + * + * Although simplistic, this test assures that the main program + * can run. While there's no substitute for a real test case, + * it does catch dynamic linking errors and such. It also provides + * some protection against accidentally building the wrong version, + * for example when using an 'old' hash in a fixed-output derivation. + * + * Examples: + * + * passthru.tests.version = testVersion { package = hello; }; + * + * passthru.tests.version = testVersion { + * package = seaweedfs; + * command = "weed version"; + * }; + * + * passthru.tests.version = testVersion { + * package = key; + * command = "KeY --help"; + * # Wrong '2.5' version in the code. Drop on next version. + * version = "2.5"; + * }; + */ + testVersion = + { package, + command ? "${package.meta.mainProgram or package.pname or package.name} --version", + version ? package.version, + }: runCommand "test-version" { nativeBuildInputs = [ package ]; meta.timeout = 60; } '' + ${command} | grep -Fw ${version} + touch $out + ''; }