-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_include_dirs.py
More file actions
99 lines (73 loc) · 3.5 KB
/
Copy pathtest_include_dirs.py
File metadata and controls
99 lines (73 loc) · 3.5 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
"""Tests for --include-dirs (and the now-functional --include-module-folders).
Covers config parsing of the comma-separated directory names and that re-including a
normally-excluded directory (e.g. build) lets Core.find_files discover manifests under it.
"""
import types
from unittest.mock import MagicMock
import pytest
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