about summary refs log tree commit diff
path: root/pkgs/build-support/trivial-builders/test/write-text-file.nix
blob: 2e6685c1980bf77311500701ff347df78c9cfc8c (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
/*
  To run:

      cd nixpkgs
      nix-build -A tests.trivial-builders.writeTextFile

  or to run an individual test case

      cd nixpkgs
      nix-build -A tests.trivial-builders.writeTextFile.foo
*/
{ lib, runCommand, runtimeShell, writeTextFile }:
let
  veryWeirdName = ''here's a name with some "bad" characters, like spaces and quotes'';
in
lib.recurseIntoAttrs {

  different-exe-name =
    let
      pkg = writeTextFile {
        name = "bar";
        destination = "/bin/foo";
        executable = true;
        text = ''
          #!${runtimeShell}
          echo hi
        '';
      };
    in
      assert pkg.meta.mainProgram == "foo";
      assert baseNameOf (lib.getExe pkg) == "foo";
      assert pkg.name == "bar";
      runCommand "test-writeTextFile-different-exe-name" {} ''
        PATH="${lib.makeBinPath [ pkg ]}:$PATH"
        x=$(foo)
        [[ "$x" == hi ]]
        touch $out
      '';

  weird-name = writeTextFile {
    name = "weird-names";
    destination = "/etc/${veryWeirdName}";
    text = ''passed!'';
    checkPhase = ''
      # intentionally hardcode everything here, to make sure
      # Nix does not mess with file paths

      name="here's a name with some \"bad\" characters, like spaces and quotes"
      fullPath="$out/etc/$name"

      if [ -f "$fullPath" ]; then
        echo "[PASS] File exists!"
      else
        echo "[FAIL] File was not created at expected path!"
        exit 1
      fi

      content=$(<"$fullPath")
      expected="passed!"

      if [ "$content" = "$expected" ]; then
        echo "[PASS] Contents match!"
      else
        echo "[FAIL] File contents don't match!"
        echo "       Expected: $expected"
        echo "       Got:      $content"
        exit 2
      fi
    '';
  };
}