Repository navigation
Expand file tree
/
Copy pathtest_include_dirs.py
More file actions
149 lines (119 loc) · 5.94 KB
/
Copy pathtest_include_dirs.py
File metadata and controls
149 lines (119 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""Test directory selection for manifest discovery."""
import sys
import types
from contextlib import nullcontext
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from socketsecurity import socketcli
from socketsecurity.config import CliConfig
from socketsecurity.core import Core
from socketsecurity.core.socket_config import (
SocketConfig,
default_exclude_dirs,
module_folder_dirs,
)
BASE_ARGS = ["--api-token", "test-token", "--repo", "test-repo"]
# ---- config parsing ------------------------------------------------------
def test_include_dirs_parses_to_list():
config = CliConfig.from_args(BASE_ARGS + ["--include-dirs", "build, dist , vendor"])
assert config.included_dirs == ["build", "dist", "vendor"]
def test_include_dirs_defaults_empty():
config = CliConfig.from_args(BASE_ARGS)
assert config.included_dirs == []
def test_include_dirs_from_config_file(tmp_path):
import json
cfg = tmp_path / "socketcli.json"
cfg.write_text(json.dumps({"socketcli": {"include_dirs": ["build", "dist"]}}), encoding="utf-8")
config = CliConfig.from_args(BASE_ARGS + ["--config", str(cfg)])
assert config.included_dirs == ["build", "dist"]
def test_module_folder_dirs_is_subset_of_defaults():
assert module_folder_dirs <= default_exclude_dirs
# ---- find_files integration ----------------------------------------------
def _make_core(excluded_dirs):
core = Core.__new__(Core)
core.config = SocketConfig(api_key="test-key", excluded_dirs=excluded_dirs)
core.cli_config = types.SimpleNamespace(exclude_paths=None)
core.sdk = MagicMock()
return core
def _seed_manifests(tmp_path):
for rel in ("requirements.txt", "build/requirements.txt", "dist/requirements.txt"):
p = tmp_path / rel
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text("flask==1.0\n", encoding="utf-8")
def test_find_files_excludes_build_by_default(tmp_path, mocker):
_seed_manifests(tmp_path)
core = _make_core(set(default_exclude_dirs))
mocker.patch.object(
core, "get_supported_patterns",
return_value={"pypi": {"requirements.txt": {"pattern": "requirements.txt"}}},
)
found = core.find_files(str(tmp_path))
assert not any("/build/" in f for f in found)
assert not any("/dist/" in f for f in found)
assert any(f.endswith("/requirements.txt") for f in found)
def test_find_files_includes_build_when_unexcluded(tmp_path, mocker):
"""Mirrors socketcli wiring: dropping a name from excluded_dirs re-includes its manifests."""
_seed_manifests(tmp_path)
core = _make_core(set(default_exclude_dirs) - {"build"})
mocker.patch.object(
core, "get_supported_patterns",
return_value={"pypi": {"requirements.txt": {"pattern": "requirements.txt"}}},
)
found = core.find_files(str(tmp_path))
assert any("/build/requirements.txt" in f for f in found)
# dist is still excluded since only build was re-included
assert not any("/dist/" in f for f in found)
def test_unexcluding_does_not_mutate_shared_defaults():
"""The socketcli flow builds a new set rather than mutating the module-level default."""
before = set(default_exclude_dirs)
config = SocketConfig(api_key="test-key")
config.excluded_dirs = set(config.excluded_dirs) - {"build"}
assert "build" not in config.excluded_dirs
assert default_exclude_dirs == before
assert "build" in default_exclude_dirs
@pytest.mark.parametrize(("flags", "include_node", "include_other"), [
pytest.param([], False, False, id="default"),
pytest.param(["--include-module-folders"], True, True, id="module-folders"),
pytest.param(["--include-dirs", "node_modules,bower_components,jspm_packages"], True, True, id="include-dirs"),
pytest.param(["--include-module-folders", "--exclude-paths", "node_modules"], False, True, id="exclude-paths"),
])
def test_cli_manifest_selection(tmp_path, monkeypatch, mocker, flags, include_node, include_other):
root_files = {"package-lock.json", "installed.cdx.json"}
node_files = {
"node_modules/@scope/parent/package.json",
"node_modules/@scope/parent/node_modules/child/yarn.lock",
"node_modules/@scope/parent/requirements.txt",
}
other_modules = {"bower_components/pkg/package.json", "nested/jspm_packages/pkg/package.json"}
excluded_files = {"node_modules/.package-lock.json", "vendor/requirements.txt", ".venv/requirements.txt"}
for name in root_files | node_files | other_modules | excluded_files:
manifest = tmp_path / name
manifest.parent.mkdir(parents=True, exist_ok=True)
manifest.touch()
sdk = MagicMock()
sdk.org.get.return_value = {"organizations": {"org-id": {"slug": "test-org", "plan": "enterprise"}}}
sdk.report.supported.return_value = {
"npm": {name: {"pattern": name} for name in ("package.json", "package-lock.json", "yarn.lock")},
"pypi": {"requirements": {"pattern": "requirements.txt"}},
"cdx": {"json": {"pattern": "{bom,c{yclone,}dx[-.]*,*[-.]c{yclone,}dx}.json"}},
}
mocker.patch.object(socketcli, "build_socket_sdk", return_value=sdk)
mocker.patch.object(socketcli, "CliClient")
mocker.patch.object(socketcli, "StreamingLogs", return_value=nullcontext(MagicMock()))
mocker.patch.object(socketcli, "apply_git_context", return_value=(False, None))
upload = mocker.patch.object(Core, "create_full_scan", return_value=types.SimpleNamespace(id="scan"))
monkeypatch.setattr(sys, "argv", [
"socketcli", *BASE_ARGS, "--branch", "main", "--target-path", str(tmp_path), *flags,
])
with pytest.raises(SystemExit) as exit_status:
socketcli.cli()
assert exit_status.value.code == 0
upload.assert_called_once()
submitted = {Path(path).relative_to(tmp_path).as_posix() for path in upload.call_args.args[0]}
expected = root_files.copy()
if include_other:
expected |= other_modules
if include_node:
expected |= node_files
assert submitted == expected