Skip to content

Commit 7b5a282

Browse files
committed
test: always ensure clean config environment
1 parent 346cf76 commit 7b5a282

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

tests/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
import pytest
22

3+
import gitlab
4+
35

46
@pytest.fixture(scope="session")
57
def test_dir(pytestconfig):
68
return pytestconfig.rootdir / "tests"
79

810

11+
@pytest.fixture(autouse=True)
12+
def mock_clean_config(monkeypatch):
13+
"""Ensures user-defined environment variables do not interfere with tests."""
14+
monkeypatch.delenv("PYTHON_GITLAB_CFG", raising=False)
15+
monkeypatch.delenv("GITLAB_PRIVATE_TOKEN", raising=False)
16+
monkeypatch.delenv("GITLAB_URL", raising=False)
17+
monkeypatch.delenv("CI_JOB_TOKEN", raising=False)
18+
monkeypatch.delenv("CI_SERVER_URL", raising=False)
19+
20+
21+
@pytest.fixture(autouse=True)
22+
def default_files(monkeypatch):
23+
"""Ensures user configuration files do not interfere with tests."""
24+
monkeypatch.setattr(gitlab.config, "_DEFAULT_FILES", [])
25+
26+
927
@pytest.fixture
1028
def valid_gitlab_ci_yml():
1129
return """---

tests/functional/cli/test_cli_variables.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pytest
44
import responses
55

6-
from gitlab import config
76
from gitlab.const import DEFAULT_URL
87

98

@@ -37,10 +36,7 @@ def test_list_project_variables_with_path(gitlab_cli, project):
3736

3837
@pytest.mark.script_launch_mode("inprocess")
3938
@responses.activate
40-
def test_list_project_variables_with_path_url_check(
41-
monkeypatch, script_runner, resp_get_project
42-
):
43-
monkeypatch.setattr(config, "_DEFAULT_FILES", [])
39+
def test_list_project_variables_with_path_url_check(script_runner, resp_get_project):
4440
resp_get_project_variables = copy.deepcopy(resp_get_project)
4541
resp_get_project_variables.update(
4642
url=f"{DEFAULT_URL}/api/v4/projects/project%2Fwith%2Fa%2Fnamespace/variables"

tests/unit/test_config.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@
100100
"""
101101

102102

103+
@pytest.fixture(autouse=True)
104+
def default_files(monkeypatch):
105+
"""Overrides mocked default files from conftest.py as we have our own mocks here."""
106+
monkeypatch.setattr(gitlab.config, "_DEFAULT_FILES", config._DEFAULT_FILES)
107+
108+
103109
def global_retry_transient_errors(value: bool) -> str:
104110
return f"""[global]
105111
default = one
@@ -129,24 +135,19 @@ def _mock_existent_file(path, *args, **kwargs):
129135
return path
130136

131137

132-
@pytest.fixture
133-
def mock_clean_env(monkeypatch):
134-
monkeypatch.delenv("PYTHON_GITLAB_CFG", raising=False)
135-
136-
137138
def test_env_config_missing_file_raises(monkeypatch):
138139
monkeypatch.setenv("PYTHON_GITLAB_CFG", "/some/path")
139140
with pytest.raises(config.GitlabConfigMissingError):
140141
config._get_config_files()
141142

142143

143-
def test_env_config_not_defined_does_not_raise(mock_clean_env, monkeypatch):
144+
def test_env_config_not_defined_does_not_raise(monkeypatch):
144145
with monkeypatch.context() as m:
145146
m.setattr(config, "_DEFAULT_FILES", [])
146147
assert config._get_config_files() == []
147148

148149

149-
def test_default_config(mock_clean_env, monkeypatch):
150+
def test_default_config(monkeypatch):
150151
with monkeypatch.context() as m:
151152
m.setattr(Path, "resolve", _mock_nonexistent_file)
152153
cp = config.GitlabConfigParser()
@@ -169,7 +170,7 @@ def test_default_config(mock_clean_env, monkeypatch):
169170

170171

171172
@mock.patch("builtins.open")
172-
def test_invalid_id(m_open, mock_clean_env, monkeypatch):
173+
def test_invalid_id(m_open, monkeypatch):
173174
fd = io.StringIO(no_default_config)
174175
fd.close = mock.Mock(return_value=None)
175176
m_open.return_value = fd

tests/unit/test_gitlab.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import logging
2121
import pickle
2222
from http.client import HTTPConnection
23-
from typing import List, Optional, Union
2423

2524
import pytest
2625
import responses
@@ -301,11 +300,7 @@ def test_gitlab_from_config(default_config):
301300
gitlab.Gitlab.from_config("one", [config_path])
302301

303302

304-
def test_gitlab_from_config_without_files_raises(monkeypatch):
305-
def no_files(config_files: Optional[List[str]] = None) -> Union[str, List[str]]:
306-
return []
307-
308-
monkeypatch.setattr(gitlab.config, "_get_config_files", no_files)
303+
def test_gitlab_from_config_without_files_raises():
309304
with pytest.raises(GitlabConfigMissingError, match="non-existing"):
310305
gitlab.Gitlab.from_config("non-existing")
311306

0 commit comments

Comments
 (0)