Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion reframe/frontend/reporting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ def _do_restore(self, testcase):
f'could not restore testcase {testcase!r}') from e


def _tail_file(filename, num_lines):
try:
with open(filename, encoding='utf-8', errors='replace') as fp:
return ''.join(fp.readlines()[-num_lines:])
except OSError:
return ''
Comment on lines +175 to +180

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed: you can use the osext.tail() function.



def _expand_report_filename(filepatt, *, newfile):
if '{sessionid}' not in os.fspath(filepatt):
return filepatt
Expand Down Expand Up @@ -539,7 +547,28 @@ def generate_xml_report(self):
testcase, 'failure', attrib={'type': 'failure',
'message': fail_phase}
)
testcase_msg.text = f"{tc['fail_phase']}: {fail_reason}"
workdir = tc.get('outputdir') or tc.get('stagedir') or ''
stdout = ''
stderr = ''
if workdir:
job_stdout = tc.get('job_stdout')
if job_stdout:
stdout = _tail_file(
os.path.join(workdir, job_stdout), 20
)

job_stderr = tc.get('job_stderr')
if job_stderr:
stderr = _tail_file(
os.path.join(workdir, job_stderr), 20
)

testcase_msg.text = '\n\n'.join([
f"{tc['fail_phase']}: {fail_reason}",
workdir,
stdout,
stderr
])

testsuite_stdout = etree.SubElement(xml_testsuite, 'system-out')
testsuite_stdout.text = ''
Expand Down
39 changes: 38 additions & 1 deletion unittests/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ def _generate_runreport(run_stats, time_start=None, time_end=None):
return report


def test_tail_file(tmp_path):
filename = tmp_path / 'output.txt'
filename.write_text('line1\nline2\nline3\n')

assert reporting._tail_file(filename, 20) == 'line1\nline2\nline3\n'
assert reporting._tail_file(filename, 2) == 'line2\nline3\n'
assert reporting._tail_file(tmp_path / 'missing.txt', 20) == ''


def test_run_report(make_runner, make_cases, common_exec_ctx, tmp_path):
runner = make_runner()
with _timer() as tm:
Expand All @@ -100,7 +109,35 @@ def test_run_report(make_runner, make_cases, common_exec_ctx, tmp_path):
report['runs'][0]['testcases'][-1]['time_total'] = None

# Validate the junit report
_validate_junit_report(report.generate_xml_report())
junit_report = report.generate_xml_report()
_validate_junit_report(junit_report)
for tc, xml_tc in zip(report['runs'][0]['testcases'],
junit_report.findall('.//testcase')):
failure = xml_tc.find('failure')
if tc['result'] != 'fail':
assert failure is None
continue

workdir = tc.get('outputdir') or tc.get('stagedir') or ''
stdout = ''
stderr = ''
if workdir:
if tc['job_stdout']:
stdout = reporting._tail_file(
os.path.join(workdir, tc['job_stdout']), 20
)

if tc['job_stderr']:
stderr = reporting._tail_file(
os.path.join(workdir, tc['job_stderr']), 20
)

assert failure.text == '\n\n'.join([
f"{tc['fail_phase']}: {tc['fail_reason']}",
workdir,
stdout,
stderr
])

# Read and validate the report using the `reporting` module
reporting.restore_session(tmp_path / 'report.json')
Expand Down
Loading