|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from struct_module.commands.init import InitCommand, BASIC_STRUCT_YAML |
| 6 | + |
| 7 | + |
| 8 | +def test_init_creates_struct_yaml(tmp_path): |
| 9 | + parser = argparse.ArgumentParser() |
| 10 | + cmd = InitCommand(parser) |
| 11 | + |
| 12 | + target_dir = tmp_path / "proj" |
| 13 | + args = parser.parse_args([str(target_dir)]) |
| 14 | + |
| 15 | + with patch('builtins.print') as mock_print: |
| 16 | + cmd.execute(args) |
| 17 | + |
| 18 | + struct_file = target_dir / '.struct.yaml' |
| 19 | + assert struct_file.exists() |
| 20 | + |
| 21 | + content = struct_file.read_text() |
| 22 | + # Basic checks for key sections |
| 23 | + assert 'pre_hooks:' in content |
| 24 | + assert 'post_hooks:' in content |
| 25 | + assert 'files:' in content |
| 26 | + assert 'README.md' in content |
| 27 | + assert 'folders:' in content |
| 28 | + assert 'github/workflows/run-struct' in content |
| 29 | + |
| 30 | + |
| 31 | +def test_init_skips_if_exists(tmp_path): |
| 32 | + parser = argparse.ArgumentParser() |
| 33 | + cmd = InitCommand(parser) |
| 34 | + |
| 35 | + target_dir = tmp_path / "proj" |
| 36 | + target_dir.mkdir(parents=True) |
| 37 | + existing = target_dir / '.struct.yaml' |
| 38 | + existing.write_text('files: []\n') |
| 39 | + |
| 40 | + args = parser.parse_args([str(target_dir)]) |
| 41 | + |
| 42 | + with patch('builtins.print') as mock_print: |
| 43 | + cmd.execute(args) |
| 44 | + # Should not overwrite existing file |
| 45 | + assert existing.read_text() == 'files: []\n' |
| 46 | + # Should print a message about skipping |
| 47 | + printed = "\n".join(c.args[0] for c in mock_print.call_args_list) |
| 48 | + assert '.struct.yaml already exists' in printed |
0 commit comments