about summary refs log tree commit diff
path: root/pkgs/test/stdenv
diff options
context:
space:
mode:
authorArtturin <Artturin@artturin.com>2023-01-17 04:20:36 +0200
committerArtturin <Artturin@artturin.com>2023-02-17 22:23:33 +0200
commit8876a5c91faf35e37e08d647dd6e9ea9d4a4f79f (patch)
tree0a4a5f066933cb1a9c1c9fb870a14542295fa2f5 /pkgs/test/stdenv
parent792907e3bf429784a42ca57805025f2737c98eed (diff)
tests.stdenv: move patch-shebangs test
Diffstat (limited to 'pkgs/test/stdenv')
-rw-r--r--pkgs/test/stdenv/default.nix4
-rw-r--r--pkgs/test/stdenv/hooks.nix4
-rw-r--r--pkgs/test/stdenv/patch-shebangs.nix70
3 files changed, 74 insertions, 4 deletions
diff --git a/pkgs/test/stdenv/default.nix b/pkgs/test/stdenv/default.nix
index bad6eb7ba7aa3..91964213bbc6e 100644
--- a/pkgs/test/stdenv/default.nix
+++ b/pkgs/test/stdenv/default.nix
@@ -98,7 +98,7 @@ in
 
 {
   # tests for hooks in `stdenv.defaultNativeBuildInputs`
-  hooks = lib.recurseIntoAttrs (import ./hooks.nix { stdenv = bootStdenv; pkgs = earlyPkgs; });
+  hooks = lib.recurseIntoAttrs (import ./hooks.nix { stdenv = bootStdenv; pkgs = earlyPkgs; inherit lib; });
 
   outputs-no-out = runCommand "outputs-no-out-assert" {
     result = testers.testBuildFailure (stdenv.mkDerivation {
@@ -158,7 +158,7 @@ in
 
   structuredAttrsByDefault = lib.recurseIntoAttrs {
 
-    hooks = lib.recurseIntoAttrs (import ./hooks.nix { stdenv = bootStdenvStructuredAttrsByDefault; pkgs = earlyPkgs; });
+    hooks = lib.recurseIntoAttrs (import ./hooks.nix { stdenv = bootStdenvStructuredAttrsByDefault; pkgs = earlyPkgs; inherit lib; });
 
     test-cc-wrapper-substitutions = ccWrapperSubstitutionsTest {
       name = "test-cc-wrapper-substitutions-structuredAttrsByDefault";
diff --git a/pkgs/test/stdenv/hooks.nix b/pkgs/test/stdenv/hooks.nix
index 3d72efae6c479..eb1b3f61bda62 100644
--- a/pkgs/test/stdenv/hooks.nix
+++ b/pkgs/test/stdenv/hooks.nix
@@ -1,4 +1,4 @@
-{ stdenv, pkgs }:
+{ stdenv, pkgs, lib }:
 
 # ordering should match defaultNativeBuildInputs
 
@@ -91,7 +91,7 @@
     '';
   };
   # TODO: add multiple-outputs
-  # TODO: move patch-shebangs test from pkgs/test/patch-shebangs/default.nix to here
+  patch-shebangs = import ./patch-shebangs.nix { inherit stdenv lib pkgs; };
   prune-libtool-files =
     let
       libFoo = pkgs.writeText "libFoo" ''
diff --git a/pkgs/test/stdenv/patch-shebangs.nix b/pkgs/test/stdenv/patch-shebangs.nix
new file mode 100644
index 0000000000000..5c49787eee3b2
--- /dev/null
+++ b/pkgs/test/stdenv/patch-shebangs.nix
@@ -0,0 +1,70 @@
+{ lib, stdenv, runCommand }:
+
+let
+  tests = {
+    bad-shebang = stdenv.mkDerivation {
+      name         = "bad-shebang";
+      dontUnpack = true;
+      installPhase = ''
+        mkdir -p $out/bin
+        echo "#!/bin/sh" > $out/bin/test
+        echo "echo -n hello" >> $out/bin/test
+        chmod +x $out/bin/test
+      '';
+      passthru = {
+        assertion = "grep -v '^#!/bin/sh' $out/bin/test > /dev/null";
+      };
+    };
+
+    ignores-nix-store = stdenv.mkDerivation {
+      name = "ignores-nix-store";
+      dontUnpack = true;
+      installPhase = ''
+        mkdir -p $out/bin
+        echo "#!$NIX_STORE/path/to/sh" > $out/bin/test
+        echo "echo -n hello" >> $out/bin/test
+        chmod +x $out/bin/test
+      '';
+      passthru = {
+        assertion = "grep \"^#!$NIX_STORE/path/to/sh\" $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
+
+    echo -n "... $name: " >&2
+
+    local rc=0
+    (out=$testout eval "$assertion") || rc=1
+
+    if [ "$rc" -eq 0 ]; then
+      echo "yes" >&2
+    else
+      echo "no" >&2
+    fi
+
+    return "$rc"
+  }
+
+  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)}
+
+  if [ "$fail" ]; then
+    echo "failed"
+    exit 1
+  else
+    echo "succeeded"
+    touch $out
+  fi
+''