|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""A test case update orchestrator script. |
| 4 | +
|
| 5 | +This script is a utility to automate the updating of failed LLVM test cases by |
| 6 | +invoking other `update_*_test_checkes.py` scripts. It provides a one-click |
| 7 | +solution to update test expectations for previously failed tests. |
| 8 | +""" |
| 9 | + |
| 10 | +import subprocess |
| 11 | +import os |
| 12 | + |
| 13 | + |
| 14 | +def get_llvm_path(): |
| 15 | + util_path = os.path.dirname(os.path.realpath(__file__)) |
| 16 | + llvm_path = os.path.dirname(util_path) |
| 17 | + print("The LLVM path is", llvm_path) |
| 18 | + return llvm_path |
| 19 | + |
| 20 | + |
| 21 | +def get_build_path(): |
| 22 | + if os.path.basename(os.getcwd()).startswith("build"): |
| 23 | + build_path = os.getcwd() |
| 24 | + else: |
| 25 | + dirs = [d for d in os.listdir('.') if os.path.isdir(d)] |
| 26 | + build_dirs = [d for d in dirs if d.startswith('build')] |
| 27 | + |
| 28 | + if len(build_dirs) != 1: |
| 29 | + print( |
| 30 | + "Cannot find the build directory. Please run this script in the build directory.") |
| 31 | + exit(1) |
| 32 | + build_path = build_dirs[0] |
| 33 | + |
| 34 | + print("The BUILD path is", build_path) |
| 35 | + return build_path |
| 36 | + |
| 37 | + |
| 38 | +def run_tool(tool_path, tool_name, tool_bin, build_path, file_path): |
| 39 | + print(tool_name.upper() + " updating: ", file_path) |
| 40 | + result = subprocess.run( |
| 41 | + ["python3", tool_path, "--"+tool_name+"="+build_path+"/bin/"+tool_bin, file_path]) |
| 42 | + return result |
| 43 | + |
| 44 | + |
| 45 | +def run(build_path, projetct_name, project_path, test_times_path): |
| 46 | + if not os.path.exists(test_times_path): |
| 47 | + print("No tests found for", projetct_name) |
| 48 | + return |
| 49 | + |
| 50 | + # read lit test records: |
| 51 | + with open(test_times_path, 'r') as f: |
| 52 | + rest_tests = [] |
| 53 | + |
| 54 | + for line in f: |
| 55 | + # split line into Time and Path |
| 56 | + parts = line.strip().split(' ', 1) |
| 57 | + run_time = float(parts[0]) |
| 58 | + file_path = project_path + "/test/" + parts[1] |
| 59 | + |
| 60 | + # If time is negative, then it is a failed test |
| 61 | + if run_time < 0: |
| 62 | + if not os.path.exists(file_path): |
| 63 | + print("NOT FOUND:", file_path) |
| 64 | + continue |
| 65 | + |
| 66 | + # open file, read first line |
| 67 | + with open(file_path, 'r') as target_file: |
| 68 | + first_line = target_file.readline().strip() |
| 69 | + if not first_line.startswith("; NOTE: Assertions") and not first_line.startswith("# NOTE: Assertions"): |
| 70 | + print("\nSKIP: ", file_path) |
| 71 | + continue |
| 72 | + |
| 73 | + tool_name = first_line.split(" ")[7] |
| 74 | + tool_path = llvm_path + "/" + tool_name |
| 75 | + |
| 76 | + # call update tools |
| 77 | + if "update_llc_test_checks" in tool_name: |
| 78 | + result = run_tool(tool_path, "llc", "llc", |
| 79 | + build_path, file_path) |
| 80 | + elif "update_cc_test_checks" in tool_name: |
| 81 | + result = run_tool(tool_path, "cc", "clang", |
| 82 | + build_path, file_path) |
| 83 | + elif "update_test_checks" in tool_name or "update_analyze_test_checks" in tool_name: |
| 84 | + result = run_tool(tool_path, "opt", "opt", |
| 85 | + build_path, file_path) |
| 86 | + elif "update_mir_test_checks" in tool_name: |
| 87 | + result = run_tool(tool_path, "llc", "llc", |
| 88 | + build_path, file_path) |
| 89 | + else: |
| 90 | + print("\nUNHANDLED: ", file_path) |
| 91 | + continue |
| 92 | + |
| 93 | + if result.returncode != 0: |
| 94 | + rest_tests.append(file_path) |
| 95 | + |
| 96 | + if len(rest_tests) != 0: |
| 97 | + for failed in rest_tests: |
| 98 | + print("FAILED: ", failed) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + llvm_path = get_llvm_path() |
| 103 | + build_path = get_build_path() |
| 104 | + |
| 105 | + llvm_test_times_path = build_path + "/test/.lit_test_times.txt" |
| 106 | + clang_test_times_path = build_path + "/tools/clang/test/.lit_test_times.txt" |
| 107 | + clang_path = llvm_path + "/clang" |
| 108 | + |
| 109 | + run(build_path, "llvm", llvm_path, llvm_test_times_path) |
| 110 | + run(build_path, "clang", clang_path, clang_test_times_path) |
0 commit comments