+ def test_skip_magic_trailing_comma(self) -> None:
+ source, _ = read_data("expression.py")
+ expected, _ = read_data("expression_skip_magic_trailing_comma.diff")
+ tmp_file = Path(black.dump_to_file(source))
+ diff_header = re.compile(
+ rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d "
+ r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d"
+ )
+ try:
+ result = BlackRunner().invoke(black.main, ["-C", "--diff", str(tmp_file)])
+ self.assertEqual(result.exit_code, 0)
+ finally:
+ os.unlink(tmp_file)
+ actual = result.output
+ actual = diff_header.sub(DETERMINISTIC_HEADER, actual)
+ actual = actual.rstrip() + "\n" # the diff output has a trailing space
+ if expected != actual:
+ dump = black.dump_to_file(actual)
+ msg = (
+ "Expected diff isn't equal to the actual. If you made changes to"
+ " expression.py and this is an anticipated difference, overwrite"
+ f" tests/data/expression_skip_magic_trailing_comma.diff with {dump}"
+ )
+ self.assertEqual(expected, actual, msg)
+
+ @pytest.mark.without_python2
+ def test_python2_should_fail_without_optional_install(self) -> None:
+ # python 3.7 and below will install typed-ast and will be able to parse Python 2
+ if sys.version_info < (3, 8):
+ return
+ source = "x = 1234l"
+ tmp_file = Path(black.dump_to_file(source))
+ try:
+ runner = BlackRunner()
+ result = runner.invoke(black.main, [str(tmp_file)])
+ self.assertEqual(result.exit_code, 123)
+ finally:
+ os.unlink(tmp_file)
+ actual = (
+ runner.stderr_bytes.decode()
+ .replace("\n", "")
+ .replace("\\n", "")
+ .replace("\\r", "")
+ .replace("\r", "")
+ )
+ msg = (
+ "The requested source code has invalid Python 3 syntax."
+ "If you are trying to format Python 2 files please reinstall Black"
+ " with the 'python2' extra: `python3 -m pip install black[python2]`."
+ )
+ self.assertIn(msg, actual)
+
+ @pytest.mark.python2