import json
import sys
from collections import defaultdict
def get_priority(result):
priorities = {
"PASS": 0, "FAIL": 1, "ERROR": 2, "SKIP": 3, }
return priorities.get(result, 4)
def aggregate_results(json_files):
combined_results = defaultdict(dict)
for json_file in json_files:
try:
with open(json_file, "r") as f:
data = json.load(f)
for utility, tests in data.items():
for test_name, result in tests.items():
if test_name not in combined_results[utility]:
combined_results[utility][test_name] = result
else:
current_priority = get_priority(
combined_results[utility][test_name]
)
new_priority = get_priority(result)
if new_priority < current_priority:
combined_results[utility][test_name] = result
except FileNotFoundError:
print(f"Warning: File '{json_file}' not found.", file=sys.stderr)
continue
except json.JSONDecodeError:
print(f"Warning: '{json_file}' is not a valid JSON file.", file=sys.stderr)
continue
return combined_results
def analyze_test_results(json_data):
total_tests = 0
pass_count = 0
fail_count = 0
skip_count = 0
xpass_count = 0 error_count = 0
for utility, tests in json_data.items():
for test_name, result in tests.items():
total_tests += 1
match result:
case "PASS":
pass_count += 1
case "FAIL":
fail_count += 1
case "SKIP":
skip_count += 1
case "ERROR":
error_count += 1
case "XPASS":
xpass_count += 1
return {
"TOTAL": total_tests,
"PASS": pass_count,
"FAIL": fail_count,
"SKIP": skip_count,
"XPASS": xpass_count,
"ERROR": error_count,
}
def main():
if len(sys.argv) < 2:
print("Usage: python analyze-gnu-results.py <json> [json ...]")
print(" For multiple files, results will be aggregated")
print(" Priority SKIP > ERROR > FAIL > PASS")
sys.exit(1)
json_files = sys.argv[1:]
output_file = None
if json_files[0].startswith("-o="):
output_file = json_files[0][3:]
json_files = json_files[1:]
if len(json_files) == 1:
try:
with open(json_files[0], "r") as file:
json_data = json.load(file)
results = analyze_test_results(json_data)
except FileNotFoundError:
print(f"Error: File '{json_files[0]}' not found.", file=sys.stderr)
sys.exit(1)
except json.JSONDecodeError:
print(
f"Error: '{json_files[0]}' is not a valid JSON file.", file=sys.stderr
)
sys.exit(1)
else:
json_data = aggregate_results(json_files)
results = analyze_test_results(json_data)
if output_file:
with open(output_file, "w") as f:
json.dump(json_data, f, indent=2)
print(f"export TOTAL={results['TOTAL']}")
print(f"export PASS={results['PASS']}")
print(f"export SKIP={results['SKIP']}")
print(f"export FAIL={results['FAIL']}")
print(f"export XPASS={results['XPASS']}")
print(f"export ERROR={results['ERROR']}")
if __name__ == "__main__":
main()