about summary refs log tree commit diff
path: root/nixpkgs/pkgs/test/stdenv/patch-shebangs.nix
blob: db9ca2fcaafef398e688eb83922ce9514e10e0ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{ 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";
      strictDeps = false;
      dontUnpack = true;
      installPhase = ''
        mkdir -p $out/bin
        echo "#!/bin/bash" > $out/bin/test
        echo "echo -n hello" >> $out/bin/test
        chmod +x $out/bin/test
        dontPatchShebangs=
      '';
      passthru = {
        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/bash" > $out/bin/test
        echo "echo -n hello" >> $out/bin/test
        chmod +x $out/bin/test
        dontPatchShebangs=
      '';
      passthru = {
        assertion = "grep \"^#!$NIX_STORE/path/to/bash\" $out/bin/test > /dev/null";
      };
    };

    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";
      };
    };

    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";
      };
    };

    without-trailing-newline = stdenv.mkDerivation {
      name = "without-trailing-newline";
      strictDeps = false;
      dontUnpack = true;
      installPhase = ''
        mkdir -p $out/bin
        printf "#!/bin/bash" > $out/bin/test
        chmod +x $out/bin/test
        dontPatchShebangs=
      '';
      passthru = {
        assertion = "grep '^#!${stdenv.shell}' $out/bin/test > /dev/null";
      };
    };

  };
in
stdenv.mkDerivation {
  name = "test-patch-shebangs";
  passthru = { inherit (tests) bad-shebang ignores-nix-store updates-nix-store split-string without-trailing-newline; };
  buildCommand = ''
    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
  '';
}