1import json 2import os 3import sys 4from pathlib import Path 5 6 7REPO_ROOT = Path(__file__).resolve().parent.parent.parent 8sys.path.insert(0, str(REPO_ROOT)) 9 10from tools.stats.import_test_stats import ( 11 copy_additional_previous_failures, 12 copy_pytest_cache, 13 get_td_heuristic_historial_edited_files_json, 14 get_td_heuristic_profiling_json, 15 get_test_class_ratings, 16 get_test_class_times, 17 get_test_file_ratings, 18 get_test_times, 19) 20from tools.stats.upload_metrics import emit_metric 21from tools.testing.discover_tests import TESTS 22from tools.testing.target_determination.determinator import ( 23 AggregatedHeuristics, 24 get_test_prioritizations, 25 TestPrioritizations, 26) 27 28 29sys.path.remove(str(REPO_ROOT)) 30 31 32def import_results() -> TestPrioritizations: 33 if not (REPO_ROOT / ".additional_ci_files/td_results.json").exists(): 34 print("No TD results found") 35 return TestPrioritizations([], {}) 36 with open(REPO_ROOT / ".additional_ci_files/td_results.json") as f: 37 td_results = json.load(f) 38 tp = TestPrioritizations.from_json(td_results) 39 40 return tp 41 42 43def main() -> None: 44 selected_tests = TESTS 45 46 aggregated_heuristics: AggregatedHeuristics = AggregatedHeuristics(selected_tests) 47 48 get_test_times() 49 get_test_class_times() 50 get_test_file_ratings() 51 get_test_class_ratings() 52 get_td_heuristic_historial_edited_files_json() 53 get_td_heuristic_profiling_json() 54 copy_pytest_cache() 55 copy_additional_previous_failures() 56 57 aggregated_heuristics = get_test_prioritizations(selected_tests) 58 59 test_prioritizations = aggregated_heuristics.get_aggregated_priorities() 60 61 print("Aggregated Heuristics") 62 print(test_prioritizations.get_info_str(verbose=False)) 63 64 if os.getenv("CI") == "true": 65 print("Emitting metrics") 66 # Split into 3 due to size constraints 67 emit_metric( 68 "td_results_final_test_prioritizations", 69 {"test_prioritizations": test_prioritizations.to_json()}, 70 ) 71 emit_metric( 72 "td_results_aggregated_heuristics", 73 {"aggregated_heuristics": aggregated_heuristics.to_json()}, 74 ) 75 76 with open(REPO_ROOT / "td_results.json", "w") as f: 77 f.write(json.dumps(test_prioritizations.to_json())) 78 79 80if __name__ == "__main__": 81 main() 82