|
| 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 | +""" |
| 6 | +import types |
| 7 | +from unittest.mock import MagicMock |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from socketsecurity.config import CliConfig |
| 12 | +from socketsecurity.core import Core |
| 13 | +from socketsecurity.core.socket_config import ( |
| 14 | + SocketConfig, |
| 15 | + default_exclude_dirs, |
| 16 | + module_folder_dirs, |
| 17 | +) |
| 18 | + |
| 19 | +BASE_ARGS = ["--api-token", "test-token", "--repo", "test-repo"] |
| 20 | + |
| 21 | + |
| 22 | +# ---- config parsing ------------------------------------------------------ |
| 23 | + |
| 24 | +def test_include_dirs_parses_to_list(): |
| 25 | + config = CliConfig.from_args(BASE_ARGS + ["--include-dirs", "build, dist , vendor"]) |
| 26 | + assert config.included_dirs == ["build", "dist", "vendor"] |
| 27 | + |
| 28 | + |
| 29 | +def test_include_dirs_defaults_empty(): |
| 30 | + config = CliConfig.from_args(BASE_ARGS) |
| 31 | + assert config.included_dirs == [] |
| 32 | + |
| 33 | + |
| 34 | +def test_include_dirs_from_config_file(tmp_path): |
| 35 | + import json |
| 36 | + cfg = tmp_path / "socketcli.json" |
| 37 | + cfg.write_text(json.dumps({"socketcli": {"include_dirs": ["build", "dist"]}}), encoding="utf-8") |
| 38 | + config = CliConfig.from_args(BASE_ARGS + ["--config", str(cfg)]) |
| 39 | + assert config.included_dirs == ["build", "dist"] |
| 40 | + |
| 41 | + |
| 42 | +def test_module_folder_dirs_is_subset_of_defaults(): |
| 43 | + assert module_folder_dirs <= default_exclude_dirs |
| 44 | + |
| 45 | + |
| 46 | +# ---- find_files integration ---------------------------------------------- |
| 47 | + |
| 48 | +def _make_core(excluded_dirs): |
| 49 | + core = Core.__new__(Core) |
| 50 | + core.config = SocketConfig(api_key="test-key", excluded_dirs=excluded_dirs) |
| 51 | + core.cli_config = types.SimpleNamespace(exclude_paths=None) |
| 52 | + core.sdk = MagicMock() |
| 53 | + return core |
| 54 | + |
| 55 | + |
| 56 | +def _seed_manifests(tmp_path): |
| 57 | + for rel in ("requirements.txt", "build/requirements.txt", "dist/requirements.txt"): |
| 58 | + p = tmp_path / rel |
| 59 | + p.parent.mkdir(parents=True, exist_ok=True) |
| 60 | + p.write_text("flask==1.0\n", encoding="utf-8") |
| 61 | + |
| 62 | + |
| 63 | +def test_find_files_excludes_build_by_default(tmp_path, mocker): |
| 64 | + _seed_manifests(tmp_path) |
| 65 | + core = _make_core(set(default_exclude_dirs)) |
| 66 | + mocker.patch.object( |
| 67 | + core, "get_supported_patterns", |
| 68 | + return_value={"pypi": {"requirements.txt": {"pattern": "requirements.txt"}}}, |
| 69 | + ) |
| 70 | + |
| 71 | + found = core.find_files(str(tmp_path)) |
| 72 | + assert not any("/build/" in f for f in found) |
| 73 | + assert not any("/dist/" in f for f in found) |
| 74 | + assert any(f.endswith("/requirements.txt") for f in found) |
| 75 | + |
| 76 | + |
| 77 | +def test_find_files_includes_build_when_unexcluded(tmp_path, mocker): |
| 78 | + """Mirrors socketcli wiring: dropping a name from excluded_dirs re-includes its manifests.""" |
| 79 | + _seed_manifests(tmp_path) |
| 80 | + core = _make_core(set(default_exclude_dirs) - {"build"}) |
| 81 | + mocker.patch.object( |
| 82 | + core, "get_supported_patterns", |
| 83 | + return_value={"pypi": {"requirements.txt": {"pattern": "requirements.txt"}}}, |
| 84 | + ) |
| 85 | + |
| 86 | + found = core.find_files(str(tmp_path)) |
| 87 | + assert any("/build/requirements.txt" in f for f in found) |
| 88 | + # dist is still excluded since only build was re-included |
| 89 | + assert not any("/dist/" in f for f in found) |
| 90 | + |
| 91 | + |
| 92 | +def test_unexcluding_does_not_mutate_shared_defaults(): |
| 93 | + """The socketcli flow builds a new set rather than mutating the module-level default.""" |
| 94 | + before = set(default_exclude_dirs) |
| 95 | + config = SocketConfig(api_key="test-key") |
| 96 | + config.excluded_dirs = set(config.excluded_dirs) - {"build"} |
| 97 | + assert "build" not in config.excluded_dirs |
| 98 | + assert default_exclude_dirs == before |
| 99 | + assert "build" in default_exclude_dirs |
0 commit comments