Skip to content

Commit 7418770

Browse files
Merge branch 'main' into cleanup-the-right-files
2 parents 1fc4614 + 132b92d commit 7418770

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

codeflash/optimization/optimizer.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from codeflash.api.aiservice import AiServiceClient, LocalAiServiceClient
1313
from codeflash.cli_cmds.console import console, logger, progress_bar
1414
from codeflash.code_utils import env_utils
15+
from codeflash.code_utils.code_utils import cleanup_paths
1516
from codeflash.code_utils.env_utils import get_pr_number
1617
from codeflash.either import is_successful
1718
from codeflash.models.models import ValidCode
@@ -248,10 +249,10 @@ def run(self) -> None:
248249
return
249250
if not env_utils.check_formatter_installed(self.args.formatter_cmds):
250251
return
251-
252252
if self.args.no_draft and is_pr_draft():
253253
logger.warning("PR is in draft mode, skipping optimization")
254254
return
255+
cleanup_paths(Optimizer.find_leftover_instrumented_test_files(self.test_cfg.tests_root))
255256

256257
function_optimizer = None
257258
file_to_funcs_to_optimize, num_optimizable_functions = self.get_optimizable_functions()
@@ -331,9 +332,27 @@ def run(self) -> None:
331332

332333
self.cleanup_temporary_paths()
333334

334-
def cleanup_temporary_paths(self) -> None:
335-
from codeflash.code_utils.code_utils import cleanup_paths
335+
@staticmethod
336+
def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]:
337+
"""Search for all paths within the test_root that match the following patterns.
336338
339+
- 'test.*__perf_test_{0,1}.py'
340+
- 'test_.*__unit_test_{0,1}.py'
341+
- 'test_.*__perfinstrumented.py'
342+
- 'test_.*__perfonlyinstrumented.py'
343+
Returns a list of matching file paths.
344+
"""
345+
import re
346+
347+
pattern = re.compile(
348+
r"(?:test.*__perf_test_\d?\.py|test_.*__unit_test_\d?\.py|test_.*__perfinstrumented\.py|test_.*__perfonlyinstrumented\.py)$"
349+
)
350+
351+
return [
352+
file_path for file_path in test_root.rglob("*") if file_path.is_file() and pattern.match(file_path.name)
353+
]
354+
355+
def cleanup_temporary_paths(self) -> None:
337356
if self.current_function_optimizer:
338357
self.current_function_optimizer.cleanup_generated_files()
339358

0 commit comments

Comments
 (0)