0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-13 21:50:33 +03:00

Merge pull request #245524 from hercules-ci/writeScriptBin-meta.mainProgram

Add meta.mainProgram to writeCBin, add tests
This commit is contained in:
Robert Hensing 2023-07-27 10:41:20 +02:00 committed by GitHub
commit f705094eac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 227 additions and 37 deletions

View file

@ -242,7 +242,11 @@ rec {
*/
writeScriptBin = name: text: writeTextFile {inherit name text; executable = true; destination = "/bin/${name}";};
writeScriptBin = name: text: writeTextFile {
inherit name text;
executable = true;
destination = "/bin/${name}";
};
/*
Similar to writeScript. Writes a Shell script and checks its syntax.
@ -374,6 +378,9 @@ rec {
# Pointless to do this on a remote machine.
preferLocalBuild = true;
allowSubstitutes = false;
meta = {
mainProgram = name;
};
}
''
n=$out/bin/$name

View file

@ -0,0 +1,33 @@
/*
Run all tests with:
cd nixpkgs
nix-build -A tests.trivial-builders
or run a specific test with:
cd nixpkgs
nix-build -A tests.trivial-builders.foo
*/
{ callPackage, lib, stdenv }:
let
inherit (lib) recurseIntoAttrs;
in
recurseIntoAttrs {
concat = callPackage ./concat-test.nix {};
linkFarm = callPackage ./link-farm.nix {};
overriding = callPackage ../test-overriding.nix {};
references =
# VM test not supported beyond linux yet
if stdenv.hostPlatform.isLinux
then callPackage ./references.nix {}
else null;
writeCBin = callPackage ./writeCBin.nix {};
writeScriptBin = callPackage ./writeScriptBin.nix {};
writeShellScript = callPackage ./write-shell-script.nix {};
writeShellScriptBin = callPackage ./writeShellScriptBin.nix {};
writeStringReferencesToFile = callPackage ./writeStringReferencesToFile.nix {};
writeTextFile = callPackage ./write-text-file.nix {};
}

View file

@ -1,34 +1,71 @@
{ writeTextFile }:
/*
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 writeTextFile {
name = "weird-names";
destination = "/etc/${veryWeirdName}";
text = ''passed!'';
checkPhase = ''
# intentionally hardcode everything here, to make sure
# Nix does not mess with file paths
in
lib.recurseIntoAttrs {
name="here's a name with some \"bad\" characters, like spaces and quotes"
fullPath="$out/etc/$name"
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
'';
if [ -f "$fullPath" ]; then
echo "[PASS] File exists!"
else
echo "[FAIL] File was not created at expected path!"
exit 1
fi
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
content=$(<"$fullPath")
expected="passed!"
name="here's a name with some \"bad\" characters, like spaces and quotes"
fullPath="$out/etc/$name"
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
'';
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
'';
};
}

View file

@ -0,0 +1,43 @@
/*
Run with:
cd nixpkgs
nix-build -A tests.trivial-builders.writeCBin
*/
{ lib, writeCBin, runCommand }:
let
output = "hello";
pkg = writeCBin "test-script" ''
#include <stdio.h>
int main () {
printf("hello\n");
return 0;
}
'';
in
assert pkg.meta.mainProgram == "test-script";
runCommand "test-writeCBin" { } ''
echo Testing with getExe...
target=${lib.getExe pkg}
expected=${lib.escapeShellArg output}
got=$("$target")
if [[ "$got" != "$expected" ]]; then
echo "wrong output: expected $expected, got $got"
exit 1
fi
echo Testing with makeBinPath...
PATH="${lib.makeBinPath [ pkg ]}:$PATH"
got=$(test-script)
if [[ "$got" != "$expected" ]]; then
echo "wrong output: expected $expected, got $got"
exit 1
fi
touch $out
''

View file

@ -0,0 +1,39 @@
/*
Run with:
cd nixpkgs
nix-build -A tests.trivial-builders.writeShellScriptBin
*/
{ lib, writeScriptBin, runCommand }:
let
output = "hello";
pkg = writeScriptBin "test-script" ''
echo ${lib.escapeShellArg output}
'';
in
assert pkg.meta.mainProgram == "test-script";
runCommand "test-writeScriptBin" { } ''
echo Testing with getExe...
target=${lib.getExe pkg}
expected=${lib.escapeShellArg output}
got=$("$target")
if [[ "$got" != "$expected" ]]; then
echo "wrong output: expected $expected, got $got"
exit 1
fi
echo Testing with makeBinPath...
PATH="${lib.makeBinPath [ pkg ]}:$PATH"
got=$(test-script)
if [[ "$got" != "$expected" ]]; then
echo "wrong output: expected $expected, got $got"
exit 1
fi
touch $out
''

View file

@ -0,0 +1,39 @@
/*
Run with:
cd nixpkgs
nix-build -A tests.trivial-builders.writeShellScriptBin
*/
{ lib, writeShellScriptBin, runCommand }:
let
output = "hello";
pkg = writeShellScriptBin "test-script" ''
echo ${lib.escapeShellArg output}
'';
in
assert pkg.meta.mainProgram == "test-script";
runCommand "test-writeShellScriptBin" { } ''
echo Testing with getExe...
target=${lib.getExe pkg}
expected=${lib.escapeShellArg output}
got=$("$target")
if [[ "$got" != "$expected" ]]; then
echo "wrong output: expected $expected, got $got"
exit 1
fi
echo Testing with makeBinPath...
PATH="${lib.makeBinPath [ pkg ]}:$PATH"
got=$(test-script)
if [[ "$got" != "$expected" ]]; then
echo "wrong output: expected $expected, got $got"
exit 1
fi
touch $out
''

View file

@ -66,15 +66,7 @@ with pkgs;
cuda = callPackage ./cuda { };
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 {};
linkFarm = callPackage ../build-support/trivial-builders/test/link-farm.nix {};
};
trivial-builders = callPackage ../build-support/trivial-builders/test/default.nix {};
writers = callPackage ../build-support/writers/test.nix {};