From 17730663ecbc9380be01e301a0b9506965e57251 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Wed, 4 Mar 2026 02:12:18 -0500 Subject: [PATCH 1/5] fix: close SQLite connections in finally blocks for Windows compatibility Ensures SQLite connections are always closed before file cleanup to prevent PermissionError on Windows where open handles block file deletion. --- tests/test_trace_benchmarks.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/tests/test_trace_benchmarks.py b/tests/test_trace_benchmarks.py index 37b75afe4..a13a42c20 100644 --- a/tests/test_trace_benchmarks.py +++ b/tests/test_trace_benchmarks.py @@ -1,6 +1,5 @@ import shutil import sqlite3 -import time from pathlib import Path import pytest @@ -121,8 +120,8 @@ def test_trace_benchmarks() -> None: assert actual[4] == expected[4], f"Mismatch at index {idx} for benchmark_function_name" assert actual[5] == expected[5], f"Mismatch at index {idx} for benchmark_module_path" assert actual[6] == expected[6], f"Mismatch at index {idx} for benchmark_line_number" - # Close connection conn.close() + conn = None generate_replay_test(output_file, replay_tests_dir) test_class_sort_path = replay_tests_dir / Path( "test_tests_pytest_benchmarks_test_test_benchmark_bubble_sort_example__replay_test_0.py" @@ -217,7 +216,8 @@ def test_code_to_optimize_bubble_sort_codeflash_trace_sorter_test_no_func(): """ assert test_sort_path.read_text("utf-8").strip() == test_sort_code.strip() finally: - # cleanup + if conn is not None: + conn.close() output_file.unlink(missing_ok=True) shutil.rmtree(replay_tests_dir) @@ -244,8 +244,6 @@ def test_trace_multithreaded_benchmark() -> None: ) function_calls = cursor.fetchall() - conn.close() - # Assert the length of function calls assert len(function_calls) == 10, f"Expected 10 function calls, but got {len(function_calls)}" function_benchmark_timings = codeflash_benchmark_plugin.get_function_benchmark_timings(output_file) @@ -281,11 +279,8 @@ def test_trace_multithreaded_benchmark() -> None: assert actual[4] == expected[4], f"Mismatch at index {idx} for benchmark_function_name" assert actual[5] == expected[5], f"Mismatch at index {idx} for benchmark_module_path" assert actual[6] == expected[6], f"Mismatch at index {idx} for benchmark_line_number" - # Close connection - conn.close() - finally: - # cleanup + conn.close() output_file.unlink(missing_ok=True) @@ -352,11 +347,6 @@ def test_trace_benchmark_decorator() -> None: assert Path(actual[3]).name == Path(expected[3]).name, f"Mismatch at index {idx} for file_path" assert actual[4] == expected[4], f"Mismatch at index {idx} for benchmark_function_name" assert actual[5] == expected[5], f"Mismatch at index {idx} for benchmark_module_path" - # Close connection - cursor.close() - conn.close() - time.sleep(2) finally: - # cleanup + conn.close() output_file.unlink(missing_ok=True) - time.sleep(1) From 59ec38eec455328000df14acb5ec286b51e628fb Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2026 07:15:22 +0000 Subject: [PATCH 2/5] fix: resolve mypy type error for conn variable in test_trace_benchmarks Initialize conn as Optional before try block to allow None assignment. Co-authored-by: Kevin Turcios --- tests/test_trace_benchmarks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_trace_benchmarks.py b/tests/test_trace_benchmarks.py index a13a42c20..34c6a7d0b 100644 --- a/tests/test_trace_benchmarks.py +++ b/tests/test_trace_benchmarks.py @@ -17,6 +17,7 @@ def test_trace_benchmarks() -> None: replay_tests_dir = benchmarks_root / "codeflash_replay_tests" tests_root = project_root / "tests" output_file = (benchmarks_root / Path("test_trace_benchmarks.trace")).resolve() + conn: sqlite3.Connection | None = None trace_benchmarks_pytest(benchmarks_root, tests_root, project_root, output_file) assert output_file.exists() try: From 2faef8ade6b7dfea2e131514a727f9026b1e4b0e Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Wed, 4 Mar 2026 02:26:53 -0500 Subject: [PATCH 3/5] fix: update JS max_loops test assertion to match constant (1_000) PR #1764 changed JS_BENCHMARKING_MAX_LOOPS from 100_000 to 1_000 but the test was updated to assert 5_000 instead of 1_000. --- tests/languages/javascript/test_support_dispatch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/languages/javascript/test_support_dispatch.py b/tests/languages/javascript/test_support_dispatch.py index 3eee21017..427b9b8ba 100644 --- a/tests/languages/javascript/test_support_dispatch.py +++ b/tests/languages/javascript/test_support_dispatch.py @@ -182,9 +182,9 @@ def test_passes_loop_parameters(self, mock_vitest_runner: MagicMock, js_support: call_kwargs = mock_vitest_runner.call_args.kwargs assert call_kwargs["min_loops"] == 10 - # JS/TS uses JS_BENCHMARKING_MAX_LOOPS (5_000) regardless of passed value + # JS/TS uses JS_BENCHMARKING_MAX_LOOPS (1_000) regardless of passed value # Actual loop count is limited by target_duration, not max_loops - assert call_kwargs["max_loops"] == 5_000 + assert call_kwargs["max_loops"] == 1_000 assert call_kwargs["target_duration_ms"] == 5000 From 887e6384b6b4bf43973884722bc0bcd999c552df Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2026 07:37:03 +0000 Subject: [PATCH 4/5] fix: apply consistent conn safety pattern in trace benchmark tests Initialize conn to None before try blocks and guard finally with if conn is not None to prevent NameError if sqlite3.connect() raises. Co-authored-by: Kevin Turcios --- tests/test_trace_benchmarks.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_trace_benchmarks.py b/tests/test_trace_benchmarks.py index 34c6a7d0b..4e0f7be47 100644 --- a/tests/test_trace_benchmarks.py +++ b/tests/test_trace_benchmarks.py @@ -232,6 +232,7 @@ def test_trace_multithreaded_benchmark() -> None: output_file = (benchmarks_root / Path("test_trace_benchmarks.trace")).resolve() trace_benchmarks_pytest(benchmarks_root, tests_root, project_root, output_file) assert output_file.exists() + conn: sqlite3.Connection | None = None try: # check contents of trace file # connect to database @@ -281,7 +282,8 @@ def test_trace_multithreaded_benchmark() -> None: assert actual[5] == expected[5], f"Mismatch at index {idx} for benchmark_module_path" assert actual[6] == expected[6], f"Mismatch at index {idx} for benchmark_line_number" finally: - conn.close() + if conn is not None: + conn.close() output_file.unlink(missing_ok=True) @@ -292,6 +294,7 @@ def test_trace_benchmark_decorator() -> None: output_file = (benchmarks_root / Path("test_trace_benchmarks.trace")).resolve() trace_benchmarks_pytest(benchmarks_root, tests_root, project_root, output_file) assert output_file.exists() + conn: sqlite3.Connection | None = None try: # check contents of trace file # connect to database @@ -349,5 +352,6 @@ def test_trace_benchmark_decorator() -> None: assert actual[4] == expected[4], f"Mismatch at index {idx} for benchmark_function_name" assert actual[5] == expected[5], f"Mismatch at index {idx} for benchmark_module_path" finally: - conn.close() + if conn is not None: + conn.close() output_file.unlink(missing_ok=True) From 77b705d2c7043d3cdf3e38b673938e9d65ba4df4 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2026 08:06:15 +0000 Subject: [PATCH 5/5] fix: use forward slashes in jest-reporter test paths for Windows compatibility Windows backslashes in paths embedded into JavaScript strings are interpreted as escape sequences by Node.js, corrupting the module path. Use .as_posix() to emit forward slashes which Node accepts on all platforms. Co-authored-by: Kevin Turcios --- tests/test_languages/test_javascript_test_runner.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_languages/test_javascript_test_runner.py b/tests/test_languages/test_javascript_test_runner.py index 9773578fb..7dccaa332 100644 --- a/tests/test_languages/test_javascript_test_runner.py +++ b/tests/test_languages/test_javascript_test_runner.py @@ -907,15 +907,17 @@ def test_reporter_produces_valid_junit_xml(self): # Create a Node.js script that exercises the reporter with mock data test_script = Path(tmpdir) / "test_reporter.js" + reporter_path_js = reporter_path.as_posix() + output_file_js = output_file.as_posix() test_script.write_text(f""" // Set env vars BEFORE requiring reporter (matches real Jest behavior) -process.env.JEST_JUNIT_OUTPUT_FILE = '{output_file}'; +process.env.JEST_JUNIT_OUTPUT_FILE = '{output_file_js}'; process.env.JEST_JUNIT_CLASSNAME = '{{filepath}}'; process.env.JEST_JUNIT_SUITE_NAME = '{{filepath}}'; process.env.JEST_JUNIT_ADD_FILE_ATTRIBUTE = 'true'; process.env.JEST_JUNIT_INCLUDE_CONSOLE_OUTPUT = 'true'; -const Reporter = require('{reporter_path}'); +const Reporter = require('{reporter_path_js}'); // Mock Jest globalConfig const globalConfig = {{ rootDir: '/tmp/project' }}; @@ -960,7 +962,7 @@ def test_reporter_produces_valid_junit_xml(self): reporter.onRunComplete([], results); console.log('OK'); -""") +""", encoding="utf-8") result = subprocess.run( ["node", str(test_script)],