Skip to content

Commit

Permalink
Fix tests when HOSTNAME is set, but empty string (#1618)
Browse files Browse the repository at this point in the history
Use a pytest provided fixture instead of implementing our own.

See #1616 (comment)

This is a fixup of cfe66fd.
  • Loading branch information
hroncok authored Jul 15, 2020
1 parent 03bc093 commit 7a65627
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/changelog/1616.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix tests when the ``HOSTNAME`` environment variable is set, but empty string - by :user:`hroncok`
29 changes: 10 additions & 19 deletions tests/unit/test_result.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import os
import signal
import socket
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"

Expand Down

0 comments on commit 7a65627

Please sign in to comment.