Skip to content

Commit

Permalink
twister: reporting: Add custom JSON encoder
Browse files Browse the repository at this point in the history
Refactor Reporting to use custom ReportingJSONEncoder and encode
all pathlib.Path derived instances there which are possible
e.g. in 'environment.options' received from command line.

Fixes: #83823

Signed-off-by: Dmitrii Golovanov <[email protected]>
  • Loading branch information
golowanow authored and kartben committed Jan 23, 2025
1 parent abb83b9 commit aa70508
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions scripts/pylib/twister/twisterlib/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import xml.etree.ElementTree as ET
from datetime import datetime
from enum import Enum
from pathlib import Path, PosixPath
from pathlib import Path

from colorama import Fore
from twisterlib.statuses import TwisterStatus
Expand All @@ -29,6 +29,13 @@ def __str__(self):
SKIP = 'skipped'


class ReportingJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Path):
return str(obj)
return super().default(obj)


class Reporting:

json_filters = {
Expand Down Expand Up @@ -294,10 +301,6 @@ def json_report(self, filename, version="NA", platform=None, filters=None):
else:
report_options = self.env.non_default_options()

# Resolve known JSON serialization problems.
for k,v in report_options.items():
report_options[k] = str(v) if type(v) in [PosixPath] else v

report = {}
report["environment"] = {"os": os.name,
"zephyr_version": version,
Expand Down Expand Up @@ -483,7 +486,7 @@ def json_report(self, filename, version="NA", platform=None, filters=None):

report["testsuites"] = suites
with open(filename, 'w') as json_file:
json.dump(report, json_file, indent=4, separators=(',',':'))
json.dump(report, json_file, indent=4, separators=(',',':'), cls=ReportingJSONEncoder)


def compare_metrics(self, filename):
Expand Down

0 comments on commit aa70508

Please sign in to comment.