From 7a65627e93a1c407f2614f9119b67ca8d42b9d70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Wed, 15 Jul 2020 17:52:04 +0200 Subject: [PATCH] Fix tests when HOSTNAME is set, but empty string (#1618) Use a pytest provided fixture instead of implementing our own. See https://github.com/tox-dev/tox/pull/1616#issuecomment-658437312 This is a fixup of cfe66fd0fe8a81bd65f79f589486b7721632a4cb. --- docs/changelog/1616.bugfix.rst | 1 + tests/unit/test_result.py | 29 ++++++++++------------------- 2 files changed, 11 insertions(+), 19 deletions(-) create mode 100644 docs/changelog/1616.bugfix.rst diff --git a/docs/changelog/1616.bugfix.rst b/docs/changelog/1616.bugfix.rst new file mode 100644 index 0000000000..36d20aa15d --- /dev/null +++ b/docs/changelog/1616.bugfix.rst @@ -0,0 +1 @@ +Fix tests when the ``HOSTNAME`` environment variable is set, but empty string - by :user:`hroncok` diff --git a/tests/unit/test_result.py b/tests/unit/test_result.py index 4a926683ba..efbd3f9cbf 100644 --- a/tests/unit/test_result.py +++ b/tests/unit/test_result.py @@ -1,3 +1,4 @@ +import functools import os import signal import socket @@ -18,22 +19,12 @@ def create_fake_pkg(tmpdir): @pytest.fixture() -def no_hostname_envvar(): - hostname = os.getenv("HOSTNAME") - if hostname: - del os.environ["HOSTNAME"] - try: - yield - finally: - try: - del os.environ["HOSTNAME"] - except KeyError: - pass - if hostname: - os.environ["HOSTNAME"] = hostname - - -def test_pre_set_header(no_hostname_envvar): +def clean_hostname_envvar(monkeypatch): + monkeypatch.delenv("HOSTNAME", raising=False) + return functools.partial(monkeypatch.setenv, "HOSTNAME") + + +def test_pre_set_header(clean_hostname_envvar): replog = ResultLog() d = replog.dict assert replog.dict == d @@ -46,7 +37,7 @@ def test_pre_set_header(no_hostname_envvar): assert replog2.dict == replog.dict -def test_set_header(pkg, no_hostname_envvar): +def test_set_header(pkg, clean_hostname_envvar): replog = ResultLog() d = replog.dict assert replog.dict == d @@ -64,8 +55,8 @@ def test_set_header(pkg, no_hostname_envvar): assert replog2.dict == replog.dict -def test_hosname_via_envvar(no_hostname_envvar): - os.environ["HOSTNAME"] = "toxicity" +def test_hosname_via_envvar(clean_hostname_envvar): + clean_hostname_envvar("toxicity") replog = ResultLog() assert replog.dict["host"] == "toxicity"