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
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 pathlib import Path
import pytest
from .common import simple_flake

system = environ.get("system")

files = simple_flake() | {"foo": {"flake.nix": File("{ outputs = _: {}; }")}, "subdir": {}}


@pytest.fixture(autouse=True)
def common_init(nix: Nix, files: Path):
    nix.settings.add_xp_feature("nix-command", "flakes")

    flake = files / "flake.nix"
    flake.write_text(f"""
        {{
            inputs.foo.url = "{files}/foo";
            outputs = a: {{
               packages.{system} = rec {{
                 test = import ./simple.nix;
                 default = test;
               }};
            }};
        }}
    """)


good_uris: list[list[str]] = [
    [],
    ["."],
    [".#"],
    [".#test"],
    ["../subdir"],
    ["../subdir#test"],
    ["$PWD"],
]


@with_files(files)
class TestFlakeSearchPath:
    @pytest.mark.parametrize("args", good_uris)
    def test_flake_search_goes_up(self, nix: Nix, files: Path, args: list[str]):
        subdir = files / "subdir"
        nix.nix(
            ["build", *[arg.replace("$PWD", str(subdir)) for arg in args]], cwd=subdir
        ).run().ok()

    def test_flake_search_inactive_with_path_uri(self, nix: Nix, files: Path):
        subdir = files / "subdir"
        assert (
            "does not contain a '/flake.nix' file"
            in nix.nix(["build", f"path:{subdir}"], cwd=subdir).run().expect(1).stderr_s
        )

    def test_flake_search_goes_up_without_installable(self, nix: Nix):
        nix.nix(["build", "--override-input", "foo", "."]).run().ok()

    def test_flake_inputs_do_not_search(self, nix: Nix, files: Path):
        flake = files / "flake.nix"
        flake.write_text(flake.read_text().replace(str(files / "foo"), str(files / "foo/subdir")))
        nix.nix(["build"]).run().expect(1)

    @pytest.mark.parametrize("args", good_uris)
    def test_flake_search_does_not_cross_git_repo(
        self, git: Git, nix: Nix, files: Path, args: list[str]
    ):
        subdir = files / "subdir"
        git(subdir, "init")
        assert (
            "is not part of a flake"
            in nix.nix(["build", *[arg.replace("$PWD", str(subdir)) for arg in args]], cwd=subdir)
            .run()
            .expect(1)
            .stderr_s
        )

    def test_flake_search_does_not_cross_git_repo_with_path_ru(
        self, git: Git, nix: Nix, files: Path
    ):
        subdir = files / "subdir"
        git(subdir, "init")
        assert (
            "does not contain a '/flake.nix' file"
            in nix.nix(["build", f"path:{subdir}"], cwd=subdir).run().expect(1).stderr_s
        )