Skip to content

Commit 2042461

Browse files
Merge branch 'fix/invalid-api-key/stop-optimization' of https://github.com/codeflash-ai/codeflash into fix/invalid-api-key/stop-optimization
2 parents f1ba78e + 86bcbc6 commit 2042461

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,19 @@ def exit_with_message(message: str, *, error_on_exit: bool = False) -> None:
355355
paneled_text(message, panel_args={"style": "red"})
356356

357357
sys.exit(1 if error_on_exit else 0)
358+
359+
360+
def extract_unique_errors(pytest_output: str) -> set[str]:
361+
unique_errors = set()
362+
363+
# Regex pattern to match error lines:
364+
# - Start with 'E' followed by optional whitespace
365+
# - Capture the actual error message
366+
pattern = r"^E\s+(.*)$"
367+
368+
for error_message in re.findall(pattern, pytest_output, re.MULTILINE):
369+
error_message = error_message.strip() # noqa: PLW2901
370+
if error_message:
371+
unique_errors.add(error_message)
372+
373+
return unique_errors

codeflash/optimization/function_optimizer.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
replace_function_definitions_in_module,
3131
)
3232
from codeflash.code_utils.code_utils import (
33-
ImportErrorPattern,
3433
cleanup_paths,
3534
create_rank_dictionary_compact,
3635
diff_length,
36+
extract_unique_errors,
3737
file_name_from_test_module_name,
3838
get_run_tmp_file,
3939
module_name_from_file_path,
@@ -1583,11 +1583,14 @@ def establish_original_code_baseline(
15831583
)
15841584
if not behavioral_results:
15851585
logger.warning(
1586-
f"force_lsp|Couldn't run any tests for original function {self.function_to_optimize.function_name}. SKIPPING OPTIMIZING THIS FUNCTION."
1586+
f"force_lsp|Couldn't run any tests for original function {self.function_to_optimize.function_name}. Skipping optimization."
15871587
)
15881588
console.rule()
15891589
return Failure("Failed to establish a baseline for the original code - bevhavioral tests failed.")
15901590
if not coverage_critic(coverage_results, self.args.test_framework):
1591+
did_pass_all_tests = all(result.did_pass for result in behavioral_results)
1592+
if not did_pass_all_tests:
1593+
return Failure("Tests failed to pass for the original code.")
15911594
return Failure(
15921595
f"Test coverage is {coverage_results.coverage}%, which is below the required threshold of {COVERAGE_THRESHOLD}%."
15931596
)
@@ -1951,12 +1954,19 @@ def run_and_parse_tests(
19511954
f"stdout: {run_result.stdout}\n"
19521955
f"stderr: {run_result.stderr}\n"
19531956
)
1954-
if "ModuleNotFoundError" in run_result.stdout:
1957+
1958+
unique_errors = extract_unique_errors(run_result.stdout)
1959+
1960+
if unique_errors:
19551961
from rich.text import Text
19561962

1957-
match = ImportErrorPattern.search(run_result.stdout).group()
1958-
panel = Panel(Text.from_markup(f"⚠️ {match} ", style="bold red"), expand=False)
1959-
console.print(panel)
1963+
for error in unique_errors:
1964+
if is_LSP_enabled():
1965+
lsp_log(LspCodeMessage(code=error, file_name="errors"))
1966+
else:
1967+
panel = Panel(Text.from_markup(f"⚠️ {error} ", style="bold red"), expand=False)
1968+
console.print(panel)
1969+
19601970
if testing_type in {TestingMode.BEHAVIOR, TestingMode.PERFORMANCE}:
19611971
results, coverage_results = parse_test_results(
19621972
test_xml_path=result_file_path,

0 commit comments

Comments
 (0)