writeTextFile: chmod before checkPhase

Set the executable bit before running the check phase, so that the check
phase can run the script to test its behaviour.

This aligns with what `concatTextFile` is doing.

Also use explicit `if` statements so that we don't silently ignore
`chmod` failures.
This commit is contained in:
Naïm Favier 2023-03-29 14:06:45 +02:00
parent 9973b3ec30
commit e6f19ea429
No known key found for this signature in database
GPG Key ID: 95AFCE8211908325
3 changed files with 23 additions and 3 deletions

View File

@ -150,9 +150,11 @@ rec {
echo -n "$text" > "$target"
fi
eval "$checkPhase"
if [ -n "$executable" ]; then
chmod +x "$target"
fi
(test -n "$executable" && chmod +x "$target") || true
eval "$checkPhase"
'';
/*
@ -411,7 +413,10 @@ rec {
mkdir -p "$(dirname "$file")"
cat $files > "$file"
(test -n "$executable" && chmod +x "$file") || true
if [ -n "$executable" ]; then
chmod +x "$file"
fi
eval "$checkPhase"
'';

View File

@ -0,0 +1,14 @@
{ lib, writeShellScript }: let
output = "hello";
in (writeShellScript "test-script" ''
echo ${lib.escapeShellArg output}
'').overrideAttrs (old: {
checkPhase = old.checkPhase or "" + ''
expected=${lib.escapeShellArg output}
got=$("$target")
if [[ "$got" != "$expected" ]]; then
echo "wrong output: expected $expected, got $got"
exit 1
fi
'';
})

View File

@ -70,6 +70,7 @@ with pkgs;
trivial-builders = recurseIntoAttrs {
writeStringReferencesToFile = callPackage ../build-support/trivial-builders/test/writeStringReferencesToFile.nix {};
writeTextFile = callPackage ../build-support/trivial-builders/test/write-text-file.nix {};
writeShellScript = callPackage ../build-support/trivial-builders/test/write-shell-script.nix {};
references = callPackage ../build-support/trivial-builders/test/references.nix {};
overriding = callPackage ../build-support/trivial-builders/test-overriding.nix {};
concat = callPackage ../build-support/trivial-builders/test/concat-test.nix {};