Repository: psf/black
File: fuzz.py (Python)

Code before:
)
def test_idempotent_any_syntatically_valid_python(
    src_contents: str, mode: black.FileMode
) -> None:
    # Before starting, let's confirm that the input string is valid Python:
    compile(src_contents, "<string>", "exec")  # else the bug is in hypothesmith

    # Then format the code...
    try:
        dst_contents = black.format_str(src_contents, mode=mode)
    except black.InvalidInput:
        # This is a bug - if it's valid Python code, as above, Black should be
        # able to cope with it.  See issues #970, #1012, #1358, and #1557.
        # TODO: remove this try-except block when issues are resolved.
        return
    except TokenError as e:
        newlines = r"(\n|\r\n)+"
        backslash = r"\\"
        exception_message = e.args[0]
        if (  # https://github.com/psf/black/issues/1012
            re.findall(f"{newlines}{backslash}{newlines}", src_contents)
            and exception_message == "EOF in multi-line statement"
        ):
            print('Warning: skipped fuzzer-generated input that contained a backslash')
            print('See https://github.com/psf/black/issues/1012 for details')
            print('To replay this fuzzer run, find the PYTHONHASHSEED environment value')
            return
        raise

    # And check that we got equivalent and stable output.
    black.assert_equivalent(src_contents, dst_contents)
    black.assert_stable(src_contents, dst_contents, mode=mode)

    # Future test: check that pure-python and mypyc versions of black
    # give identical output for identical input?


if __name__ == "__main__":
    # Run tests, including shrinking and reporting any known failures.
    test_idempotent_any_syntatically_valid_python()

    # If Atheris is available, run coverage-guided fuzzing.
    # (if you want only bounded fuzzing, just use `pytest fuzz.py`)
    try:
        import sys
        import atheris
    except ImportError:
        pass
    else:
        test = test_idempotent_any_syntatically_valid_python
        atheris.Setup(sys.argv, test.hypothesis.fuzz_one_input)

Review comment (refactor):
```suggestion
        if (
            sys.version_info[:2] <= (3, 7)
            and e.args[0] == "EOF in multi-line statement"
            and re.search(r"\r?\n\\\r?\n", src_contents) is not None
        ):
            # A trailing backslash at EOF was accepted in Python <= 3.7, but a
            # SyntaxError in later versions.  We've decided to just ignore this
            # until the problem goes away when 3.7 reaches end of life in 2023.
            # See https://github.com/psf/black/issues/1012
```

I'd simplify the regex logic, add a check for Python version, and have a code comment rather than printing advice - it's very unlikely that anyone would want to reproduce this behaviour, and it would make the logs very noisy if running for a long time.

(and thanks to the `derandomize=True` setting you'd only need to re-run the test)

Code after:
)
def test_idempotent_any_syntatically_valid_python(
    src_contents: str, mode: black.FileMode
) -> None:
    # Before starting, let's confirm that the input string is valid Python:
    compile(src_contents, "<string>", "exec")  # else the bug is in hypothesmith

    # Then format the code...
    try:
        dst_contents = black.format_str(src_contents, mode=mode)
    except black.InvalidInput:
        # This is a bug - if it's valid Python code, as above, Black should be
        # able to cope with it.  See issues #970, #1012, #1358, and #1557.
        # TODO: remove this try-except block when issues are resolved.
        return
    except TokenError as e:
        if (
            e.args[0] == "EOF in multi-line statement"
            and re.search(r"\r?\n\\\r?\n", src_contents) is not None
        ):
            # This is a bug - if it's valid Python code, as above, Black should be
            # able to cope with it.  See issue #1012.
            # TODO: remove this block when the issue is resolved.
            return
        raise

    # And check that we got equivalent and stable output.
    black.assert_equivalent(src_contents, dst_contents)
    black.assert_stable(src_contents, dst_contents, mode=mode)

    # Future test: check that pure-python and mypyc versions of black
    # give identical output for identical input?


if __name__ == "__main__":
    # Run tests, including shrinking and reporting any known failures.
    test_idempotent_any_syntatically_valid_python()

    # If Atheris is available, run coverage-guided fuzzing.
    # (if you want only bounded fuzzing, just use `pytest fuzz.py`)
    try:
        import sys
        import atheris
    except ImportError:
        pass
    else:
        test = test_idempotent_any_syntatically_valid_python
        atheris.Setup(sys.argv, test.hypothesis.fuzz_one_input)
        atheris.Fuzz()