Files
computer-playbook/tests/unit/python/cli/console/test_navigation.py
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).

This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.

Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:

  * 7 `src:` values built from a role variable holding a bare filename
    (svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
    The variable doubles as the destination name inside the container, so the
    language segment belongs at the call site, not in the variable.
  * `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
    through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
    18 roles ship exactly that and have no template fallback: the play would
    have died on each.
  * `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
    interface now reads `files/javascript/javascript.js`.
  * `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
    path through `path_absolute(... 'files', ...)`.
  * 4 `ansible.builtin.script:` invocations naming a bare command
    (pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
  * `dev-python/files/shell/install.sh` pointed at a sibling role's script.

Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 01:31:09 +02:00

159 lines
5.2 KiB
Python

import unittest
from cli.console import navigation
from cli.console.constants import PROMPT_PREFIX
DOTDOT = ".." # nocheck: project-root-import REPL nav segment, not path construction
DOTDOT_DOTDOT = (
"../.." # nocheck: project-root-import REPL nav segment, not path construction
)
class TestNormalize(unittest.TestCase):
def test_empty_argv_becomes_help(self):
self.assertEqual(navigation.normalize([]), ["--help"])
def test_strips_cli_prefix(self):
self.assertEqual(
navigation.normalize(["cli", "build", "tree"]), ["build", "tree"]
)
def test_help_alias_lowercase(self):
self.assertEqual(navigation.normalize(["help"]), ["--help"])
def test_help_alias_question_mark(self):
self.assertEqual(navigation.normalize(["?"]), ["--help"])
def test_help_alias_h(self):
self.assertEqual(navigation.normalize(["h"]), ["--help"])
def test_help_alias_preserves_following_args(self):
self.assertEqual(navigation.normalize(["help", "meta"]), ["--help", "meta"])
def test_pass_through_unknown(self):
self.assertEqual(navigation.normalize(["meta", "env"]), ["meta", "env"])
class TestIsCategory(unittest.TestCase):
def test_empty_path_is_root_category(self):
self.assertTrue(navigation.is_category([]))
def test_known_top_level_category(self):
self.assertTrue(navigation.is_category(["administration"]))
def test_reserved_core_dir_is_not_category(self):
self.assertFalse(navigation.is_category(["core"]))
def test_unknown_path_is_not_category(self):
self.assertFalse(navigation.is_category(["does-not-exist"]))
def test_leaf_command_is_not_category(self):
self.assertFalse(navigation.is_category(["meta", "env"]))
class TestResolveCd(unittest.TestCase):
def test_empty_target_resolves_to_root(self):
self.assertEqual(navigation.resolve_cd(["administration"], ""), [])
def test_slash_resolves_to_root(self):
self.assertEqual(navigation.resolve_cd(["administration"], "/"), [])
def test_absolute_target(self):
self.assertEqual(
navigation.resolve_cd(["meta"], "/administration"), ["administration"]
)
def test_relative_segment_appends(self):
self.assertEqual(
navigation.resolve_cd([], "administration"), ["administration"]
)
def test_dotdot_pops_one_level(self):
self.assertEqual(navigation.resolve_cd(["administration"], DOTDOT), [])
def test_dotdot_at_root_stays_at_root(self):
self.assertEqual(navigation.resolve_cd([], DOTDOT), [])
def test_dotdot_chain_pops_multiple_levels(self):
self.assertEqual(
navigation.resolve_cd(["administration", "deploy"], DOTDOT_DOTDOT), []
)
def test_invalid_segment_returns_none(self):
self.assertIsNone(navigation.resolve_cd([], "does-not-exist"))
class TestPrompt(unittest.TestCase):
def test_root_prompt(self):
self.assertEqual(navigation.prompt([]), f"{PROMPT_PREFIX}infinito> ")
def test_nested_prompt(self):
self.assertEqual(
navigation.prompt(["administration", "deploy"]),
f"{PROMPT_PREFIX}infinito administration deploy> ",
)
class TestIsNavigationToken(unittest.TestCase):
def test_slash_alone(self):
self.assertTrue(navigation.is_navigation_token("/"))
def test_dotdot_alone(self):
self.assertTrue(navigation.is_navigation_token(DOTDOT))
def test_absolute_path(self):
self.assertTrue(navigation.is_navigation_token("/administration"))
def test_relative_with_dotdot_prefix(self):
self.assertTrue(navigation.is_navigation_token("../meta"))
def test_path_with_slash(self):
self.assertTrue(navigation.is_navigation_token("administration/deploy"))
def test_plain_token_is_not_nav(self):
self.assertFalse(navigation.is_navigation_token("administration"))
def test_empty_is_not_nav(self):
self.assertFalse(navigation.is_navigation_token(""))
class TestResolveArgv(unittest.TestCase):
def test_relative_when_command_exists_under_current(self):
self.assertEqual(
navigation.resolve_argv(["administration"], ["deploy"]),
["administration", "deploy"],
)
def test_absolute_fallback_when_command_missing_under_current(self):
self.assertEqual(
navigation.resolve_argv(["administration"], ["meta", "env"]),
["meta", "env"],
)
def test_flag_first_always_prepends_current(self):
self.assertEqual(
navigation.resolve_argv(["administration"], ["--help"]),
["administration", "--help"],
)
def test_root_passes_through(self):
self.assertEqual(
navigation.resolve_argv([], ["meta", "env"]),
["meta", "env"],
)
class TestCommandExists(unittest.TestCase):
def test_known_subpackage(self):
self.assertTrue(navigation.command_exists([], "administration"))
def test_unknown_token(self):
self.assertFalse(navigation.command_exists([], "does-not-exist"))
def test_reserved_at_root_rejected(self):
self.assertFalse(navigation.command_exists([], "core"))
if __name__ == "__main__":
unittest.main()