summary refs log tree commit diff
path: root/pkgs/test/make-binary-wrapper/default.nix
blob: 0b9f7e5abea2e1dacd27301db7254cea79363548 (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
{ lib, stdenv, runCommand, makeBinaryWrapper }:

let
  makeGoldenTest = { name, filename }: stdenv.mkDerivation {
    name = name;
    dontUnpack = true;
    buildInputs = [ makeBinaryWrapper ];
    phases = [ "installPhase" ];
    installPhase = ''
      source ${./golden-test-utils.sh}
      mkdir -p $out/bin
      command=$(getInputCommand "${filename}")
      eval "$command" > "$out/bin/result"
    '';
    passthru = {
      assertion = ''
        source ${./golden-test-utils.sh}
        contents=$(getOutputText "${filename}")
        echo "$contents" | diff $out/bin/result -
      '';
    };
  };
  tests = {
    add-flags = makeGoldenTest { name = "add-flags"; filename = ./add-flags.c; };
    argv0 = makeGoldenTest { name = "argv0"; filename = ./argv0.c; };
    basic = makeGoldenTest { name = "basic"; filename = ./basic.c; };
    combination = makeGoldenTest { name = "combination"; filename = ./combination.c; };
    env = makeGoldenTest { name = "env"; filename = ./env.c; };
    prefix = makeGoldenTest { name = "prefix"; filename = ./prefix.c; };
    suffix = makeGoldenTest { name = "suffix"; filename = ./suffix.c; };
  };
in runCommand "make-binary-wrapper-test" {
  passthru = tests;
  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 makeCWrapper 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
''