"""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