Skip to content

test: always ensure clean config environment #2187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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