|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import json |
| 3 | +import sys |
| 4 | + |
| 5 | + |
| 6 | +def analyze_test_results(json_data): |
| 7 | + # Counters for test results |
| 8 | + total_tests = 0 |
| 9 | + pass_count = 0 |
| 10 | + fail_count = 0 |
| 11 | + skip_count = 0 |
| 12 | + error_count = 0 # Although not in the JSON, included for compatibility |
| 13 | + |
| 14 | + # Analyze each utility's tests |
| 15 | + for utility, tests in json_data.items(): |
| 16 | + for test_name, result in tests.items(): |
| 17 | + total_tests += 1 |
| 18 | + |
| 19 | + if result == "PASS": |
| 20 | + pass_count += 1 |
| 21 | + elif result == "FAIL": |
| 22 | + fail_count += 1 |
| 23 | + elif result == "SKIP": |
| 24 | + skip_count += 1 |
| 25 | + |
| 26 | + # Return the statistics |
| 27 | + return { |
| 28 | + "TOTAL": total_tests, |
| 29 | + "PASS": pass_count, |
| 30 | + "FAIL": fail_count, |
| 31 | + "SKIP": skip_count, |
| 32 | + "ERROR": error_count, |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +def main(): |
| 37 | + # Check if a file argument was provided |
| 38 | + if len(sys.argv) != 2: |
| 39 | + print("Usage: python script.py <json_file>") |
| 40 | + sys.exit(1) |
| 41 | + |
| 42 | + json_file = sys.argv[1] |
| 43 | + |
| 44 | + try: |
| 45 | + # Parse the JSON data from the specified file |
| 46 | + with open(json_file, "r") as file: |
| 47 | + json_data = json.load(file) |
| 48 | + |
| 49 | + # Analyze the results |
| 50 | + results = analyze_test_results(json_data) |
| 51 | + |
| 52 | + # Export the results as environment variables |
| 53 | + # For use in shell, print export statements |
| 54 | + print(f"export TOTAL={results['TOTAL']}") |
| 55 | + print(f"export PASS={results['PASS']}") |
| 56 | + print(f"export SKIP={results['SKIP']}") |
| 57 | + print(f"export FAIL={results['FAIL']}") |
| 58 | + print(f"export ERROR={results['ERROR']}") |
| 59 | + |
| 60 | + except FileNotFoundError: |
| 61 | + print(f"Error: File '{json_file}' not found.", file=sys.stderr) |
| 62 | + sys.exit(1) |
| 63 | + except json.JSONDecodeError: |
| 64 | + print(f"Error: '{json_file}' is not a valid JSON", file=sys.stderr) |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + main() |
0 commit comments