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

Add property-based fuzz test #81

Merged
merged 2 commits into from Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
*.egg
*.egg-info
*.py[cod]
.hypothesis/
.tox
dist
42 changes: 42 additions & 0 deletions test_mccabe.py
@@ -1,11 +1,17 @@
import unittest
import sys

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

import pytest
try:
import hypothesmith
from hypothesis import HealthCheck, given, settings, strategies as st
except ImportError:
hypothesmith = None

import mccabe
from mccabe import get_code_complexity
Expand Down Expand Up @@ -233,5 +239,41 @@ def test_get_module_complexity(self):
self.assertEqual(0, mccabe.get_module_complexity("mccabe.py"))


# This test uses the Hypothesis and Hypothesmith libraries to generate random
# syntatically-valid Python source code and applies McCabe on it.
@settings(
max_examples=1000, # roughly 1k tests/minute, or half that under coverage
derandomize=False, # deterministic mode to avoid CI flakiness
deadline=None, # ignore Hypothesis' health checks; we already know that
suppress_health_check=HealthCheck.all(), # this is slow and filter-heavy.
)
@given(
# Note that while Hypothesmith might generate code unlike that written by
# humans, it's a general test that should pass for any *valid* source code.
# (so e.g. running it against code scraped of the internet might also help)
src_contents=hypothesmith.from_grammar() | hypothesmith.from_node(),
max_complexity=st.integers(min_value=1),
)
@pytest.mark.skipif(not hypothesmith, reason="hypothesmith could not be imported")
def test_idempotent_any_syntatically_valid_python(
MaxG87 marked this conversation as resolved.
Show resolved Hide resolved
src_contents: str, max_complexity: int
) -> None:
"""Property-based tests for mccabe.

This test case is based on a similar test for Black, the code formatter.
Black's test was written by Zac Hatfield-Dodds, the author of Hypothesis
and the Hypothesmith tool for source code generation. You can run this
file with `python`, `pytest`, or (soon) a coverage-guided fuzzer Zac is
working on.
"""

# Before starting, let's confirm that the input string is valid Python:
compile(src_contents, "<string>", "exec") # else bug is in hypothesmith

# Then try to apply get_complexity_number to the code...
get_code_complexity(src_contents, max_complexity)


if __name__ == "__main__":
test_idempotent_any_syntatically_valid_python()
unittest.main()
2 changes: 2 additions & 0 deletions tox.ini
Expand Up @@ -5,6 +5,8 @@ envlist =
[testenv]
deps =
pytest
hypothesis ; python_version >= "3.6"
hypothesmith ; python_version >= "3.6"
commands =
pytest

Expand Down