-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathtransform-test-error-json-format.py
executable file
·53 lines (41 loc) · 1.64 KB
/
transform-test-error-json-format.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
"""
Transform test error JSON file formatter (without prettier).
It makes error json easier to review by breaking down "errorMessage"
into list of strings (delimiter: ". ").
"""
import sys
from pathlib import Path
# To allow this script to be executed from other directories
sys.path.insert(0, str(Path(__file__).absolute().parent.parent))
import json
from typing import Final, Type
from bin._file_formatter import FileFormatter
class TransformTestErrorJSONFormatter(FileFormatter):
_ERROR_MESSAGE_KEY: Final[str] = "errorMessage"
_BREAKDOWN_ERROR_MESSAGE_KEY: Final[str] = "_autoGeneratedBreakdownErrorMessage"
_DELIMITER: Final[str] = ". "
@staticmethod
def description() -> str:
return "Transform test error JSON file formatter"
def format_str(self, input_str: str) -> str:
"""
It makes error json easier to review by breaking down "errorMessage"
into list of strings (delimiter: ". ").
"""
obj = json.loads(input_str)
error_message = obj.get(self._ERROR_MESSAGE_KEY)
if isinstance(error_message, str):
tokens = error_message.split(self._DELIMITER)
obj[self._BREAKDOWN_ERROR_MESSAGE_KEY] = [
token if index == len(tokens) - 1 else token + self._DELIMITER for index, token in enumerate(tokens)
]
return json.dumps(obj, indent=2, sort_keys=True) + "\n"
@staticmethod
def decode_exception() -> Type[Exception]:
return json.JSONDecodeError
@staticmethod
def file_extension() -> str:
return ".json"
if __name__ == "__main__":
TransformTestErrorJSONFormatter.main()