summary refs log tree commit diff
path: root/pkgs/test/patch-shebangs/default.nix
blob: 5c49787eee3b230ffce6dc6363559bd28a42e88c (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
{ 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
''