|
| 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.python.restore_pip_dependencies import ( |
| 9 | + PIP_LOCK_FILE_NAME, |
| 10 | + RestorePipDependencies, |
| 11 | +) |
| 12 | +from cycode.cli.models import Document |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def mock_ctx(tmp_path: Path) -> typer.Context: |
| 17 | + ctx = MagicMock(spec=typer.Context) |
| 18 | + ctx.obj = {'monitor': False} |
| 19 | + ctx.params = {'path': str(tmp_path)} |
| 20 | + return ctx |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def restore_pip(mock_ctx: typer.Context) -> RestorePipDependencies: |
| 25 | + return RestorePipDependencies(mock_ctx, is_git_diff=False, command_timeout=30) |
| 26 | + |
| 27 | + |
| 28 | +class TestIsProject: |
| 29 | + def test_plain_pyproject_toml_matches(self, restore_pip: RestorePipDependencies) -> None: |
| 30 | + content = '[project]\nname = "my-project"\ndependencies = ["requests"]\n' |
| 31 | + doc = Document('pyproject.toml', content) |
| 32 | + assert restore_pip.is_project(doc) is True |
| 33 | + |
| 34 | + def test_pyproject_toml_with_poetry_section_does_not_match(self, restore_pip: RestorePipDependencies) -> None: |
| 35 | + content = '[tool.poetry]\nname = "my-project"\n' |
| 36 | + doc = Document('pyproject.toml', content) |
| 37 | + assert restore_pip.is_project(doc) is False |
| 38 | + |
| 39 | + def test_pyproject_toml_with_uv_section_does_not_match(self, restore_pip: RestorePipDependencies) -> None: |
| 40 | + content = '[tool.uv]\nindex-url = "https://example.com"\n' |
| 41 | + doc = Document('pyproject.toml', content) |
| 42 | + assert restore_pip.is_project(doc) is False |
| 43 | + |
| 44 | + def test_pyproject_toml_with_existing_pylock_matches( |
| 45 | + self, restore_pip: RestorePipDependencies, tmp_path: Path |
| 46 | + ) -> None: |
| 47 | + (tmp_path / 'pyproject.toml').write_text('[project]\nname = "test"\n') |
| 48 | + (tmp_path / PIP_LOCK_FILE_NAME).write_text('lock-version = "1.0"\n') |
| 49 | + doc = Document( |
| 50 | + str(tmp_path / 'pyproject.toml'), |
| 51 | + '[project]\nname = "test"\n', |
| 52 | + absolute_path=str(tmp_path / 'pyproject.toml'), |
| 53 | + ) |
| 54 | + assert restore_pip.is_project(doc) is True |
| 55 | + |
| 56 | + def test_requirements_txt_matches(self, restore_pip: RestorePipDependencies) -> None: |
| 57 | + doc = Document('requirements.txt', 'requests==2.31.0\n') |
| 58 | + assert restore_pip.is_project(doc) is True |
| 59 | + |
| 60 | + def test_setup_py_does_not_match(self, restore_pip: RestorePipDependencies) -> None: |
| 61 | + doc = Document('setup.py', 'from setuptools import setup\nsetup()\n') |
| 62 | + assert restore_pip.is_project(doc) is False |
| 63 | + |
| 64 | + def test_empty_pyproject_toml_does_not_match(self, restore_pip: RestorePipDependencies) -> None: |
| 65 | + # Same conservative behavior as Poetry/Uv's own is_project: empty content can't be |
| 66 | + # confirmed as plain-pip, so don't claim it. |
| 67 | + doc = Document('pyproject.toml', '') |
| 68 | + assert restore_pip.is_project(doc) is False |
| 69 | + |
| 70 | + |
| 71 | +class TestGetCommands: |
| 72 | + def test_get_commands_for_pyproject_toml(self, restore_pip: RestorePipDependencies) -> None: |
| 73 | + commands = restore_pip.get_commands('/path/to/pyproject.toml') |
| 74 | + assert commands == [['pip', 'lock', '.']] |
| 75 | + |
| 76 | + def test_get_commands_for_requirements_txt(self, restore_pip: RestorePipDependencies) -> None: |
| 77 | + commands = restore_pip.get_commands('/path/to/requirements.txt') |
| 78 | + assert commands == [['pip', 'lock', '-r', 'requirements.txt', '-o', PIP_LOCK_FILE_NAME]] |
| 79 | + |
| 80 | + def test_get_lock_file_name(self, restore_pip: RestorePipDependencies) -> None: |
| 81 | + assert restore_pip.get_lock_file_name() == PIP_LOCK_FILE_NAME |
| 82 | + |
| 83 | + |
| 84 | +class TestTryRestoreDependencies: |
| 85 | + def test_existing_pylock_returned_directly_for_pyproject_toml( |
| 86 | + self, restore_pip: RestorePipDependencies, tmp_path: Path |
| 87 | + ) -> None: |
| 88 | + lock_content = 'lock-version = "1.0"\n\n[[packages]]\nname = "requests"\n' |
| 89 | + (tmp_path / 'pyproject.toml').write_text('[project]\nname = "test"\n') |
| 90 | + (tmp_path / PIP_LOCK_FILE_NAME).write_text(lock_content) |
| 91 | + |
| 92 | + doc = Document( |
| 93 | + str(tmp_path / 'pyproject.toml'), |
| 94 | + '[project]\nname = "test"\n', |
| 95 | + absolute_path=str(tmp_path / 'pyproject.toml'), |
| 96 | + ) |
| 97 | + result = restore_pip.try_restore_dependencies(doc) |
| 98 | + |
| 99 | + assert result is not None |
| 100 | + assert PIP_LOCK_FILE_NAME in result.path |
| 101 | + assert result.content == lock_content |
| 102 | + |
| 103 | + def test_existing_pylock_returned_directly_for_requirements_txt( |
| 104 | + self, restore_pip: RestorePipDependencies, tmp_path: Path |
| 105 | + ) -> None: |
| 106 | + lock_content = 'lock-version = "1.0"\n\n[[packages]]\nname = "requests"\n' |
| 107 | + (tmp_path / 'requirements.txt').write_text('requests==2.31.0\n') |
| 108 | + (tmp_path / PIP_LOCK_FILE_NAME).write_text(lock_content) |
| 109 | + |
| 110 | + doc = Document( |
| 111 | + str(tmp_path / 'requirements.txt'), |
| 112 | + 'requests==2.31.0\n', |
| 113 | + absolute_path=str(tmp_path / 'requirements.txt'), |
| 114 | + ) |
| 115 | + result = restore_pip.try_restore_dependencies(doc) |
| 116 | + |
| 117 | + assert result is not None |
| 118 | + assert result.content == lock_content |
| 119 | + |
| 120 | + |
| 121 | +_BASE_MODULE = 'cycode.cli.files_collector.sca.base_restore_dependencies' |
| 122 | + |
| 123 | + |
| 124 | +class TestRestoreWithoutExistingLock: |
| 125 | + def test_pyproject_toml_runs_pip_lock_dot(self, restore_pip: RestorePipDependencies, tmp_path: Path) -> None: |
| 126 | + manifest_content = '[project]\nname = "test"\ndependencies = ["requests"]\n' |
| 127 | + (tmp_path / 'pyproject.toml').write_text(manifest_content) |
| 128 | + doc = Document( |
| 129 | + str(tmp_path / 'pyproject.toml'), manifest_content, absolute_path=str(tmp_path / 'pyproject.toml') |
| 130 | + ) |
| 131 | + |
| 132 | + seen_commands = [] |
| 133 | + |
| 134 | + def side_effect( |
| 135 | + commands: list, |
| 136 | + timeout: int, |
| 137 | + output_file_path: Optional[str] = None, |
| 138 | + working_directory: Optional[str] = None, |
| 139 | + ) -> str: |
| 140 | + seen_commands.extend(commands) |
| 141 | + (tmp_path / PIP_LOCK_FILE_NAME).write_text('lock-version = "1.0"\n') |
| 142 | + return 'output' |
| 143 | + |
| 144 | + with patch(f'{_BASE_MODULE}.execute_commands', side_effect=side_effect): |
| 145 | + result = restore_pip.try_restore_dependencies(doc) |
| 146 | + |
| 147 | + assert result is not None |
| 148 | + assert seen_commands == [['pip', 'lock', '.']] |
| 149 | + |
| 150 | + def test_requirements_txt_runs_pip_lock_dash_r(self, restore_pip: RestorePipDependencies, tmp_path: Path) -> None: |
| 151 | + (tmp_path / 'requirements.txt').write_text('requests==2.31.0\n') |
| 152 | + doc = Document( |
| 153 | + str(tmp_path / 'requirements.txt'), |
| 154 | + 'requests==2.31.0\n', |
| 155 | + absolute_path=str(tmp_path / 'requirements.txt'), |
| 156 | + ) |
| 157 | + |
| 158 | + seen_commands = [] |
| 159 | + |
| 160 | + def side_effect( |
| 161 | + commands: list, |
| 162 | + timeout: int, |
| 163 | + output_file_path: Optional[str] = None, |
| 164 | + working_directory: Optional[str] = None, |
| 165 | + ) -> str: |
| 166 | + seen_commands.extend(commands) |
| 167 | + (tmp_path / PIP_LOCK_FILE_NAME).write_text('lock-version = "1.0"\n') |
| 168 | + return 'output' |
| 169 | + |
| 170 | + with patch(f'{_BASE_MODULE}.execute_commands', side_effect=side_effect): |
| 171 | + result = restore_pip.try_restore_dependencies(doc) |
| 172 | + |
| 173 | + assert result is not None |
| 174 | + assert seen_commands == [['pip', 'lock', '-r', 'requirements.txt', '-o', PIP_LOCK_FILE_NAME]] |
| 175 | + |
| 176 | + |
| 177 | +class TestCleanup: |
| 178 | + def test_generated_lockfile_is_deleted_after_restore( |
| 179 | + self, restore_pip: RestorePipDependencies, tmp_path: Path |
| 180 | + ) -> None: |
| 181 | + manifest_content = '[project]\nname = "test"\ndependencies = ["requests"]\n' |
| 182 | + (tmp_path / 'pyproject.toml').write_text(manifest_content) |
| 183 | + doc = Document( |
| 184 | + str(tmp_path / 'pyproject.toml'), manifest_content, absolute_path=str(tmp_path / 'pyproject.toml') |
| 185 | + ) |
| 186 | + lock_path = tmp_path / PIP_LOCK_FILE_NAME |
| 187 | + |
| 188 | + def side_effect( |
| 189 | + commands: list, |
| 190 | + timeout: int, |
| 191 | + output_file_path: Optional[str] = None, |
| 192 | + working_directory: Optional[str] = None, |
| 193 | + ) -> str: |
| 194 | + lock_path.write_text('lock-version = "1.0"\n') |
| 195 | + return 'output' |
| 196 | + |
| 197 | + with patch(f'{_BASE_MODULE}.execute_commands', side_effect=side_effect): |
| 198 | + result = restore_pip.try_restore_dependencies(doc) |
| 199 | + |
| 200 | + assert result is not None |
| 201 | + assert not lock_path.exists(), f'{PIP_LOCK_FILE_NAME} must be deleted after restore' |
| 202 | + |
| 203 | + def test_preexisting_lockfile_is_not_deleted(self, restore_pip: RestorePipDependencies, tmp_path: Path) -> None: |
| 204 | + lock_content = 'lock-version = "1.0"\n' |
| 205 | + (tmp_path / 'pyproject.toml').write_text('[project]\nname = "test"\n') |
| 206 | + lock_path = tmp_path / PIP_LOCK_FILE_NAME |
| 207 | + lock_path.write_text(lock_content) |
| 208 | + doc = Document( |
| 209 | + str(tmp_path / 'pyproject.toml'), |
| 210 | + '[project]\nname = "test"\n', |
| 211 | + absolute_path=str(tmp_path / 'pyproject.toml'), |
| 212 | + ) |
| 213 | + |
| 214 | + result = restore_pip.try_restore_dependencies(doc) |
| 215 | + |
| 216 | + assert result is not None |
| 217 | + assert lock_path.exists(), f'Pre-existing {PIP_LOCK_FILE_NAME} must not be deleted' |
0 commit comments