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
This commit is contained in:
Jon Dufresne
2020-11-27 14:10:36 -08:00
parent e0704fb32e
commit c1437d5f79
2 changed files with 4 additions and 19 deletions

View File

@@ -24,7 +24,7 @@ import requests
from sphinx.builders.linkcheck import CheckExternalLinksBuilder, RateLimit from sphinx.builders.linkcheck import CheckExternalLinksBuilder, RateLimit
from sphinx.util.console import strip_colors 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<ts>.*)\].*") ts_re = re.compile(r".*\[(?P<ts>.*)\].*")
@@ -361,8 +361,9 @@ def test_connect_to_selfsigned_with_tls_cacerts(app):
@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-https', freshenv=True) @pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-https', freshenv=True)
def test_connect_to_selfsigned_with_requests_env_var(app): def test_connect_to_selfsigned_with_requests_env_var(monkeypatch, app):
with modify_env(REQUESTS_CA_BUNDLE=CERT_FILE), https_server(OKHandler): monkeypatch.setenv("REQUESTS_CA_BUNDLE", CERT_FILE)
with https_server(OKHandler):
app.builder.build_all() app.builder.build_all()
with open(app.outdir / 'output.json') as fp: with open(app.outdir / 'output.json') as fp:

View File

@@ -1,6 +1,5 @@
import contextlib import contextlib
import http.server import http.server
import os
import pathlib import pathlib
import ssl import ssl
import threading import threading
@@ -48,18 +47,3 @@ def create_server(thread_class):
http_server = create_server(HttpServerThread) http_server = create_server(HttpServerThread)
https_server = create_server(HttpsServerThread) 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]