|
| 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.npm.restore_bun_dependencies import ( |
| 9 | + BUN_LOCK_FILE_NAME, |
| 10 | + RestoreBunDependencies, |
| 11 | + _parse_bun_version, |
| 12 | +) |
| 13 | +from cycode.cli.models import Document |
| 14 | + |
| 15 | +_BUN_MODULE = 'cycode.cli.files_collector.sca.npm.restore_bun_dependencies' |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_ctx(tmp_path: Path) -> typer.Context: |
| 20 | + ctx = MagicMock(spec=typer.Context) |
| 21 | + ctx.obj = {'monitor': False} |
| 22 | + ctx.params = {'path': str(tmp_path)} |
| 23 | + return ctx |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def restore_bun(mock_ctx: typer.Context) -> RestoreBunDependencies: |
| 28 | + return RestoreBunDependencies(mock_ctx, is_git_diff=False, command_timeout=30) |
| 29 | + |
| 30 | + |
| 31 | +class TestIsProject: |
| 32 | + def test_package_json_with_bun_lock_matches(self, restore_bun: RestoreBunDependencies, tmp_path: Path) -> None: |
| 33 | + (tmp_path / 'package.json').write_text('{"name": "test"}') |
| 34 | + (tmp_path / 'bun.lock').write_text('{"lockfileVersion": 1}\n') |
| 35 | + doc = Document(str(tmp_path / 'package.json'), '{"name": "test"}', absolute_path=str(tmp_path / 'package.json')) |
| 36 | + assert restore_bun.is_project(doc) is True |
| 37 | + |
| 38 | + def test_package_json_with_package_manager_bun_matches(self, restore_bun: RestoreBunDependencies) -> None: |
| 39 | + content = '{"name": "test", "packageManager": "bun@1.1.0"}' |
| 40 | + doc = Document('package.json', content) |
| 41 | + assert restore_bun.is_project(doc) is True |
| 42 | + |
| 43 | + def test_package_json_with_engines_bun_matches(self, restore_bun: RestoreBunDependencies) -> None: |
| 44 | + content = '{"name": "test", "engines": {"bun": ">=1"}}' |
| 45 | + doc = Document('package.json', content) |
| 46 | + assert restore_bun.is_project(doc) is True |
| 47 | + |
| 48 | + def test_package_json_with_no_bun_signal_does_not_match( |
| 49 | + self, restore_bun: RestoreBunDependencies, tmp_path: Path |
| 50 | + ) -> None: |
| 51 | + (tmp_path / 'package.json').write_text('{"name": "test"}') |
| 52 | + doc = Document(str(tmp_path / 'package.json'), '{"name": "test"}', absolute_path=str(tmp_path / 'package.json')) |
| 53 | + assert restore_bun.is_project(doc) is False |
| 54 | + |
| 55 | + def test_package_json_with_yarn_lock_does_not_match( |
| 56 | + self, restore_bun: RestoreBunDependencies, tmp_path: Path |
| 57 | + ) -> None: |
| 58 | + (tmp_path / 'package.json').write_text('{"name": "test"}') |
| 59 | + (tmp_path / 'yarn.lock').write_text('# yarn lockfile v1\n') |
| 60 | + doc = Document(str(tmp_path / 'package.json'), '{"name": "test"}', absolute_path=str(tmp_path / 'package.json')) |
| 61 | + assert restore_bun.is_project(doc) is False |
| 62 | + |
| 63 | + def test_tsconfig_json_does_not_match(self, restore_bun: RestoreBunDependencies) -> None: |
| 64 | + doc = Document('tsconfig.json', '{"compilerOptions": {}}') |
| 65 | + assert restore_bun.is_project(doc) is False |
| 66 | + |
| 67 | + def test_package_manager_yarn_does_not_match(self, restore_bun: RestoreBunDependencies) -> None: |
| 68 | + content = '{"name": "test", "packageManager": "yarn@4.0.0"}' |
| 69 | + doc = Document('package.json', content) |
| 70 | + assert restore_bun.is_project(doc) is False |
| 71 | + |
| 72 | + def test_invalid_json_content_does_not_match(self, restore_bun: RestoreBunDependencies) -> None: |
| 73 | + doc = Document('package.json', 'not valid json') |
| 74 | + assert restore_bun.is_project(doc) is False |
| 75 | + |
| 76 | + |
| 77 | +class TestTryRestoreDependencies: |
| 78 | + def test_existing_bun_lock_returned_directly(self, restore_bun: RestoreBunDependencies, tmp_path: Path) -> None: |
| 79 | + bun_lock_content = '{"lockfileVersion": 1, "packages": {"package": ["package@1.0.0", "", {}, ""]}}\n' |
| 80 | + (tmp_path / 'package.json').write_text('{"name": "test"}') |
| 81 | + (tmp_path / 'bun.lock').write_text(bun_lock_content) |
| 82 | + |
| 83 | + doc = Document(str(tmp_path / 'package.json'), '{"name": "test"}', absolute_path=str(tmp_path / 'package.json')) |
| 84 | + result = restore_bun.try_restore_dependencies(doc) |
| 85 | + |
| 86 | + assert result is not None |
| 87 | + assert BUN_LOCK_FILE_NAME in result.path |
| 88 | + assert result.content == bun_lock_content |
| 89 | + |
| 90 | + def test_get_lock_file_name(self, restore_bun: RestoreBunDependencies) -> None: |
| 91 | + assert restore_bun.get_lock_file_name() == BUN_LOCK_FILE_NAME |
| 92 | + |
| 93 | + def test_get_commands_returns_bun_install(self, restore_bun: RestoreBunDependencies) -> None: |
| 94 | + commands = restore_bun.get_commands('/path/to/package.json') |
| 95 | + assert commands == [['bun', 'install', '--ignore-scripts']] |
| 96 | + |
| 97 | + |
| 98 | +_BASE_MODULE = 'cycode.cli.files_collector.sca.base_restore_dependencies' |
| 99 | + |
| 100 | + |
| 101 | +class TestParseBunVersion: |
| 102 | + def test_parses_full_semver(self) -> None: |
| 103 | + assert _parse_bun_version('1.2.3') == (1, 2) |
| 104 | + |
| 105 | + def test_parses_with_surrounding_whitespace(self) -> None: |
| 106 | + assert _parse_bun_version(' 1.2.0\n') == (1, 2) |
| 107 | + |
| 108 | + def test_none_input_returns_none(self) -> None: |
| 109 | + assert _parse_bun_version(None) is None |
| 110 | + |
| 111 | + def test_non_version_string_returns_none(self) -> None: |
| 112 | + assert _parse_bun_version('not-a-version') is None |
| 113 | + |
| 114 | + |
| 115 | +class TestBunVersionGate: |
| 116 | + def test_supported_version_proceeds_to_restore(self, restore_bun: RestoreBunDependencies, tmp_path: Path) -> None: |
| 117 | + content = '{"name": "test", "packageManager": "bun@1.2.0"}' |
| 118 | + (tmp_path / 'package.json').write_text(content) |
| 119 | + doc = Document(str(tmp_path / 'package.json'), content, absolute_path=str(tmp_path / 'package.json')) |
| 120 | + |
| 121 | + with ( |
| 122 | + patch(f'{_BUN_MODULE}.shell', return_value='1.2.5'), |
| 123 | + patch.object( |
| 124 | + restore_bun.__class__.__bases__[0], 'try_restore_dependencies', return_value=None |
| 125 | + ) as mock_super, |
| 126 | + ): |
| 127 | + restore_bun.try_restore_dependencies(doc) |
| 128 | + mock_super.assert_called_once_with(doc) |
| 129 | + |
| 130 | + def test_old_version_skips_restore(self, restore_bun: RestoreBunDependencies, tmp_path: Path) -> None: |
| 131 | + content = '{"name": "test", "packageManager": "bun@1.1.0"}' |
| 132 | + (tmp_path / 'package.json').write_text(content) |
| 133 | + doc = Document(str(tmp_path / 'package.json'), content, absolute_path=str(tmp_path / 'package.json')) |
| 134 | + |
| 135 | + with ( |
| 136 | + patch(f'{_BUN_MODULE}.shell', return_value='1.1.38'), |
| 137 | + patch.object(restore_bun.__class__.__bases__[0], 'try_restore_dependencies') as mock_super, |
| 138 | + ): |
| 139 | + result = restore_bun.try_restore_dependencies(doc) |
| 140 | + assert result is None |
| 141 | + mock_super.assert_not_called() |
| 142 | + |
| 143 | + def test_missing_bun_skips_restore(self, restore_bun: RestoreBunDependencies, tmp_path: Path) -> None: |
| 144 | + content = '{"name": "test", "packageManager": "bun@1.2.0"}' |
| 145 | + (tmp_path / 'package.json').write_text(content) |
| 146 | + doc = Document(str(tmp_path / 'package.json'), content, absolute_path=str(tmp_path / 'package.json')) |
| 147 | + |
| 148 | + with ( |
| 149 | + patch(f'{_BUN_MODULE}.shell', return_value=None), |
| 150 | + patch.object(restore_bun.__class__.__bases__[0], 'try_restore_dependencies') as mock_super, |
| 151 | + ): |
| 152 | + result = restore_bun.try_restore_dependencies(doc) |
| 153 | + assert result is None |
| 154 | + mock_super.assert_not_called() |
| 155 | + |
| 156 | + def test_existing_lockfile_skips_version_check(self, restore_bun: RestoreBunDependencies, tmp_path: Path) -> None: |
| 157 | + (tmp_path / 'package.json').write_text('{"name": "test"}') |
| 158 | + (tmp_path / 'bun.lock').write_text('{"lockfileVersion": 1}\n') |
| 159 | + doc = Document(str(tmp_path / 'package.json'), '{"name": "test"}', absolute_path=str(tmp_path / 'package.json')) |
| 160 | + |
| 161 | + with patch(f'{_BUN_MODULE}.shell') as mock_shell: |
| 162 | + result = restore_bun.try_restore_dependencies(doc) |
| 163 | + assert result is not None |
| 164 | + mock_shell.assert_not_called() |
| 165 | + |
| 166 | + |
| 167 | +class TestCleanup: |
| 168 | + def test_generated_lockfile_is_deleted_after_restore( |
| 169 | + self, restore_bun: RestoreBunDependencies, tmp_path: Path |
| 170 | + ) -> None: |
| 171 | + # bun: no pre-existing bun.lock but package.json indicates bun (supported version installed) |
| 172 | + content = '{"name": "test", "packageManager": "bun@1.2.0"}' |
| 173 | + (tmp_path / 'package.json').write_text(content) |
| 174 | + doc = Document(str(tmp_path / 'package.json'), content, absolute_path=str(tmp_path / 'package.json')) |
| 175 | + lock_path = tmp_path / BUN_LOCK_FILE_NAME |
| 176 | + |
| 177 | + def side_effect( |
| 178 | + commands: list, |
| 179 | + timeout: int, |
| 180 | + output_file_path: Optional[str] = None, |
| 181 | + working_directory: Optional[str] = None, |
| 182 | + ) -> str: |
| 183 | + lock_path.write_text('{"lockfileVersion": 1}\n') |
| 184 | + return 'output' |
| 185 | + |
| 186 | + with ( |
| 187 | + patch(f'{_BUN_MODULE}.shell', return_value='1.2.5'), |
| 188 | + patch(f'{_BASE_MODULE}.execute_commands', side_effect=side_effect), |
| 189 | + ): |
| 190 | + result = restore_bun.try_restore_dependencies(doc) |
| 191 | + |
| 192 | + assert result is not None |
| 193 | + assert not lock_path.exists(), f'{BUN_LOCK_FILE_NAME} must be deleted after restore' |
| 194 | + |
| 195 | + def test_preexisting_lockfile_is_not_deleted(self, restore_bun: RestoreBunDependencies, tmp_path: Path) -> None: |
| 196 | + lock_content = '{"lockfileVersion": 1, "packages": {"pkg": ["pkg@1.0.0", "", {}, ""]}}\n' |
| 197 | + (tmp_path / 'package.json').write_text('{"name": "test"}') |
| 198 | + lock_path = tmp_path / BUN_LOCK_FILE_NAME |
| 199 | + lock_path.write_text(lock_content) |
| 200 | + doc = Document(str(tmp_path / 'package.json'), '{"name": "test"}', absolute_path=str(tmp_path / 'package.json')) |
| 201 | + |
| 202 | + result = restore_bun.try_restore_dependencies(doc) |
| 203 | + |
| 204 | + assert result is not None |
| 205 | + assert lock_path.exists(), f'Pre-existing {BUN_LOCK_FILE_NAME} must not be deleted' |
0 commit comments