Skip to content

Commit

Permalink
test: always ensure clean config environment
Browse files Browse the repository at this point in the history
  • Loading branch information
nejch authored and JohnVillalovos committed Jul 26, 2022
1 parent 4b798fc commit 8d4f13b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
18 changes: 18 additions & 0 deletions 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 """---
Expand Down
6 changes: 1 addition & 5 deletions tests/functional/cli/test_cli_variables.py
Expand Up @@ -3,7 +3,6 @@
import pytest
import responses

from gitlab import config
from gitlab.const import DEFAULT_URL


Expand Down Expand Up @@ -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"
Expand Down
17 changes: 9 additions & 8 deletions tests/unit/test_config.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions tests/unit/test_gitlab.py
Expand Up @@ -20,7 +20,6 @@
import logging
import pickle
from http.client import HTTPConnection
from typing import List, Optional, Union

import pytest
import responses
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 8d4f13b

Please sign in to comment.