Skip to content
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

fuzzy testing with pysource_codegen #3908

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
55 changes: 55 additions & 0 deletions scripts/find_issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pathlib import Path

from pysource_codegen import generate
from pysource_minimize import minimize

import black

base_path = Path(__file__).parent


def bug_in_code(src_contents: str, mode: black.FileMode) -> bool:
try:
dst_contents = black.format_str(src_contents, mode=mode)

black.assert_equivalent(src_contents, dst_contents)
black.assert_stable(src_contents, dst_contents, mode=mode)
except Exception as e:
print("error:", e)
return True
return False


def find_issue() -> None:
mode = black.FileMode(
line_length=80,
string_normalization=True,
is_pyi=False,
magic_trailing_comma=False,
)
for seed in range(1000):
src_code = generate(seed)
print("seed:", seed)

compile(src_code, "<string>", "exec")

if bug_in_code(src_code, mode):
minimized = minimize(src_code, lambda code: bug_in_code(code, mode))
assert bug_in_code(minimized, mode)

print("seed:", seed)
print("minimized code:")
print(minimized)

if "with" in minimized:
# propably known issue
# https://github.com/psf/black/issues/3678
# https://github.com/psf/black/issues/3677
continue

(base_path / "min_code.py").write_text(minimized)
return


if __name__ == "__main__":
find_issue()
2 changes: 2 additions & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ pytest >= 6.1.1
pytest-xdist >= 3.0.2
pytest-cov >= 4.1.0
tox
pysource_codegen >= 0.4.1
pysource_minimize >= 0.4.0
Loading