about summary refs log tree commit diff
path: root/pkgs/test/stdenv
diff options
context:
space:
mode:
authorArtturin <Artturin@artturin.com>2023-01-17 07:55:51 +0200
committerArtturin <Artturin@artturin.com>2023-02-17 22:23:35 +0200
commit883daacbaaabbf826bffe9a414b06e1d935e3133 (patch)
treeed22ed3e1fd6a07a9bdd386d4949ef3e3c1c60e3 /pkgs/test/stdenv
parent8876a5c91faf35e37e08d647dd6e9ea9d4a4f79f (diff)
tests.stdenv: add hooks.patch-shebangs.split-string & tweak tests
Diffstat (limited to 'pkgs/test/stdenv')
-rw-r--r--pkgs/test/stdenv/patch-shebangs.nix102
1 files changed, 65 insertions, 37 deletions
diff --git a/pkgs/test/stdenv/patch-shebangs.nix b/pkgs/test/stdenv/patch-shebangs.nix
index 5c49787eee3b2..fb52f38ecc91b 100644
--- a/pkgs/test/stdenv/patch-shebangs.nix
+++ b/pkgs/test/stdenv/patch-shebangs.nix
@@ -1,70 +1,98 @@
-{ lib, stdenv, runCommand }:
+{ 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
 
 let
   tests = {
     bad-shebang = stdenv.mkDerivation {
-      name         = "bad-shebang";
+      name = "bad-shebang";
+      strictDeps = false;
       dontUnpack = true;
       installPhase = ''
         mkdir -p $out/bin
-        echo "#!/bin/sh" > $out/bin/test
+        echo "#!/bin/bash" > $out/bin/test
         echo "echo -n hello" >> $out/bin/test
         chmod +x $out/bin/test
+        dontPatchShebangs=
       '';
       passthru = {
-        assertion = "grep -v '^#!/bin/sh' $out/bin/test > /dev/null";
+        assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
       };
     };
 
     ignores-nix-store = stdenv.mkDerivation {
       name = "ignores-nix-store";
+      strictDeps = false;
       dontUnpack = true;
       installPhase = ''
         mkdir -p $out/bin
-        echo "#!$NIX_STORE/path/to/sh" > $out/bin/test
+        echo "#!$NIX_STORE/path/to/bash" > $out/bin/test
         echo "echo -n hello" >> $out/bin/test
         chmod +x $out/bin/test
+        dontPatchShebangs=
       '';
       passthru = {
-        assertion = "grep \"^#!$NIX_STORE/path/to/sh\" $out/bin/test > /dev/null";
+        assertion = "grep \"^#!$NIX_STORE/path/to/bash\" $out/bin/test > /dev/null";
       };
     };
+
+    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";
+      };
+    };
+
   };
-in runCommand "patch-shebangs-test" {
-  passthru = { inherit (tests) bad-shebang ignores-nix-store; };
-  meta.platforms = lib.platforms.all;
-} ''
-  validate() {
-    local name=$1
-    local testout=$2
-    local assertion=$3
+in
+stdenv.mkDerivation {
+  name = "test-patch-shebangs";
+  passthru = { inherit (tests) bad-shebang ignores-nix-store split-string; };
+  buildCommand = ''
+    validate() {
+      local name=$1
+      local testout=$2
+      local assertion=$3
 
-    echo -n "... $name: " >&2
+      echo -n "... $name: " >&2
 
-    local rc=0
-    (out=$testout eval "$assertion") || rc=1
+      local rc=0
+      (out=$testout eval "$assertion") || rc=1
 
-    if [ "$rc" -eq 0 ]; then
-      echo "yes" >&2
-    else
-      echo "no" >&2
-    fi
+      if [ "$rc" -eq 0 ]; then
+        echo "yes" >&2
+      else
+        echo "no" >&2
+      fi
 
-    return "$rc"
-  }
+      return "$rc"
+    }
 
-  echo "checking whether patchShebangs works properly... ">&2
+    echo "checking whether patchShebangs works properly... ">&2
 
-  fail=
-  ${lib.concatStringsSep "\n" (lib.mapAttrsToList (_: test: ''
-    validate "${test.name}" "${test}" ${lib.escapeShellArg test.assertion} || fail=1
-  '') tests)}
+    fail=
+    ${lib.concatStringsSep "\n" (lib.mapAttrsToList (_: test: ''
+      validate "${test.name}" "${test}" ${lib.escapeShellArg test.assertion} || fail=1
+    '') tests)}
 
-  if [ "$fail" ]; then
-    echo "failed"
-    exit 1
-  else
-    echo "succeeded"
-    touch $out
-  fi
-''
+    if [ "$fail" ]; then
+      echo "failed"
+      exit 1
+    else
+      echo "succeeded"
+      touch $out
+    fi
+  '';
+}