|
| 1 | +"""Tests for ``JustfileFixture`` module-aware recipe resolution.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import shutil |
| 6 | +from textwrap import dedent |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from pytest_just import JustfileFixture |
| 12 | +from pytest_just.errors import UnknownRecipeError |
| 13 | + |
| 14 | + |
| 15 | +pytestmark = pytest.mark.skipif(shutil.which("just") is None, reason="just binary is required") |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def module_tree(tmp_path: Path) -> JustfileFixture: |
| 20 | + """Justfile with a module and submodule.""" |
| 21 | + (tmp_path / "justfile").write_text(dedent(""" |
| 22 | + mod infra |
| 23 | + |
| 24 | + root: |
| 25 | + @echo root |
| 26 | + """), encoding="utf-8") |
| 27 | + mod = tmp_path / "infra" |
| 28 | + mod.mkdir() |
| 29 | + (mod / "mod.just").write_text(dedent(""" |
| 30 | + mod deploy |
| 31 | + |
| 32 | + _require-infra: |
| 33 | + @true |
| 34 | + |
| 35 | + status: _require-infra |
| 36 | + @echo ok |
| 37 | + """), encoding="utf-8") |
| 38 | + sub = mod / "deploy" |
| 39 | + sub.mkdir() |
| 40 | + (sub / "mod.just").write_text(dedent(""" |
| 41 | + [private] |
| 42 | + up profile='default': _require-deploy |
| 43 | + @echo up {{ profile }} |
| 44 | + |
| 45 | + _require-deploy: |
| 46 | + @true |
| 47 | + """), encoding="utf-8") |
| 48 | + return JustfileFixture(root=tmp_path) |
| 49 | + |
| 50 | + |
| 51 | +def test_recipes_flattened_by_namepath(module_tree: JustfileFixture) -> None: |
| 52 | + """Recipes from modules appear keyed by full namepath.""" |
| 53 | + names = module_tree.recipe_names(include_private=True) |
| 54 | + assert "root" in names |
| 55 | + assert "infra::status" in names |
| 56 | + assert "infra::_require-infra" in names |
| 57 | + assert "infra::deploy::up" in names |
| 58 | + assert "infra::deploy::_require-deploy" in names |
| 59 | + |
| 60 | + |
| 61 | +def test_is_private_with_namepath(module_tree: JustfileFixture) -> None: |
| 62 | + """Privacy checks work with full module paths.""" |
| 63 | + assert not module_tree.is_private("root") |
| 64 | + assert not module_tree.is_private("infra::status") |
| 65 | + assert module_tree.is_private("infra::_require-infra") |
| 66 | + assert module_tree.is_private("infra::deploy::up") |
| 67 | + |
| 68 | + |
| 69 | +def test_dependencies_with_namepath(module_tree: JustfileFixture) -> None: |
| 70 | + """Dependency inspection works with full module paths. |
| 71 | +
|
| 72 | + Note: just stores dependency names as module-local names (not namepaths), |
| 73 | + so dependencies() returns local names like ``_require-infra``. |
| 74 | + """ |
| 75 | + assert "_require-infra" in module_tree.dependencies("infra::status") |
| 76 | + assert "_require-deploy" in module_tree.dependencies("infra::deploy::up") |
| 77 | + |
| 78 | + |
| 79 | +def test_parameters_with_namepath(module_tree: JustfileFixture) -> None: |
| 80 | + """Parameter inspection works with full module paths.""" |
| 81 | + assert "profile" in module_tree.parameter_names("infra::deploy::up") |
| 82 | + |
| 83 | + |
| 84 | +def test_unknown_namepath_raises(module_tree: JustfileFixture) -> None: |
| 85 | + """Unknown namepath raises UnknownRecipeError.""" |
| 86 | + with pytest.raises(UnknownRecipeError): |
| 87 | + module_tree.dependencies("infra::nonexistent") |
| 88 | + |
| 89 | + |
| 90 | +def test_all_modules(module_tree: JustfileFixture) -> None: |
| 91 | + """all_modules discovers module paths from the dump.""" |
| 92 | + assert "infra" in module_tree.all_modules |
| 93 | + assert "infra::deploy" in module_tree.all_modules |
| 94 | + |
| 95 | + |
| 96 | +def test_recipe_names_excludes_private_by_default(module_tree: JustfileFixture) -> None: |
| 97 | + """Public recipe listing excludes private recipes across modules.""" |
| 98 | + public = module_tree.recipe_names() |
| 99 | + assert "infra::status" in public |
| 100 | + assert "infra::_require-infra" not in public |
| 101 | + assert "infra::deploy::up" not in public # marked [private] |
| 102 | + |
| 103 | + |
| 104 | +def test_flatten_static_method() -> None: |
| 105 | + """_flatten works as a standalone static method.""" |
| 106 | + dump = { |
| 107 | + "recipes": { |
| 108 | + "root": {"namepath": "root", "private": False}, |
| 109 | + }, |
| 110 | + "modules": { |
| 111 | + "foo": { |
| 112 | + "recipes": { |
| 113 | + "bar": {"namepath": "foo::bar", "private": False}, |
| 114 | + }, |
| 115 | + "modules": { |
| 116 | + "baz": { |
| 117 | + "recipes": { |
| 118 | + "qux": {"namepath": "foo::baz::qux", "private": True}, |
| 119 | + }, |
| 120 | + "modules": {}, |
| 121 | + } |
| 122 | + }, |
| 123 | + } |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + modules: list[str] = [] |
| 128 | + flat = JustfileFixture._flatten(dump, modules) |
| 129 | + assert set(flat.keys()) == {"root", "foo::bar", "foo::baz::qux"} |
| 130 | + assert flat["foo::baz::qux"]["private"] is True |
| 131 | + assert sorted(modules) == ["foo", "foo::baz"] |
| 132 | + |
| 133 | + |
| 134 | +def test_flatten_collects_modules() -> None: |
| 135 | + """_flatten collects module paths when given a list.""" |
| 136 | + dump = { |
| 137 | + "recipes": {}, |
| 138 | + "modules": { |
| 139 | + "foo": { |
| 140 | + "recipes": {}, |
| 141 | + "modules": { |
| 142 | + "bar": { |
| 143 | + "recipes": {}, |
| 144 | + "modules": {}, |
| 145 | + } |
| 146 | + }, |
| 147 | + } |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + modules: list[str] = [] |
| 152 | + JustfileFixture._flatten(dump, modules) |
| 153 | + assert sorted(modules) == ["foo", "foo::bar"] |
0 commit comments