From 10ec5da56e9932501104f39c3ca351a93d734053 Mon Sep 17 00:00:00 2001 From: Naïm Favier Date: Sun, 19 Dec 2021 23:29:07 +0100 Subject: tests.trivial-builders.overriding: update after shellDryRun --- .../trivial-builders/test-overriding.nix | 63 ++++++++++------------ 1 file changed, 27 insertions(+), 36 deletions(-) (limited to 'pkgs/build-support/trivial-builders') diff --git a/pkgs/build-support/trivial-builders/test-overriding.nix b/pkgs/build-support/trivial-builders/test-overriding.nix index ddd5dc05075..a16bbbee1b1 100644 --- a/pkgs/build-support/trivial-builders/test-overriding.nix +++ b/pkgs/build-support/trivial-builders/test-overriding.nix @@ -1,10 +1,11 @@ # Check that overriding works for trivial-builders like # `writeShellScript` via `overrideAttrs`. This is useful -# to override the `checkPhase`, e. g. when you want -# to enable extglob in `writeShellScript`. +# to override the `checkPhase`, e. g. if you want +# to disable extglob in `writeShellScript`. # -# Run using `nix-build -A tests.trivial-overriding`. +# Run using `nix-build -A tests.trivial-builders.overriding`. { lib +, stdenv , runtimeShell , runCommand , callPackage @@ -21,33 +22,6 @@ let rm success ''; - # Reuse the old `checkPhase` of `writeShellScript`, but enable extglob. - allowExtglob = old: { - checkPhase = '' - # make sure we don't change the settings for - # the rest of the derivation's build - ( - export BASHOPTS - shopt -s extglob - ${old.checkPhase} - ) - ''; - }; - - # Run old checkPhase, but only succeed if it fails. - # This HACK is required because we can't introspect build failures - # in nix: With `assertFail` we want to make sure that the default - # `checkPhase` would fail if extglob was used in the script. - assertFail = old: { - # write old checkPhase into a shell script, so we can check for - # the phase to fail even though we have `set -e`. - checkPhase = '' - if source ${writeShellScript "old-check-phase" old.checkPhase} 2>/dev/null; then - exit 1 - fi - ''; - }; - simpleCase = case: writeShellScript "test-trivial-overriding-${case}" extglobScript; @@ -70,16 +44,33 @@ let executable = true; }; - mkCase = f: type: isBin: + disallowExtglob = x: x.overrideAttrs (_: { + checkPhase = '' + ${stdenv.shell} -n "$target" + ''; + }); + + # Run old checkPhase, but only succeed if it fails. + # This HACK is required because we can't introspect build failures + # in nix: With `assertFail` we want to make sure that the default + # `checkPhase` would fail if extglob was used in the script. + assertFail = x: x.overrideAttrs (old: { + checkPhase = '' + if + ${old.checkPhase} + then exit 1; fi + ''; + }); + + mkCase = case: outcome: isBin: let - drv = (f type).overrideAttrs - (if type == "succ" then allowExtglob else assertFail); + drv = lib.pipe outcome ([ case ] ++ lib.optionals (outcome == "fail") [ disallowExtglob assertFail ]); in if isBin then "${drv}/bin/${drv.name}" else drv; writeTextOverrides = { - # Enabling globbing in checkPhase + # Make sure extglob works by default simpleSucc = mkCase simpleCase "succ" false; - # Ensure it's possible to fail; in this case globbing is not enabled. + # Ensure it's possible to fail; in this case extglob is not enabled simpleFail = mkCase simpleCase "fail" false; # Do the same checks after wrapping with callPackage # to make sure callPackage doesn't mess with the override @@ -103,7 +94,7 @@ let name = script.name or (builtins.baseNameOf script); in writeShellScript "run-${name}" '' if [ "$(${script})" != "success" ]; then - echo "Failed in ${script}" + echo "Failed in ${name}" exit 1 fi ''; -- cgit 1.4.1