diff --git a/pkgs/build-support/setup-hooks/patch-shebangs.sh b/pkgs/build-support/setup-hooks/patch-shebangs.sh index 5bd1f5078ad00..6ee5987f905d0 100644 --- a/pkgs/build-support/setup-hooks/patch-shebangs.sh +++ b/pkgs/build-support/setup-hooks/patch-shebangs.sh @@ -133,9 +133,9 @@ patchShebangs() { sed -e "1 s|.*|#\!$escapedInterpreterLine|" "$f" > "$tmpFile" # Make original file writable if it is read-only - local restoreReadOnly + local restoreReadOnly= if [[ ! -w "$f" ]]; then - chmod +w "$f" + chmod u+w "$f" restoreReadOnly=true fi @@ -144,7 +144,7 @@ patchShebangs() { cat "$tmpFile" > "$f" rm "$tmpFile" if [[ -n "${restoreReadOnly:-}" ]]; then - chmod -w "$f" + chmod u-w "$f" fi touch --date "$timestamp" "$f" diff --git a/pkgs/test/stdenv/patch-shebangs.nix b/pkgs/test/stdenv/patch-shebangs.nix index 487d144e77337..12d26ccb5a974 100644 --- a/pkgs/test/stdenv/patch-shebangs.nix +++ b/pkgs/test/stdenv/patch-shebangs.nix @@ -164,19 +164,37 @@ let . ${../../stdenv/generic/setup.sh} . ${../../build-support/setup-hooks/patch-shebangs.sh} mkdir -p $out/bin - echo "#!/bin/bash" > $out/bin/test - echo "echo -n hello" >> $out/bin/test - chmod 555 $out/bin/test - original_perms=$(stat -c %a $out/bin/test) - patchShebangs $out/bin/test - new_perms=$(stat -c %a $out/bin/test) - if ! [ "$original_perms" = "$new_perms" ]; then - echo "Permissions changed from $original_perms to $new_perms" - exit 1 - fi + modes=(555 575 755 775 777) + for mode in "''${modes[@]}" + do + target=$out/bin/test-$mode + echo "#!/bin/bash" > "$target" + echo "echo -n hello" >> "$target" + chmod $mode "$target" + if ! [ "$mode" = "$(stat -c %a "$target")" ]; then + echo "chmod didn't set up test permissions as expected" + exit 1 + fi + done + # This is structured as two loops + one patchShebangs so that at + # least one test exercises patchShebangs on multiple scripts in one + # invocation. + patchShebangs $out/bin + for mode in "''${modes[@]}" + do + target=$out/bin/test-$mode + new_perms=$(stat -c %a "$target") + if ! [ "$mode" = "$new_perms" ]; then + echo "Permissions changed from $mode to $new_perms" + exit 1 + fi + done '' ]; - assertion = "grep '^#!${lib.getExe earlyBash}' $out/bin/test > /dev/null"; + # This tests that any of the test files have this line, when we should + # really be testing all of them. But it's a little redundant with other + # tests anyway. + assertion = "grep '^#!${lib.getExe earlyBash}' $out/bin/test-* > /dev/null"; }) // { meta = { };