Skip to content

Commit 9425dfe

Browse files
committed
chore: modify git_url tests to interact directly with the global defaults config object
Signed-off-by: Nathan Nguyen <[email protected]>
1 parent 407eebb commit 9425dfe

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

tests/slsa_analyzer/test_git_url.py

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import configparser
77
import os
88
from pathlib import Path
9-
from unittest.mock import MagicMock, patch
109

1110
import pytest
1211

12+
from macaron.config.defaults import defaults, load_defaults
1313
from macaron.slsa_analyzer import git_url
1414

1515

@@ -154,33 +154,19 @@ def test_get_allowed_git_service_domains(
154154

155155

156156
@pytest.mark.parametrize(
157-
("default_config_input", "override_config_input", "expected_allowed_domain_set"),
157+
("user_config_input", "expected_allowed_domain_set"),
158158
[
159159
pytest.param(
160160
# The current behavior is: we always enable GitHub and public GitLab by default.
161161
# User config cannot disable either of the two.
162162
"""
163163
[git_service.github]
164164
domain = github.com
165-
166-
[git_service.gitlab.public]
167-
domain = gitlab.com
168-
""",
169-
"""
170-
[git_service.github]
171-
domain = github.com
172165
""",
173166
{"github.com", "gitlab.com"},
174167
id="Only GitHub in user config",
175168
),
176169
pytest.param(
177-
"""
178-
[git_service.github]
179-
domain = github.com
180-
181-
[git_service.gitlab.public]
182-
domain = gitlab.com
183-
""",
184170
"""
185171
[git_service.gitlab.private]
186172
domain = internal.gitlab.org
@@ -191,37 +177,41 @@ def test_get_allowed_git_service_domains(
191177
],
192178
)
193179
def test_get_allowed_git_service_domains_with_override(
194-
default_config_input: str,
195-
override_config_input: str,
180+
user_config_input: str,
196181
expected_allowed_domain_set: set[str],
197182
tmp_path: Path,
198183
) -> None:
199184
"""Test the get allowed git service domains function, in multi-config files scenario."""
200-
default_filepath = tmp_path / "default.ini"
201-
override_filepath = tmp_path / "override.ini"
202-
with open(default_filepath, "w", encoding="utf-8") as default_file:
203-
default_file.write(default_config_input)
204-
with open(override_filepath, "w", encoding="utf-8") as override_file:
205-
override_file.write(override_config_input)
185+
user_config_path = os.path.join(tmp_path, "config.ini")
186+
with open(user_config_path, "w", encoding="utf-8") as user_config_file:
187+
user_config_file.write(user_config_input)
188+
# We don't have to worry about modifying the ``defaults`` object causing test
189+
# pollution here, since we reload the ``defaults`` object before every test with the
190+
# ``setup_test`` fixture.
191+
load_defaults(user_config_path)
206192

207-
config = configparser.ConfigParser()
208-
config.read([default_filepath, override_filepath])
193+
assert set(git_url.get_allowed_git_service_domains(defaults)) == expected_allowed_domain_set
209194

210-
assert set(git_url.get_allowed_git_service_domains(config)) == expected_allowed_domain_set
211195

196+
def test_get_remote_vcs_url_with_user_defined_allowed_domains(tmp_path: Path) -> None:
197+
"""Test the vcs URL validator method with user-defined allowed domains."""
198+
url = "https://internal.gitlab.org/org/name"
199+
assert git_url.get_remote_vcs_url(url) == ""
212200

213-
@patch("macaron.slsa_analyzer.git_url.get_allowed_git_service_domains")
214-
def test_get_remote_vcs_url_with_invalid_allowed_domains(
215-
get_allowed_domains_fn: MagicMock,
216-
) -> None:
217-
"""Test the vcs URL validator method without any allowed git hosts."""
218-
get_allowed_domains_fn.return_value = []
219-
assert git_url.get_remote_vcs_url("https://github.com/org/name.git") == ""
220-
assert git_url.get_remote_vcs_url("https://gitlab.com/org") == ""
221-
222-
get_allowed_domains_fn.return_value = ["invalid host"]
223-
assert git_url.get_remote_vcs_url("https://github.com/org/name.git") == ""
224-
assert git_url.get_remote_vcs_url("https://gitlab.com/org") == ""
201+
user_config_path = os.path.join(tmp_path, "config.ini")
202+
with open(user_config_path, "w", encoding="utf-8") as user_config_file:
203+
user_config_file.write(
204+
"""
205+
[git_service.gitlab.private]
206+
domain = internal.gitlab.org
207+
"""
208+
)
209+
# We don't have to worry about modifying the ``defaults`` object causing test
210+
# pollution here, since we reload the ``defaults`` object before every test with the
211+
# ``setup_test`` fixture.
212+
load_defaults(user_config_path)
213+
214+
assert git_url.get_remote_vcs_url(url) == url
225215

226216

227217
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)