2024-05-02 00:46:19 +00:00
|
|
|
# Check that overriding works for trivial-builders like
|
|
|
|
# `writeShellScript` via `overrideAttrs`. This is useful
|
|
|
|
# to override the `checkPhase`, e. g. if you want
|
|
|
|
# to disable extglob in `writeShellScript`.
|
|
|
|
#
|
|
|
|
# Run using `nix-build -A tests.trivial-builders.overriding`.
|
2024-05-13 21:24:10 +00:00
|
|
|
{ lib, stdenv, runtimeShell, runCommand, callPackage, writeShellScript
|
|
|
|
, writeTextFile, writeShellScriptBin }:
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
extglobScript = ''
|
|
|
|
shopt -s extglob
|
|
|
|
touch success
|
|
|
|
echo @(success|failure)
|
|
|
|
rm success
|
|
|
|
'';
|
|
|
|
|
|
|
|
simpleCase = case:
|
|
|
|
writeShellScript "test-trivial-overriding-${case}" extglobScript;
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
callPackageCase = case:
|
|
|
|
callPackage ({ writeShellScript }:
|
|
|
|
writeShellScript "test-trivial-callpackage-overriding-${case}"
|
|
|
|
extglobScript) { };
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
binCase = case:
|
|
|
|
writeShellScriptBin "test-trivial-overriding-bin-${case}" extglobScript;
|
|
|
|
|
|
|
|
# building this derivation would fail without overriding
|
|
|
|
textFileCase = writeTextFile {
|
|
|
|
name = "test-trivial-overriding-text-file";
|
|
|
|
checkPhase = "false";
|
|
|
|
text = ''
|
|
|
|
#!${runtimeShell}
|
|
|
|
echo success
|
|
|
|
'';
|
|
|
|
executable = true;
|
|
|
|
};
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
disallowExtglob = x:
|
|
|
|
x.overrideAttrs (_: {
|
2024-05-02 00:46:19 +00:00
|
|
|
checkPhase = ''
|
|
|
|
${stdenv.shell} -n "$target"
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
# 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: {
|
2024-05-02 00:46:19 +00:00
|
|
|
checkPhase = ''
|
|
|
|
if
|
|
|
|
${old.checkPhase}
|
|
|
|
then exit 1; fi
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
|
|
|
|
mkCase = case: outcome: isBin:
|
|
|
|
let
|
2024-05-13 21:24:10 +00:00
|
|
|
drv = lib.pipe outcome ([ case ]
|
|
|
|
++ lib.optionals (outcome == "fail") [ disallowExtglob assertFail ]);
|
2024-05-02 00:46:19 +00:00
|
|
|
in if isBin then "${drv}/bin/${drv.name}" else drv;
|
|
|
|
|
|
|
|
writeTextOverrides = {
|
|
|
|
# Make sure extglob works by default
|
|
|
|
simpleSucc = mkCase simpleCase "succ" false;
|
|
|
|
# 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
|
|
|
|
callpSucc = mkCase callPackageCase "succ" false;
|
|
|
|
callpFail = mkCase callPackageCase "fail" false;
|
|
|
|
# Do the same check using `writeShellScriptBin`
|
|
|
|
binSucc = mkCase binCase "succ" true;
|
|
|
|
binFail = mkCase binCase "fail" true;
|
|
|
|
# Check that we can also override plain writeTextFile
|
2024-05-13 21:24:10 +00:00
|
|
|
textFileSuccess = textFileCase.overrideAttrs (_: { checkPhase = "true"; });
|
2024-05-02 00:46:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# `runTest` forces nix to build the script of our test case and
|
|
|
|
# run its `checkPhase` which is our main interest. Additionally
|
|
|
|
# it executes the script and thus makes sure that extglob also
|
|
|
|
# works at run time.
|
|
|
|
runTest = script:
|
2024-05-13 21:24:10 +00:00
|
|
|
let name = script.name or (builtins.baseNameOf script);
|
2024-05-02 00:46:19 +00:00
|
|
|
in writeShellScript "run-${name}" ''
|
|
|
|
if [ "$(${script})" != "success" ]; then
|
|
|
|
echo "Failed in ${name}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2024-05-13 21:24:10 +00:00
|
|
|
in runCommand "test-writeShellScript-overriding" {
|
2024-05-02 00:46:19 +00:00
|
|
|
passthru = { inherit writeTextOverrides; };
|
|
|
|
} ''
|
|
|
|
${lib.concatMapStrings (test: ''
|
2024-05-13 21:24:10 +00:00
|
|
|
${runTest test}
|
|
|
|
'') (lib.attrValues writeTextOverrides)}
|
2024-05-02 00:46:19 +00:00
|
|
|
touch "$out"
|
|
|
|
''
|