Skip to content

Commit

Permalink
fix: tmpdir workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil committed Jan 7, 2024
1 parent b28818a commit 98fbf8b
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,54 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import pytest
from xml.etree import ElementTree as ET
from tempfile import NamedTemporaryFile

pytest_plugins = ("pytester",)


def test_metadata(testdir):
testdir.makepyfile(
def test_metadata(pytester):
pytester.makepyfile(
"""
def test_pass(metadata):
for k in ['Python', 'Platform', 'Packages']:
assert k in metadata
assert 'JENKINS_URL' not in metadata
"""
)
result = testdir.runpytest()
result = pytester.runpytest()
assert result.ret == 0


def test_environment_variables(testdir, monkeypatch):
def test_environment_variables(pytester, monkeypatch):
monkeypatch.setenv("JENKINS_URL", "foo")
monkeypatch.setenv("GIT_COMMIT", "bar")
testdir.makepyfile(
pytester.makepyfile(
"""
def test_pass(metadata):
assert metadata.get('JENKINS_URL') == 'foo'
assert metadata.get('GIT_COMMIT') == 'bar'
"""
)
result = testdir.runpytest()
result = pytester.runpytest()
assert result.ret == 0


def test_additional_metadata(testdir):
testdir.makepyfile(
def test_additional_metadata(pytester):
pytester.makepyfile(
"""
def test_pass(metadata):
assert metadata.get('Dave') == 'Hunt'
assert metadata.get('Jim') == 'Bob'
"""
)
result = testdir.runpytest("--metadata", "Dave", "Hunt", "--metadata", "Jim", "Bob")
result = pytester.runpytest(
"--metadata", "Dave", "Hunt", "--metadata", "Jim", "Bob"
)
assert result.ret == 0


@pytest.mark.parametrize("junit_format", ["xunit1", "xunit2"])
def test_junit_integration(testdir, junit_format):
testdir.makepyfile(
def test_junit_integration(pytester, junit_format):
pytester.makepyfile(
"""
import pytest
Expand All @@ -59,15 +60,15 @@ def test_pass():
pass
"""
)
result = testdir.runpytest(
result = pytester.runpytest(
"--metadata",
"Daffy",
"Duck",
"--junit-xml=results.xml",
"--override-ini='junit_family={}'".format(junit_format),
)
assert result.ret == 0
results_file = testdir.tmpdir.join("results.xml")
results_file = pytester.tmpdir.join("results.xml")
assert results_file.exists()
with results_file.open() as fd:
xml = ET.parse(fd)
Expand All @@ -77,77 +78,74 @@ def test_pass():
assert {"name": "Daffy", "value": "Duck"} in xml_metadata


def test_additional_metadata_from_json(testdir):
testdir.makepyfile(
def test_additional_metadata_from_json(pytester):
pytester.makepyfile(
"""
def test_pass(metadata):
assert metadata.get('Imran') == 'Mumtaz'
"""
)
result = testdir.runpytest("--metadata-from-json", '{"Imran": "Mumtaz"}')
result = pytester.runpytest("--metadata-from-json", '{"Imran": "Mumtaz"}')
assert result.ret == 0


def test_additional_metadata_from_json_file(testdir):
testdir.makepyfile(
def test_additional_metadata_from_json_file(pytester):
pytester.makepyfile(
"""
def test_pass(metadata):
assert metadata.get('John') == 'Cena'
"""
)
pytester.makefile(".json", temp='{"John": "Cena"}')

json_temp = NamedTemporaryFile(delete=False)
json_temp.write('{"John": "Cena"}'.encode(encoding="utf-8"))
json_temp.flush()
result = testdir.runpytest("--metadata-from-json-file", json_temp.name)
result = pytester.runpytest("--metadata-from-json-file", "temp.json")
assert result.ret == 0


def test_additional_metadata_using_key_values_json_str_and_file(testdir):
testdir.makepyfile(
def test_additional_metadata_using_key_values_json_str_and_file(pytester):
pytester.makepyfile(
"""
def test_pass(metadata):
assert metadata.get('John') == 'Cena'
assert metadata.get('Dwayne') == 'Johnson'
assert metadata.get('Andre') == 'The Giant'
"""
)
json_temp = NamedTemporaryFile(delete=False)
json_temp.write('{"Andre": "The Giant"}'.encode(encoding="utf-8"))
json_temp.flush()
result = testdir.runpytest(
pytester.makefile(".json", temp='{"Andre": "The Giant"}')

result = pytester.runpytest(
"--metadata",
"John",
"Cena",
"--metadata-from-json",
'{"Dwayne": "Johnson"}',
"--metadata-from-json-file",
json_temp.name,
"temp.json",
)
assert result.ret == 0


def test_metadata_hook(testdir):
testdir.makeconftest(
def test_metadata_hook(pytester):
pytester.makeconftest(
"""
import pytest
@pytest.hookimpl(optionalhook=True)
def pytest_metadata(metadata):
metadata['Dave'] = 'Hunt'
"""
)
testdir.makepyfile(
pytester.makepyfile(
"""
def test_pass(metadata):
assert metadata.get('Dave') == 'Hunt'
"""
)
result = testdir.runpytest()
result = pytester.runpytest()
assert result.ret == 0


def test_report_header(testdir):
result = testdir.runpytest()
def test_report_header(pytester):
result = pytester.runpytest()
assert not any(line.startswith("metadata:") for line in result.stdout.lines)
result = testdir.runpytest("-v")
result = pytester.runpytest("-v")
assert any(line.startswith("metadata:") for line in result.stdout.lines)

0 comments on commit 98fbf8b

Please sign in to comment.