nixpkgs/pkgs/build-support/trivial-builders/test/write-text-file.nix
Silvan Mosberger 4f0dadbf38 treewide: format all inactive Nix files
After final improvements to the official formatter implementation,
this commit now performs the first treewide reformat of Nix files using it.
This is part of the implementation of RFC 166.

Only "inactive" files are reformatted, meaning only files that
aren't being touched by any PR with activity in the past 2 months.
This is to avoid conflicts for PRs that might soon be merged.
Later we can do a full treewide reformat to get the rest,
which should not cause as many conflicts.

A CI check has already been running for some time to ensure that new and
already-formatted files are formatted, so the files being reformatted here
should also stay formatted.

This commit was automatically created and can be verified using

    nix-build a08b3a4d19.tar.gz \
      --argstr baseRev b32a094368
    result/bin/apply-formatting $NIXPKGS_PATH
2024-12-10 20:26:33 +01:00

77 lines
1.7 KiB
Nix

/*
To run:
cd nixpkgs
nix-build -A tests.trivial-builders.writeTextFile
or to run an individual test case
cd nixpkgs
nix-build -A tests.trivial-builders.writeTextFile.foo
*/
{
lib,
runCommand,
runtimeShell,
writeTextFile,
}:
let
veryWeirdName = ''here's a name with some "bad" characters, like spaces and quotes'';
in
lib.recurseIntoAttrs {
different-exe-name =
let
pkg = writeTextFile {
name = "bar";
destination = "/bin/foo";
executable = true;
text = ''
#!${runtimeShell}
echo hi
'';
};
in
assert pkg.meta.mainProgram == "foo";
assert baseNameOf (lib.getExe pkg) == "foo";
assert pkg.name == "bar";
runCommand "test-writeTextFile-different-exe-name" { } ''
PATH="${lib.makeBinPath [ pkg ]}:$PATH"
x=$(foo)
[[ "$x" == hi ]]
touch $out
'';
weird-name = writeTextFile {
name = "weird-names";
destination = "/etc/${veryWeirdName}";
text = ''passed!'';
checkPhase = ''
# intentionally hardcode everything here, to make sure
# Nix does not mess with file paths
name="here's a name with some \"bad\" characters, like spaces and quotes"
fullPath="$out/etc/$name"
if [ -f "$fullPath" ]; then
echo "[PASS] File exists!"
else
echo "[FAIL] File was not created at expected path!"
exit 1
fi
content=$(<"$fullPath")
expected="passed!"
if [ "$content" = "$expected" ]; then
echo "[PASS] Contents match!"
else
echo "[FAIL] File contents don't match!"
echo " Expected: $expected"
echo " Got: $content"
exit 2
fi
'';
};
}