From 8d4f13b192afd5d4610eeaf2bbea71c3b6a25964 Mon Sep 17 00:00:00 2001 From: Nejc Habjan Date: Sun, 24 Jul 2022 22:53:01 +0200 Subject: [PATCH] test: always ensure clean config environment --- tests/conftest.py | 18 ++++++++++++++++++ tests/functional/cli/test_cli_variables.py | 6 +----- tests/unit/test_config.py | 17 +++++++++-------- tests/unit/test_gitlab.py | 7 +------ 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index fdcafee7a..06e99f6e9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,11 +1,29 @@ import pytest +import gitlab + @pytest.fixture(scope="session") def test_dir(pytestconfig): return pytestconfig.rootdir / "tests" +@pytest.fixture(autouse=True) +def mock_clean_config(monkeypatch): + """Ensures user-defined environment variables do not interfere with tests.""" + monkeypatch.delenv("PYTHON_GITLAB_CFG", raising=False) + monkeypatch.delenv("GITLAB_PRIVATE_TOKEN", raising=False) + monkeypatch.delenv("GITLAB_URL", raising=False) + monkeypatch.delenv("CI_JOB_TOKEN", raising=False) + monkeypatch.delenv("CI_SERVER_URL", raising=False) + + +@pytest.fixture(autouse=True) +def default_files(monkeypatch): + """Ensures user configuration files do not interfere with tests.""" + monkeypatch.setattr(gitlab.config, "_DEFAULT_FILES", []) + + @pytest.fixture def valid_gitlab_ci_yml(): return """--- diff --git a/tests/functional/cli/test_cli_variables.py b/tests/functional/cli/test_cli_variables.py index 5195f16ff..3eb8e031e 100644 --- a/tests/functional/cli/test_cli_variables.py +++ b/tests/functional/cli/test_cli_variables.py @@ -3,7 +3,6 @@ import pytest import responses -from gitlab import config from gitlab.const import DEFAULT_URL @@ -37,10 +36,7 @@ def test_list_project_variables_with_path(gitlab_cli, project): @pytest.mark.script_launch_mode("inprocess") @responses.activate -def test_list_project_variables_with_path_url_check( - monkeypatch, script_runner, resp_get_project -): - monkeypatch.setattr(config, "_DEFAULT_FILES", []) +def test_list_project_variables_with_path_url_check(script_runner, resp_get_project): resp_get_project_variables = copy.deepcopy(resp_get_project) resp_get_project_variables.update( url=f"{DEFAULT_URL}/api/v4/projects/project%2Fwith%2Fa%2Fnamespace/variables" diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 4a96bf8bd..c4fcbef61 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -100,6 +100,12 @@ """ +@pytest.fixture(autouse=True) +def default_files(monkeypatch): + """Overrides mocked default files from conftest.py as we have our own mocks here.""" + monkeypatch.setattr(gitlab.config, "_DEFAULT_FILES", config._DEFAULT_FILES) + + def global_retry_transient_errors(value: bool) -> str: return f"""[global] default = one @@ -129,24 +135,19 @@ def _mock_existent_file(path, *args, **kwargs): return path -@pytest.fixture -def mock_clean_env(monkeypatch): - monkeypatch.delenv("PYTHON_GITLAB_CFG", raising=False) - - def test_env_config_missing_file_raises(monkeypatch): monkeypatch.setenv("PYTHON_GITLAB_CFG", "/some/path") with pytest.raises(config.GitlabConfigMissingError): config._get_config_files() -def test_env_config_not_defined_does_not_raise(mock_clean_env, monkeypatch): +def test_env_config_not_defined_does_not_raise(monkeypatch): with monkeypatch.context() as m: m.setattr(config, "_DEFAULT_FILES", []) assert config._get_config_files() == [] -def test_default_config(mock_clean_env, monkeypatch): +def test_default_config(monkeypatch): with monkeypatch.context() as m: m.setattr(Path, "resolve", _mock_nonexistent_file) cp = config.GitlabConfigParser() @@ -169,7 +170,7 @@ def test_default_config(mock_clean_env, monkeypatch): @mock.patch("builtins.open") -def test_invalid_id(m_open, mock_clean_env, monkeypatch): +def test_invalid_id(m_open, monkeypatch): fd = io.StringIO(no_default_config) fd.close = mock.Mock(return_value=None) m_open.return_value = fd diff --git a/tests/unit/test_gitlab.py b/tests/unit/test_gitlab.py index 8126cf4f1..203f123a3 100644 --- a/tests/unit/test_gitlab.py +++ b/tests/unit/test_gitlab.py @@ -20,7 +20,6 @@ import logging import pickle from http.client import HTTPConnection -from typing import List, Optional, Union import pytest import responses @@ -301,11 +300,7 @@ def test_gitlab_from_config(default_config): gitlab.Gitlab.from_config("one", [config_path]) -def test_gitlab_from_config_without_files_raises(monkeypatch): - def no_files(config_files: Optional[List[str]] = None) -> Union[str, List[str]]: - return [] - - monkeypatch.setattr(gitlab.config, "_get_config_files", no_files) +def test_gitlab_from_config_without_files_raises(): with pytest.raises(GitlabConfigMissingError, match="non-existing"): gitlab.Gitlab.from_config("non-existing")