Skip to content

Commit 132b92d

Browse files
Merge pull request #454 from codeflash-ai/just-clean-all-leftover-instrumented-tests
Just clean all leftover instrumented tests
2 parents 2185af9 + a756e56 commit 132b92d

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()
@@ -326,9 +327,27 @@ def run(self) -> None:
326327

327328
self.cleanup_temporary_paths()
328329

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

0 commit comments

Comments
 (0)