Files
nixpkgs/pkgs/test/replace-vars/default.nix
Dark Steveneq 646b892680
Some checks failed
Periodic Merges (6h) / master → staging-nixos (push) Failing after 12m50s
Periodic Merges (6h) / master → staging-next (push) Failing after 12m54s
Periodic Merges (24h) / merge-base(master,staging) → haskell-updates (push) Failing after 11m54s
Periodic Merges (6h) / staging-next → staging (push) Failing after 12m13s
Periodic Merges (24h) / staging-next-25.05 → staging-25.05 (push) Failing after 13m24s
Periodic Merges (24h) / release-25.05 → staging-next-25.05 (push) Failing after 14m28s
push sheeet
2025-10-09 14:15:47 +02:00

169 lines
4.9 KiB
Nix

{
replaceVars,
replaceVarsWith,
emptyDirectory,
emptyFile,
lib,
runCommand,
testers,
}:
let
inherit (testers) testEqualContents testBuildFailure;
mkTests =
callReplaceVars: mkExpectation:
lib.recurseIntoAttrs {
succeeds = testEqualContents {
assertion = "replaceVars-succeeds";
actual = callReplaceVars ./source.txt {
free = "free";
"equal in" = "are the same in";
brotherhood = "shared humanity";
};
expected = mkExpectation (
builtins.toFile "source.txt" ''
All human beings are born free and are the same in dignity and rights.
They are endowed with reason and conscience and should act towards
one another in a spirit of shared humanity.
-- eroosevelt@humanrights.un.org
''
);
};
# There might eventually be a usecase for this, but it's not supported at the moment.
fails-on-directory =
runCommand "replaceVars-fails" { failed = testBuildFailure (callReplaceVars emptyDirectory { }); }
''
grep -e "ERROR: file.*empty-directory.*does not exist" $failed/testBuildFailure.log
touch $out
'';
fails-in-build-phase =
runCommand "replaceVars-fails"
{ failed = testBuildFailure (callReplaceVars emptyFile { not-found = "boo~"; }); }
''
grep -e "ERROR: pattern @not-found@ doesn't match anything in file.*empty-file" $failed/testBuildFailure.log
touch $out
'';
fails-in-check-phase =
runCommand "replaceVars-fails"
{
failed =
let
src = builtins.toFile "source.txt" ''
Header.
before @whatIsThis@ middle @It'sOdd2Me@ after.
@cannot detect due to space@
Trailer.
'';
in
testBuildFailure (callReplaceVars src { });
}
''
grep -e "unsubstituted Nix identifiers.*source.txt" $failed/testBuildFailure.log
grep -F "@whatIsThis@" $failed/testBuildFailure.log
grep -F "@It'sOdd2Me@" $failed/testBuildFailure.log
grep -F 'more precise `substitute` function' $failed/testBuildFailure.log
# Shouldn't see irrelevant details.
! grep -q -E -e "Header|before|middle|after|Trailer" $failed/testBuildFailure.log
# Shouldn't see the "cannot detect" version.
! grep -q -F "cannot detect due to space" $failed/testBuildFailure.log
touch $out
'';
succeeds-with-exemption = testEqualContents {
assertion = "replaceVars-succeeds";
actual = callReplaceVars ./source.txt {
free = "free";
"equal in" = "are the same in";
brotherhood = null;
};
expected = mkExpectation (
builtins.toFile "source.txt" ''
All human beings are born free and are the same in dignity and rights.
They are endowed with reason and conscience and should act towards
one another in a spirit of @brotherhood@.
-- eroosevelt@humanrights.un.org
''
);
};
fails-in-check-phase-with-exemption =
runCommand "replaceVars-fails"
{
failed =
let
src = builtins.toFile "source.txt" ''
@a@
@b@
@c@
'';
in
testBuildFailure (
callReplaceVars src {
a = "a";
b = null;
}
);
}
''
grep -e "unsubstituted Nix identifiers.*source.txt" $failed/testBuildFailure.log
grep -F "@c@" $failed/testBuildFailure.log
! grep -F "@b@" $failed/testBuildFailure.log
touch $out
'';
fails-in-check-phase-with-bad-exemption =
runCommand "replaceVars-fails"
{
failed =
let
src = builtins.toFile "source.txt" ''
@a@
@b@
'';
in
testBuildFailure (
callReplaceVars src {
a = "a";
b = null;
c = null;
}
);
}
''
grep -e "ERROR: pattern @c@ doesn't match anything in file.*source.txt" $failed/testBuildFailure.log
touch $out
'';
};
in
{
replaceVars = mkTests replaceVars lib.id;
replaceVarsWith =
mkTests
(
src: replacements:
replaceVarsWith {
inherit src replacements;
dir = "bin";
isExecutable = true;
}
)
(
file:
runCommand "expected" { inherit file; } ''
install -Dm755 "$file" "$out/bin/$(stripHash "$file")"
''
);
}