Files
computer-playbook/tests/unit/plugins/lookup/test_path_absolute.py
T
kevinveenbirkenbachandClaude Opus 4.8 23d1447566 refactor(ansible): add path_absolute lookup, replace playbook_dir joins
Introduce plugins/lookup/path_absolute.py as the single source for the
repo-root anchor: lookup('path_absolute', 'roles/...') returns an absolute
path under playbook_dir, joining every term. Replace all hand-built
playbook_dir path constructions across .yml/.yaml and .j2 templates with it:

  [playbook_dir, '<rel>'] | path_join   -> lookup('path_absolute', '<rel>')
  playbook_dir ~ '/<rel>'               -> lookup('path_absolute', '<rel>')
  {{ playbook_dir }}/<rel>              -> lookup('path_absolute', '<rel>')

Unrelated repo_path symbols in cli/meta/roles/applications/resolution and the
single bare discover_playwright_roles(playbook_dir) call are left untouched.

Add tests/lint/ansible/test_playbook_dir_via_lookup.py: fails the build if any
of the three construction forms reappears in .yml/.yaml/.j2, pointing callers
at the lookup. Unit test covers the plugin.

Touching these files pulled their pre-existing narration comments into
test_comments_valid scope; clean them (delete restatement/banners, keep real
trip-wires as Workaround/Upstream/Invariant markers). Drop caution, important
and pitfall from the comment-lint marker vocabulary and remove the comments
that used them.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-29 20:36:57 +02:00

43 lines
1.4 KiB
Python

import unittest
from ansible.errors import AnsibleError
from plugins.lookup.path_absolute import LookupModule
_VARS = {"playbook_dir": "/opt/src/infinito"}
class TestPathAbsoluteLookup(unittest.TestCase):
def setUp(self):
self.lookup = LookupModule()
def test_joins_whole_relative_path(self):
out = self.lookup.run(
["roles/sys-svc-compose/tasks/utils/swarm/x.yml"], variables=_VARS
)
self.assertEqual(
out, ["/opt/src/infinito/roles/sys-svc-compose/tasks/utils/swarm/x.yml"]
)
def test_matches_path_join_filter_result(self):
whole = self.lookup.run(["roles/a/b.yml"], variables=_VARS)[0]
split = self.lookup.run(["roles", "a", "b.yml"], variables=_VARS)[0]
self.assertEqual(whole, split)
self.assertEqual(whole, "/opt/src/infinito/roles/a/b.yml")
def test_joins_term_with_variable_segment(self):
out = self.lookup.run(["roles", "web-app-keycloak"], variables=_VARS)
self.assertEqual(out, ["/opt/src/infinito/roles/web-app-keycloak"])
def test_strips_surrounding_whitespace_and_slashes(self):
out = self.lookup.run([" roles/a/b.yml "], variables=_VARS)
self.assertEqual(out, ["/opt/src/infinito/roles/a/b.yml"])
def test_missing_playbook_dir_raises(self):
with self.assertRaises(AnsibleError):
self.lookup.run(["roles/a.yml"], variables={})
if __name__ == "__main__":
unittest.main()