Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
42 changes: 42 additions & 0 deletions codeflash/code_utils/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,55 @@

import ast
import os
import re
import shutil
import site
from contextlib import contextmanager
from functools import lru_cache
from pathlib import Path
from tempfile import TemporaryDirectory

import tomlkit

from codeflash.cli_cmds.console import logger
from codeflash.code_utils.config_parser import find_pyproject_toml


@contextmanager
def custom_addopts() -> None:
pyproject_file = find_pyproject_toml()
original_content = None
non_blacklist_plugin_args = ""

try:
# Read original file
if pyproject_file.exists():
with Path.open(pyproject_file, encoding="utf-8") as f:
original_content = f.read()
data = tomlkit.parse(original_content)

# Backup original addopts
original_addopts = data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "")
# nothing to do if no addopts present
if original_addopts != "":
non_blacklist_plugin_args = re.sub(r"-n +\S+", "", " ".join(original_addopts)).split(" ")
if non_blacklist_plugin_args != original_addopts:
data["tool"]["pytest"]["ini_options"]["addopts"] = non_blacklist_plugin_args
# Write modified file
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
f.write(tomlkit.dumps(data))

yield

finally:
# Restore original file
if (
original_content
and pyproject_file.exists()
and tuple(original_addopts) not in {(), tuple(non_blacklist_plugin_args)}
):
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
f.write(original_content)


def encoded_tokens_len(s: str) -> int:
Expand Down
10 changes: 5 additions & 5 deletions codeflash/verification/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import TYPE_CHECKING

from codeflash.cli_cmds.console import logger
from codeflash.code_utils.code_utils import get_run_tmp_file
from codeflash.code_utils.code_utils import custom_addopts, get_run_tmp_file
from codeflash.code_utils.compat import IS_POSIX, SAFE_SYS_EXECUTABLE
from codeflash.code_utils.config_consts import TOTAL_LOOPING_TIME
from codeflash.code_utils.coverage_utils import prepare_coverage_files
Expand All @@ -23,8 +23,9 @@ def execute_test_subprocess(
cmd_list: list[str], cwd: Path, env: dict[str, str] | None, timeout: int = 600
) -> subprocess.CompletedProcess:
"""Execute a subprocess with the given command list, working directory, environment variables, and timeout."""
logger.debug(f"executing test run with command: {' '.join(cmd_list)}")
return subprocess.run(cmd_list, capture_output=True, cwd=cwd, env=env, text=True, timeout=timeout, check=False)
with custom_addopts():
logger.debug(f"executing test run with command: {' '.join(cmd_list)}")
return subprocess.run(cmd_list, capture_output=True, cwd=cwd, env=env, text=True, timeout=timeout, check=False)


def run_behavioral_tests(
Expand Down Expand Up @@ -97,6 +98,7 @@ def run_behavioral_tests(
coverage_cmd.extend(shlex.split(pytest_cmd, posix=IS_POSIX)[1:])

blocklist_args = [f"-p no:{plugin}" for plugin in BEHAVIORAL_BLOCKLISTED_PLUGINS if plugin != "cov"]

results = execute_test_subprocess(
coverage_cmd + common_pytest_args + blocklist_args + result_args + test_files,
cwd=cwd,
Expand Down Expand Up @@ -252,7 +254,6 @@ def run_benchmarking_tests(
pytest_test_env = test_env.copy()
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
blocklist_args = [f"-p no:{plugin}" for plugin in BENCHMARKING_BLOCKLISTED_PLUGINS]

results = execute_test_subprocess(
pytest_cmd_list + pytest_args + blocklist_args + result_args + test_files,
cwd=cwd,
Expand All @@ -278,7 +279,6 @@ def run_unittest_tests(
log_level = ["-v"] if verbose else []
files = [str(file) for file in test_file_paths]
output_file = ["--output-file", str(result_file_path)]

results = execute_test_subprocess(
unittest_cmd_list + log_level + files + output_file, cwd=cwd, env=test_env, timeout=600
)
Expand Down
4 changes: 2 additions & 2 deletions codeflash/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# These version placeholders will be replaced by poetry-dynamic-versioning during `poetry build`.
__version__ = "0.12.3"
__version_tuple__ = (0, 12, 3)
__version__ = "0.12.3.post54.dev0+dcf92867"
__version_tuple__ = (0, 12, 3, "post54", "dev0", "dcf92867")
Loading