DEVX-133: fix: auto-discover molecule root instead of hardcoding gitea-runner

This commit is contained in:
emil User
2026-07-13 02:25:59 +00:00
parent 7b624b0525
commit 50dcb67083
6 changed files with 93 additions and 22 deletions
+26 -9
View File
@@ -9,10 +9,10 @@ from click.testing import CliRunner
from devx.molecule.distribute_molecule import (
DEFAULT_ROLES_ROOT,
MOLECULE_ROOT,
PLATFORMS,
MultiRoleTestPair,
TestPair,
_default_molecule_root,
_load_molecule_weights,
_lpt_distribute,
_scenario_weight,
@@ -43,8 +43,25 @@ class TestDiscoverScenarios:
discover_scenarios(tmp_path / "nonexistent")
assert "not found" in str(exc.value)
def test_default_root_constant(self) -> None:
assert Path("ansible/roles/gitea-runner/molecule") == MOLECULE_ROOT
def test_default_root_auto_discovery(self, tmp_path: Path) -> None:
"""_default_molecule_root auto-discovers first role with molecule/ dir."""
# When no roles exist, returns a fallback path
with patch("devx.molecule.distribute_molecule.DEFAULT_ROLES_ROOT", tmp_path / "roles"):
result = _default_molecule_root()
assert "molecule" in str(result)
# When a role has molecule/, it's discovered
(tmp_path / "roles" / "my_role" / "molecule").mkdir(parents=True)
with patch("devx.molecule.distribute_molecule.DEFAULT_ROLES_ROOT", tmp_path / "roles"):
result = _default_molecule_root()
assert result == tmp_path / "roles" / "my_role" / "molecule"
def test_default_root_no_molecule_dirs(self, tmp_path: Path) -> None:
"""When roles exist but none have molecule/, returns fallback path."""
(tmp_path / "roles" / "role_without_molecule").mkdir(parents=True)
with patch("devx.molecule.distribute_molecule.DEFAULT_ROLES_ROOT", tmp_path / "roles"):
result = _default_molecule_root()
assert result == tmp_path / "roles" / "molecule"
class TestPairEncoding:
@@ -150,7 +167,7 @@ class TestCli:
root = tmp_path / "molecule"
(root / "alpha").mkdir(parents=True)
(root / "beta").mkdir(parents=True)
with patch("devx.molecule.distribute_molecule.MOLECULE_ROOT", root):
with patch("devx.molecule.distribute_molecule._default_molecule_root", return_value=root):
runner = CliRunner()
result = runner.invoke(cli, ["--list"])
assert result.exit_code == 0
@@ -176,7 +193,7 @@ class TestCli:
root = tmp_path / "molecule"
for s in ["a", "b", "c"]:
(root / s).mkdir(parents=True)
with patch("devx.molecule.distribute_molecule.MOLECULE_ROOT", root):
with patch("devx.molecule.distribute_molecule._default_molecule_root", return_value=root):
runner = CliRunner()
result = runner.invoke(cli, ["--max-runners", "3"])
assert result.exit_code == 0
@@ -191,7 +208,7 @@ class TestCli:
root = tmp_path / "molecule"
(root / "alpha").mkdir(parents=True)
with patch("devx.molecule.distribute_molecule.MOLECULE_ROOT", root):
with patch("devx.molecule.distribute_molecule._default_molecule_root", return_value=root):
runner = CliRunner()
# 1-based index: "1" maps to internal 0
result = runner.invoke(cli, ["--runner-index", "1", "--max-runners", "3"])
@@ -209,7 +226,7 @@ class TestGithubEnv:
scenario = root / "alpha"
scenario.mkdir(parents=True)
(scenario / "molecule.yml").write_text("name: alpha\n")
with patch("devx.molecule.distribute_molecule.MOLECULE_ROOT", root):
with patch("devx.molecule.distribute_molecule._default_molecule_root", return_value=root):
runner = CliRunner()
result = runner.invoke(cli, ["--runner-index", "1", "--max-runners", "3", "--github-env"])
assert result.exit_code == 0
@@ -224,7 +241,7 @@ class TestGithubEnv:
scenario = root / "alpha"
scenario.mkdir(parents=True)
(scenario / "molecule.yml").write_text("name: alpha\n")
with patch("devx.molecule.distribute_molecule.MOLECULE_ROOT", root):
with patch("devx.molecule.distribute_molecule._default_molecule_root", return_value=root):
runner = CliRunner()
result = runner.invoke(
cli, ["--runner-index", "5", "--max-runners", "3", "--github-env", "--skip-if-excess"]
@@ -240,7 +257,7 @@ class TestGithubEnv:
scenario = root / "alpha"
scenario.mkdir(parents=True)
(scenario / "molecule.yml").write_text("name: alpha\n")
with patch("devx.molecule.distribute_molecule.MOLECULE_ROOT", root):
with patch("devx.molecule.distribute_molecule._default_molecule_root", return_value=root):
runner = CliRunner()
result = runner.invoke(cli, ["--runner-index", "1", "--max-runners", "3", "--github-env"])
assert result.exit_code != 0