(
code: str, inputs: List[List[Any]], entry_point: str, mode="branch"
)
| 78 | |
| 79 | |
| 80 | def test_code_coverage( |
| 81 | code: str, inputs: List[List[Any]], entry_point: str, mode="branch" |
| 82 | ): |
| 83 | def safety_test(code: str, inputs: List[List[Any]], entry_point: str): |
| 84 | for input_list in inputs: |
| 85 | code += f"{entry_point}({construct_inputs_sig(input_list)})\n" |
| 86 | reliability_guard() |
| 87 | try: |
| 88 | with swallow_io(): |
| 89 | with time_limit(1): |
| 90 | exec(code, {}) |
| 91 | except: |
| 92 | sys.exit(1) |
| 93 | |
| 94 | p = multiprocessing.Process(target=safety_test, args=(code, inputs, entry_point)) |
| 95 | p.start() |
| 96 | p.join() |
| 97 | safe = p.exitcode == 0 |
| 98 | if p.is_alive(): |
| 99 | p.terminate() |
| 100 | p.kill() |
| 101 | if not safe: |
| 102 | print("Potentially dangerous code, refuse coverage test.") |
| 103 | return None |
| 104 | |
| 105 | with open("tmp_src.py", "w") as f: |
| 106 | f.write(code) |
| 107 | import tmp_src |
| 108 | |
| 109 | importlib.reload(tmp_src) |
| 110 | func = getattr(tmp_src, f"{entry_point}", None) |
| 111 | assert func != None, f"{entry_point = } not exist" |
| 112 | |
| 113 | cov = coverage.Coverage(branch=True) |
| 114 | cov.start() |
| 115 | with swallow_io(): |
| 116 | for input_list in inputs: |
| 117 | func(*input_list) |
| 118 | cov.stop() |
| 119 | with Capturing() as outputs: |
| 120 | cov.lcov_report(outfile="-") |
| 121 | |
| 122 | ret = parse_lcov(outputs, func, mode) |
| 123 | |
| 124 | os.remove("tmp_src.py") |
| 125 | return ret |
| 126 | |
| 127 | |
| 128 | def test_solution_coverage( |
no test coverage detected