Files
lix/tests/functional2/commands/test_why_depends.py
T
Rebecca Turnerandrootile cb34b56fea tests/functional2: Fix Python LSP by adjusting imports
See: cl/4840

When importing Python modules, we include `functional2` in the module
path, like this:

    from functional2.testlib.fixtures.env import ManagedEnv

This means that python expects to see a file like
`functional2/testlib/fixtures/env.py`. We run `pytest` from `tests/` in
the `justfile` and have `tests/functional2/__init__.py` so `pytest` in
`meson` is able to find these imports.

However, language servers generally consider the `pyproject.toml` to be
the project root, so (e.g.) `pyright` is unable to follow any of the
`functional2` imports, leading to lots of spurious errors.

In cl/4840 I moved `tests/functional2/pyproject.toml` to
`tests/pyproject.toml`, which worked but was considered aesthetically
unappealing.

This diff is much larger but it's a more elegant solution.

Change-Id: I2983c7b87f88f59a4e3521451a9f5acd6a6a6964
2026-01-23 15:06:51 +01:00

72 lines
2.4 KiB
Python

from testlib.fixtures.file_helper import with_files
from testlib.fixtures.nix import Nix
from testlib.utils import get_global_asset_pack
@with_files(get_global_asset_pack("dependencies"))
def test_command_doesnt_crash(nix: Nix):
nix.nix_store(["--init"]).run().ok()
nix.nix(
["why-depends", "--derivation", "--file", "./dependencies.nix", "input2_drv", "input1_drv"],
flake=True,
build=True,
).run().ok()
nix.nix(
["why-depends", "--file", "./dependencies.nix", "input2_drv", "input1_drv"],
flake=True,
build=True,
).run().ok()
def built_files(nix: Nix):
nix.nix_build(["./dependencies.nix", "-A", "input0_drv", "-o", "dep"]).run().ok()
nix.nix_build(["./dependencies.nix", "-A", "input3_drv", "-o", "dep3"]).run().ok()
nix.nix_build(["./dependencies.nix", "-o", "toplevel"]).run().ok()
@with_files(get_global_asset_pack("dependencies"))
def test_fast_depends(nix: Nix):
built_files(nix)
res = nix.nix(["why-depends", "./toplevel", "./dep"], flake=True, build=True).run().ok()
assert "input-2" in res.stdout_plain
assert "reference-to-input-2" not in res.stdout_plain
@with_files(get_global_asset_pack("dependencies"))
def test_precise_depends(nix: Nix):
built_files(nix)
res = (
nix.nix(["why-depends", "./toplevel", "./dep", "--precise"], flake=True, build=True)
.run()
.ok()
)
out = res.stdout_plain
assert "input-2" in out
assert "reference-to-input-2" in out
lines = out.splitlines()
assert "reference-to-input-2 -> " in lines[1]
assert "dependencies-input-2" in lines[2]
assert "input0: " in lines[3]
assert "dependencies-input-0" in lines[4]
@with_files(get_global_asset_pack("dependencies"))
def test_self_ref(nix: Nix):
built_files(nix)
res = nix.nix(["why-depends", "./toplevel", "./toplevel"], flake=True, build=True).run().ok()
lines = res.stdout_plain.splitlines()
assert "dependencies-top" in lines[0]
assert "dependencies-top" in lines[1]
@with_files(get_global_asset_pack("dependencies"))
def test_all(nix: Nix):
built_files(nix)
res = (
nix.nix(["why-depends", "./toplevel", "./dep3", "--all"], flake=True, build=True).run().ok()
)
lines = res.stdout_plain.splitlines()
assert "dependencies-top" in lines[0]
assert "input-2" in lines[1]
assert "input-3" in lines[2]