patchShebangs: add more tests

- Add test to ensure ability to patch read-only-files (see https://github.com/NixOS/nixpkgs/pull/414448#issuecomment-3016010151)
- Add test to ensure the timestamp is preserved
- Add test to ensure read-only permissions are preserved
This commit is contained in:
DavHau 2025-07-01 12:20:54 +07:00
parent d762c7ac78
commit f2f55c5267

View File

@ -105,6 +105,105 @@ let
assertion = "grep '^#!/usr/bin/builtin' $out/bin/test > /dev/null";
};
};
read-only-script =
(derivation {
name = "read-only-script";
system = stdenv.buildPlatform.system;
builder = "${stdenv.__bootPackages.stdenv.__bootPackages.bashNonInteractive}/bin/bash";
initialPath = [
stdenv.__bootPackages.stdenv.__bootPackages.coreutils
];
strictDeps = false;
args = [
"-c"
''
set -euo pipefail
. ${../../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
patchShebangs $out/bin/test
''
];
assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
})
// {
meta = { };
};
preserves-read-only =
(derivation {
name = "preserves-read-only";
system = stdenv.buildPlatform.system;
builder = "${stdenv.__bootPackages.stdenv.__bootPackages.bashNonInteractive}/bin/bash";
initialPath = [
stdenv.__bootPackages.stdenv.__bootPackages.coreutils
];
strictDeps = false;
args = [
"-c"
''
set -euo pipefail
. ${../../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
''
];
assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
})
// {
meta = { };
};
# Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281
preserves-timestamp =
(derivation {
name = "preserves-timestamp";
system = stdenv.buildPlatform.system;
builder = "${stdenv.__bootPackages.stdenv.__bootPackages.bashNonInteractive}/bin/bash";
initialPath = [
stdenv.__bootPackages.stdenv.__bootPackages.coreutils
];
strictDeps = false;
args = [
"-c"
''
set -euo pipefail
. ${../../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 +x $out/bin/test
# Set a specific timestamp (2000-01-01 00:00:00)
touch -t 200001010000 $out/bin/test
original_timestamp=$(stat -c %Y $out/bin/test)
patchShebangs $out/bin/test
new_timestamp=$(stat -c %Y $out/bin/test)
if ! [ "$original_timestamp" = "$new_timestamp" ]; then
echo "Timestamp changed from $original_timestamp to $new_timestamp"
exit 1
fi
''
];
assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
})
// {
meta = { };
};
};
in
stdenv.mkDerivation {
@ -116,6 +215,10 @@ stdenv.mkDerivation {
updates-nix-store
split-string
without-trailing-newline
dont-patch-builtins
read-only-script
preserves-read-only
preserves-timestamp
;
};
buildCommand = ''