Skip to content

Commit a135cc4

Browse files
committed
refactoring
1 parent b4256af commit a135cc4

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,39 @@ def custom_addopts() -> None:
5252
f.write(original_content)
5353

5454

55+
@contextmanager
56+
def add_addopts_to_pyproject() -> None:
57+
pyproject_file = find_pyproject_toml()
58+
original_content = None
59+
try:
60+
# Read original file
61+
if pyproject_file.exists():
62+
with Path.open(pyproject_file, encoding="utf-8") as f:
63+
original_content = f.read()
64+
data = tomlkit.parse(original_content)
65+
data["tool"]["pytest"] = {}
66+
data["tool"]["pytest"]["ini_options"] = {}
67+
data["tool"]["pytest"]["ini_options"]["addopts"] = [
68+
"-n=auto",
69+
"-n",
70+
"1",
71+
"-n 1",
72+
"-n 1",
73+
"-n auto",
74+
]
75+
with Path.open(
76+
(Path(__file__).parent.parent.parent / "pyproject.toml").resolve(), "w", encoding="utf-8"
77+
) as f:
78+
f.write(tomlkit.dumps(data))
79+
80+
yield
81+
82+
finally:
83+
# Restore original file
84+
with Path.open(Path("pyproject.toml"), "w", encoding="utf-8") as f:
85+
f.write(original_content)
86+
87+
5588
def encoded_tokens_len(s: str) -> int:
5689
"""Return the approximate length of the encoded tokens.
5790

tests/scripts/end_to_end_test_topological_sort.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,12 @@
22
import pathlib
33
import tomlkit
44

5+
from codeflash.code_utils.code_utils import add_addopts_to_pyproject
56
from end_to_end_test_utilities import CoverageExpectation, TestConfig, run_codeflash_command, run_with_retries
67

78

89
def run_test(expected_improvement_pct: int) -> bool:
9-
try:
10-
# Modify Pyproject file
11-
with pathlib.Path.open(
12-
(pathlib.Path(__file__).parent.parent.parent / "pyproject.toml").resolve(), encoding="utf-8"
13-
) as f:
14-
original_content = f.read()
15-
data = tomlkit.parse(original_content)
16-
data["tool"]["pytest"] = {}
17-
data["tool"]["pytest"]["ini_options"] = {}
18-
data["tool"]["pytest"]["ini_options"]["addopts"] = ["-n=auto", "-n", "1", "-n 1", "-n 1", "-n auto"]
19-
with pathlib.Path.open(
20-
(pathlib.Path(__file__).parent.parent.parent / "pyproject.toml").resolve(), "w", encoding="utf-8"
21-
) as f:
22-
f.write(tomlkit.dumps(data))
10+
with add_addopts_to_pyproject():
2311
config = TestConfig(
2412
file_path="topological_sort.py",
2513
function_name="Graph.topologicalSort",
@@ -35,9 +23,6 @@ def run_test(expected_improvement_pct: int) -> bool:
3523
)
3624
cwd = (pathlib.Path(__file__).parent.parent.parent / "code_to_optimize").resolve()
3725
return_var = run_codeflash_command(cwd, config, expected_improvement_pct)
38-
finally:
39-
with pathlib.Path.open(pathlib.Path("pyproject.toml"), "w", encoding="utf-8") as f:
40-
f.write(original_content)
4126
return return_var
4227

4328

0 commit comments

Comments
 (0)