From c1437d5f7981d1d807c9702b93eebd440466a279 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 27 Nov 2020 14:10:36 -0800 Subject: [PATCH] Replace modify_env() with pytest monkeypatch.setenv() The same test utility function is built into pytest. Can avoid the duplication. https://docs.pytest.org/en/latest/monkeypatch.html#monkeypatching-environment-variables --- tests/test_build_linkcheck.py | 7 ++++--- tests/utils.py | 16 ---------------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/tests/test_build_linkcheck.py b/tests/test_build_linkcheck.py index eae4b75db..e62276931 100644 --- a/tests/test_build_linkcheck.py +++ b/tests/test_build_linkcheck.py @@ -24,7 +24,7 @@ import requests from sphinx.builders.linkcheck import CheckExternalLinksBuilder, RateLimit from sphinx.util.console import strip_colors -from .utils import CERT_FILE, http_server, https_server, modify_env +from .utils import CERT_FILE, http_server, https_server ts_re = re.compile(r".*\[(?P.*)\].*") @@ -361,8 +361,9 @@ def test_connect_to_selfsigned_with_tls_cacerts(app): @pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-https', freshenv=True) -def test_connect_to_selfsigned_with_requests_env_var(app): - with modify_env(REQUESTS_CA_BUNDLE=CERT_FILE), https_server(OKHandler): +def test_connect_to_selfsigned_with_requests_env_var(monkeypatch, app): + monkeypatch.setenv("REQUESTS_CA_BUNDLE", CERT_FILE) + with https_server(OKHandler): app.builder.build_all() with open(app.outdir / 'output.json') as fp: diff --git a/tests/utils.py b/tests/utils.py index 329695c8e..9430c9beb 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,6 +1,5 @@ import contextlib import http.server -import os import pathlib import ssl import threading @@ -48,18 +47,3 @@ def create_server(thread_class): http_server = create_server(HttpServerThread) https_server = create_server(HttpsServerThread) - - -@contextlib.contextmanager -def modify_env(**env): - original_env = os.environ.copy() - for k, v in env.items(): - os.environ[k] = v - try: - yield - finally: - for k in env: - try: - os.environ[k] = original_env[k] - except KeyError: - del os.environ[k]