| 39 | ), |
| 40 | ) |
| 41 | def test_idempotent_any_syntatically_valid_python( |
| 42 | src_contents: str, mode: black.FileMode |
| 43 | ) -> None: |
| 44 | # Before starting, let's confirm that the input string is valid Python: |
| 45 | compile(src_contents, "<string>", "exec") # else the bug is in hypothesmith |
| 46 | |
| 47 | # Then format the code... |
| 48 | try: |
| 49 | dst_contents = black.format_str(src_contents, mode=mode) |
| 50 | except black.InvalidInput: |
| 51 | # This is a bug - if it's valid Python code, as above, Black should be |
| 52 | # able to cope with it. See issues #970, #1012 |
| 53 | # TODO: remove this try-except block when issues are resolved. |
| 54 | return |
| 55 | except TokenError as e: |
| 56 | if ( # Special-case logic for backslashes followed by newlines or end-of-input |
| 57 | e.args[0] == "EOF in multi-line statement" |
| 58 | and re.search(r"\\($|\r?\n)", src_contents) is not None |
| 59 | ): |
| 60 | # This is a bug - if it's valid Python code, as above, Black should be |
| 61 | # able to cope with it. See issue #1012. |
| 62 | # TODO: remove this block when the issue is resolved. |
| 63 | return |
| 64 | raise |
| 65 | |
| 66 | # And check that we got equivalent and stable output. |
| 67 | black.assert_equivalent(src_contents, dst_contents) |
| 68 | black.assert_stable(src_contents, dst_contents, mode=mode) |
| 69 | |
| 70 | # Future test: check that pure-python and mypyc versions of black |
| 71 | # give identical output for identical input? |
| 72 | |
| 73 | |
| 74 | if __name__ == "__main__": |