mirror of
https://github.com/codeflash-ai/codeflash.git
synced 2026-05-04 18:25:17 +00:00
fix: pass language to format_generated_code for correct temp file extension
format_generated_code was called without the language parameter in process_review(), defaulting to "python". This created temp files with .py extension when formatting JS/TS code, causing prettier to fail. Trace IDs: 11e9745d, 1578f081, 7e8abab2 (73 affected logs total) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
6f6c0398b3
commit
f95a06afe6
2 changed files with 59 additions and 2 deletions
|
|
@ -2514,13 +2514,15 @@ class FunctionOptimizer:
|
||||||
for test_file, count in map_gen_test_file_to_no_of_tests.items()
|
for test_file, count in map_gen_test_file_to_no_of_tests.items()
|
||||||
):
|
):
|
||||||
formatted_generated_test = format_generated_code(
|
formatted_generated_test = format_generated_code(
|
||||||
test.generated_original_test_source, self.args.formatter_cmds
|
test.generated_original_test_source, self.args.formatter_cmds, language=code_lang
|
||||||
)
|
)
|
||||||
generated_tests_str += f"```{code_lang}\n{formatted_generated_test}\n```"
|
generated_tests_str += f"```{code_lang}\n{formatted_generated_test}\n```"
|
||||||
generated_tests_str += "\n\n"
|
generated_tests_str += "\n\n"
|
||||||
|
|
||||||
if concolic_test_str:
|
if concolic_test_str:
|
||||||
formatted_generated_test = format_generated_code(concolic_test_str, self.args.formatter_cmds)
|
formatted_generated_test = format_generated_code(
|
||||||
|
concolic_test_str, self.args.formatter_cmds, language=code_lang
|
||||||
|
)
|
||||||
generated_tests_str += f"```{code_lang}\n{formatted_generated_test}\n```\n\n"
|
generated_tests_str += f"```{code_lang}\n{formatted_generated_test}\n```\n\n"
|
||||||
|
|
||||||
existing_tests, replay_tests, _concolic_tests = existing_tests_source_for(
|
existing_tests, replay_tests, _concolic_tests = existing_tests_source_for(
|
||||||
|
|
|
||||||
|
|
@ -1394,6 +1394,61 @@ def test_format_generated_code_unicode():
|
||||||
assert "Hello, 世界! 🌍" in result
|
assert "Hello, 世界! 🌍" in result
|
||||||
|
|
||||||
|
|
||||||
|
def test_format_generated_code_uses_correct_extension_for_javascript():
|
||||||
|
"""Test that format_generated_code creates temp files with .js extension for JavaScript code."""
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
js_code = """function test() {
|
||||||
|
return 42;
|
||||||
|
}"""
|
||||||
|
|
||||||
|
captured_paths = []
|
||||||
|
original_apply = format_code.__module__
|
||||||
|
|
||||||
|
with patch("codeflash.code_utils.formatter.apply_formatter_cmds") as mock_apply:
|
||||||
|
mock_apply.return_value = (Path("/tmp/temp.js"), js_code, False)
|
||||||
|
format_generated_code(js_code, ["npx prettier --write $file"], language="javascript")
|
||||||
|
# Verify the temp file path has .js extension
|
||||||
|
call_args = mock_apply.call_args
|
||||||
|
original_temp_path = call_args[0][1] # second positional arg is the path
|
||||||
|
assert original_temp_path.suffix == ".js", (
|
||||||
|
f"Expected .js extension for JavaScript, got {original_temp_path.suffix}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_format_generated_code_uses_correct_extension_for_typescript():
|
||||||
|
"""Test that format_generated_code creates temp files with .ts extension for TypeScript code."""
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
ts_code = """function test(): number {
|
||||||
|
return 42;
|
||||||
|
}"""
|
||||||
|
|
||||||
|
with patch("codeflash.code_utils.formatter.apply_formatter_cmds") as mock_apply:
|
||||||
|
mock_apply.return_value = (Path("/tmp/temp.ts"), ts_code, False)
|
||||||
|
format_generated_code(ts_code, ["npx prettier --write $file"], language="typescript")
|
||||||
|
call_args = mock_apply.call_args
|
||||||
|
original_temp_path = call_args[0][1]
|
||||||
|
assert original_temp_path.suffix == ".ts", (
|
||||||
|
f"Expected .ts extension for TypeScript, got {original_temp_path.suffix}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_format_generated_code_defaults_to_py_extension():
|
||||||
|
"""Test that format_generated_code defaults to .py extension when no language specified."""
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
py_code = """def test():
|
||||||
|
return 42"""
|
||||||
|
|
||||||
|
with patch("codeflash.code_utils.formatter.apply_formatter_cmds") as mock_apply:
|
||||||
|
mock_apply.return_value = (Path("/tmp/temp.py"), py_code, False)
|
||||||
|
format_generated_code(py_code, ["black $file"])
|
||||||
|
call_args = mock_apply.call_args
|
||||||
|
original_temp_path = call_args[0][1]
|
||||||
|
assert original_temp_path.suffix == ".py", f"Expected .py extension for Python, got {original_temp_path.suffix}"
|
||||||
|
|
||||||
|
|
||||||
def test_format_generated_code_f_strings():
|
def test_format_generated_code_f_strings():
|
||||||
"""Test format_generated_code with f-strings."""
|
"""Test format_generated_code with f-strings."""
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue