diff options
author | Robert Hensing <roberth@users.noreply.github.com> | 2023-07-27 10:41:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-27 10:41:20 +0200 |
commit | f705094eac9feaafa7c35ad525fe454d4fabc5c8 (patch) | |
tree | 925a16993586c7a2cf02428aa123a9e2d8168ca1 | |
parent | 8ab9fbba17efb60aa2ba14f583308e0667027523 (diff) | |
parent | 0dd3989cef1fb458101ab6fd94221449401ec2af (diff) | |
download | nixpkgs-f705094eac9feaafa7c35ad525fe454d4fabc5c8.tar nixpkgs-f705094eac9feaafa7c35ad525fe454d4fabc5c8.tar.gz nixpkgs-f705094eac9feaafa7c35ad525fe454d4fabc5c8.tar.bz2 nixpkgs-f705094eac9feaafa7c35ad525fe454d4fabc5c8.tar.lz nixpkgs-f705094eac9feaafa7c35ad525fe454d4fabc5c8.tar.xz nixpkgs-f705094eac9feaafa7c35ad525fe454d4fabc5c8.tar.zst nixpkgs-f705094eac9feaafa7c35ad525fe454d4fabc5c8.zip |
Merge pull request #245524 from hercules-ci/writeScriptBin-meta.mainProgram
Add meta.mainProgram to writeCBin, add tests
7 files changed, 231 insertions, 41 deletions
diff --git a/pkgs/build-support/trivial-builders/default.nix b/pkgs/build-support/trivial-builders/default.nix index 9099a38b75b..d095974513c 100644 --- a/pkgs/build-support/trivial-builders/default.nix +++ b/pkgs/build-support/trivial-builders/default.nix @@ -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 diff --git a/pkgs/build-support/trivial-builders/test/default.nix b/pkgs/build-support/trivial-builders/test/default.nix new file mode 100644 index 00000000000..683f4b9fd04 --- /dev/null +++ b/pkgs/build-support/trivial-builders/test/default.nix @@ -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 {}; +} diff --git a/pkgs/build-support/trivial-builders/test/write-text-file.nix b/pkgs/build-support/trivial-builders/test/write-text-file.nix index ac83a75fca4..2e6685c1980 100644 --- a/pkgs/build-support/trivial-builders/test/write-text-file.nix +++ b/pkgs/build-support/trivial-builders/test/write-text-file.nix @@ -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 - - 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 - ''; +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 + ''; + }; } diff --git a/pkgs/build-support/trivial-builders/test/writeCBin.nix b/pkgs/build-support/trivial-builders/test/writeCBin.nix new file mode 100644 index 00000000000..56cab45b380 --- /dev/null +++ b/pkgs/build-support/trivial-builders/test/writeCBin.nix @@ -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 + '' + diff --git a/pkgs/build-support/trivial-builders/test/writeScriptBin.nix b/pkgs/build-support/trivial-builders/test/writeScriptBin.nix new file mode 100644 index 00000000000..1487443130d --- /dev/null +++ b/pkgs/build-support/trivial-builders/test/writeScriptBin.nix @@ -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 + '' + diff --git a/pkgs/build-support/trivial-builders/test/writeShellScriptBin.nix b/pkgs/build-support/trivial-builders/test/writeShellScriptBin.nix new file mode 100644 index 00000000000..e93410e25bc --- /dev/null +++ b/pkgs/build-support/trivial-builders/test/writeShellScriptBin.nix @@ -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 + '' + diff --git a/pkgs/test/default.nix b/pkgs/test/default.nix index b6793d25b6e..6bfa1c4393c 100644 --- a/pkgs/test/default.nix +++ b/pkgs/test/default.nix @@ -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 {}; |