Skip to content

Commit e81e0cc

Browse files
committed
CM-55551 - format files
1 parent 2280794 commit e81e0cc

File tree

2 files changed

+32
-38
lines changed

2 files changed

+32
-38
lines changed

cycode/cli/files_collector/sca/npm/restore_npm_dependencies.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
ALTERNATIVE_LOCK_FILES = ['yarn.lock', 'pnpm-lock.yaml', 'deno.lock']
1717
NPM_LOCK_FILE_NAMES = [NPM_LOCK_FILE_NAME, *ALTERNATIVE_LOCK_FILES]
1818
NPM_MANIFEST_FILE_NAME = 'package.json'
19+
20+
1921
class RestoreNpmDependencies(BaseRestoreDependencies):
2022
def __init__(self, ctx: typer.Context, is_git_diff: bool, command_timeout: int) -> None:
2123
super().__init__(ctx, is_git_diff, command_timeout)
@@ -52,10 +54,7 @@ def _find_existing_lockfile(self, manifest_dir: str) -> tuple[Optional[str], lis
5254
Returns:
5355
Tuple of (lockfile_path if found, list of checked lockfiles with status).
5456
"""
55-
lock_file_paths = [
56-
os.path.join(manifest_dir, lock_file_name)
57-
for lock_file_name in NPM_LOCK_FILE_NAMES
58-
]
57+
lock_file_paths = [os.path.join(manifest_dir, lock_file_name) for lock_file_name in NPM_LOCK_FILE_NAMES]
5958

6059
existing_lock_file = None
6160
checked_lockfiles = []
@@ -69,9 +68,7 @@ def _find_existing_lockfile(self, manifest_dir: str) -> tuple[Optional[str], lis
6968

7069
return existing_lock_file, checked_lockfiles
7170

72-
def _create_document_from_lockfile(
73-
self, document: Document, lockfile_path: str
74-
) -> Optional[Document]:
71+
def _create_document_from_lockfile(self, document: Document, lockfile_path: str) -> Optional[Document]:
7572
"""Create a Document from an existing lockfile.
7673
7774
Args:
@@ -128,8 +125,9 @@ def try_restore_dependencies(self, document: Document) -> Optional[Document]:
128125
return super().try_restore_dependencies(document)
129126

130127
# Check for existing lockfiles
131-
logger.debug('Checking for existing lockfiles in directory, %s',
132-
{'directory': manifest_dir, 'path': document.path})
128+
logger.debug(
129+
'Checking for existing lockfiles in directory, %s', {'directory': manifest_dir, 'path': document.path}
130+
)
133131
existing_lock_file, checked_lockfiles = self._find_existing_lockfile(manifest_dir)
134132

135133
logger.debug(

tests/cli/files_collector/sca/npm/test_restore_npm_dependencies.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ class TestRestoreNpmDependenciesAlternativeLockfiles:
4040
],
4141
)
4242
def test_lockfile_exists_should_skip_npm_install(
43-
self,
44-
restore_npm_dependencies: RestoreNpmDependencies,
45-
tmp_path: Path,
46-
lockfile_name: str,
47-
lockfile_content: str,
48-
expected_content: str,
43+
self,
44+
restore_npm_dependencies: RestoreNpmDependencies,
45+
tmp_path: Path,
46+
lockfile_name: str,
47+
lockfile_content: str,
48+
expected_content: str,
4949
) -> None:
5050
"""Test that when any lockfile exists, npm install is skipped."""
5151
# Setup: Create package.json and lockfile
@@ -70,7 +70,7 @@ def test_lockfile_exists_should_skip_npm_install(
7070
assert result.content == expected_content
7171

7272
def test_no_lockfile_exists_should_proceed_with_normal_flow(
73-
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
73+
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
7474
) -> None:
7575
"""Test that when no lockfile exists, normal flow proceeds (will run npm install)."""
7676
# Setup: Create only package.json (no lockfile)
@@ -85,9 +85,9 @@ def test_no_lockfile_exists_should_proceed_with_normal_flow(
8585

8686
# Mock the base class's try_restore_dependencies to verify it's called
8787
with patch.object(
88-
restore_npm_dependencies.__class__.__bases__[0],
89-
'try_restore_dependencies',
90-
return_value=None,
88+
restore_npm_dependencies.__class__.__bases__[0],
89+
'try_restore_dependencies',
90+
return_value=None,
9191
) as mock_super:
9292
# Execute
9393
restore_npm_dependencies.try_restore_dependencies(document)
@@ -104,10 +104,10 @@ class TestRestoreNpmDependenciesPathResolution:
104104
[True, False],
105105
)
106106
def test_path_resolution_with_different_path_types(
107-
self,
108-
restore_npm_dependencies: RestoreNpmDependencies,
109-
tmp_path: Path,
110-
has_absolute_path: bool,
107+
self,
108+
restore_npm_dependencies: RestoreNpmDependencies,
109+
tmp_path: Path,
110+
has_absolute_path: bool,
111111
) -> None:
112112
"""Test path resolution with absolute or relative paths."""
113113
package_json_path = tmp_path / 'package.json'
@@ -127,9 +127,7 @@ def test_path_resolution_with_different_path_types(
127127
assert result is not None
128128
assert result.content == 'lockfileVersion: 5.4\n'
129129

130-
def test_path_resolution_in_monitor_mode(
131-
self, tmp_path: Path
132-
) -> None:
130+
def test_path_resolution_in_monitor_mode(self, tmp_path: Path) -> None:
133131
"""Test path resolution in monitor mode."""
134132
# Setup monitor mode context
135133
ctx = MagicMock(spec=typer.Context)
@@ -160,7 +158,7 @@ def test_path_resolution_in_monitor_mode(
160158
assert result.content == 'lockfileVersion: 5.4\n'
161159

162160
def test_path_resolution_with_nested_directory(
163-
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
161+
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
164162
) -> None:
165163
"""Test path resolution with a nested directory structure."""
166164
subdir = tmp_path / 'src' / 'app'
@@ -188,7 +186,7 @@ class TestRestoreNpmDependenciesEdgeCases:
188186
"""Test edge cases and error scenarios."""
189187

190188
def test_empty_lockfile_should_still_be_used(
191-
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
189+
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
192190
) -> None:
193191
"""Test that the empty lockfile is still used (prevents npm install)."""
194192
package_json_path = tmp_path / 'package.json'
@@ -210,7 +208,7 @@ def test_empty_lockfile_should_still_be_used(
210208
assert result.content == ''
211209

212210
def test_multiple_lockfiles_should_use_first_found(
213-
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
211+
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
214212
) -> None:
215213
"""Test that when multiple lockfiles exist, the first one found is used (package-lock.json has priority)."""
216214
package_json_path = tmp_path / 'package.json'
@@ -237,7 +235,7 @@ def test_multiple_lockfiles_should_use_first_found(
237235
assert result.content == '{"lockfileVersion": 2}\n'
238236

239237
def test_multiple_alternative_lockfiles_should_use_first_found(
240-
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
238+
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
241239
) -> None:
242240
"""Test that when multiple alternative lockfiles exist (but no package-lock.json),
243241
the first one found is used."""
@@ -263,7 +261,7 @@ def test_multiple_alternative_lockfiles_should_use_first_found(
263261
assert result.content == '# yarn lockfile\n'
264262

265263
def test_lockfile_in_different_directory_should_not_be_found(
266-
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
264+
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
267265
) -> None:
268266
"""Test that lockfile in a different directory is not found."""
269267
package_json_path = tmp_path / 'package.json'
@@ -282,17 +280,17 @@ def test_lockfile_in_different_directory_should_not_be_found(
282280

283281
# Mock the base class to verify it's called (since lockfile not found)
284282
with patch.object(
285-
restore_npm_dependencies.__class__.__bases__[0],
286-
'try_restore_dependencies',
287-
return_value=None,
283+
restore_npm_dependencies.__class__.__bases__[0],
284+
'try_restore_dependencies',
285+
return_value=None,
288286
) as mock_super:
289287
restore_npm_dependencies.try_restore_dependencies(document)
290288

291289
# Should proceed with normal flow since lockfile not in same directory
292290
mock_super.assert_called_once_with(document)
293291

294292
def test_non_json_file_should_not_trigger_restore(
295-
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
293+
self, restore_npm_dependencies: RestoreNpmDependencies, tmp_path: Path
296294
) -> None:
297295
"""Test that non-JSON files don't trigger restore."""
298296
text_file = tmp_path / 'readme.txt'
@@ -340,9 +338,7 @@ def test_get_lock_file_names(self, restore_npm_dependencies: RestoreNpmDependenc
340338
for alt_lock in ALTERNATIVE_LOCK_FILES:
341339
assert alt_lock in lock_file_names
342340

343-
def test_prepare_manifest_file_path_for_command(
344-
self, restore_npm_dependencies: RestoreNpmDependencies
345-
) -> None:
341+
def test_prepare_manifest_file_path_for_command(self, restore_npm_dependencies: RestoreNpmDependencies) -> None:
346342
"""Test prepare_manifest_file_path_for_command removes package.json from the path."""
347343
result = restore_npm_dependencies.prepare_manifest_file_path_for_command('/path/to/package.json')
348344
assert result == '/path/to'

0 commit comments

Comments
 (0)