Skip to content

Commit

Permalink
print server logs after test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Ableytner committed Mar 16, 2024
1 parent 27e1634 commit 21b4c3d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
45 changes: 43 additions & 2 deletions mcserverwrapper/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import sys
import pathlib

from pytest import Metafunc
import pytest
from pytest import Metafunc, TestReport, Session, ExitCode

from .helpers.common_helper import get_mcserver_log
from .integration_tests import test_forge

# Adding source path to sys path
Expand All @@ -26,7 +28,7 @@
from .fixtures import newest_server_jar

from .helpers.common_helper import get_vanilla_urls
#pylint: enable=wrong-import-position
# pylint: enable=wrong-import-position

def pytest_generate_tests(metafunc: Metafunc):
"""Pytest hook"""
Expand All @@ -48,3 +50,42 @@ def pytest_generate_tests(metafunc: Metafunc):
metafunc.parametrize(argnames="forge_download_url",
argvalues=test_forge.FORGE_URLS.values(),
ids=[f"test_version_{item}" for item in test_forge.FORGE_URLS])

# the arguments are needed for the pytest hooks to work correctly
# pylint: disable=unused-argument
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
"""Save mcserverwrapper.log to the current pytest item"""

# execute all other hooks to obtain the report object
rep: TestReport = yield

# we only look at actual failing test calls, not setup/teardown
if rep.when == "call" and rep.outcome == "failed":
item.mcserverlog = get_mcserver_log()

return rep

@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session: Session, exitstatus: ExitCode):
"""Print all saved mcserverwrapper.log after all tests finished"""

if len(session.items) == 0:
return

print("")
print("")
print("=" * 20, end="")
print(" McServerWrapper.log ", end="")
print("=" * 20)

for item in session.items:
# skip tests that didn't fail
if hasattr(item, "mcserverlog"):
print("")
print("-" * 20, end="")
print(f" {item.name} ", end="")
print("-" * 20, end="\n")
for line in item.mcserverlog.split("\n"):
print(line)
# pylint: enable=unused-argument
15 changes: 15 additions & 0 deletions mcserverwrapper/test/helpers/common_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from javascript import require, once

from mcserverwrapper.src.util import logger

mineflayer = require('mineflayer')

def setup_workspace():
Expand Down Expand Up @@ -122,6 +124,19 @@ def download_file(url, counter=""):
file.write(req.content)
return local_filename

def get_mcserver_log() -> str:
"""Return the current mcserverwrapper.log as a string if it exists"""

if logger.logfile_path is None:
print("Logger was not yet setup, cannot print logfile")
return ""

data = f"Printing out {logger.LOGFILE_NAME}:\n"
with open(logger.logfile_path, "r", encoding="utf8") as f:
for line in f.readlines():
data += line
return data

def get_vanilla_urls():
"""Function written by @Pfefan"""

Expand Down

0 comments on commit 21b4c3d

Please sign in to comment.