nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at python-updates 147 lines 3.7 kB view raw
1# Run with: 2# nix-build -A tests.trivial-builders.writeShellApplication 3{ 4 writeShellApplication, 5 writeTextFile, 6 runCommand, 7 lib, 8 linkFarm, 9 diffutils, 10 hello, 11}: 12let 13 checkShellApplication = 14 args@{ name, expected, ... }: 15 let 16 writeShellApplicationArgs = removeAttrs args [ "expected" ]; 17 script = writeShellApplication writeShellApplicationArgs; 18 executable = lib.getExe script; 19 expected' = writeTextFile { 20 name = "${name}-expected"; 21 text = expected; 22 }; 23 actual = "${name}-actual"; 24 in 25 runCommand name { } '' 26 echo "Running test executable ${name}" 27 ${executable} > ${actual} 28 echo "Got output from test executable:" 29 cat ${actual} 30 echo "Checking test output against expected output:" 31 ${diffutils}/bin/diff --color --unified ${expected'} ${actual} 32 touch $out 33 ''; 34in 35linkFarm "writeShellApplication-tests" { 36 test-meta = 37 let 38 args = { 39 name = "test-meta"; 40 text = ""; 41 meta.description = "Test for the `writeShellApplication` `meta` argument"; 42 }; 43 script = writeShellApplication args; 44 in 45 assert script.meta.mainProgram == args.name; 46 assert script.meta.description == args.meta.description; 47 script; 48 49 test-runtime-inputs = checkShellApplication { 50 name = "test-runtime-inputs"; 51 text = '' 52 hello 53 ''; 54 runtimeInputs = [ hello ]; 55 expected = "Hello, world!\n"; 56 }; 57 58 test-runtime-env = checkShellApplication { 59 name = "test-runtime-env"; 60 runtimeEnv = { 61 MY_COOL_ENV_VAR = "my-cool-env-value"; 62 MY_OTHER_COOL_ENV_VAR = "my-other-cool-env-value"; 63 # Check that we can serialize a bunch of different types: 64 BOOL = true; 65 INT = 1; 66 LIST = [ 67 1 68 2 69 3 70 ]; 71 MAP = { 72 a = "a"; 73 b = "b"; 74 }; 75 }; 76 text = '' 77 echo "$MY_COOL_ENV_VAR" 78 echo "$MY_OTHER_COOL_ENV_VAR" 79 ''; 80 expected = '' 81 my-cool-env-value 82 my-other-cool-env-value 83 ''; 84 }; 85 86 test-check-phase = checkShellApplication { 87 name = "test-check-phase"; 88 text = ""; 89 checkPhase = '' 90 echo "echo -n hello" > $target 91 ''; 92 expected = "hello"; 93 }; 94 95 test-argument-forwarding = checkShellApplication { 96 name = "test-argument-forwarding"; 97 text = ""; 98 derivationArgs.MY_BUILD_TIME_VARIABLE = "puppy"; 99 derivationArgs.postCheck = '' 100 if [[ "$MY_BUILD_TIME_VARIABLE" != puppy ]]; then 101 echo "\$MY_BUILD_TIME_VARIABLE is not set to 'puppy'!" 102 exit 1 103 fi 104 ''; 105 meta.description = "Test checking that `writeShellApplication` forwards extra arguments to `stdenv.mkDerivation`"; 106 expected = ""; 107 }; 108 109 test-exclude-shell-checks = writeShellApplication { 110 name = "test-exclude-shell-checks"; 111 excludeShellChecks = [ "SC2016" ]; 112 text = '' 113 # Triggers SC2016: Expressions don't expand in single quotes, use double 114 # quotes for that. 115 echo '$SHELL' 116 ''; 117 }; 118 119 test-bash-options-pipefail = checkShellApplication { 120 name = "test-bash-options-pipefail"; 121 text = '' 122 touch my-test-file 123 echo puppy | grep doggy | sed 's/doggy/puppy/g' 124 # ^^^^^^^^^^ This will fail. 125 true 126 ''; 127 # Don't use `pipefail`: 128 bashOptions = [ 129 "errexit" 130 "nounset" 131 ]; 132 expected = ""; 133 }; 134 135 test-bash-options-nounset = checkShellApplication { 136 name = "test-bash-options-nounset"; 137 text = '' 138 echo -n "$someUndefinedVariable" 139 ''; 140 # Don't use `nounset`: 141 bashOptions = [ ]; 142 # Don't warn about the undefined variable at build time: 143 excludeShellChecks = [ "SC2154" ]; 144 expected = ""; 145 }; 146 147}