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
from testlib.fixtures.nix import Nix
from testlib.fixtures.git import Git
from testlib.fixtures.file_helper import with_files, File
from testlib.environ import environ
from testlib.utils import get_global_asset_pack, get_global_asset
from pathlib import Path
import pytest

system = environ.get("system")

files = {
    "templates": get_global_asset_pack(".git")
    | {
        "flake.nix": File("""{
          description = "Some templates";

          outputs = { self }: {
            templates = rec {
              trivial = {
                path = ./trivial;
                description = "A trivial flake";
                welcomeText = ''
                    Welcome to my trivial flake
                '';
              };
              default = trivial;
            };
          };
        }"""),
        "trivial": {
            "flake.nix": File(f"""{{
              description = "A flake for building Hello World";

              outputs = {{ self, nixpkgs }}: {{
                packages.{system} = rec {{
                  hello = nixpkgs.legacyPackages.{system}.hello;
                  default = hello;
                }};
              }};
            }}"""),
            "a": File("a"),
            "b": File("b"),
        },
    },
    "flake": get_global_asset_pack(".git"),
    "nixpkgs": get_global_asset_pack(".git")
    | {
        "config.nix": get_global_asset("config.nix"),
        "flake.nix": File(f"""{{
          outputs = {{ self }}: {{
            legacyPackages.{system}.hello =
              with import ./config.nix;
              mkDerivation {{
                name = "hello";
                buildCommand = "echo hello > $out";
              }};
          }};
        }}"""),
    },
    "registry.json": File(""),
}


@pytest.fixture(autouse=True)
def common_init(nix: Nix, files: Path, git: Git):
    nix.settings.add_xp_feature("nix-command", "flakes")
    nix.settings["flake-registry"] = str(files / "registry.json")

    git(files / "templates", "add", "flake.nix", "trivial/")
    git(files / "templates", "commit", "-m", "Initial")
    git(files / "nixpkgs", "add", "flake.nix", "config.nix")

    nix.nix(
        [
            "registry",
            "add",
            "--registry",
            files / "registry.json",
            "templates",
            f"git+file://{files}/templates",
        ]
    ).run().ok()
    nix.nix(
        [
            "registry",
            "add",
            "--registry",
            files / "registry.json",
            "nixpkgs",
            f"git+file://{files}/nixpkgs",
        ]
    ).run().ok()


@with_files(files)
class TestFlakeTemplates:
    def test_check(self, nix: Nix, files: Path):
        assert (
            "checking template 'templates.trivial'"
            in nix.nix(["flake", "check", files / "templates"]).run().ok().stderr_s
        )

    def test_show(self, nix: Nix, files: Path):
        assert (
            "A trivial flake" in nix.nix(["flake", "show", files / "templates"]).run().ok().stdout_s
        )

    def test_show_json(self, nix: Nix, files: Path):
        result = nix.nix(["flake", "show", files / "templates", "--json"]).run().json()
        assert result["templates"]["default"]["description"] == "A trivial flake"

    def test_init(self, nix: Nix, files: Path):
        stderr = nix.nix(["flake", "init"], cwd=files / "flake").run().ok().stderr_s
        assert f"wrote: {files}/flake/flake.nix" in stderr

    class TestWithInit:
        @pytest.fixture(autouse=True)
        def init_flake(self, nix: Nix, files: Path):
            nix.nix(["flake", "init"], cwd=files / "flake").run().ok()

        def test_init_idempotent(self, nix: Nix, files: Path):
            stderr = nix.nix(["flake", "init"], cwd=files / "flake").run().ok().stderr_s
            assert "skipping identical file" in stderr

        class TestInRepo:
            @pytest.fixture(autouse=True)
            def init_repo(self, files: Path, git: Git):
                git(files / "flake", "add", "flake.nix")

            def test_check(self, nix: Nix, files: Path):
                stderr = nix.nix(["flake", "check", files / "flake"]).run().ok().stderr_s
                assert f"fetching git input 'git+file://{files}/flake'" in stderr
                assert f"fetching git input 'git+file://{files}/nixpkgs'" in stderr
                assert "evaluating flake" in stderr

            def test_show(self, nix: Nix, files: Path):
                stdout = nix.nix(["flake", "show", files / "flake"]).run().ok().stdout_s
                assert "hello: package 'hello'" in stdout

            def test_show_json(self, nix: Nix, files: Path):
                result = nix.nix(["flake", "show", files / "flake", "--json"]).run().json()
                assert result["packages"][system]["default"]["name"] == "hello"
                assert result["packages"][system]["hello"]["name"] == "hello"

    def test_benign_conflict_idempotence(self, nix: Nix, files: Path):
        (files / "flake/a").write_text("a")

        stderr = nix.nix(["flake", "init"], cwd=files / "flake").run().ok().stderr_s
        assert "skipping identical file" in stderr
        assert f"wrote: {files}/flake/flake.nix" in stderr

    def test_init_conflict(self, nix: Nix, files: Path):
        (files / "flake/a").write_text("b")

        stderr = nix.nix(["flake", "init"], cwd=files / "flake").run().expect(1).stderr_s
        assert f"refusing to overwrite existing file '{files}/flake/a'" in stderr

    def test_new(self, nix: Nix):
        stderr = nix.nix(["flake", "new", "-t", "templates#trivial", "flake2"]).run().ok().stderr_s
        assert f"wrote: {nix.env.dirs.home}/flake2/flake.nix" in stderr

    class TestNew:
        def test_idempotent(self, nix: Nix, files: Path):
            nix.nix(["flake", "new", "-t", "templates#trivial", files / "flake2"]).run().ok()
            stderr = (
                nix.nix(["flake", "new", "-t", "templates#trivial", files / "flake2"])
                .run()
                .ok()
                .stderr_s
            )
            assert "skipping identical file" in stderr

        def test_check(self, nix: Nix, files: Path):
            nix.nix(["flake", "new", "-t", "templates#trivial", files / "flake2"]).run().ok()
            stderr = nix.nix(["flake", "check", files / "flake2"]).run().ok().stderr_s
            assert "evaluating flake" in stderr