summaryrefslogtreecommitdiff
path: root/pkgs/build-support/dev-shell-tools/tests/default.nix
blob: d1be8699b751548a415c86ac7f7c5c9dcc284bcb (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
  devShellTools,
  emptyFile,
  lib,
  stdenv,
  hello,
  writeText,
  zlib,
  nixosTests,
}:
let
  inherit (lib)
    concatLines
    escapeShellArg
    isString
    mapAttrsToList
    ;
in
lib.recurseIntoAttrs {

  # nix-build -A tests.devShellTools.nixos
  nixos = nixosTests.docker-tools-nix-shell;

  # nix-build -A tests.devShellTools.valueToString
  valueToString =
    let
      inherit (devShellTools) valueToString;
    in

    stdenv.mkDerivation {
      name = "devShellTools-valueToString-built-tests";

      # Test inputs
      inherit emptyFile hello;
      one = 1;
      boolTrue = true;
      boolFalse = false;
      foo = "foo";
      list = [
        1
        2
        3
      ];
      pathDefaultNix = ./default.nix;
      packages = [
        hello
        emptyFile
      ];
      # TODO: nested lists

      buildCommand = ''
        touch $out
        ( set -x
          [[ "$one" = ${escapeShellArg (valueToString 1)} ]]
          [[ "$boolTrue" = ${escapeShellArg (valueToString true)} ]]
          [[ "$boolFalse" = ${escapeShellArg (valueToString false)} ]]
          [[ "$foo" = ${escapeShellArg (valueToString "foo")} ]]
          [[ "$hello" = ${escapeShellArg (valueToString hello)} ]]
          [[ "$list" = ${
            escapeShellArg (valueToString [
              1
              2
              3
            ])
          } ]]
          [[ "$packages" = ${
            escapeShellArg (valueToString [
              hello
              emptyFile
            ])
          } ]]
          [[ "$pathDefaultNix" = ${escapeShellArg (valueToString ./default.nix)} ]]
          [[ "$emptyFile" = ${escapeShellArg (valueToString emptyFile)} ]]
        ) >log 2>&1 || { cat log; exit 1; }
      '';
    };

  # nix-build -A tests.devShellTools.valueToString
  unstructuredDerivationInputEnv =
    let
      inherit (devShellTools) unstructuredDerivationInputEnv;

      drvAttrs = {
        one = 1;
        boolTrue = true;
        boolFalse = false;
        foo = "foo";
        list = [
          1
          2
          3
        ];
        pathDefaultNix = ./default.nix;
        stringWithDep = "Exe: ${hello}/bin/hello";
        aPackageAttrSet = hello;
        anOutPath = hello.outPath;
        anAnAlternateOutput = zlib.dev;
        args = [
          "args must not be added to the environment"
          "Nix doesn't do it."
        ];

        passAsFile = [ "bar" ];
        bar = ''
          bar
          ${writeText "qux" "yea"}
        '';
      };
      result = unstructuredDerivationInputEnv { inherit drvAttrs; };
    in
    assert
      result // { barPath = "<check later>"; } == {
        one = "1";
        boolTrue = "1";
        boolFalse = "";
        foo = "foo";
        list = "1 2 3";
        pathDefaultNix = "${./default.nix}";
        stringWithDep = "Exe: ${hello}/bin/hello";
        aPackageAttrSet = "${hello}";
        anOutPath = "${hello.outPath}";
        anAnAlternateOutput = "${zlib.dev}";

        passAsFile = "bar";
        barPath = "<check later>";
      };

    # Not runCommand, because it alters `passAsFile`
    stdenv.mkDerivation (
      {
        name = "devShellTools-unstructuredDerivationInputEnv-built-tests";

        exampleBarPathString =
          assert isString result.barPath;
          result.barPath;

        dontUnpack = true;
        dontBuild = true;
        dontFixup = true;
        doCheck = true;

        installPhase = "touch $out";

        checkPhase = ''
          fail() {
            echo "$@" >&2
            exit 1
          }
          checkAttr() {
            echo checking attribute $1...
            if [[ "$2" != "$3" ]]; then
              echo "expected: $3"
              echo "actual: $2"
              exit 1
            fi
          }
          ${concatLines (
            mapAttrsToList (name: value: "checkAttr ${name} \"\$${name}\" ${escapeShellArg value}") (
              removeAttrs result [
                "args"

                # Nix puts it in workdir, which is not a concept for
                # unstructuredDerivationInputEnv, so we have to put it in the
                # store instead. This means the full path won't match.
                "barPath"
              ]
            )
          )}
          (
            set -x

            diff $exampleBarPathString $barPath
          )

          ''${args:+fail "args should not be set by Nix. We don't expect it to and unstructuredDerivationInputEnv removes it."}
          if [[ "''${builder:-x}" == x ]]; then
            fail "builder should be set by Nix. We don't remove it in unstructuredDerivationInputEnv."
          fi
        '';
      }
      // removeAttrs drvAttrs [
        # This would break the derivation. Instead, we have a check in the derivation to make sure Nix doesn't set it.
        "args"
      ]
    );
}