|
| 1 | +from pathlib import Path |
| 2 | +from typing import Optional |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +import typer |
| 7 | + |
| 8 | +from cycode.cli.files_collector.sca.maven.restore_gradle_dependencies import ( |
| 9 | + BUILD_GRADLE_DEP_TREE_FILE_NAME, |
| 10 | + BUILD_GRADLE_FILE_NAME, |
| 11 | + BUILD_GRADLE_KTS_FILE_NAME, |
| 12 | + RestoreGradleDependencies, |
| 13 | +) |
| 14 | +from cycode.cli.models import Document |
| 15 | + |
| 16 | +_BASE_MODULE = 'cycode.cli.files_collector.sca.base_restore_dependencies' |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_ctx(tmp_path: Path) -> typer.Context: |
| 21 | + ctx = MagicMock(spec=typer.Context) |
| 22 | + ctx.obj = {'monitor': False, 'gradle_all_sub_projects': False} |
| 23 | + ctx.params = {'path': str(tmp_path)} |
| 24 | + return ctx |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def restore_gradle(mock_ctx: typer.Context) -> RestoreGradleDependencies: |
| 29 | + return RestoreGradleDependencies(mock_ctx, is_git_diff=False, command_timeout=30) |
| 30 | + |
| 31 | + |
| 32 | +class TestIsProject: |
| 33 | + def test_build_gradle_matches(self, restore_gradle: RestoreGradleDependencies) -> None: |
| 34 | + doc = Document('build.gradle', 'apply plugin: "java"\n') |
| 35 | + assert restore_gradle.is_project(doc) is True |
| 36 | + |
| 37 | + def test_build_gradle_kts_matches(self, restore_gradle: RestoreGradleDependencies) -> None: |
| 38 | + doc = Document('build.gradle.kts', 'plugins { java }\n') |
| 39 | + assert restore_gradle.is_project(doc) is True |
| 40 | + |
| 41 | + def test_pom_xml_does_not_match(self, restore_gradle: RestoreGradleDependencies) -> None: |
| 42 | + doc = Document('pom.xml', '<project/>') |
| 43 | + assert restore_gradle.is_project(doc) is False |
| 44 | + |
| 45 | + def test_settings_gradle_does_not_match(self, restore_gradle: RestoreGradleDependencies) -> None: |
| 46 | + doc = Document('settings.gradle', 'rootProject.name = "test"') |
| 47 | + assert restore_gradle.is_project(doc) is False |
| 48 | + |
| 49 | + |
| 50 | +class TestCleanup: |
| 51 | + def test_generated_dep_tree_file_is_deleted_after_restore( |
| 52 | + self, restore_gradle: RestoreGradleDependencies, tmp_path: Path |
| 53 | + ) -> None: |
| 54 | + (tmp_path / BUILD_GRADLE_FILE_NAME).write_text('apply plugin: "java"\n') |
| 55 | + doc = Document( |
| 56 | + str(tmp_path / BUILD_GRADLE_FILE_NAME), |
| 57 | + 'apply plugin: "java"\n', |
| 58 | + absolute_path=str(tmp_path / BUILD_GRADLE_FILE_NAME), |
| 59 | + ) |
| 60 | + output_path = tmp_path / BUILD_GRADLE_DEP_TREE_FILE_NAME |
| 61 | + |
| 62 | + def side_effect( |
| 63 | + commands: list, |
| 64 | + timeout: int, |
| 65 | + output_file_path: Optional[str] = None, |
| 66 | + working_directory: Optional[str] = None, |
| 67 | + ) -> str: |
| 68 | + # Gradle uses create_output_file_manually=True; output_file_path is provided |
| 69 | + target = output_file_path or str(output_path) |
| 70 | + Path(target).write_text('compileClasspath - Compile classpath:\n\\--- org.example:lib:1.0\n') |
| 71 | + return 'dep tree output' |
| 72 | + |
| 73 | + with patch(f'{_BASE_MODULE}.execute_commands', side_effect=side_effect): |
| 74 | + result = restore_gradle.try_restore_dependencies(doc) |
| 75 | + |
| 76 | + assert result is not None |
| 77 | + assert not output_path.exists(), f'{BUILD_GRADLE_DEP_TREE_FILE_NAME} must be deleted after restore' |
| 78 | + |
| 79 | + def test_preexisting_dep_tree_file_is_not_deleted( |
| 80 | + self, restore_gradle: RestoreGradleDependencies, tmp_path: Path |
| 81 | + ) -> None: |
| 82 | + dep_tree_content = 'compileClasspath - Compile classpath:\n\\--- org.example:lib:1.0\n' |
| 83 | + (tmp_path / BUILD_GRADLE_FILE_NAME).write_text('apply plugin: "java"\n') |
| 84 | + output_path = tmp_path / BUILD_GRADLE_DEP_TREE_FILE_NAME |
| 85 | + output_path.write_text(dep_tree_content) |
| 86 | + doc = Document( |
| 87 | + str(tmp_path / BUILD_GRADLE_FILE_NAME), |
| 88 | + 'apply plugin: "java"\n', |
| 89 | + absolute_path=str(tmp_path / BUILD_GRADLE_FILE_NAME), |
| 90 | + ) |
| 91 | + |
| 92 | + result = restore_gradle.try_restore_dependencies(doc) |
| 93 | + |
| 94 | + assert result is not None |
| 95 | + assert output_path.exists(), f'Pre-existing {BUILD_GRADLE_DEP_TREE_FILE_NAME} must not be deleted' |
| 96 | + |
| 97 | + def test_kts_build_file_also_cleaned_up(self, restore_gradle: RestoreGradleDependencies, tmp_path: Path) -> None: |
| 98 | + (tmp_path / BUILD_GRADLE_KTS_FILE_NAME).write_text('plugins { java }\n') |
| 99 | + doc = Document( |
| 100 | + str(tmp_path / BUILD_GRADLE_KTS_FILE_NAME), |
| 101 | + 'plugins { java }\n', |
| 102 | + absolute_path=str(tmp_path / BUILD_GRADLE_KTS_FILE_NAME), |
| 103 | + ) |
| 104 | + output_path = tmp_path / BUILD_GRADLE_DEP_TREE_FILE_NAME |
| 105 | + |
| 106 | + def side_effect( |
| 107 | + commands: list, |
| 108 | + timeout: int, |
| 109 | + output_file_path: Optional[str] = None, |
| 110 | + working_directory: Optional[str] = None, |
| 111 | + ) -> str: |
| 112 | + target = output_file_path or str(output_path) |
| 113 | + Path(target).write_text('compileClasspath\n') |
| 114 | + return 'output' |
| 115 | + |
| 116 | + with patch(f'{_BASE_MODULE}.execute_commands', side_effect=side_effect): |
| 117 | + result = restore_gradle.try_restore_dependencies(doc) |
| 118 | + |
| 119 | + assert result is not None |
| 120 | + assert not output_path.exists(), f'{BUILD_GRADLE_DEP_TREE_FILE_NAME} must be deleted after restore' |
0 commit comments