MCPcopy Index your code
hub / github.com/evalplus/evalplus / test_code_coverage

Function test_code_coverage

tools/_experimental/evaluate_coverage.py:80–125  ·  view source on GitHub ↗
(
    code: str, inputs: List[List[Any]], entry_point: str, mode="branch"
)

Source from the content-addressed store, hash-verified

78
79
80def 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
128def test_solution_coverage(

Callers 1

test_solution_coverageFunction · 0.70

Calls 4

swallow_ioFunction · 0.90
funcFunction · 0.85
CapturingClass · 0.70
parse_lcovFunction · 0.70

Tested by

no test coverage detected