Skip to content

Commit 16d21e7

Browse files
committed
reinsert
1 parent 7278ab3 commit 16d21e7

18 files changed

+271
-218
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ def get_run_tmp_file(file_path: Path) -> Path:
228228

229229

230230
def path_belongs_to_site_packages(file_path: Path) -> bool:
231+
file_path_resolved = file_path.resolve()
231232
site_packages = [Path(p) for p in site.getsitepackages()]
232-
return any(file_path.resolve().is_relative_to(site_package_path) for site_package_path in site_packages)
233+
return any(file_path_resolved.is_relative_to(site_package_path) for site_package_path in site_packages)
233234

234235

235236
def is_class_defined_in_file(class_name: str, file_path: Path) -> bool:

codeflash/code_utils/env_utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool =
1818
if formatter_cmds[0] == "disabled":
1919
return return_code
2020
tmp_code = """print("hello world")"""
21-
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", suffix=".py") as f:
22-
f.write(tmp_code)
23-
f.flush()
24-
tmp_file = Path(f.name)
21+
with tempfile.TemporaryDirectory() as tmpdir:
22+
tmp_file = Path(tmpdir) / "test_codeflash_formatter.py"
23+
tmp_file.write_text(tmp_code, encoding="utf-8")
2524
try:
2625
format_code(formatter_cmds, tmp_file, print_status=False, exit_on_failure=exit_on_failure)
2726
except Exception:
2827
exit_with_message(
2928
"⚠️ Codeflash requires a code formatter to be installed in your environment, but none was found. Please install a supported formatter, verify the formatter-cmds in your codeflash pyproject.toml config and try again.",
3029
error_on_exit=True,
3130
)
32-
return return_code
31+
return return_code
3332

3433

3534
@lru_cache(maxsize=1)

codeflash/code_utils/instrument_existing_tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ def visit_FunctionDef(self, node: ast.FunctionDef, test_class_name: str | None =
212212
args=[
213213
ast.JoinedStr(
214214
values=[
215-
ast.Constant(value=f"{get_run_tmp_file(Path('test_return_values_'))}"),
215+
ast.Constant(
216+
value=f"{get_run_tmp_file(Path('test_return_values_')).as_posix()}"
217+
),
216218
ast.FormattedValue(
217219
value=ast.Name(id="codeflash_iteration", ctx=ast.Load()),
218220
conversion=-1,

codeflash/models/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def markdown(self) -> str:
165165
"""Returns the markdown representation of the code, including the file path where possible."""
166166
return "\n".join(
167167
[
168-
f"```python{':' + str(code_string.file_path) if code_string.file_path else ''}\n{code_string.code.strip()}\n```"
168+
f"```python{':' + code_string.file_path.as_posix() if code_string.file_path else ''}\n{code_string.code.strip()}\n```"
169169
for code_string in self.code_strings
170170
]
171171
)

codeflash/verification/instrument_codeflash_capture.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def instrument_codeflash_capture(
3333
modified_code = add_codeflash_capture_to_init(
3434
target_classes={class_parent.name},
3535
fto_name=function_to_optimize.function_name,
36-
tmp_dir_path=str(get_run_tmp_file(Path("test_return_values"))),
36+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
3737
code=original_code,
3838
tests_root=tests_root,
3939
is_fto=True,
@@ -46,7 +46,7 @@ def instrument_codeflash_capture(
4646
modified_code = add_codeflash_capture_to_init(
4747
target_classes=helper_classes,
4848
fto_name=function_to_optimize.function_name,
49-
tmp_dir_path=str(get_run_tmp_file(Path("test_return_values"))),
49+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
5050
code=original_code,
5151
tests_root=tests_root,
5252
is_fto=False,
@@ -124,7 +124,7 @@ def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
124124
keywords=[
125125
ast.keyword(arg="function_name", value=ast.Constant(value=f"{node.name}.__init__")),
126126
ast.keyword(arg="tmp_dir_path", value=ast.Constant(value=self.tmp_dir_path)),
127-
ast.keyword(arg="tests_root", value=ast.Constant(value=str(self.tests_root))),
127+
ast.keyword(arg="tests_root", value=ast.Constant(value=self.tests_root.as_posix())),
128128
ast.keyword(arg="is_fto", value=ast.Constant(value=self.is_fto)),
129129
],
130130
)

tests/test_code_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def test_get_run_tmp_file_reuses_temp_directory() -> None:
254254

255255

256256
def test_path_belongs_to_site_packages_with_site_package_path(monkeypatch: pytest.MonkeyPatch) -> None:
257-
site_packages = [Path("/usr/local/lib/python3.9/site-packages")]
257+
site_packages = [Path("/usr/local/lib/python3.9/site-packages").resolve()]
258258
monkeypatch.setattr(site, "getsitepackages", lambda: site_packages)
259259

260260
file_path = Path("/usr/local/lib/python3.9/site-packages/some_package")
@@ -465,4 +465,4 @@ def another_function():
465465
pass
466466
"""
467467
result = has_any_async_functions(code)
468-
assert result is False
468+
assert result is False

tests/test_codeflash_capture.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_example_test_3(self):
4242
class MyClass:
4343
def __init__(self):
4444
self.x = 2
45-
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
45+
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.as_posix()}')}}|TEST_INFO_END")
4646
"""
4747
test_file_name = "test_stack_info_temp.py"
4848

@@ -117,7 +117,7 @@ def test_example_test_3(self):
117117
class MyClass:
118118
def __init__(self):
119119
self.x = 2
120-
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
120+
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.as_posix()}')}}|TEST_INFO_END")
121121
"""
122122
test_file_name = "test_stack_info_temp.py"
123123

@@ -181,7 +181,7 @@ def test_example_test_3(self):
181181
class MyClass:
182182
def __init__(self):
183183
self.x = 2
184-
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
184+
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.as_posix()}')}}|TEST_INFO_END")
185185
"""
186186
test_dir = (Path(__file__).parent.parent / "code_to_optimize" / "tests" / "pytest").resolve()
187187
test_file_name = "test_stack_info_temp.py"
@@ -261,7 +261,7 @@ class MyClass:
261261
def __init__(self):
262262
self.x = 2
263263
# Print out the detected test info each time we instantiate MyClass
264-
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
264+
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.as_posix()}')}}|TEST_INFO_END")
265265
"""
266266

267267
test_file_name = "test_stack_info_recursive_temp.py"
@@ -343,7 +343,7 @@ def test_example_test():
343343
class MyClass:
344344
def __init__(self):
345345
self.x = 2
346-
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
346+
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.as_posix()}')}}|TEST_INFO_END")
347347
"""
348348
test_dir = (Path(__file__).parent.parent / "code_to_optimize" / "tests" / "pytest").resolve()
349349
test_file_name = "test_stack_info_temp.py"
@@ -410,10 +410,11 @@ def test_example_test_3(self):
410410
self.assertTrue(True)
411411
"""
412412
test_dir = (Path(__file__).parent.parent / "code_to_optimize" / "tests" / "pytest").resolve()
413+
tmp_dir_path = get_run_tmp_file(Path("test_return_values"))
413414
sample_code = f"""
414415
from codeflash.verification.codeflash_capture import codeflash_capture
415416
class MyClass:
416-
@codeflash_capture(function_name="some_function", tmp_dir_path="{get_run_tmp_file(Path("test_return_values"))}", tests_root="{test_dir!s}")
417+
@codeflash_capture(function_name="some_function", tmp_dir_path="{tmp_dir_path.as_posix()}", tests_root="{test_dir.as_posix()}")
417418
def __init__(self, x=2):
418419
self.x = x
419420
"""
@@ -528,6 +529,7 @@ def test_example_test_3(self):
528529
self.assertTrue(True)
529530
"""
530531
test_dir = (Path(__file__).parent.parent / "code_to_optimize" / "tests" / "pytest").resolve()
532+
tmp_dir_path = get_run_tmp_file(Path("test_return_values"))
531533
# MyClass did not have an init function, we created the init function with the codeflash_capture decorator using instrumentation
532534
sample_code = f"""
533535
from codeflash.verification.codeflash_capture import codeflash_capture
@@ -536,7 +538,7 @@ def __init__(self):
536538
self.x = 2
537539
538540
class MyClass(ParentClass):
539-
@codeflash_capture(function_name="some_function", tmp_dir_path="{get_run_tmp_file(Path("test_return_values"))}", tests_root="{test_dir!s}")
541+
@codeflash_capture(function_name="some_function", tmp_dir_path="{tmp_dir_path.as_posix()}", tests_root="{test_dir.as_posix()}")
540542
def __init__(self, *args, **kwargs):
541543
super().__init__(*args, **kwargs)
542544
"""
@@ -648,14 +650,15 @@ def test_example_test():
648650
649651
"""
650652
test_dir = (Path(__file__).parent.parent / "code_to_optimize" / "tests" / "pytest").resolve()
653+
tmp_dir_path = get_run_tmp_file(Path("test_return_values"))
651654
sample_code = f"""
652655
from codeflash.verification.codeflash_capture import codeflash_capture
653656
654657
class MyClass:
655658
@codeflash_capture(
656659
function_name="some_function",
657-
tmp_dir_path="{get_run_tmp_file(Path("test_return_values"))}",
658-
tests_root="{test_dir!s}"
660+
tmp_dir_path="{tmp_dir_path.as_posix()}",
661+
tests_root="{test_dir.as_posix()}"
659662
)
660663
def __init__(self, x=2):
661664
self.x = x
@@ -765,13 +768,14 @@ def test_helper_classes():
765768
assert MyClass().target_function() == 6
766769
"""
767770
test_dir = (Path(__file__).parent.parent / "code_to_optimize" / "tests" / "pytest").resolve()
771+
tmp_dir_path = get_run_tmp_file(Path("test_return_values"))
768772
original_code = f"""
769773
from codeflash.verification.codeflash_capture import codeflash_capture
770774
from code_to_optimize.tests.pytest.helper_file_1 import HelperClass1
771775
from code_to_optimize.tests.pytest.helper_file_2 import HelperClass2, AnotherHelperClass
772776
773777
class MyClass:
774-
@codeflash_capture(function_name='MyClass.__init__', tmp_dir_path='{get_run_tmp_file(Path("test_return_values"))}', tests_root="{test_dir!s}" , is_fto=True)
778+
@codeflash_capture(function_name='MyClass.__init__', tmp_dir_path='{tmp_dir_path.as_posix()}', tests_root="{test_dir.as_posix()}" , is_fto=True)
775779
def __init__(self):
776780
self.x = 1
777781
@@ -785,7 +789,7 @@ def target_function(self):
785789
from codeflash.verification.codeflash_capture import codeflash_capture
786790
787791
class HelperClass1:
788-
@codeflash_capture(function_name='HelperClass1.__init__', tmp_dir_path='{get_run_tmp_file(Path("test_return_values"))}', tests_root="{test_dir!s}", is_fto=False)
792+
@codeflash_capture(function_name='HelperClass1.__init__', tmp_dir_path='{tmp_dir_path.as_posix()}', tests_root="{test_dir.as_posix()}", is_fto=False)
789793
def __init__(self):
790794
self.y = 1
791795
@@ -797,15 +801,15 @@ def helper1(self):
797801
from codeflash.verification.codeflash_capture import codeflash_capture
798802
799803
class HelperClass2:
800-
@codeflash_capture(function_name='HelperClass2.__init__', tmp_dir_path='{get_run_tmp_file(Path("test_return_values"))}', tests_root="{test_dir!s}", is_fto=False)
804+
@codeflash_capture(function_name='HelperClass2.__init__', tmp_dir_path='{tmp_dir_path.as_posix()}', tests_root="{test_dir.as_posix()}", is_fto=False)
801805
def __init__(self):
802806
self.z = 2
803807
804808
def helper2(self):
805809
return 2
806810
807811
class AnotherHelperClass:
808-
@codeflash_capture(function_name='AnotherHelperClass.__init__', tmp_dir_path='{get_run_tmp_file(Path("test_return_values"))}', tests_root="{test_dir!s}", is_fto=False)
812+
@codeflash_capture(function_name='AnotherHelperClass.__init__', tmp_dir_path='{tmp_dir_path.as_posix()}', tests_root="{test_dir.as_posix()}", is_fto=False)
809813
def __init__(self, *args, **kwargs):
810814
super().__init__(*args, **kwargs)
811815

0 commit comments

Comments
 (0)