Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions codeflash/code_utils/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from codeflash.cli_cmds.console import logger

_tmpdir = TemporaryDirectory(prefix="codeflash_")

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


def get_run_tmp_file(file_path: Path) -> Path:
def get_run_tmp_file(file_path: Path | str) -> Path:
if not hasattr(get_run_tmp_file, "tmpdir"):
get_run_tmp_file.tmpdir = TemporaryDirectory(prefix="codeflash_")
return Path(get_run_tmp_file.tmpdir.name) / file_path
get_run_tmp_file.tmpdir = _tmpdir
if isinstance(file_path, str):
file_path = Path(file_path)
return (Path(get_run_tmp_file.tmpdir.name) / file_path).resolve()


def path_belongs_to_site_packages(file_path: Path) -> bool:
Expand Down
6 changes: 3 additions & 3 deletions codeflash/code_utils/coverage_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def build_fully_qualified_name(function_name: str, code_context: CodeOptimizatio

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

while current_path != current_path.parent:
candidate_path = str(Path(current_path.name) / candidates[-1])
candidate_path = (Path(current_path.name) / candidates[-1]).as_posix()
candidates.append(candidate_path)
current_path = current_path.parent

Expand Down
2 changes: 1 addition & 1 deletion codeflash/code_utils/instrument_existing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def visit_FunctionDef(self, node: ast.FunctionDef, test_class_name: str | None =
args=[
ast.JoinedStr(
values=[
ast.Constant(value=f"{get_run_tmp_file(Path('test_return_values_'))}"),
ast.Constant(value=get_run_tmp_file("test_return_values_").as_posix()),
ast.FormattedValue(
value=ast.Name(id="codeflash_iteration", ctx=ast.Load()),
conversion=-1,
Expand Down
8 changes: 4 additions & 4 deletions codeflash/verification/instrument_codeflash_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def instrument_codeflash_capture(
modified_code = add_codeflash_capture_to_init(
target_classes={class_parent.name},
fto_name=function_to_optimize.function_name,
tmp_dir_path=str(get_run_tmp_file(Path("test_return_values"))),
tmp_dir_path=get_run_tmp_file("test_return_values").as_posix(),
code=original_code,
tests_root=tests_root,
tests_root=tests_root.as_posix(),
is_fto=True,
)
function_to_optimize.file_path.write_text(modified_code, encoding="utf-8")
Expand All @@ -43,9 +43,9 @@ def instrument_codeflash_capture(
modified_code = add_codeflash_capture_to_init(
target_classes=helper_classes,
fto_name=function_to_optimize.function_name,
tmp_dir_path=str(get_run_tmp_file(Path("test_return_values"))),
tmp_dir_path=get_run_tmp_file("test_return_values").as_posix(),
code=original_code,
tests_root=tests_root,
tests_root=tests_root.as_posix(),
is_fto=False,
)
file_path.write_text(modified_code, encoding="utf-8")
Expand Down
57 changes: 25 additions & 32 deletions tests/test_code_context_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,9 @@ def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
lifespan=self.__duration__,
)
'''
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(code)
f.flush()
file_path = Path(f.name).resolve()
with tempfile.TemporaryDirectory() as temp_dir:
file_path = Path(temp_dir) / "test.py"
file_path.write_text(code)
opt = Optimizer(
Namespace(
project_root=file_path.parent.resolve(),
Expand All @@ -378,7 +377,7 @@ def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
test_framework="pytest",
pytest_cmd="pytest",
experiment_id=None,
test_project_root=Path().resolve(),
test_project_root=Path.cwd(),
)
)
function_to_optimize = FunctionToOptimize(
Expand Down Expand Up @@ -567,10 +566,9 @@ def __repr__(self):
def helper_method(self):
return self.x
"""
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(code)
f.flush()
file_path = Path(f.name).resolve()
with tempfile.TemporaryDirectory() as temp_dir:
file_path = Path(temp_dir) / "test.py"
file_path.write_text(code)
opt = Optimizer(
Namespace(
project_root=file_path.parent.resolve(),
Expand All @@ -579,7 +577,7 @@ def helper_method(self):
test_framework="pytest",
pytest_cmd="pytest",
experiment_id=None,
test_project_root=Path().resolve(),
test_project_root=Path.cwd(),
)
)
function_to_optimize = FunctionToOptimize(
Expand Down Expand Up @@ -647,10 +645,9 @@ def __repr__(self):
def helper_method(self):
return self.x
"""
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(code)
f.flush()
file_path = Path(f.name).resolve()
with tempfile.TemporaryDirectory() as temp_dir:
file_path = Path(temp_dir) / "test.py"
file_path.write_text(code)
opt = Optimizer(
Namespace(
project_root=file_path.parent.resolve(),
Expand All @@ -659,7 +656,7 @@ def helper_method(self):
test_framework="pytest",
pytest_cmd="pytest",
experiment_id=None,
test_project_root=Path().resolve(),
test_project_root=Path.cwd(),
)
)
function_to_optimize = FunctionToOptimize(
Expand Down Expand Up @@ -727,10 +724,9 @@ def __repr__(self):
def helper_method(self):
return self.x
"""
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(code)
f.flush()
file_path = Path(f.name).resolve()
with tempfile.TemporaryDirectory() as temp_dir:
file_path = Path(temp_dir) / "test.py"
file_path.write_text(code)
opt = Optimizer(
Namespace(
project_root=file_path.parent.resolve(),
Expand All @@ -739,7 +735,7 @@ def helper_method(self):
test_framework="pytest",
pytest_cmd="pytest",
experiment_id=None,
test_project_root=Path().resolve(),
test_project_root=Path.cwd(),
)
)
function_to_optimize = FunctionToOptimize(
Expand Down Expand Up @@ -797,10 +793,9 @@ def __repr__(self):
def helper_method(self):
return self.x
"""
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(code)
f.flush()
file_path = Path(f.name).resolve()
with tempfile.TemporaryDirectory() as temp_dir:
file_path = Path(temp_dir) / "test.py"
file_path.write_text(code)
opt = Optimizer(
Namespace(
project_root=file_path.parent.resolve(),
Expand Down Expand Up @@ -848,10 +843,9 @@ def __repr__(self):
def helper_method(self):
return self.x
"""
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(code)
f.flush()
file_path = Path(f.name).resolve()
with tempfile.TemporaryDirectory() as temp_dir:
file_path = Path(temp_dir) / "test.py"
file_path.write_text(code)
opt = Optimizer(
Namespace(
project_root=file_path.parent.resolve(),
Expand Down Expand Up @@ -1282,10 +1276,9 @@ def target_method(self):
def outside_method():
return 1
"""
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(code)
f.flush()
file_path = Path(f.name).resolve()
with tempfile.TemporaryDirectory() as temp_dir:
file_path = Path(temp_dir) / "test.py"
file_path.write_text(code)
opt = Optimizer(
Namespace(
project_root=file_path.parent.resolve(),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
get_imports_from_file,
get_qualified_name,
get_run_tmp_file,
has_any_async_functions,
is_class_defined_in_file,
module_name_from_file_path,
path_belongs_to_site_packages,
has_any_async_functions,
)
from codeflash.code_utils.concolic_utils import clean_concolic_tests
from codeflash.code_utils.coverage_utils import generate_candidates, prepare_coverage_files
Expand Down Expand Up @@ -254,7 +254,7 @@ def test_get_run_tmp_file_reuses_temp_directory() -> None:


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

file_path = Path("/usr/local/lib/python3.9/site-packages/some_package")
Expand Down
38 changes: 19 additions & 19 deletions tests/test_codeflash_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_example_test_3(self):
class MyClass:
def __init__(self):
self.x = 2
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.resolve().as_posix()}')}}|TEST_INFO_END")
"""
test_file_name = "test_stack_info_temp.py"

Expand All @@ -54,7 +54,7 @@ def __init__(self):
with sample_code_path.open("w") as f:
f.write(sample_code)
result = execute_test_subprocess(
cwd=test_dir, env={}, cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
cwd=test_dir, env=os.environ.copy(), cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
)
assert not result.stderr
assert result.returncode == 0
Expand Down Expand Up @@ -117,7 +117,7 @@ def test_example_test_3(self):
class MyClass:
def __init__(self):
self.x = 2
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.resolve().as_posix()}')}}|TEST_INFO_END")
"""
test_file_name = "test_stack_info_temp.py"

Expand All @@ -129,7 +129,7 @@ def __init__(self):
with sample_code_path.open("w") as f:
f.write(sample_code)
result = execute_test_subprocess(
cwd=test_dir, env={}, cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
cwd=test_dir, env=os.environ.copy(), cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
)
assert not result.stderr
assert result.returncode == 0
Expand Down Expand Up @@ -181,7 +181,7 @@ def test_example_test_3(self):
class MyClass:
def __init__(self):
self.x = 2
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.resolve().as_posix()}')}}|TEST_INFO_END")
"""
test_dir = (Path(__file__).parent.parent / "code_to_optimize" / "tests" / "pytest").resolve()
test_file_name = "test_stack_info_temp.py"
Expand All @@ -194,7 +194,7 @@ def __init__(self):
with sample_code_path.open("w") as f:
f.write(sample_code)
result = execute_test_subprocess(
cwd=test_dir, env={}, cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
cwd=test_dir, env=os.environ.copy(), cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
)
assert not result.stderr
assert result.returncode == 0
Expand Down Expand Up @@ -261,7 +261,7 @@ class MyClass:
def __init__(self):
self.x = 2
# Print out the detected test info each time we instantiate MyClass
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.resolve().as_posix()}')}}|TEST_INFO_END")
"""

test_file_name = "test_stack_info_recursive_temp.py"
Expand All @@ -279,7 +279,7 @@ def __init__(self):

# Run pytest as a subprocess
result = execute_test_subprocess(
cwd=test_dir, env={}, cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
cwd=test_dir, env=os.environ.copy(), cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
)

# Check for errors
Expand Down Expand Up @@ -343,7 +343,7 @@ def test_example_test():
class MyClass:
def __init__(self):
self.x = 2
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir!s}')}}|TEST_INFO_END")
print(f"TEST_INFO_START|{{get_test_info_from_stack('{test_dir.resolve().as_posix()}')}}|TEST_INFO_END")
"""
test_dir = (Path(__file__).parent.parent / "code_to_optimize" / "tests" / "pytest").resolve()
test_file_name = "test_stack_info_temp.py"
Expand All @@ -356,7 +356,7 @@ def __init__(self):
with sample_code_path.open("w") as f:
f.write(sample_code)
result = execute_test_subprocess(
cwd=test_dir, env={}, cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
cwd=test_dir, env=os.environ.copy(), cmd_list=[f"{SAFE_SYS_EXECUTABLE}", "-m", "pytest", test_file_name, "-s"]
)
assert not result.stderr
assert result.returncode == 0
Expand Down Expand Up @@ -413,7 +413,7 @@ def test_example_test_3(self):
sample_code = f"""
from codeflash.verification.codeflash_capture import codeflash_capture
class MyClass:
@codeflash_capture(function_name="some_function", tmp_dir_path="{get_run_tmp_file(Path("test_return_values"))}", tests_root="{test_dir!s}")
@codeflash_capture(function_name="some_function", tmp_dir_path="{get_run_tmp_file(Path("test_return_values")).resolve().as_posix()}", tests_root="{test_dir.resolve().as_posix()}")
def __init__(self, x=2):
self.x = x
"""
Expand Down Expand Up @@ -536,7 +536,7 @@ def __init__(self):
self.x = 2

class MyClass(ParentClass):
@codeflash_capture(function_name="some_function", tmp_dir_path="{get_run_tmp_file(Path("test_return_values"))}", tests_root="{test_dir!s}")
@codeflash_capture(function_name="some_function", tmp_dir_path="{get_run_tmp_file(Path("test_return_values")).resolve().as_posix()}", tests_root="{test_dir.resolve().as_posix()}")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""
Expand Down Expand Up @@ -653,9 +653,9 @@ def test_example_test():

class MyClass:
@codeflash_capture(
function_name="some_function",
tmp_dir_path="{get_run_tmp_file(Path("test_return_values"))}",
tests_root="{test_dir!s}"
function_name="some_function",
tmp_dir_path="{get_run_tmp_file(Path("test_return_values")).resolve().as_posix()}",
tests_root="{test_dir.resolve().as_posix()}"
)
def __init__(self, x=2):
self.x = x
Expand Down Expand Up @@ -771,7 +771,7 @@ def test_helper_classes():
from code_to_optimize.tests.pytest.helper_file_2 import HelperClass2, AnotherHelperClass

class MyClass:
@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)
@codeflash_capture(function_name='MyClass.__init__', tmp_dir_path='{get_run_tmp_file(Path("test_return_values")).resolve().as_posix()}', tests_root="{test_dir.resolve().as_posix()}" , is_fto=True)
def __init__(self):
self.x = 1

Expand All @@ -785,7 +785,7 @@ def target_function(self):
from codeflash.verification.codeflash_capture import codeflash_capture

class HelperClass1:
@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)
@codeflash_capture(function_name='HelperClass1.__init__', tmp_dir_path='{get_run_tmp_file(Path("test_return_values")).resolve().as_posix()}', tests_root="{test_dir.resolve().as_posix()}", is_fto=False)
def __init__(self):
self.y = 1

Expand All @@ -797,15 +797,15 @@ def helper1(self):
from codeflash.verification.codeflash_capture import codeflash_capture

class HelperClass2:
@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)
@codeflash_capture(function_name='HelperClass2.__init__', tmp_dir_path='{get_run_tmp_file(Path("test_return_values")).resolve().as_posix()}', tests_root="{test_dir.resolve().as_posix()}", is_fto=False)
def __init__(self):
self.z = 2

def helper2(self):
return 2

class AnotherHelperClass:
@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)
@codeflash_capture(function_name='AnotherHelperClass.__init__', tmp_dir_path='{get_run_tmp_file(Path("test_return_values")).resolve().as_posix()}', tests_root="{test_dir.resolve().as_posix()}", is_fto=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand Down
Loading
Loading