Raise AssertionError if `src` and `dst` aren't equivalent.
(src: str, dst: str)
| 1631 | |
| 1632 | |
| 1633 | def assert_equivalent(src: str, dst: str) -> None: |
| 1634 | """Raise AssertionError if `src` and `dst` aren't equivalent.""" |
| 1635 | try: |
| 1636 | src_ast = parse_ast(src) |
| 1637 | except Exception as exc: |
| 1638 | raise SourceASTParseError( |
| 1639 | "cannot use --safe with this file; failed to parse source file AST: " |
| 1640 | f"{exc}\n" |
| 1641 | "This could be caused by running Black with an older Python version " |
| 1642 | "that does not support new syntax used in your source file." |
| 1643 | ) from exc |
| 1644 | |
| 1645 | try: |
| 1646 | dst_ast = parse_ast(dst) |
| 1647 | except Exception as exc: |
| 1648 | log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst) |
| 1649 | raise ASTSafetyError( |
| 1650 | f"INTERNAL ERROR: {_black_info()} produced invalid code: {exc}. " |
| 1651 | "Please report a bug on https://github.com/psf/black/issues. " |
| 1652 | f"This invalid output might be helpful: {log}" |
| 1653 | ) from None |
| 1654 | |
| 1655 | src_ast_str = "\n".join(stringify_ast(src_ast)) |
| 1656 | dst_ast_str = "\n".join(stringify_ast(dst_ast)) |
| 1657 | if src_ast_str != dst_ast_str: |
| 1658 | log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst")) |
| 1659 | raise ASTSafetyError( |
| 1660 | f"INTERNAL ERROR: {_black_info()} produced code that is not equivalent to" |
| 1661 | " the source. Please report a bug on https://github.com/psf/black/issues." |
| 1662 | f" This diff might be helpful: {log}" |
| 1663 | ) from None |
| 1664 | |
| 1665 | |
| 1666 | def assert_stable( |
no test coverage detected