|
1 | | -"""Tests for --include-dirs (and the now-functional --include-module-folders). |
2 | | -
|
3 | | -Covers config parsing of the comma-separated directory names and that re-including a |
4 | | -normally-excluded directory (e.g. build) lets Core.find_files discover manifests under it. |
5 | | -""" |
| 1 | +"""Test directory selection for manifest discovery.""" |
| 2 | +import sys |
6 | 3 | import types |
| 4 | +from contextlib import nullcontext |
| 5 | +from pathlib import Path |
7 | 6 | from unittest.mock import MagicMock |
8 | 7 |
|
| 8 | +import pytest |
| 9 | + |
| 10 | +from socketsecurity import socketcli |
9 | 11 | from socketsecurity.config import CliConfig |
10 | 12 | from socketsecurity.core import Core |
11 | 13 | from socketsecurity.core.socket_config import ( |
@@ -95,3 +97,53 @@ def test_unexcluding_does_not_mutate_shared_defaults(): |
95 | 97 | assert "build" not in config.excluded_dirs |
96 | 98 | assert default_exclude_dirs == before |
97 | 99 | assert "build" in default_exclude_dirs |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize(("flags", "include_node", "include_other"), [ |
| 103 | + pytest.param([], False, False, id="default"), |
| 104 | + pytest.param(["--include-module-folders"], True, True, id="module-folders"), |
| 105 | + pytest.param(["--include-dirs", "node_modules,bower_components,jspm_packages"], True, True, id="include-dirs"), |
| 106 | + pytest.param(["--include-module-folders", "--exclude-paths", "node_modules"], False, True, id="exclude-paths"), |
| 107 | +]) |
| 108 | +def test_cli_manifest_selection(tmp_path, monkeypatch, mocker, flags, include_node, include_other): |
| 109 | + root_files = {"package-lock.json", "installed.cdx.json"} |
| 110 | + node_files = { |
| 111 | + "node_modules/@scope/parent/package.json", |
| 112 | + "node_modules/@scope/parent/node_modules/child/yarn.lock", |
| 113 | + "node_modules/@scope/parent/requirements.txt", |
| 114 | + } |
| 115 | + other_modules = {"bower_components/pkg/package.json", "nested/jspm_packages/pkg/package.json"} |
| 116 | + excluded_files = {"node_modules/.package-lock.json", "vendor/requirements.txt", ".venv/requirements.txt"} |
| 117 | + for name in root_files | node_files | other_modules | excluded_files: |
| 118 | + manifest = tmp_path / name |
| 119 | + manifest.parent.mkdir(parents=True, exist_ok=True) |
| 120 | + manifest.touch() |
| 121 | + |
| 122 | + sdk = MagicMock() |
| 123 | + sdk.org.get.return_value = {"organizations": {"org-id": {"slug": "test-org", "plan": "enterprise"}}} |
| 124 | + sdk.report.supported.return_value = { |
| 125 | + "npm": {name: {"pattern": name} for name in ("package.json", "package-lock.json", "yarn.lock")}, |
| 126 | + "pypi": {"requirements": {"pattern": "requirements.txt"}}, |
| 127 | + "cdx": {"json": {"pattern": "{bom,c{yclone,}dx[-.]*,*[-.]c{yclone,}dx}.json"}}, |
| 128 | + } |
| 129 | + mocker.patch.object(socketcli, "build_socket_sdk", return_value=sdk) |
| 130 | + mocker.patch.object(socketcli, "CliClient") |
| 131 | + mocker.patch.object(socketcli, "StreamingLogs", return_value=nullcontext(MagicMock())) |
| 132 | + mocker.patch.object(socketcli, "apply_git_context", return_value=(False, None)) |
| 133 | + upload = mocker.patch.object(Core, "create_full_scan", return_value=types.SimpleNamespace(id="scan")) |
| 134 | + monkeypatch.setattr(sys, "argv", [ |
| 135 | + "socketcli", *BASE_ARGS, "--branch", "main", "--target-path", str(tmp_path), *flags, |
| 136 | + ]) |
| 137 | + |
| 138 | + with pytest.raises(SystemExit) as exit_status: |
| 139 | + socketcli.cli() |
| 140 | + |
| 141 | + assert exit_status.value.code == 0 |
| 142 | + upload.assert_called_once() |
| 143 | + submitted = {Path(path).relative_to(tmp_path).as_posix() for path in upload.call_args.args[0]} |
| 144 | + expected = root_files.copy() |
| 145 | + if include_other: |
| 146 | + expected |= other_modules |
| 147 | + if include_node: |
| 148 | + expected |= node_files |
| 149 | + assert submitted == expected |
0 commit comments