Skip to content

Commit

Permalink
JSONExporter will log a "file not found" warning in the log instead o…
Browse files Browse the repository at this point in the history
…f raising an exception.
  • Loading branch information
yuxuan-ms committed Dec 9, 2024
1 parent f626618 commit 23f2903
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
1 change: 1 addition & 0 deletions doc/newsfragments/3167_changed.json_exporter.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``JSONExporter`` will log a "file not found" warning in the log instead of raising an exception.
76 changes: 41 additions & 35 deletions testplan/exporters/testing/json/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,6 @@
from ..base import Exporter


def save_attachments(report: TestReport, directory: str) -> Dict[str, str]:
"""
Saves the report attachments to the given directory.
:param report: Testplan report.
:param directory: directory to save attachments in
:return: dictionary of destination paths
"""
moved_attachments = {}
attachments = getattr(report, "attachments", None)
if attachments:
for dst, src in attachments.items():
src = pathlib.Path(src)
dst_path = pathlib.Path(directory) / dst
makedirs(dst_path.parent)
if not src.is_file():
dirname = src.parent
# Try retrieving the file from "_attachments" directory that is
# near to the test report, the downloaded report might be moved
src = pathlib.Path.cwd() / ATTACHMENTS / dst
if not src.is_file():
raise FileNotFoundError(
f'Attachment "{dst}" not found in either {dirname} or'
f' the nearest "{ATTACHMENTS}" directory of test report'
)
copyfile(src=src, dst=dst_path)
moved_attachments[dst] = str(dst_path)

return moved_attachments


def save_resource_data(
report: TestReport, directory: pathlib.Path
) -> pathlib.Path:
Expand Down Expand Up @@ -176,8 +145,9 @@ def export(
with open(assertions_filepath, "w") as json_file:
json.dump(assertions, json_file)

meta["attachments"] = save_attachments(
report=source, directory=attachments_dir
meta["attachments"] = self.save_attachments(
report=source,
directory=attachments_dir,
)
meta["version"] = 2
meta["attachments"][structure_filename] = str(
Expand All @@ -192,8 +162,9 @@ def export(
with open(json_path, "w") as json_file:
json.dump(meta, json_file)
else:
data["attachments"] = save_attachments(
report=source, directory=attachments_dir
data["attachments"] = self.save_attachments(
report=source,
directory=attachments_dir,
)
data["version"] = 1

Expand All @@ -208,6 +179,41 @@ def export(
)
return result

def save_attachments(
self, report: TestReport, directory: str
) -> Dict[str, str]:
"""
Saves the report attachments to the given directory.
:param report: Testplan report.
:param directory: directory to save attachments in
:return: dictionary of destination paths
"""
moved_attachments = {}
attachments = getattr(report, "attachments", None)
if attachments:
for dst, src in attachments.items():
src = pathlib.Path(src)
dst_path = pathlib.Path(directory) / dst
makedirs(dst_path.parent)
if not src.is_file():
dirname = src.parent
# Try retrieving the file from "_attachments" directory that is
# near to the test report, the downloaded report might be moved
src = pathlib.Path.cwd() / ATTACHMENTS / dst
if not src.is_file():
self.logger.warning(
'Attachment "%s" not found in either %s or the nearest "%s" directory of test report',
dst,
dirname,
ATTACHMENTS,
)
continue
copyfile(src=src, dst=dst_path)
moved_attachments[dst] = str(dst_path)

return moved_attachments

@staticmethod
def split_json_report(data):
"""Split a single Json into several parts."""
Expand Down
2 changes: 1 addition & 1 deletion testplan/runners/pools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def rebase_task_path(self, task: Task) -> None:
def discard_running_tasks(self):
self._discard_running.set()

def __str__(self):
def __repr__(self):
return f"{self.__class__.__name__}[{self.cfg.index}]"


Expand Down

0 comments on commit 23f2903

Please sign in to comment.