""" Tests for ZIP utilities """ import pytest import tempfile import zipfile import shutil from pathlib import Path from unittest.mock import Mock from src.utils.zip_utils import ZipHandler class TestZipHandler: def setup_method(self): """Setup test fixtures""" self.zip_handler = ZipHandler() # Create temporary directory for test files self.test_dir = Path(tempfile.mkdtemp()) def teardown_method(self): """Clean up test fixtures""" if self.test_dir.exists(): shutil.rmtree(self.test_dir) # Clean up any ZIP handler temp directories if self.zip_handler.temp_base_dir.exists(): shutil.rmtree(self.zip_handler.temp_base_dir) def test_is_zip_file_valid_zip(self): """Test ZIP file detection with valid ZIP file""" # Create a valid ZIP file zip_file = self.test_dir / "test.zip" with zipfile.ZipFile(zip_file, 'w') as zf: zf.writestr("test.txt", "test content") assert self.zip_handler.is_zip_file(str(zip_file)) is True def test_is_zip_file_invalid_zip(self): """Test ZIP file detection with invalid ZIP file""" # Create a file that's not a ZIP not_zip_file = self.test_dir / "test.txt" not_zip_file.write_text("This is not a ZIP file") assert self.zip_handler.is_zip_file(str(not_zip_file)) is False def test_is_zip_file_nonexistent(self): """Test ZIP file detection with non-existent file""" nonexistent_file = self.test_dir / "does_not_exist.zip" assert self.zip_handler.is_zip_file(str(nonexistent_file)) is False def test_is_zip_file_wrong_extension(self): """Test ZIP file detection with wrong extension""" # Create a valid ZIP file but with wrong extension zip_file = self.test_dir / "test.txt" with zipfile.ZipFile(zip_file, 'w') as zf: zf.writestr("test.txt", "test content") assert self.zip_handler.is_zip_file(str(zip_file)) is False def test_extract_ada_project_success(self): """Test successful Ada project extraction""" # Create a ZIP file with Ada files zip_file = self.test_dir / "ada_project.zip" with zipfile.ZipFile(zip_file, 'w') as zf: zf.writestr("main.ads", "package Main is end Main;") zf.writestr("utils/helper.adb", "package body Helper is end Helper;") zf.writestr("config.ada", "package Config is end Config;") zf.writestr("readme.txt", "Not an Ada file") # Extract the project ada_project_path, session_dir = self.zip_handler.extract_ada_project(str(zip_file)) # Verify extraction assert Path(ada_project_path).exists() assert Path(session_dir).exists() # Verify Ada files were extracted ada_project = Path(ada_project_path) assert (ada_project / "main.ads").exists() assert (ada_project / "utils" / "helper.adb").exists() assert (ada_project / "config.ada").exists() assert (ada_project / "readme.txt").exists() def test_extract_ada_project_no_ada_files(self): """Test extraction with ZIP containing no Ada files""" # Create a ZIP file without Ada files zip_file = self.test_dir / "no_ada.zip" with zipfile.ZipFile(zip_file, 'w') as zf: zf.writestr("readme.txt", "No Ada files here") zf.writestr("config.json", '{"key": "value"}') # Should raise ValueError with pytest.raises(ValueError, match="No Ada files found"): self.zip_handler.extract_ada_project(str(zip_file)) def test_extract_ada_project_nonexistent_zip(self): """Test extraction with non-existent ZIP file""" nonexistent_zip = self.test_dir / "does_not_exist.zip" with pytest.raises(FileNotFoundError, match="does not exist"): self.zip_handler.extract_ada_project(str(nonexistent_zip)) def test_extract_ada_project_invalid_zip(self): """Test extraction with invalid ZIP file""" # Create a file that's not a ZIP invalid_zip = self.test_dir / "invalid.zip" invalid_zip.write_text("Not a ZIP file") with pytest.raises(zipfile.BadZipFile, match="Not a valid ZIP file"): self.zip_handler.extract_ada_project(str(invalid_zip)) def test_find_ada_project_root_single_file(self): """Test finding project root with single Ada file""" # Create directory structure with single Ada file test_extracted = self.test_dir / "extracted" test_extracted.mkdir() subdir = test_extracted / "project" subdir.mkdir() (subdir / "main.ads").write_text("package Main is end Main;") # Find root root = self.zip_handler._find_ada_project_root(test_extracted) assert root == subdir def test_find_ada_project_root_multiple_files(self): """Test finding project root with multiple Ada files""" # Create directory structure with multiple Ada files test_extracted = self.test_dir / "extracted" test_extracted.mkdir() # Create Ada files in same directory (test_extracted / "main.ads").write_text("package Main is end Main;") (test_extracted / "utils.adb").write_text("package body Utils is end Utils;") # Find root root = self.zip_handler._find_ada_project_root(test_extracted) assert root == test_extracted def test_find_ada_project_root_nested_structure(self): """Test finding project root with nested directory structure""" # Create nested directory structure test_extracted = self.test_dir / "extracted" test_extracted.mkdir() project_root = test_extracted / "my_project" project_root.mkdir() # Create Ada files in nested structure (project_root / "main.ads").write_text("package Main is end Main;") subdir = project_root / "src" subdir.mkdir() (subdir / "utils.adb").write_text("package body Utils is end Utils;") # Find root root = self.zip_handler._find_ada_project_root(test_extracted) assert root == project_root def test_create_python_project_zip_success(self): """Test successful Python project ZIP creation""" # Create a Python project directory structure python_project = self.test_dir / "python_project" python_project.mkdir() # Create project files src_dir = python_project / "src" src_dir.mkdir() (src_dir / "main.py").write_text("def main(): pass") (src_dir / "__init__.py").write_text("") tests_dir = python_project / "tests" tests_dir.mkdir() (tests_dir / "test_main.py").write_text("def test_main(): pass") (tests_dir / "__init__.py").write_text("") (python_project / "README.md").write_text("# Python Project") # Create ZIP zip_path = self.zip_handler.create_python_project_zip(str(python_project)) # Verify ZIP was created assert Path(zip_path).exists() assert Path(zip_path).suffix == ".zip" # Verify ZIP contents with zipfile.ZipFile(zip_path, 'r') as zf: file_list = zf.namelist() assert "src/main.py" in file_list assert "src/__init__.py" in file_list assert "tests/test_main.py" in file_list assert "tests/__init__.py" in file_list assert "README.md" in file_list def test_create_python_project_zip_nonexistent_project(self): """Test ZIP creation with non-existent project directory""" nonexistent_project = self.test_dir / "does_not_exist" with pytest.raises(FileNotFoundError, match="does not exist"): self.zip_handler.create_python_project_zip(str(nonexistent_project)) def test_create_python_project_zip_custom_output_path(self): """Test ZIP creation with custom output path""" # Create a Python project directory python_project = self.test_dir / "python_project" python_project.mkdir() (python_project / "main.py").write_text("def main(): pass") # Create ZIP with custom path custom_zip_path = self.test_dir / "custom_name.zip" zip_path = self.zip_handler.create_python_project_zip( str(python_project), str(custom_zip_path) ) # Verify custom path was used assert zip_path == str(custom_zip_path) assert custom_zip_path.exists() def test_extract_ada_project_with_session_id(self): """Test Ada project extraction with custom session ID""" # Create a ZIP file with Ada files zip_file = self.test_dir / "ada_project.zip" with zipfile.ZipFile(zip_file, 'w') as zf: zf.writestr("main.ads", "package Main is end Main;") # Extract with custom session ID custom_session_id = "test_session_123" ada_project_path, session_dir = self.zip_handler.extract_ada_project( str(zip_file), session_id=custom_session_id ) # Verify session ID was used in path assert custom_session_id in session_dir assert Path(session_dir).exists() assert Path(ada_project_path).exists()