-
Notifications
You must be signed in to change notification settings - Fork 22
Clean concolic tests #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c6c6fff
first pass
KRRT7 b79db77
cleanup
KRRT7 04fc3e6
Update codeflash/code_utils/code_replacer.py
KRRT7 af1f078
Revert "Update codeflash/code_utils/code_replacer.py"
KRRT7 8879e2e
blocklisted funcs bugfix
KRRT7 252381f
codeflash suggestion
KRRT7 b8bc645
redundant Path() calls
KRRT7 5c6e1bd
move concolic related code to it's own file
KRRT7 9095ca5
update tests
KRRT7 57afa8c
⚡️ Speed up method `AssertCleanup.transform_asserts` by 46% in PR #26…
codeflash-ai[bot] 59250cc
Merge remote-tracking branch 'origin/main' into clean_concolic_tests
KRRT7 b9c19e9
Merge pull request #30 from codeflash-ai/codeflash/optimize-pr26-2025…
KRRT7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,93 @@ | ||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import ast | ||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| class AssertCleanup: | ||||||||||||||||||||||||
| def transform_asserts(self, code: str) -> str: | ||||||||||||||||||||||||
| lines = code.splitlines() | ||||||||||||||||||||||||
| result_lines = [] | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| for line in lines: | ||||||||||||||||||||||||
| transformed = self._transform_assert_line(line) | ||||||||||||||||||||||||
| if transformed is not None: | ||||||||||||||||||||||||
| result_lines.append(transformed) | ||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| result_lines.append(line) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return "\n".join(result_lines) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def _transform_assert_line(self, line: str) -> Optional[str]: | ||||||||||||||||||||||||
| indent = line[: len(line) - len(line.lstrip())] | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| assert_match = re.match(r"\s*assert\s+(.*?)(?:\s*==\s*.*)?$", line) | ||||||||||||||||||||||||
| if assert_match: | ||||||||||||||||||||||||
| expression = assert_match.group(1).strip() | ||||||||||||||||||||||||
KRRT7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||
| if expression.startswith("not "): | ||||||||||||||||||||||||
| return f"{indent}{expression}" | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| expression = re.sub(r"[,;]\s*$", "", expression) | ||||||||||||||||||||||||
| return f"{indent}{expression}" | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| unittest_match = re.match(r"(\s*)self\.assert([A-Za-z]+)\((.*)\)$", line) | ||||||||||||||||||||||||
| if unittest_match: | ||||||||||||||||||||||||
| indent, assert_method, args = unittest_match.groups() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if args: | ||||||||||||||||||||||||
| arg_parts = self._split_top_level_args(args) | ||||||||||||||||||||||||
| if arg_parts and arg_parts[0]: | ||||||||||||||||||||||||
| return f"{indent}{arg_parts[0]}" | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def _split_top_level_args(self, args_str: str) -> list[str]: | ||||||||||||||||||||||||
| result = [] | ||||||||||||||||||||||||
| current = [] | ||||||||||||||||||||||||
| depth = 0 | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| for char in args_str: | ||||||||||||||||||||||||
| if char in "([{": | ||||||||||||||||||||||||
| depth += 1 | ||||||||||||||||||||||||
| current.append(char) | ||||||||||||||||||||||||
|
Comment on lines
+46
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
| elif char in ")]}": | ||||||||||||||||||||||||
| depth -= 1 | ||||||||||||||||||||||||
| current.append(char) | ||||||||||||||||||||||||
| elif char == "," and depth == 0: | ||||||||||||||||||||||||
KRRT7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
| result.append("".join(current).strip()) | ||||||||||||||||||||||||
| current = [] | ||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| current.append(char) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if current: | ||||||||||||||||||||||||
| result.append("".join(current).strip()) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return result | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def clean_concolic_tests(test_suite_code: str) -> str: | ||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||
| can_parse = True | ||||||||||||||||||||||||
| tree = ast.parse(test_suite_code) | ||||||||||||||||||||||||
| except SyntaxError: | ||||||||||||||||||||||||
| can_parse = False | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if not can_parse: | ||||||||||||||||||||||||
| return AssertCleanup().transform_asserts(test_suite_code) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| for node in ast.walk(tree): | ||||||||||||||||||||||||
| if isinstance(node, ast.FunctionDef) and node.name.startswith("test_"): | ||||||||||||||||||||||||
| new_body = [] | ||||||||||||||||||||||||
| for stmt in node.body: | ||||||||||||||||||||||||
| if isinstance(stmt, ast.Assert): | ||||||||||||||||||||||||
KRRT7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
| if isinstance(stmt.test, ast.Compare) and isinstance(stmt.test.left, ast.Call): | ||||||||||||||||||||||||
| new_body.append(ast.Expr(value=stmt.test.left)) | ||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| new_body.append(stmt) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| new_body.append(stmt) | ||||||||||||||||||||||||
| node.body = new_body | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return ast.unparse(tree).strip() | ||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.