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
import pytest
from testlib.fixtures.nix import Nix
from testlib.fixtures.file_helper import File, with_files


class TestImpureEval:
    @pytest.fixture(autouse=True)
    def setup(self, nix: Nix):
        nix.settings.add_xp_feature("nix-command")
        self.nix = nix

    def eval(self, expr: str, *args: str) -> str:
        return (
            self.nix.nix(["eval", "--impure", "--raw", *args, "--expr", expr])
            .run()
            .ok()
            .stdout_plain
        )

    @pytest.mark.parametrize("path", ["/foo", "/bar"])
    def test_store_dir(self, path: str):
        result = self.eval("builtins.storeDir", "--store", f"dummy://?store={path}")
        assert result == path

    @pytest.mark.parametrize("system", ["foo", "bar"])
    def test_current_system_system_only(self, system: str):
        result = self.eval("builtins.currentSystem", "--system", system)
        assert result == system

    @pytest.mark.parametrize("system", ["foo", "bar"])
    def test_current_system_both(self, system: str):
        result = self.eval("builtins.currentSystem", "--system", system, "--eval-system", system)
        assert result == system

    @pytest.mark.parametrize("system", ["foo", "bar"])
    def test_current_system_eval_system_only(self, system: str):
        result = self.eval("builtins.currentSystem", "--eval-system", system)
        assert result == system

    @pytest.mark.parametrize("system", ["foo", "bar"])
    def test_current_system_both_override(self, system: str):
        result = self.eval("builtins.currentSystem", "--system", system, "--eval-system", "baz")
        assert result == "baz"

    @with_files({"eval.nix": File('{ attr.foo = "bar"; }')})
    def test_nix_path_search(self, nix: Nix):
        result = (
            nix.nix(
                [
                    "eval",
                    "--option",
                    "nix-path",
                    f"foo={nix.env.dirs.home}",
                    "-f",
                    "<foo/eval.nix>",
                    "attr.foo",
                ]
            )
            .run()
            .ok()
        )
        assert result.stdout_plain == '"bar"'