Skip to content

Commit c805624

Browse files
committed
Merge remote-tracking branch 'origin/main' into line-profiler
2 parents e6a9066 + a74c04a commit c805624

File tree

7 files changed

+69
-14
lines changed

7 files changed

+69
-14
lines changed

codeflash/cli_cmds/cmd_init.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def init_codeflash() -> None:
7373

7474
install_github_app()
7575

76-
install_github_actions()
76+
install_github_actions(override_formatter_check=True)
7777

7878
click.echo(
7979
f"{LF}"
@@ -362,9 +362,9 @@ def check_for_toml_or_setup_file() -> str | None:
362362
return cast(str, project_name)
363363

364364

365-
def install_github_actions() -> None:
365+
def install_github_actions(override_formatter_check: bool=False) -> None:
366366
try:
367-
config, config_file_path = parse_config_file()
367+
config, config_file_path = parse_config_file(override_formatter_check=override_formatter_check)
368368

369369
ph("cli-github-actions-install-started")
370370
try:

codeflash/code_utils/config_parser.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def find_pyproject_toml(config_file: Path | None = None) -> Path:
3131
raise ValueError(msg)
3232

3333

34-
def parse_config_file(config_file_path: Path | None = None) -> tuple[dict[str, Any], Path]:
34+
def parse_config_file(config_file_path: Path | None = None, override_formatter_check: bool=False) -> tuple[dict[str, Any], Path]:
3535
config_file_path = find_pyproject_toml(config_file_path)
3636
try:
3737
with config_file_path.open("rb") as f:
@@ -85,10 +85,12 @@ def parse_config_file(config_file_path: Path | None = None) -> tuple[dict[str, A
8585
"In pyproject.toml, Codeflash only supports the 'test-framework' as pytest and unittest."
8686
)
8787
if len(config["formatter-cmds"]) > 0:
88-
assert config["formatter-cmds"][0] != "your-formatter $file", (
89-
"The formatter command is not set correctly in pyproject.toml. Please set the "
90-
"formatter command in the 'formatter-cmds' key. More info - https://docs.codeflash.ai/configuration"
91-
)
88+
#see if this is happening during Github actions setup
89+
if not override_formatter_check:
90+
assert config["formatter-cmds"][0] != "your-formatter $file", (
91+
"The formatter command is not set correctly in pyproject.toml. Please set the "
92+
"formatter command in the 'formatter-cmds' key. More info - https://docs.codeflash.ai/configuration"
93+
)
9294
for key in list(config.keys()):
9395
if "-" in key:
9496
config[key.replace("-", "_")] = config[key]

codeflash/discovery/pytest_new_process_discovery.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def pytest_collection_finish(self, session) -> None:
1616
collected_tests.extend(session.items)
1717
pytest_rootdir = session.config.rootdir
1818

19+
def pytest_collection_modifyitems(config, items):
20+
skip_benchmark = pytest.mark.skip(reason="Skipping benchmark tests")
21+
for item in items:
22+
if "benchmark" in item.fixturenames:
23+
item.add_marker(skip_benchmark)
1924

2025
def parse_pytest_collection_results(pytest_tests: list[Any]) -> list[dict[str, str]]:
2126
test_results = []
@@ -34,7 +39,7 @@ def parse_pytest_collection_results(pytest_tests: list[Any]) -> list[dict[str, s
3439

3540
try:
3641
exitcode = pytest.main(
37-
[tests_root, "-pno:logging", "--collect-only", "-m", "not skip"], plugins=[PytestCollectionPlugin()]
42+
[tests_root, "-p no:logging", "--collect-only", "-m", "not skip",], plugins=[PytestCollectionPlugin()]
3843
)
3944
except Exception as e: # noqa: BLE001
4045
print(f"Failed to collect tests: {e!s}") # noqa: T201

codeflash/verification/test_runner.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
if TYPE_CHECKING:
1717
from codeflash.models.models import TestFiles
1818

19+
BEHAVIORAL_BLOCKLISTED_PLUGINS = ["benchmark"]
20+
BENCHMARKING_BLOCKLISTED_PLUGINS = ["codspeed", "cov", "benchmark", "profiling"]
1921

2022
def execute_test_subprocess(
2123
cmd_list: list[str], cwd: Path, env: dict[str, str] | None, timeout: int = 600
@@ -88,8 +90,9 @@ def run_behavioral_tests(
8890
else:
8991
coverage_cmd.extend(shlex.split(pytest_cmd, posix=IS_POSIX)[1:])
9092

93+
blocklist_args = [f"-p no:{plugin}" for plugin in BEHAVIORAL_BLOCKLISTED_PLUGINS if plugin != "cov"]
9194
results = execute_test_subprocess(
92-
coverage_cmd + common_pytest_args + result_args + test_files, cwd=cwd, env=pytest_test_env, timeout=600
95+
coverage_cmd + common_pytest_args + blocklist_args + result_args + test_files, cwd=cwd, env=pytest_test_env, timeout=600
9396
)
9497
logger.debug(
9598
f"Result return code: {results.returncode}, "
@@ -107,8 +110,9 @@ def run_behavioral_tests(
107110
f"{'Result stderr:' + str(results.stderr) if results.stderr else ''}"
108111
)
109112
else:
113+
blocklist_args = [f"-p no:{plugin}" for plugin in BEHAVIORAL_BLOCKLISTED_PLUGINS]
110114
results = execute_test_subprocess(
111-
pytest_cmd_list + common_pytest_args + result_args + test_files,
115+
pytest_cmd_list + common_pytest_args + blocklist_args + result_args + test_files,
112116
cwd=cwd,
113117
env=pytest_test_env,
114118
timeout=600, # TODO: Make this dynamic
@@ -182,8 +186,10 @@ def run_benchmarking_tests(
182186
result_args = [f"--junitxml={result_file_path.as_posix()}", "-o", "junit_logging=all"]
183187
pytest_test_env = test_env.copy()
184188
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
189+
blocklist_args = [f"-p no:{plugin}" for plugin in BENCHMARKING_BLOCKLISTED_PLUGINS]
190+
185191
results = execute_test_subprocess(
186-
pytest_cmd_list + pytest_args + result_args + test_files,
192+
pytest_cmd_list + pytest_args + blocklist_args + result_args + test_files,
187193
cwd=cwd,
188194
env=pytest_test_env,
189195
timeout=600, # TODO: Make this dynamic

docs/docs/how-codeflash-works.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To optimize code, Codeflash first gathers all necessary context from the codebas
2525

2626
## Verification of correctness
2727

28-
![Verification](/img/verification.svg)
28+
![Verification](/img/codeflash_arch_diagram.gif)
2929

3030
The goal of correctness verification is to ensure that when the original code is replaced by the new code, there are no behavioral changes in the code and the rest of the system. This means the replacement should be completely safe.
3131

@@ -60,4 +60,4 @@ Codeflash implements several techniques to measure code performance accurately.
6060

6161
## Creating Pull Requests
6262

63-
Once an optimization passes all checks, Codeflash creates a pull request through the Codeflash GitHub app directly in your repository. The pull request includes the new code, the speedup percentage, an explanation of the optimization, test statistics including coverage, and the test content itself. You can review and merge the new code if it meets your standards. Feel free to modify the code as needed—we welcome your improvements!
63+
Once an optimization passes all checks, Codeflash creates a pull request through the Codeflash GitHub app directly in your repository. The pull request includes the new code, the speedup percentage, an explanation of the optimization, test statistics including coverage, and the test content itself. You can review and merge the new code if it meets your standards. Feel free to modify the code as needed—we welcome your improvements!
3.36 MB
Loading

tests/test_unit_test_discovery.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,48 @@ def test_unit_test_discovery_unittest():
3434
# assert len(tests) > 0
3535
# Unittest discovery within a pytest environment does not work
3636

37+
def test_benchmark_unit_test_discovery_pytest():
38+
with tempfile.TemporaryDirectory() as tmpdirname:
39+
# Create a dummy test file
40+
test_file_path = Path(tmpdirname) / "test_dummy.py"
41+
test_file_content = """
42+
from bubble_sort import sorter
43+
44+
def test_benchmark_sort(benchmark):
45+
benchmark(sorter, [5, 4, 3, 2, 1, 0])
46+
47+
def test_normal_test():
48+
assert sorter(list(reversed(range(100)))) == list(range(100))
49+
50+
def test_normal_test2():
51+
assert sorter(list(reversed(range(100)))) == list(range(100))"""
52+
test_file_path.write_text(test_file_content)
53+
path_obj_tempdirname = Path(tmpdirname)
54+
55+
# Create a file that the test file is testing
56+
code_file_path = path_obj_tempdirname / "bubble_sort.py"
57+
code_file_content = """
58+
def sorter(arr):
59+
return sorted(arr)"""
60+
code_file_path.write_text(code_file_content)
61+
62+
# Create a TestConfig with the temporary directory as the root
63+
test_config = TestConfig(
64+
tests_root=path_obj_tempdirname,
65+
project_root_path=path_obj_tempdirname,
66+
test_framework="pytest",
67+
tests_project_rootdir=path_obj_tempdirname.parent,
68+
)
69+
70+
# Discover tests
71+
tests = discover_unit_tests(test_config)
72+
assert len(tests) == 1
73+
assert 'bubble_sort.sorter' in tests
74+
assert len(tests['bubble_sort.sorter']) == 2
75+
functions = [test.tests_in_file.test_function for test in tests['bubble_sort.sorter']]
76+
assert 'test_normal_test' in functions
77+
assert 'test_normal_test2' in functions
78+
assert 'test_benchmark_sort' not in functions
3779

3880
def test_discover_tests_pytest_with_temp_dir_root():
3981
with tempfile.TemporaryDirectory() as tmpdirname:

0 commit comments

Comments
 (0)