fix: reset language singleton in test to prevent cross-test pollution
test_parse_line_profile_results_non_python_java_json set Language.JAVA but never reset it, causing test_java_diff_ignored_when_language_is_python to fail when tests ran in this order. Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
This commit is contained in:
parent
c1b8029667
commit
0672e11342
1 changed files with 43 additions and 40 deletions
|
|
@ -4,55 +4,58 @@ 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)
|
||||
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
tmp_path = Path(tmpdir)
|
||||
source_file = tmp_path / "Util.java"
|
||||
source_file.write_text(
|
||||
"""public class Util {
|
||||
try:
|
||||
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",
|
||||
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"
|
||||
)
|
||||
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)]
|
||||
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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue