6
6
import configparser
7
7
import os
8
8
from pathlib import Path
9
- from unittest .mock import MagicMock , patch
10
9
11
10
import pytest
12
11
12
+ from macaron .config .defaults import defaults , load_defaults
13
13
from macaron .slsa_analyzer import git_url
14
14
15
15
@@ -154,33 +154,19 @@ def test_get_allowed_git_service_domains(
154
154
155
155
156
156
@pytest .mark .parametrize (
157
- ("default_config_input" , "override_config_input " , "expected_allowed_domain_set" ),
157
+ ("user_config_input " , "expected_allowed_domain_set" ),
158
158
[
159
159
pytest .param (
160
160
# The current behavior is: we always enable GitHub and public GitLab by default.
161
161
# User config cannot disable either of the two.
162
162
"""
163
163
[git_service.github]
164
164
domain = github.com
165
-
166
- [git_service.gitlab.public]
167
- domain = gitlab.com
168
- """ ,
169
- """
170
- [git_service.github]
171
- domain = github.com
172
165
""" ,
173
166
{"github.com" , "gitlab.com" },
174
167
id = "Only GitHub in user config" ,
175
168
),
176
169
pytest .param (
177
- """
178
- [git_service.github]
179
- domain = github.com
180
-
181
- [git_service.gitlab.public]
182
- domain = gitlab.com
183
- """ ,
184
170
"""
185
171
[git_service.gitlab.private]
186
172
domain = internal.gitlab.org
@@ -191,37 +177,41 @@ def test_get_allowed_git_service_domains(
191
177
],
192
178
)
193
179
def test_get_allowed_git_service_domains_with_override (
194
- default_config_input : str ,
195
- override_config_input : str ,
180
+ user_config_input : str ,
196
181
expected_allowed_domain_set : set [str ],
197
182
tmp_path : Path ,
198
183
) -> None :
199
184
"""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 )
206
192
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
209
194
210
- assert set (git_url .get_allowed_git_service_domains (config )) == expected_allowed_domain_set
211
195
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 ) == ""
212
200
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
225
215
226
216
227
217
@pytest .mark .parametrize (
0 commit comments