refactor: remove redundant try/finally; rely on conftest autouse fixture for language cleanup

The conftest.py autouse fixture already resets _current_language before/after
each test, making per-test try/finally cleanup unnecessary.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
This commit is contained in:
claude[bot] 2026-03-18 18:23:49 +00:00
parent 0672e11342
commit 4c9abfb2aa

View file

@ -4,58 +4,54 @@ from tempfile import TemporaryDirectory
from codeflash.languages import set_current_language
from codeflash.languages.base import Language
from codeflash.languages.current import reset_current_language
from codeflash.languages.java.line_profiler import JavaLineProfiler
def test_parse_line_profile_results_non_python_java_json():
set_current_language(Language.JAVA)
try:
with TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
source_file = tmp_path / "Util.java"
source_file.write_text(
"""public class Util {
with TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
source_file = tmp_path / "Util.java"
source_file.write_text(
"""public class Util {
public static int f() {
int x = 1;
return x;
}
}
""",
encoding="utf-8",
)
profile_file = tmp_path / "line_profiler_output.json"
profile_data = {
f"{source_file.as_posix()}:3": {
"hits": 6,
"time": 1000,
"file": source_file.as_posix(),
"line": 3,
"content": "int x = 1;",
},
f"{source_file.as_posix()}:4": {
"hits": 6,
"time": 2000,
"file": source_file.as_posix(),
"line": 4,
"content": "return x;",
},
}
profile_file.write_text(json.dumps(profile_data), encoding="utf-8")
results = JavaLineProfiler.parse_results(profile_file)
assert results["unit"] == 1e-9
assert results["str_out"] == (
"# Timer unit: 1e-09 s\n"
"## Function: Util.java\n"
"## Total time: 3e-06 s\n"
"| Hits | Time | Per Hit | % Time | Line Contents |\n"
"|-------:|-------:|----------:|---------:|:----------------|\n"
"| 6 | 1000 | 166.7 | 33.3 | int x = 1; |\n"
"| 6 | 2000 | 333.3 | 66.7 | return x; |\n"
encoding="utf-8",
)
assert (source_file.as_posix(), 3, "Util.java") in results["timings"]
assert results["timings"][(source_file.as_posix(), 3, "Util.java")] == [(3, 6, 1000), (4, 6, 2000)]
finally:
reset_current_language()
profile_file = tmp_path / "line_profiler_output.json"
profile_data = {
f"{source_file.as_posix()}:3": {
"hits": 6,
"time": 1000,
"file": source_file.as_posix(),
"line": 3,
"content": "int x = 1;",
},
f"{source_file.as_posix()}:4": {
"hits": 6,
"time": 2000,
"file": source_file.as_posix(),
"line": 4,
"content": "return x;",
},
}
profile_file.write_text(json.dumps(profile_data), encoding="utf-8")
results = JavaLineProfiler.parse_results(profile_file)
assert results["unit"] == 1e-9
assert results["str_out"] == (
"# Timer unit: 1e-09 s\n"
"## Function: Util.java\n"
"## Total time: 3e-06 s\n"
"| Hits | Time | Per Hit | % Time | Line Contents |\n"
"|-------:|-------:|----------:|---------:|:----------------|\n"
"| 6 | 1000 | 166.7 | 33.3 | int x = 1; |\n"
"| 6 | 2000 | 333.3 | 66.7 | return x; |\n"
)
assert (source_file.as_posix(), 3, "Util.java") in results["timings"]
assert results["timings"][(source_file.as_posix(), 3, "Util.java")] == [(3, 6, 1000), (4, 6, 2000)]