Skip to content

Commit 3a03d40

Browse files
committed
Merge branch 'windows-fixes' of https://github.com/codeflash-ai/codeflash into windows-fixes
2 parents c6f27a5 + e138387 commit 3a03d40

File tree

6 files changed

+276
-273
lines changed

6 files changed

+276
-273
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from codeflash.cli_cmds.console import logger
1111

12+
_tmpdir = TemporaryDirectory(prefix="codeflash_")
1213

1314
def get_qualified_name(module_name: str, full_qualified_name: str) -> str:
1415
if not full_qualified_name:
@@ -78,10 +79,12 @@ def get_all_function_names(code: str) -> tuple[bool, list[str]]:
7879
return True, function_names
7980

8081

81-
def get_run_tmp_file(file_path: Path) -> Path:
82+
def get_run_tmp_file(file_path: Path | str) -> Path:
8283
if not hasattr(get_run_tmp_file, "tmpdir"):
83-
get_run_tmp_file.tmpdir = TemporaryDirectory(prefix="codeflash_")
84-
return Path(get_run_tmp_file.tmpdir.name) / file_path
84+
get_run_tmp_file.tmpdir = _tmpdir
85+
if isinstance(file_path, str):
86+
file_path = Path(file_path)
87+
return (Path(get_run_tmp_file.tmpdir.name) / file_path).resolve()
8588

8689

8790
def path_belongs_to_site_packages(file_path: Path) -> bool:

codeflash/code_utils/coverage_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def build_fully_qualified_name(function_name: str, code_context: CodeOptimizatio
4141

4242
def generate_candidates(source_code_path: Path) -> list[str]:
4343
"""Generate all the possible candidates for coverage data based on the source code path."""
44-
candidates = [source_code_path.name]
45-
current_path = source_code_path.parent
44+
candidates: list[str] = [source_code_path.name]
45+
current_path: Path = source_code_path.parent
4646

4747
while current_path != current_path.parent:
48-
candidate_path = str(Path(current_path.name) / candidates[-1])
48+
candidate_path = (Path(current_path.name) / candidates[-1]).as_posix()
4949
candidates.append(candidate_path)
5050
current_path = current_path.parent
5151

codeflash/code_utils/instrument_existing_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ 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(value=get_run_tmp_file("test_return_values_").as_posix()),
216216
ast.FormattedValue(
217217
value=ast.Name(id="codeflash_iteration", ctx=ast.Load()),
218218
conversion=-1,

codeflash/verification/instrument_codeflash_capture.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def instrument_codeflash_capture(
3030
modified_code = add_codeflash_capture_to_init(
3131
target_classes={class_parent.name},
3232
fto_name=function_to_optimize.function_name,
33-
tmp_dir_path=str(get_run_tmp_file(Path("test_return_values"))),
33+
tmp_dir_path=get_run_tmp_file("test_return_values").as_posix(),
3434
code=original_code,
35-
tests_root=tests_root,
35+
tests_root=tests_root.as_posix(),
3636
is_fto=True,
3737
)
3838
function_to_optimize.file_path.write_text(modified_code, encoding="utf-8")
@@ -43,9 +43,9 @@ def instrument_codeflash_capture(
4343
modified_code = add_codeflash_capture_to_init(
4444
target_classes=helper_classes,
4545
fto_name=function_to_optimize.function_name,
46-
tmp_dir_path=str(get_run_tmp_file(Path("test_return_values"))),
46+
tmp_dir_path=get_run_tmp_file("test_return_values").as_posix(),
4747
code=original_code,
48-
tests_root=tests_root,
48+
tests_root=tests_root.as_posix(),
4949
is_fto=False,
5050
)
5151
file_path.write_text(modified_code, encoding="utf-8")

tests/test_code_context_extractor.py

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,9 @@ def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
356356
lifespan=self.__duration__,
357357
)
358358
'''
359-
with tempfile.NamedTemporaryFile(mode="w") as f:
360-
f.write(code)
361-
f.flush()
362-
file_path = Path(f.name).resolve()
359+
with tempfile.TemporaryDirectory() as temp_dir:
360+
file_path = Path(temp_dir) / "test.py"
361+
file_path.write_text(code)
363362
opt = Optimizer(
364363
Namespace(
365364
project_root=file_path.parent.resolve(),
@@ -368,7 +367,7 @@ def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
368367
test_framework="pytest",
369368
pytest_cmd="pytest",
370369
experiment_id=None,
371-
test_project_root=Path().resolve(),
370+
test_project_root=Path.cwd(),
372371
)
373372
)
374373
function_to_optimize = FunctionToOptimize(
@@ -557,10 +556,9 @@ def __repr__(self):
557556
def helper_method(self):
558557
return self.x
559558
"""
560-
with tempfile.NamedTemporaryFile(mode="w") as f:
561-
f.write(code)
562-
f.flush()
563-
file_path = Path(f.name).resolve()
559+
with tempfile.TemporaryDirectory() as temp_dir:
560+
file_path = Path(temp_dir) / "test.py"
561+
file_path.write_text(code)
564562
opt = Optimizer(
565563
Namespace(
566564
project_root=file_path.parent.resolve(),
@@ -569,7 +567,7 @@ def helper_method(self):
569567
test_framework="pytest",
570568
pytest_cmd="pytest",
571569
experiment_id=None,
572-
test_project_root=Path().resolve(),
570+
test_project_root=Path.cwd(),
573571
)
574572
)
575573
function_to_optimize = FunctionToOptimize(
@@ -637,10 +635,9 @@ def __repr__(self):
637635
def helper_method(self):
638636
return self.x
639637
"""
640-
with tempfile.NamedTemporaryFile(mode="w") as f:
641-
f.write(code)
642-
f.flush()
643-
file_path = Path(f.name).resolve()
638+
with tempfile.TemporaryDirectory() as temp_dir:
639+
file_path = Path(temp_dir) / "test.py"
640+
file_path.write_text(code)
644641
opt = Optimizer(
645642
Namespace(
646643
project_root=file_path.parent.resolve(),
@@ -649,7 +646,7 @@ def helper_method(self):
649646
test_framework="pytest",
650647
pytest_cmd="pytest",
651648
experiment_id=None,
652-
test_project_root=Path().resolve(),
649+
test_project_root=Path.cwd(),
653650
)
654651
)
655652
function_to_optimize = FunctionToOptimize(
@@ -717,10 +714,9 @@ def __repr__(self):
717714
def helper_method(self):
718715
return self.x
719716
"""
720-
with tempfile.NamedTemporaryFile(mode="w") as f:
721-
f.write(code)
722-
f.flush()
723-
file_path = Path(f.name).resolve()
717+
with tempfile.TemporaryDirectory() as temp_dir:
718+
file_path = Path(temp_dir) / "test.py"
719+
file_path.write_text(code)
724720
opt = Optimizer(
725721
Namespace(
726722
project_root=file_path.parent.resolve(),
@@ -729,7 +725,7 @@ def helper_method(self):
729725
test_framework="pytest",
730726
pytest_cmd="pytest",
731727
experiment_id=None,
732-
test_project_root=Path().resolve(),
728+
test_project_root=Path.cwd(),
733729
)
734730
)
735731
function_to_optimize = FunctionToOptimize(
@@ -787,10 +783,9 @@ def __repr__(self):
787783
def helper_method(self):
788784
return self.x
789785
"""
790-
with tempfile.NamedTemporaryFile(mode="w") as f:
791-
f.write(code)
792-
f.flush()
793-
file_path = Path(f.name).resolve()
786+
with tempfile.TemporaryDirectory() as temp_dir:
787+
file_path = Path(temp_dir) / "test.py"
788+
file_path.write_text(code)
794789
opt = Optimizer(
795790
Namespace(
796791
project_root=file_path.parent.resolve(),
@@ -838,10 +833,9 @@ def __repr__(self):
838833
def helper_method(self):
839834
return self.x
840835
"""
841-
with tempfile.NamedTemporaryFile(mode="w") as f:
842-
f.write(code)
843-
f.flush()
844-
file_path = Path(f.name).resolve()
836+
with tempfile.TemporaryDirectory() as temp_dir:
837+
file_path = Path(temp_dir) / "test.py"
838+
file_path.write_text(code)
845839
opt = Optimizer(
846840
Namespace(
847841
project_root=file_path.parent.resolve(),
@@ -1272,10 +1266,9 @@ def target_method(self):
12721266
def outside_method():
12731267
return 1
12741268
"""
1275-
with tempfile.NamedTemporaryFile(mode="w") as f:
1276-
f.write(code)
1277-
f.flush()
1278-
file_path = Path(f.name).resolve()
1269+
with tempfile.TemporaryDirectory() as temp_dir:
1270+
file_path = Path(temp_dir) / "test.py"
1271+
file_path.write_text(code)
12791272
opt = Optimizer(
12801273
Namespace(
12811274
project_root=file_path.parent.resolve(),

0 commit comments

Comments
 (0)