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
from testlib.fixtures.file_helper import with_files, CopyFile
from testlib.fixtures.nix import Nix
from testlib.utils import get_global_asset_pack

import pytest

_files = {
    "check-refs.nix": CopyFile("assets/test_reference_checks/check-refs.nix"),
    **get_global_asset_pack("dependencies"),
}


@with_files(_files)
def test_references_detected(nix: Nix):
    dep, test1, test2 = (
        nix.nix_build(["check-refs.nix", "-A", "dep", "-A", "test1", "-A", "test2"])
        .run()
        .ok()
        .stdout_plain.splitlines()
    )

    refs_test1, refs_test2 = (
        nix.nix_store(["-q", "--references", path]).run().ok().stdout_plain.splitlines()
        for path in [test1, test2]
    )

    assert dep in refs_test1
    assert test1 not in refs_test1

    assert dep not in refs_test2
    assert test2 not in refs_test2

    # test2 has a reference to file `aux-ref` (${src})
    assert any("aux-ref" in p for p in refs_test2)


@with_files(_files)
def test_allowed_references_error(nix: Nix):
    # empty `allowedReferences` throws on detected references
    error = nix.nix_build(["check-refs.nix", "-A", "test3"]).run().expect(1).stderr_plain
    assert "check-refs-3' is not allowed to have direct references to the following paths:" in error


@with_files(_files)
@pytest.mark.parametrize("ok_attr", ["test4", "test5", "test7"])
def test_allowed_references_ok(nix: Nix, ok_attr: str):
    # two derivations where the path matches allowedReferences
    nix.nix_build(["check-refs.nix", "-A", ok_attr]).run().ok()


@with_files(_files)
def test_allowed_reference_self_reference(nix: Nix):
    # self-reference that's not allowed
    error = (
        nix.nix_build(["check-refs.nix", "-A", "test6"]).run().expect(1).stderr_plain.splitlines()
    )
    assert (
        "check-refs-6' is not allowed to have direct references to the following paths:"
        in error[-2]
    )
    assert "check-refs-6" in error[-1]


@with_files(_files)
def test_tofile_no_drv_output(nix: Nix):
    error = nix.nix_build(["check-refs.nix", "-A", "test8"]).run().expect(1).stderr_plain

    assert (
        "error: files created by builtins.toFile may not reference derivations, but builder.sh references"
        in error
    )


@with_files(_files)
def test_disallowed_references(nix: Nix):
    # disallowedReferences error fires
    error = (
        nix.nix_build(["check-refs.nix", "-A", "test9"]).run().expect(1).stderr_plain.splitlines()
    )

    assert (
        "check-refs-9' is not allowed to have direct references to the following paths" in error[-2]
    )
    assert "dependencies-top" in error[-1]

    # Dependencies from `disallowedReferences` are used at build-time, but are not
    # referenced by the final store path -> success.
    nix.nix_build(["check-refs.nix", "-A", "test10"]).run().ok()


@with_files(_files)
def test_structured_attrs_discard(nix: Nix):
    result = nix.nix_build(["check-refs.nix", "-A", "test11"]).run().ok().stdout_plain.splitlines()

    assert not (nix.nix_store(["-q", "--references", *result]).run().ok().stdout_plain.splitlines())


@with_files(_files)
def test_reference_specifier(nix: Nix):
    error = (
        nix.nix_build(["check-refs.nix", "-A", "test12"]).run().expect(1).stderr_plain.splitlines()
    )

    assert "output check for 'lib' contains an illegal reference specifier 'dev'" in error[-1]


@with_files(
    get_global_asset_pack("dependencies")
    | {
        "regression-reference-checks.nix": CopyFile(
            "assets/test_reference_checks/regression-reference-checks.nix"
        )
    }
)
def test_regression_partial_build(nix: Nix):
    out, _ = (
        nix.nix_build(
            ["regression-reference-checks.nix", "-A", "out", "-A", "man", "--no-out-link"]
        )
        .run()
        .ok()
        .stdout_plain.splitlines()
    )

    nix.nix_store(["--delete", out]).run().ok()

    nix.nix_build(["regression-reference-checks.nix", "-A", "out"]).run().ok()