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
from testlib.fixtures.nix import Nix
from testlib.fixtures.file_helper import with_files, File
from testlib.fixtures.command import CommandResult
from pathlib import Path
from .common import simple_flake
import tarfile
import pytest

flake_nix = """
{
  inputs.flake2.url = "file://$TEST_ROOT/flake.tar.gz";

  outputs = { self, flake2 }: {

    a1 = builtins.fetchTarball {
      #type = "tarball";
      url = "file://$TEST_ROOT/flake.tar.gz";
      sha256 = "$hash";
    };

    a2 = ./foo;

    a3 = ./.;

    a4 = self.outPath;

    # FIXME
    a5 = self;

    a6 = flake2.outPath;

    # FIXME
    a7 = "${flake2}/config.nix";

    # This is only allowed in impure mode.
    a8 = builtins.storePath $dep;

    a9 = "$dep";

    drvCall = with import ./config.nix; mkDerivation {
      name = "simple";
      builder = ./simple.builder.sh;
      PATH = "";
      goodPath = path;
    };

    a10 = builtins.unsafeDiscardOutputDependency self.drvCall.drvPath;

    a11 = self.drvCall.drvPath;

    a12 = self.drvCall.outPath;

    a13 = "${self.drvCall.drvPath}${self.drvCall.outPath}";
  };
}
"""

files = {
    "flake1": simple_flake() | {"foo": File("bar")},
    "flake2": simple_flake(),
    "common.sh": File("nothing important"),
}


@with_files(files)
class TestBuild:
    @pytest.fixture(autouse=True)
    def common_init(self, nix: Nix, files: Path):
        self.nix = nix
        self.out = nix.env.dirs.home / "result"

        nix.settings.add_xp_feature("nix-command", "flakes")

        with tarfile.open(files / "flake.tar.gz", "w:gz") as tar:

            def drop_prefix(ti: tarfile.TarInfo) -> tarfile.TarInfo:
                ti.name = ti.name.replace(str(files)[1:] + "/", "")
                return ti

            tar.add(files / "flake2", filter=drop_prefix)

        flake_hash = nix.hash_path(files / "flake2")
        dep = nix.nix(["store", "add-path", files / "common.sh"]).run().ok().stdout_s.strip()

        flake = files / "flake1/flake.nix"
        flake.write_text(
            flake_nix.replace("$TEST_ROOT", str(files))
            .replace("$hash", flake_hash)
            .replace("$dep", dep)
        )

    def build(self, *args) -> CommandResult:
        return self.nix.nix(["build", "--json", "--out-link", self.out, *args]).run()

    @pytest.mark.parametrize("attr", ["a1", "a6"])
    def test_build_simple_nix(self, files: Path, attr: str):
        self.build(f"{files}/flake1#{attr}").ok()
        assert (self.out / "simple.nix").exists()

    def test_build_a2(self, files: Path):
        self.build(f"{files}/flake1#a2").ok()
        assert self.out.read_text() == "bar"

    @pytest.mark.parametrize("attr", ["a3", "a4"])
    def test_build_plain(self, files: Path, attr: str):
        self.build(f"{files}/flake1#{attr}").ok()

    def test_build_a8(self, files: Path):
        self.build("--impure", f"{files}/flake1#a8").ok()
        assert self.out.read_text() == "nothing important"

    def test_build_a9(self, files: Path):
        result = self.build(f"{files}/flake1#a9").expect(1)
        assert (
            "has 0 entries in its context. It should only have exactly one entry" in result.stderr_s
        )

    def test_build_a10(self, files: Path):
        self.build(f"{files}/flake1#a10").ok()
        assert str(self.out.readlink()).endswith("simple.drv")

    def test_build_a11(self, files: Path):
        result = self.build(f"{files}/flake1#a11").expect(1)
        assert (
            "has a context which refers to a complete source and binary closure" in result.stderr_s
        )

    def test_build_a12(self, files: Path):
        self.build(f"{files}/flake1#a12").ok()
        assert (self.out / "hello").exists()

    def test_build_a13(self, files: Path):
        result = self.build("--impure", f"{files}/flake1#a13").expect(1)
        assert (
            "has 2 entries in its context. It should only have exactly one entry" in result.stderr_s
        )