| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
GNU Test Results Analyzer and Aggregator |
| 5 |
|
| 6 |
This script analyzes and aggregates test results from the GNU test suite. |
| 7 |
It parses JSON files containing test results (PASS/FAIL/SKIP/ERROR) and: |
| 8 |
1. Counts the number of tests in each result category |
| 9 |
2. Can aggregate results from multiple JSON files with priority ordering |
| 10 |
3. Outputs shell export statements for use in GitHub Actions workflows |
| 11 |
|
| 12 |
Priority order for aggregation (highest to lowest): |
| 13 |
- PASS: Takes precedence over all other results (best outcome) |
| 14 |
- FAIL: Takes precedence over ERROR and SKIP |
| 15 |
- ERROR: Takes precedence over SKIP |
| 16 |
- SKIP: Lowest priority |
| 17 |
|
| 18 |
Usage: |
| 19 |
- Single file: |
| 20 |
python analyze-gnu-results.py test-results.json |
| 21 |
|
| 22 |
- Multiple files (with aggregation): |
| 23 |
python analyze-gnu-results.py file1.json file2.json |
| 24 |
|
| 25 |
- With output file for aggregated results: |
| 26 |
python analyze-gnu-results.py -o=output.json file1.json file2.json |
| 27 |
|
| 28 |
Output: |
| 29 |
Prints shell export statements for TOTAL, PASS, FAIL, SKIP, XPASS, and ERROR |
| 30 |
that can be evaluated in a shell environment. |
| 31 |
""" |
| 32 |
|
| 33 |
import json |
| 34 |
import sys |
| 35 |
from collections import defaultdict |
| 36 |
|
| 37 |
|
| 38 |
def get_priority(result): |
| 39 |
"""Return a priority value for result status (lower is higher priority)""" |
| 40 |
priorities = { |
| 41 |
"PASS": 0, # PASS is highest priority (best result) |
| 42 |
"FAIL": 1, # FAIL is second priority |
| 43 |
"ERROR": 2, # ERROR is third priority |
| 44 |
"SKIP": 3, # SKIP is lowest priority |
| 45 |
} |
| 46 |
return priorities.get(result, 4) # Unknown states have lowest priority |
| 47 |
|
| 48 |
|
| 49 |
def aggregate_results(json_files): |
| 50 |
""" |
| 51 |
Aggregate test results from multiple JSON files. |
| 52 |
Prioritizes results in the order: SKIP > ERROR > FAIL > PASS |
| 53 |
""" |
| 54 |
# Combined results dictionary |
| 55 |
combined_results = defaultdict(dict) |
| 56 |
|
| 57 |
# Process each JSON file |
| 58 |
for json_file in json_files: |
| 59 |
try: |
| 60 |
with open(json_file, "r") as f: |
| 61 |
data = json.load(f) |
| 62 |
|
| 63 |
# For each utility and its tests |
| 64 |
for utility, tests in data.items(): |
| 65 |
for test_name, result in tests.items(): |
| 66 |
# If this test hasn't been seen yet, add it |
| 67 |
if test_name not in combined_results[utility]: |
| 68 |
combined_results[utility][test_name] = result |
| 69 |
else: |
| 70 |
# If it has been seen, apply priority rules |
| 71 |
current_priority = get_priority( |
| 72 |
combined_results[utility][test_name] |
| 73 |
) |
| 74 |
new_priority = get_priority(result) |
| 75 |
|
| 76 |
# Lower priority value means higher precedence |
| 77 |
if new_priority < current_priority: |
| 78 |
combined_results[utility][test_name] = result |
| 79 |
except FileNotFoundError: |
| 80 |
print(f"Warning: File '{json_file}' not found.", file=sys.stderr) |
| 81 |
continue |
| 82 |
except json.JSONDecodeError: |
| 83 |
print(f"Warning: '{json_file}' is not a valid JSON file.", file=sys.stderr) |
| 84 |
continue |
| 85 |
|
| 86 |
return combined_results |
| 87 |
|
| 88 |
|
| 89 |
def analyze_test_results(json_data): |
| 90 |
""" |
| 91 |
Analyze test results from GNU test suite JSON data. |
| 92 |
Counts PASS, FAIL, SKIP results for all tests. |
| 93 |
""" |
| 94 |
# Counters for test results |
| 95 |
total_tests = 0 |
| 96 |
pass_count = 0 |
| 97 |
fail_count = 0 |
| 98 |
skip_count = 0 |
| 99 |
xpass_count = 0 # Not in JSON data but included for compatibility |
| 100 |
error_count = 0 # Not in JSON data but included for compatibility |
| 101 |
|
| 102 |
# Analyze each utility's tests |
| 103 |
for tests in json_data.values(): |
| 104 |
for result in tests.values(): |
| 105 |
total_tests += 1 |
| 106 |
|
| 107 |
match result: |
| 108 |
case "PASS": |
| 109 |
pass_count += 1 |
| 110 |
case "FAIL": |
| 111 |
fail_count += 1 |
| 112 |
case "SKIP": |
| 113 |
skip_count += 1 |
| 114 |
case "ERROR": |
| 115 |
error_count += 1 |
| 116 |
case "XPASS": |
| 117 |
xpass_count += 1 |
| 118 |
|
| 119 |
# Return the statistics |
| 120 |
return { |
| 121 |
"TOTAL": total_tests, |
| 122 |
"PASS": pass_count, |
| 123 |
"FAIL": fail_count, |
| 124 |
"SKIP": skip_count, |
| 125 |
"XPASS": xpass_count, |
| 126 |
"ERROR": error_count, |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
def main(): |
| 131 |
""" |
| 132 |
Main function to process JSON files and export variables. |
| 133 |
Supports both single file analysis and multi-file aggregation. |
| 134 |
""" |
| 135 |
# Check if file arguments were provided |
| 136 |
if len(sys.argv) < 2: |
| 137 |
print("Usage: python analyze-gnu-results.py <json> [json ...]") |
| 138 |
print(" For multiple files, results will be aggregated") |
| 139 |
print(" Priority SKIP > ERROR > FAIL > PASS") |
| 140 |
sys.exit(1) |
| 141 |
|
| 142 |
json_files = sys.argv[1:] |
| 143 |
output_file = None |
| 144 |
|
| 145 |
# Check if the first argument is an output file (starts with -) |
| 146 |
if json_files[0].startswith("-o="): |
| 147 |
output_file = json_files[0][3:] |
| 148 |
json_files = json_files[1:] |
| 149 |
|
| 150 |
# Process the files |
| 151 |
if len(json_files) == 1: |
| 152 |
# Single file analysis |
| 153 |
try: |
| 154 |
with open(json_files[0], "r") as file: |
| 155 |
json_data = json.load(file) |
| 156 |
results = analyze_test_results(json_data) |
| 157 |
except FileNotFoundError: |
| 158 |
print(f"Error: File '{json_files[0]}' not found.", file=sys.stderr) |
| 159 |
sys.exit(1) |
| 160 |
except json.JSONDecodeError: |
| 161 |
print( |
| 162 |
f"Error: '{json_files[0]}' is not a valid JSON file.", file=sys.stderr |
| 163 |
) |
| 164 |
sys.exit(1) |
| 165 |
else: |
| 166 |
# Multiple files - aggregate them |
| 167 |
json_data = aggregate_results(json_files) |
| 168 |
results = analyze_test_results(json_data) |
| 169 |
|
| 170 |
# Save aggregated data if output file is specified |
| 171 |
if output_file: |
| 172 |
with open(output_file, "w") as f: |
| 173 |
json.dump(json_data, f, indent=2) |
| 174 |
|
| 175 |
# Print export statements for shell evaluation |
| 176 |
print(f"export TOTAL={results['TOTAL']}") |
| 177 |
print(f"export PASS={results['PASS']}") |
| 178 |
print(f"export SKIP={results['SKIP']}") |
| 179 |
print(f"export FAIL={results['FAIL']}") |
| 180 |
print(f"export XPASS={results['XPASS']}") |
| 181 |
print(f"export ERROR={results['ERROR']}") |
| 182 |
|
| 183 |
|
| 184 |
if __name__ == "__main__": |
| 185 |
main() |