|
| 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_uv_dependencies import ( |
| 9 | + UV_LOCK_FILE_NAME, |
| 10 | + RestoreUvDependencies, |
| 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_uv(mock_ctx: typer.Context) -> RestoreUvDependencies: |
| 25 | + return RestoreUvDependencies(mock_ctx, is_git_diff=False, command_timeout=30) |
| 26 | + |
| 27 | + |
| 28 | +class TestIsProject: |
| 29 | + def test_pyproject_toml_with_uv_lock_matches(self, restore_uv: RestoreUvDependencies, tmp_path: Path) -> None: |
| 30 | + (tmp_path / 'pyproject.toml').write_text('[build-system]\nrequires = ["hatchling"]\n') |
| 31 | + (tmp_path / 'uv.lock').write_text('version = 1\n') |
| 32 | + doc = Document( |
| 33 | + str(tmp_path / 'pyproject.toml'), |
| 34 | + '[build-system]\nrequires = ["hatchling"]\n', |
| 35 | + absolute_path=str(tmp_path / 'pyproject.toml'), |
| 36 | + ) |
| 37 | + assert restore_uv.is_project(doc) is True |
| 38 | + |
| 39 | + def test_pyproject_toml_with_tool_uv_section_matches(self, restore_uv: RestoreUvDependencies) -> None: |
| 40 | + content = '[tool.uv]\ndev-dependencies = ["pytest"]\n' |
| 41 | + doc = Document('pyproject.toml', content) |
| 42 | + assert restore_uv.is_project(doc) is True |
| 43 | + |
| 44 | + def test_pyproject_toml_without_uv_signals_does_not_match( |
| 45 | + self, restore_uv: RestoreUvDependencies, tmp_path: Path |
| 46 | + ) -> None: |
| 47 | + content = '[tool.poetry]\nname = "my-project"\n' |
| 48 | + (tmp_path / 'pyproject.toml').write_text(content) |
| 49 | + doc = Document( |
| 50 | + str(tmp_path / 'pyproject.toml'), |
| 51 | + content, |
| 52 | + absolute_path=str(tmp_path / 'pyproject.toml'), |
| 53 | + ) |
| 54 | + assert restore_uv.is_project(doc) is False |
| 55 | + |
| 56 | + def test_requirements_txt_does_not_match(self, restore_uv: RestoreUvDependencies) -> None: |
| 57 | + doc = Document('requirements.txt', 'requests==2.31.0\n') |
| 58 | + assert restore_uv.is_project(doc) is False |
| 59 | + |
| 60 | + def test_empty_content_does_not_match(self, restore_uv: RestoreUvDependencies, tmp_path: Path) -> None: |
| 61 | + (tmp_path / 'pyproject.toml').write_text('') |
| 62 | + doc = Document( |
| 63 | + str(tmp_path / 'pyproject.toml'), |
| 64 | + '', |
| 65 | + absolute_path=str(tmp_path / 'pyproject.toml'), |
| 66 | + ) |
| 67 | + assert restore_uv.is_project(doc) is False |
| 68 | + |
| 69 | + |
| 70 | +class TestTryRestoreDependencies: |
| 71 | + def test_existing_uv_lock_returned_directly(self, restore_uv: RestoreUvDependencies, tmp_path: Path) -> None: |
| 72 | + lock_content = 'version = 1\n\n[[package]]\nname = "requests"\n' |
| 73 | + (tmp_path / 'pyproject.toml').write_text('[tool.uv]\n') |
| 74 | + (tmp_path / 'uv.lock').write_text(lock_content) |
| 75 | + |
| 76 | + doc = Document( |
| 77 | + str(tmp_path / 'pyproject.toml'), |
| 78 | + '[tool.uv]\n', |
| 79 | + absolute_path=str(tmp_path / 'pyproject.toml'), |
| 80 | + ) |
| 81 | + result = restore_uv.try_restore_dependencies(doc) |
| 82 | + |
| 83 | + assert result is not None |
| 84 | + assert UV_LOCK_FILE_NAME in result.path |
| 85 | + assert result.content == lock_content |
| 86 | + |
| 87 | + def test_get_lock_file_name(self, restore_uv: RestoreUvDependencies) -> None: |
| 88 | + assert restore_uv.get_lock_file_name() == UV_LOCK_FILE_NAME |
| 89 | + |
| 90 | + def test_get_commands_returns_uv_lock(self, restore_uv: RestoreUvDependencies) -> None: |
| 91 | + commands = restore_uv.get_commands('/path/to/pyproject.toml') |
| 92 | + assert commands == [['uv', 'lock']] |
| 93 | + |
| 94 | + |
| 95 | +_BASE_MODULE = 'cycode.cli.files_collector.sca.base_restore_dependencies' |
| 96 | + |
| 97 | + |
| 98 | +class TestCleanup: |
| 99 | + def test_generated_lockfile_is_deleted_after_restore( |
| 100 | + self, restore_uv: RestoreUvDependencies, tmp_path: Path |
| 101 | + ) -> None: |
| 102 | + manifest_content = '[tool.uv]\ndev-dependencies = ["pytest"]\n' |
| 103 | + (tmp_path / 'pyproject.toml').write_text(manifest_content) |
| 104 | + doc = Document( |
| 105 | + str(tmp_path / 'pyproject.toml'), manifest_content, absolute_path=str(tmp_path / 'pyproject.toml') |
| 106 | + ) |
| 107 | + lock_path = tmp_path / UV_LOCK_FILE_NAME |
| 108 | + |
| 109 | + def side_effect( |
| 110 | + commands: list, |
| 111 | + timeout: int, |
| 112 | + output_file_path: Optional[str] = None, |
| 113 | + working_directory: Optional[str] = None, |
| 114 | + ) -> str: |
| 115 | + lock_path.write_text('version = 1\n') |
| 116 | + return 'output' |
| 117 | + |
| 118 | + with patch(f'{_BASE_MODULE}.execute_commands', side_effect=side_effect): |
| 119 | + result = restore_uv.try_restore_dependencies(doc) |
| 120 | + |
| 121 | + assert result is not None |
| 122 | + assert not lock_path.exists(), f'{UV_LOCK_FILE_NAME} must be deleted after restore' |
| 123 | + |
| 124 | + def test_preexisting_lockfile_is_not_deleted(self, restore_uv: RestoreUvDependencies, tmp_path: Path) -> None: |
| 125 | + lock_content = 'version = 1\n\n[[package]]\nname = "requests"\n' |
| 126 | + (tmp_path / 'pyproject.toml').write_text('[tool.uv]\n') |
| 127 | + lock_path = tmp_path / UV_LOCK_FILE_NAME |
| 128 | + lock_path.write_text(lock_content) |
| 129 | + doc = Document( |
| 130 | + str(tmp_path / 'pyproject.toml'), |
| 131 | + '[tool.uv]\n', |
| 132 | + absolute_path=str(tmp_path / 'pyproject.toml'), |
| 133 | + ) |
| 134 | + |
| 135 | + result = restore_uv.try_restore_dependencies(doc) |
| 136 | + |
| 137 | + assert result is not None |
| 138 | + assert lock_path.exists(), f'Pre-existing {UV_LOCK_FILE_NAME} must not be deleted' |
0 commit comments