Skip to content

Commit 44cd7f0

Browse files
committed
Add coverage for new SARIF scoping, config file behavior
Signed-off-by: lelia <lelia@socket.dev>
1 parent e35491b commit 44cd7f0

File tree

2 files changed

+501
-5
lines changed

2 files changed

+501
-5
lines changed

tests/unit/test_config.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,79 @@ def test_sarif_file_implies_enable_sarif(self):
9191
config = CliConfig.from_args(self.BASE_ARGS + ["--sarif-file", "out.sarif"])
9292
assert config.enable_sarif is True
9393
assert config.sarif_file == "out.sarif"
94+
95+
def test_sarif_scope_full_without_reach_exits(self):
96+
"""--sarif-scope full without --reach should exit with code 1"""
97+
with pytest.raises(SystemExit) as exc_info:
98+
CliConfig.from_args(self.BASE_ARGS + ["--sarif-scope", "full"])
99+
assert exc_info.value.code == 1
100+
101+
def test_sarif_scope_full_with_reach_succeeds(self):
102+
"""--sarif-scope full with --reach should parse successfully"""
103+
config = CliConfig.from_args(self.BASE_ARGS + ["--sarif-scope", "full", "--reach"])
104+
assert config.sarif_scope == "full"
105+
assert config.reach is True
106+
107+
def test_sarif_reachability_without_reach_exits(self):
108+
with pytest.raises(SystemExit) as exc_info:
109+
CliConfig.from_args(self.BASE_ARGS + ["--sarif-reachability", "reachable"])
110+
assert exc_info.value.code == 1
111+
112+
def test_sarif_reachability_with_reach_succeeds(self):
113+
config = CliConfig.from_args(
114+
self.BASE_ARGS + ["--reach", "--sarif-scope", "full", "--sarif-reachability", "potentially"]
115+
)
116+
assert config.sarif_reachability == "potentially"
117+
assert config.reach is True
118+
119+
def test_sarif_grouping_alert_requires_full_scope(self):
120+
with pytest.raises(SystemExit) as exc_info:
121+
CliConfig.from_args(self.BASE_ARGS + ["--reach", "--sarif-grouping", "alert"])
122+
assert exc_info.value.code == 1
123+
124+
def test_legacy_sarif_reachable_only_maps_to_reachability(self):
125+
config = CliConfig.from_args(self.BASE_ARGS + ["--reach", "--sarif-reachable-only"])
126+
assert config.sarif_reachability == "reachable"
127+
128+
def test_config_file_toml_sets_defaults(self, tmp_path):
129+
config_path = tmp_path / "socketcli.toml"
130+
config_path.write_text(
131+
"[socketcli]\n"
132+
"reach = true\n"
133+
"sarif_scope = \"full\"\n"
134+
"sarif_grouping = \"alert\"\n"
135+
"sarif_reachability = \"reachable\"\n",
136+
encoding="utf-8",
137+
)
138+
139+
config = CliConfig.from_args(self.BASE_ARGS + ["--config", str(config_path)])
140+
assert config.reach is True
141+
assert config.sarif_scope == "full"
142+
assert config.sarif_grouping == "alert"
143+
assert config.sarif_reachability == "reachable"
144+
145+
def test_cli_flag_overrides_config_file(self, tmp_path):
146+
config_path = tmp_path / "socketcli.toml"
147+
config_path.write_text(
148+
"[socketcli]\n"
149+
"reach = true\n"
150+
"sarif_scope = \"full\"\n",
151+
encoding="utf-8",
152+
)
153+
154+
config = CliConfig.from_args(
155+
self.BASE_ARGS + ["--config", str(config_path), "--sarif-scope", "diff"]
156+
)
157+
assert config.reach is True
158+
assert config.sarif_scope == "diff"
159+
160+
def test_config_file_json_sets_defaults(self, tmp_path):
161+
config_path = tmp_path / "socketcli.json"
162+
config_path.write_text(
163+
"{\"socketcli\": {\"reach\": true, \"sarif_scope\": \"full\", \"sarif_grouping\": \"alert\"}}",
164+
encoding="utf-8",
165+
)
166+
config = CliConfig.from_args(self.BASE_ARGS + ["--config", str(config_path)])
167+
assert config.reach is True
168+
assert config.sarif_scope == "full"
169+
assert config.sarif_grouping == "alert"

0 commit comments

Comments
 (0)