2023-01-17 06:55:51 +01:00
|
|
|
{ lib, stdenv, pkgs }:
|
|
|
|
|
|
|
|
# since the tests are using a early stdenv, the stdenv will have dontPatchShebangs=1, so it has to be unset
|
|
|
|
# https://github.com/NixOS/nixpkgs/blob/768a982bfc9d29a6bd3beb963ed4b054451ce3d0/pkgs/stdenv/linux/default.nix#L148-L153
|
|
|
|
|
|
|
|
# strictDeps has to be disabled because the shell isn't in buildInputs
|
2018-09-16 19:53:45 +02:00
|
|
|
|
|
|
|
let
|
2021-02-09 04:50:20 +01:00
|
|
|
tests = {
|
|
|
|
bad-shebang = stdenv.mkDerivation {
|
2023-01-17 06:55:51 +01:00
|
|
|
name = "bad-shebang";
|
|
|
|
strictDeps = false;
|
2021-02-09 04:50:20 +01:00
|
|
|
dontUnpack = true;
|
|
|
|
installPhase = ''
|
|
|
|
mkdir -p $out/bin
|
2023-01-17 06:55:51 +01:00
|
|
|
echo "#!/bin/bash" > $out/bin/test
|
2021-02-09 04:50:20 +01:00
|
|
|
echo "echo -n hello" >> $out/bin/test
|
|
|
|
chmod +x $out/bin/test
|
2023-01-17 06:55:51 +01:00
|
|
|
dontPatchShebangs=
|
2021-02-09 04:50:20 +01:00
|
|
|
'';
|
|
|
|
passthru = {
|
2023-01-17 06:55:51 +01:00
|
|
|
assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
|
2021-02-09 04:50:20 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
ignores-nix-store = stdenv.mkDerivation {
|
|
|
|
name = "ignores-nix-store";
|
2023-01-17 06:55:51 +01:00
|
|
|
strictDeps = false;
|
2021-02-09 04:50:20 +01:00
|
|
|
dontUnpack = true;
|
|
|
|
installPhase = ''
|
|
|
|
mkdir -p $out/bin
|
2023-01-17 06:55:51 +01:00
|
|
|
echo "#!$NIX_STORE/path/to/bash" > $out/bin/test
|
2021-02-09 04:50:20 +01:00
|
|
|
echo "echo -n hello" >> $out/bin/test
|
|
|
|
chmod +x $out/bin/test
|
2023-01-17 06:55:51 +01:00
|
|
|
dontPatchShebangs=
|
2021-02-09 04:50:20 +01:00
|
|
|
'';
|
|
|
|
passthru = {
|
2023-01-17 06:55:51 +01:00
|
|
|
assertion = "grep \"^#!$NIX_STORE/path/to/bash\" $out/bin/test > /dev/null";
|
2021-02-09 04:50:20 +01:00
|
|
|
};
|
|
|
|
};
|
2023-01-17 06:55:51 +01:00
|
|
|
|
2023-06-19 07:56:21 +02:00
|
|
|
updates-nix-store = stdenv.mkDerivation {
|
|
|
|
name = "updates-nix-store";
|
|
|
|
strictDeps = false;
|
|
|
|
dontUnpack = true;
|
|
|
|
installPhase = ''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
echo "#!$NIX_STORE/path/to/bash" > $out/bin/test
|
|
|
|
echo "echo -n hello" >> $out/bin/test
|
|
|
|
chmod +x $out/bin/test
|
|
|
|
patchShebangs --update $out/bin/test
|
|
|
|
dontPatchShebangs=1
|
|
|
|
'';
|
|
|
|
passthru = {
|
|
|
|
assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-01-17 06:55:51 +01:00
|
|
|
split-string = stdenv.mkDerivation {
|
|
|
|
name = "split-string";
|
|
|
|
strictDeps = false;
|
|
|
|
dontUnpack = true;
|
|
|
|
installPhase = ''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
echo "#!/usr/bin/env -S bash --posix" > $out/bin/test
|
|
|
|
echo "echo -n hello" >> $out/bin/test
|
|
|
|
chmod +x $out/bin/test
|
|
|
|
dontPatchShebangs=
|
|
|
|
'';
|
|
|
|
passthru = {
|
|
|
|
assertion = "grep -v '^#!${pkgs.coreutils}/bin/env -S ${stdenv.shell} --posix' $out/bin/test > /dev/null";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-09-16 19:53:45 +02:00
|
|
|
};
|
2023-01-17 06:55:51 +01:00
|
|
|
in
|
|
|
|
stdenv.mkDerivation {
|
|
|
|
name = "test-patch-shebangs";
|
2023-06-19 07:56:21 +02:00
|
|
|
passthru = { inherit (tests) bad-shebang ignores-nix-store updates-nix-store split-string; };
|
2023-01-17 06:55:51 +01:00
|
|
|
buildCommand = ''
|
|
|
|
validate() {
|
|
|
|
local name=$1
|
|
|
|
local testout=$2
|
|
|
|
local assertion=$3
|
2021-02-09 04:50:20 +01:00
|
|
|
|
2023-01-17 06:55:51 +01:00
|
|
|
echo -n "... $name: " >&2
|
2021-02-09 04:50:20 +01:00
|
|
|
|
2023-01-17 06:55:51 +01:00
|
|
|
local rc=0
|
|
|
|
(out=$testout eval "$assertion") || rc=1
|
2021-02-09 04:50:20 +01:00
|
|
|
|
2023-01-17 06:55:51 +01:00
|
|
|
if [ "$rc" -eq 0 ]; then
|
|
|
|
echo "yes" >&2
|
|
|
|
else
|
|
|
|
echo "no" >&2
|
|
|
|
fi
|
2021-02-09 04:50:20 +01:00
|
|
|
|
2023-01-17 06:55:51 +01:00
|
|
|
return "$rc"
|
|
|
|
}
|
2021-02-09 04:50:20 +01:00
|
|
|
|
2023-01-17 06:55:51 +01:00
|
|
|
echo "checking whether patchShebangs works properly... ">&2
|
2021-02-09 04:50:20 +01:00
|
|
|
|
2023-01-17 06:55:51 +01:00
|
|
|
fail=
|
|
|
|
${lib.concatStringsSep "\n" (lib.mapAttrsToList (_: test: ''
|
|
|
|
validate "${test.name}" "${test}" ${lib.escapeShellArg test.assertion} || fail=1
|
|
|
|
'') tests)}
|
2021-02-09 04:50:20 +01:00
|
|
|
|
2023-01-17 06:55:51 +01:00
|
|
|
if [ "$fail" ]; then
|
|
|
|
echo "failed"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "succeeded"
|
|
|
|
touch $out
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
}
|