Skip to content

Commit

Permalink
Beyondevil/tmpdir workaround (#85)
Browse files Browse the repository at this point in the history
* Release v3.0.0 (#69)

* fix: tmpdir workaround
  • Loading branch information
BeyondEvil committed Jan 7, 2024
1 parent 2fdb15e commit 65ab3e4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
publish:
if: github.repository == 'pytest-dev/pytest-base-url'
if: github.repository == 'pytest-dev/pytest-metadata'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions src/pytest_metadata/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def pytest_configure(config):
config.stash[metadata_key].update(
json.loads(config.getoption("metadata_from_json"))
)

if config.getoption("metadata_from_json_file"):
with open(config.getoption("metadata_from_json_file"), "r") as json_file:
config.stash[metadata_key].update(json.load(json_file))
Expand Down
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.path.joinpath("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 65ab3e4

Please sign in to comment.