Skip to content

Commit 47f6c02

Browse files
authored
Merge pull request #265 from codeflash-ai/error-output-nonverbose
Rich error output during baseline establishment (CF-646)
2 parents ae63d4a + 9d8e0a2 commit 47f6c02

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from codeflash.cli_cmds.console import logger
1616
from codeflash.code_utils.config_parser import find_pyproject_toml
1717

18+
ImportErrorPattern = re.compile(r"ModuleNotFoundError.*$", re.MULTILINE)
19+
1820

1921
@contextmanager
2022
def custom_addopts() -> None:

codeflash/discovery/discover_unit_tests.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414

1515
import pytest
1616
from pydantic.dataclasses import dataclass
17+
from rich.panel import Panel
18+
from rich.text import Text
1719

1820
from codeflash.cli_cmds.console import console, logger, test_files_progress_bar
19-
from codeflash.code_utils.code_utils import custom_addopts, get_run_tmp_file, module_name_from_file_path
21+
from codeflash.code_utils.code_utils import (
22+
ImportErrorPattern,
23+
custom_addopts,
24+
get_run_tmp_file,
25+
module_name_from_file_path,
26+
)
2027
from codeflash.code_utils.compat import SAFE_SYS_EXECUTABLE, codeflash_cache_db
2128
from codeflash.models.models import CodePosition, FunctionCalledInTest, TestsInFile, TestType
2229

@@ -180,6 +187,10 @@ def discover_tests_pytest(
180187
logger.warning(
181188
f"Failed to collect tests. Pytest Exit code: {exitcode}={pytest.ExitCode(exitcode).name}\n {error_section}"
182189
)
190+
if "ModuleNotFoundError" in result.stdout:
191+
match = ImportErrorPattern.search(result.stdout).group()
192+
panel = Panel(Text.from_markup(f"⚠️ {match} ", style="bold red"), expand=False)
193+
console.print(panel)
183194

184195
elif 0 <= exitcode <= 5:
185196
logger.warning(f"Failed to collect tests. Pytest Exit code: {exitcode}={pytest.ExitCode(exitcode).name}")

codeflash/optimization/function_optimizer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from codeflash.code_utils import env_utils
2424
from codeflash.code_utils.code_replacer import replace_function_definitions_in_module
2525
from codeflash.code_utils.code_utils import (
26+
ImportErrorPattern,
2627
cleanup_paths,
2728
file_name_from_test_module_name,
2829
get_run_tmp_file,
@@ -1192,6 +1193,12 @@ def run_and_parse_tests(
11921193
f"stdout: {run_result.stdout}\n"
11931194
f"stderr: {run_result.stderr}\n"
11941195
)
1196+
if "ModuleNotFoundError" in run_result.stdout:
1197+
from rich.text import Text
1198+
1199+
match = ImportErrorPattern.search(run_result.stdout).group()
1200+
panel = Panel(Text.from_markup(f"⚠️ {match} ", style="bold red"), expand=False)
1201+
console.print(panel)
11951202
if testing_type in {TestingMode.BEHAVIOR, TestingMode.PERFORMANCE}:
11961203
results, coverage_results = parse_test_results(
11971204
test_xml_path=result_file_path,

tests/test_test_runner.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import re
2+
13
import os
24
import tempfile
35
from pathlib import Path
46

7+
from codeflash.code_utils.code_utils import ImportErrorPattern
58
from codeflash.models.models import TestFile, TestFiles, TestType
69
from codeflash.verification.parse_test_output import parse_test_xml
710
from codeflash.verification.test_runner import run_behavioral_tests
@@ -96,3 +99,51 @@ def test_sort():
9699
)
97100
assert results[0].did_pass, "Test did not pass as expected"
98101
result_file.unlink(missing_ok=True)
102+
103+
code = """import torch
104+
def sorter(arr):
105+
print(torch.ones(1))
106+
arr.sort()
107+
return arr
108+
109+
def test_sort():
110+
arr = [5, 4, 3, 2, 1, 0]
111+
output = sorter(arr)
112+
assert output == [0, 1, 2, 3, 4, 5]
113+
"""
114+
cur_dir_path = Path(__file__).resolve().parent
115+
config = TestConfig(
116+
tests_root=cur_dir_path,
117+
project_root_path=cur_dir_path,
118+
test_framework="pytest",
119+
tests_project_rootdir=cur_dir_path.parent,
120+
)
121+
122+
test_env = os.environ.copy()
123+
test_env["CODEFLASH_TEST_ITERATION"] = "0"
124+
test_env["CODEFLASH_TRACER_DISABLE"] = "1"
125+
if "PYTHONPATH" not in test_env:
126+
test_env["PYTHONPATH"] = str(config.project_root_path)
127+
else:
128+
test_env["PYTHONPATH"] += os.pathsep + str(config.project_root_path)
129+
130+
with tempfile.NamedTemporaryFile(prefix="test_xx", suffix=".py", dir=cur_dir_path) as fp:
131+
test_files = TestFiles(
132+
test_files=[TestFile(instrumented_behavior_file_path=Path(fp.name), test_type=TestType.EXISTING_UNIT_TEST)]
133+
)
134+
fp.write(code.encode("utf-8"))
135+
fp.flush()
136+
result_file, process, _, _ = run_behavioral_tests(
137+
test_files,
138+
test_framework=config.test_framework,
139+
cwd=Path(config.project_root_path),
140+
test_env=test_env,
141+
pytest_timeout=1,
142+
pytest_target_runtime_seconds=1,
143+
)
144+
results = parse_test_xml(
145+
test_xml_file_path=result_file, test_files=test_files, test_config=config, run_result=process
146+
)
147+
match = ImportErrorPattern.search(process.stdout).group()
148+
assert match=="ModuleNotFoundError: No module named 'torch'"
149+
result_file.unlink(missing_ok=True)

0 commit comments

Comments
 (0)