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
import re
import sys

from testlib.fixtures.file_helper import with_files, CopyFile
from testlib.fixtures.nix import Nix
from testlib.utils import get_global_asset_pack

_files = {
    "output-cycles.nix": CopyFile("assets/test_build/output-cycles.nix"),
    **get_global_asset_pack("dependencies"),
}


def _assert_cycle_tree(output: str, regexes: list[str]):
    lines = output.splitlines()
    start = next((k for k, v in enumerate(lines) if "Shown below are the files inside" in v), None)

    assert start is not None

    for line, regex in enumerate(regexes, start=start + 1):
        assert re.search(regex, lines[line])


def _assert_cycle_message(err: str):
    assert (
        len(
            re.findall(
                r"cycle detected in build of '.*' in the references of output 'bar' from output 'foo'",
                err,
            )
        )
        == 1
    )


@with_files(_files)
def test_cycle(nix: Nix):
    res = nix.nix_build(["output-cycles.nix", "-A", "cycle"]).run().expect(1)
    err = res.stderr_plain
    _assert_cycle_message(err)

    if sys.platform == "linux":
        _assert_cycle_tree(
            err,
            [
                r"/store/.*-cycle-bar",
                r"└───lib/libfoo: ….*cycle-baz.*",
                r"    →.*/store/.*-cycle-baz",
                r"    └───share/lalala:.*-cycle-foo.*",
            ],
        )


@with_files(_files)
def test_cycle_in_dependency(nix: Nix):
    res = nix.nix_build(["output-cycles.nix", "-A", "as_dependency"]).run().expect(1)
    err = res.stderr_plain
    _assert_cycle_message(err)

    assert "error: 1 dependencies of derivation" in err


@with_files(_files)
def test_cycle_with_deps(nix: Nix):
    res = nix.nix_build(["output-cycles.nix", "-A", "cycle-with-deps"]).run().expect(1)
    err = res.stderr_plain
    _assert_cycle_message(err)

    if sys.platform == "linux":
        _assert_cycle_tree(
            err,
            [
                r"/store/.*-cycle-with-deps-bar",
                r"└───txt: ….*cycle-with-deps-foo.*",
                r"    →.*/store/.*-cycle-with-deps-foo",
                r"    └───txt:.*-cycle-with-deps-bar.*",
            ],
        )