Spaces:
Sleeping
Sleeping
File size: 6,630 Bytes
0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 5737bc8 503a15e 5737bc8 503a15e 5737bc8 503a15e 5737bc8 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe 503a15e 0d91ffe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | """Unit tests for project_handler module."""
import pytest
import tempfile
import os
import zipfile
from pathlib import Path
from src.tools.project_handler import ProjectHandler
class TestUnzipProject:
"""Test cases for unzip_project method."""
def test_unzip_project_with_default_target_dir(self):
"""Test unzip_project uses system temp directory by default."""
# Create mock model and project handler
mock_model = None # Not needed for this test
project_handler = ProjectHandler(mock_model)
# Create a test zip file
with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as temp_zip:
with zipfile.ZipFile(temp_zip.name, 'w') as zf:
zf.writestr('test.ads', 'package Test is\nend Test;')
try:
# Call unzip_project with default target_dir
result_path = project_handler.unzip_project(temp_zip.name)
# Assert result path is under system temp directory
assert result_path.startswith(tempfile.gettempdir())
# Assert directory exists and contains extracted file
assert os.path.exists(result_path)
assert os.path.exists(os.path.join(result_path, 'test.ads'))
finally:
# Cleanup
os.unlink(temp_zip.name)
def test_unzip_project_with_custom_target_dir(self):
"""Test unzip_project uses provided target directory."""
# Create mock model and project handler
mock_model = None # Not needed for this test
project_handler = ProjectHandler(mock_model)
with tempfile.TemporaryDirectory() as custom_dir:
# Create a test zip file
with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as temp_zip:
with zipfile.ZipFile(temp_zip.name, 'w') as zf:
zf.writestr('main.adb', 'procedure Main is\nbegin\n null;\nend Main;')
try:
# Call unzip_project with custom target_dir
result_path = project_handler.unzip_project(temp_zip.name, custom_dir)
# Assert result path is under custom directory
assert result_path.startswith(custom_dir)
# Assert directory name contains UUID pattern
dir_name = os.path.basename(result_path)
assert dir_name.startswith('ada_project_')
assert len(dir_name) == len('ada_project_') + 32 # UUID hex length
# Assert directory exists and contains extracted file
assert os.path.exists(result_path)
assert os.path.exists(os.path.join(result_path, 'main.adb'))
finally:
# Cleanup
os.unlink(temp_zip.name)
def test_unzip_project_with_invalid_zip_file(self):
"""Test unzip_project raises exception for invalid ZIP files."""
# Create mock model and project handler
mock_model = None # Not needed for this test
project_handler = ProjectHandler(mock_model)
# Create a text file that's not a ZIP
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as temp_file:
temp_file.write("This is not a zip file")
temp_path = temp_file.name
try:
# Should raise an exception when trying to unzip invalid file
with pytest.raises(zipfile.BadZipFile):
project_handler.unzip_project(temp_path)
finally:
# Cleanup
os.unlink(temp_path)
def test_unzip_project_with_nonexistent_file(self):
"""Test unzip_project raises exception for non-existent files."""
# Create mock model and project handler
mock_model = None # Not needed for this test
project_handler = ProjectHandler(mock_model)
nonexistent_path = "/path/that/does/not/exist.zip"
# Should raise FileNotFoundError for non-existent file
with pytest.raises(FileNotFoundError):
project_handler.unzip_project(nonexistent_path)
class TestBuildDirectoryTree:
"""Test cases for build_directory_tree method."""
def test_build_directory_tree_simple_structure(self):
"""Test build_directory_tree returns proper tree structure."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create test directory structure
os.makedirs(os.path.join(temp_dir, "src"))
os.makedirs(os.path.join(temp_dir, "tests"))
# Create test files
Path(os.path.join(temp_dir, "main.adb")).write_text("procedure Main is")
Path(os.path.join(temp_dir, "src", "utils.ads")).write_text("package Utils is")
Path(os.path.join(temp_dir, "tests", "test_main.adb")).write_text("procedure Test_Main is")
# Call build_directory_tree (static method)
tree = ProjectHandler.build_directory_tree(temp_dir)
# Assert tree structure contains expected elements
assert isinstance(tree, str)
assert "main.adb" in tree
assert "src/" in tree
assert "utils.ads" in tree
assert "tests/" in tree
assert "test_main.adb" in tree
def test_build_directory_tree_nested_structure(self):
"""Test build_directory_tree handles nested directories."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create nested structure
nested_path = os.path.join(temp_dir, "project", "src", "modules")
os.makedirs(nested_path)
# Create files at different levels
Path(os.path.join(temp_dir, "project", "main.adb")).write_text("main")
Path(os.path.join(temp_dir, "project", "src", "core.ads")).write_text("core")
Path(os.path.join(nested_path, "parser.adb")).write_text("parser")
tree = ProjectHandler.build_directory_tree(temp_dir)
# Assert nested structure is represented
assert "project/" in tree
assert "src/" in tree
assert "modules/" in tree
assert "main.adb" in tree
assert "core.ads" in tree
assert "parser.adb" in tree |