mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Finished Graph and Tree implementation
This commit is contained in:
0
tests/unit/cli/meta/__init__.py
Normal file
0
tests/unit/cli/meta/__init__.py
Normal file
56
tests/unit/cli/meta/test_graph.py
Normal file
56
tests/unit/cli/meta/test_graph.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import unittest
|
||||
import tempfile
|
||||
import shutil
|
||||
import os
|
||||
import yaml
|
||||
from cli.generate import graph
|
||||
|
||||
|
||||
class TestGraphLogic(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp_dir = tempfile.mkdtemp()
|
||||
self.role_name = "role_a"
|
||||
self.role_path = os.path.join(self.temp_dir, self.role_name)
|
||||
os.makedirs(os.path.join(self.role_path, "meta"))
|
||||
os.makedirs(os.path.join(self.role_path, "tasks"))
|
||||
|
||||
# Write meta/main.yml
|
||||
with open(os.path.join(self.role_path, "meta", "main.yml"), 'w') as f:
|
||||
yaml.dump({
|
||||
"galaxy_info": {
|
||||
"author": "tester",
|
||||
"run_after": []
|
||||
},
|
||||
"dependencies": []
|
||||
}, f)
|
||||
|
||||
# Write tasks/main.yml
|
||||
with open(os.path.join(self.role_path, "tasks", "main.yml"), 'w') as f:
|
||||
yaml.dump([
|
||||
{"include_role": "some_other_role"},
|
||||
{"import_role": {"name": "another_role"}}
|
||||
], f)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.temp_dir)
|
||||
|
||||
def test_load_meta_returns_dict(self):
|
||||
meta_path = graph.find_role_meta(self.temp_dir, self.role_name)
|
||||
meta = graph.load_meta(meta_path)
|
||||
self.assertIsInstance(meta, dict)
|
||||
self.assertIn('galaxy_info', meta)
|
||||
|
||||
def test_load_tasks_include_role(self):
|
||||
task_path = graph.find_role_tasks(self.temp_dir, self.role_name)
|
||||
includes = graph.load_tasks(task_path, 'include_role')
|
||||
self.assertIn("some_other_role", includes)
|
||||
|
||||
def test_build_mappings_structure(self):
|
||||
result = graph.build_mappings(self.role_name, self.temp_dir, max_depth=1)
|
||||
self.assertIsInstance(result, dict)
|
||||
for key in graph.ALL_KEYS:
|
||||
self.assertIn(key, result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
41
tests/unit/cli/meta/test_tree.py
Normal file
41
tests/unit/cli/meta/test_tree.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import unittest
|
||||
import tempfile
|
||||
import shutil
|
||||
import os
|
||||
import json
|
||||
from cli.generate import tree
|
||||
|
||||
|
||||
class TestTreeMain(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Create a temporary roles directory with a fake role
|
||||
self.temp_dir = tempfile.mkdtemp()
|
||||
self.role_name = "testrole"
|
||||
self.role_path = os.path.join(self.temp_dir, self.role_name)
|
||||
os.makedirs(os.path.join(self.role_path, "meta"))
|
||||
|
||||
meta_path = os.path.join(self.role_path, "meta", "main.yml")
|
||||
with open(meta_path, 'w') as f:
|
||||
f.write("galaxy_info:\n author: test\n run_after: []\ndependencies: []\n")
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.temp_dir)
|
||||
|
||||
def test_find_roles(self):
|
||||
roles = list(tree.find_roles(self.temp_dir))
|
||||
self.assertEqual(len(roles), 1)
|
||||
self.assertEqual(roles[0][0], self.role_name)
|
||||
|
||||
def test_main_execution_does_not_raise(self):
|
||||
# Mocking sys.argv and running main should not raise
|
||||
import sys
|
||||
old_argv = sys.argv
|
||||
sys.argv = ['tree.py', '-d', self.temp_dir, '-p']
|
||||
try:
|
||||
tree.main()
|
||||
finally:
|
||||
sys.argv = old_argv
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in New Issue
Block a user