Skip to content

Commit

Permalink
Testplan result importer to best effort fix attachment path (#1162)
Browse files Browse the repository at this point in the history
Co-authored-by: Pyifan <[email protected]>
  • Loading branch information
Pyifan and Pyifan authored Dec 18, 2024
1 parent c704044 commit d9520ee
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions testplan/importers/testplan.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""
Implements one-phase importer for Testplan JSON format.
"""
import os
import pathlib
from typing import List

from testplan.common.utils.json import json_loads
from testplan.defaults import ATTACHMENTS
from testplan.importers import ImportedResult, ResultImporter
from testplan.report import ReportCategories, TestGroupReport, TestReport
from testplan.report.testing.schemas import TestReportSchema
Expand Down Expand Up @@ -42,6 +45,47 @@ def import_result(self) -> ImportedResult:
""" """
with open(self.path) as fp:
result_json = json_loads(fp.read())
self.fix_attachments_path(result_json)
result = self.schema.load(result_json)

return TestplanImportedResult(result)

def fix_attachments_path(
self,
report: dict,
attachment_dir: pathlib.Path = None,
):
"""
Best effort fix attachment path in case report.json and _attachments are copied around
"""
attachment_dir = (
str(attachment_dir)
if attachment_dir
else os.path.join(os.path.dirname(self.path), ATTACHMENTS)
)
if report.get("attachments"):
for dst, src in report["attachments"].items():
if os.path.isfile(src):
# attachment path is correct
break
else:
# attempt to fix attachment path
alt_src = os.path.join(attachment_dir, dst)
if os.path.isfile(alt_src):
report["attachments"][dst] = alt_src

# recursively fix entries
def _fix_attachments_path(report):
if report.get("entries"):
for entry in report["entries"]:
_fix_attachments_path(entry)
elif report.get("source_path") and report.get("dst_path"):
if os.path.isfile(report["source_path"]):
# attachment path is correct
return
alt_src = os.path.join(attachment_dir, report["dst_path"])
if os.path.isfile(alt_src):
report["source_path"] = alt_src

_fix_attachments_path(report)
return report

0 comments on commit d9520ee

Please sign in to comment.