]> git.madduck.net Git - etc/vim.git/blobdiff - tests/test_black.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Handle ImportError from multiprocessing module (#1400)
[etc/vim.git] / tests / test_black.py
index a0e57dc72d4a14e20b7a24162ed3ee3ec6a6072f..1b574c012edfcd963535c349af271d2e06299959 100644 (file)
@@ -35,6 +35,7 @@ ff = partial(black.format_file_in_place, mode=black.FileMode(), fast=True)
 fs = partial(black.format_str, mode=black.FileMode())
 THIS_FILE = Path(__file__)
 THIS_DIR = THIS_FILE.parent
 fs = partial(black.format_str, mode=black.FileMode())
 THIS_FILE = Path(__file__)
 THIS_DIR = THIS_FILE.parent
+PROJECT_ROOT = THIS_DIR.parent
 DETERMINISTIC_HEADER = "[Deterministic header]"
 EMPTY_LINE = "# EMPTY LINE WITH WHITESPACE" + " (this comment will be removed)"
 PY36_ARGS = [
 DETERMINISTIC_HEADER = "[Deterministic header]"
 EMPTY_LINE = "# EMPTY LINE WITH WHITESPACE" + " (this comment will be removed)"
 PY36_ARGS = [
@@ -54,7 +55,7 @@ def read_data(name: str, data: bool = True) -> Tuple[str, str]:
         name += ".py"
     _input: List[str] = []
     _output: List[str] = []
         name += ".py"
     _input: List[str] = []
     _output: List[str] = []
-    base_dir = THIS_DIR / "data" if data else THIS_DIR
+    base_dir = THIS_DIR / "data" if data else PROJECT_ROOT
     with open(base_dir / name, "r", encoding="utf8") as test:
         lines = test.readlines()
     result = _input
     with open(base_dir / name, "r", encoding="utf8") as test:
         lines = test.readlines()
     result = _input
@@ -82,7 +83,7 @@ def cache_dir(exists: bool = True) -> Iterator[Path]:
 
 
 @contextmanager
 
 
 @contextmanager
-def event_loop(close: bool) -> Iterator[None]:
+def event_loop() -> Iterator[None]:
     policy = asyncio.get_event_loop_policy()
     loop = policy.new_event_loop()
     asyncio.set_event_loop(loop)
     policy = asyncio.get_event_loop_policy()
     loop = policy.new_event_loop()
     asyncio.set_event_loop(loop)
@@ -90,8 +91,7 @@ def event_loop(close: bool) -> Iterator[None]:
         yield
 
     finally:
         yield
 
     finally:
-        if close:
-            loop.close()
+        loop.close()
 
 
 @contextmanager
 
 
 @contextmanager
@@ -157,9 +157,18 @@ class BlackTestCase(unittest.TestCase):
     ) -> None:
         runner = BlackRunner()
         if ignore_config:
     ) -> None:
         runner = BlackRunner()
         if ignore_config:
-            args = ["--config", str(THIS_DIR / "empty.toml"), *args]
+            args = ["--verbose", "--config", str(THIS_DIR / "empty.toml"), *args]
         result = runner.invoke(black.main, args)
         result = runner.invoke(black.main, args)
-        self.assertEqual(result.exit_code, exit_code, msg=runner.stderr_bytes.decode())
+        self.assertEqual(
+            result.exit_code,
+            exit_code,
+            msg=(
+                f"Failed with args: {args}\n"
+                f"stdout: {runner.stdout_bytes.decode()!r}\n"
+                f"stderr: {runner.stderr_bytes.decode()!r}\n"
+                f"exception: {result.exception}"
+            ),
+        )
 
     @patch("black.dump_to_file", dump_to_stderr)
     def checkSourceFile(self, name: str) -> None:
 
     @patch("black.dump_to_file", dump_to_stderr)
     def checkSourceFile(self, name: str) -> None:
@@ -194,43 +203,43 @@ class BlackTestCase(unittest.TestCase):
         self.checkSourceFile("tests/test_black.py")
 
     def test_black(self) -> None:
         self.checkSourceFile("tests/test_black.py")
 
     def test_black(self) -> None:
-        self.checkSourceFile("black.py")
+        self.checkSourceFile("src/black/__init__.py")
 
     def test_pygram(self) -> None:
 
     def test_pygram(self) -> None:
-        self.checkSourceFile("blib2to3/pygram.py")
+        self.checkSourceFile("src/blib2to3/pygram.py")
 
     def test_pytree(self) -> None:
 
     def test_pytree(self) -> None:
-        self.checkSourceFile("blib2to3/pytree.py")
+        self.checkSourceFile("src/blib2to3/pytree.py")
 
     def test_conv(self) -> None:
 
     def test_conv(self) -> None:
-        self.checkSourceFile("blib2to3/pgen2/conv.py")
+        self.checkSourceFile("src/blib2to3/pgen2/conv.py")
 
     def test_driver(self) -> None:
 
     def test_driver(self) -> None:
-        self.checkSourceFile("blib2to3/pgen2/driver.py")
+        self.checkSourceFile("src/blib2to3/pgen2/driver.py")
 
     def test_grammar(self) -> None:
 
     def test_grammar(self) -> None:
-        self.checkSourceFile("blib2to3/pgen2/grammar.py")
+        self.checkSourceFile("src/blib2to3/pgen2/grammar.py")
 
     def test_literals(self) -> None:
 
     def test_literals(self) -> None:
-        self.checkSourceFile("blib2to3/pgen2/literals.py")
+        self.checkSourceFile("src/blib2to3/pgen2/literals.py")
 
     def test_parse(self) -> None:
 
     def test_parse(self) -> None:
-        self.checkSourceFile("blib2to3/pgen2/parse.py")
+        self.checkSourceFile("src/blib2to3/pgen2/parse.py")
 
     def test_pgen(self) -> None:
 
     def test_pgen(self) -> None:
-        self.checkSourceFile("blib2to3/pgen2/pgen.py")
+        self.checkSourceFile("src/blib2to3/pgen2/pgen.py")
 
     def test_tokenize(self) -> None:
 
     def test_tokenize(self) -> None:
-        self.checkSourceFile("blib2to3/pgen2/tokenize.py")
+        self.checkSourceFile("src/blib2to3/pgen2/tokenize.py")
 
     def test_token(self) -> None:
 
     def test_token(self) -> None:
-        self.checkSourceFile("blib2to3/pgen2/token.py")
+        self.checkSourceFile("src/blib2to3/pgen2/token.py")
 
     def test_setup(self) -> None:
         self.checkSourceFile("setup.py")
 
     def test_piping(self) -> None:
 
     def test_setup(self) -> None:
         self.checkSourceFile("setup.py")
 
     def test_piping(self) -> None:
-        source, expected = read_data("../black", data=False)
+        source, expected = read_data("src/black/__init__", data=False)
         result = BlackRunner().invoke(
             black.main,
             ["-", "--fast", f"--line-length={black.DEFAULT_LINE_LENGTH}"],
         result = BlackRunner().invoke(
             black.main,
             ["-", "--fast", f"--line-length={black.DEFAULT_LINE_LENGTH}"],
@@ -1251,7 +1260,7 @@ class BlackTestCase(unittest.TestCase):
             with src.open("r") as fobj:
                 self.assertEqual(fobj.read(), "print('hello')")
 
             with src.open("r") as fobj:
                 self.assertEqual(fobj.read(), "print('hello')")
 
-    @event_loop(close=False)
+    @event_loop()
     def test_cache_multiple_files(self) -> None:
         mode = black.FileMode()
         with cache_dir() as workspace, patch(
     def test_cache_multiple_files(self) -> None:
         mode = black.FileMode()
         with cache_dir() as workspace, patch(
@@ -1273,27 +1282,6 @@ class BlackTestCase(unittest.TestCase):
             self.assertIn(one, cache)
             self.assertIn(two, cache)
 
             self.assertIn(one, cache)
             self.assertIn(two, cache)
 
-    @patch("black.ProcessPoolExecutor", autospec=True)
-    def test_works_in_mono_process_only_environment(self, mock_executor) -> None:
-        mock_executor.side_effect = OSError()
-        mode = black.FileMode()
-        with cache_dir() as workspace:
-            one = (workspace / "one.py").resolve()
-            with one.open("w") as fobj:
-                fobj.write("print('hello')")
-            two = (workspace / "two.py").resolve()
-            with two.open("w") as fobj:
-                fobj.write("print('hello')")
-            black.write_cache({}, [one], mode)
-            self.invokeBlack([str(workspace)])
-            with one.open("r") as fobj:
-                self.assertEqual(fobj.read(), "print('hello')")
-            with two.open("r") as fobj:
-                self.assertEqual(fobj.read(), 'print("hello")\n')
-            cache = black.read_cache(mode)
-            self.assertIn(one, cache)
-            self.assertIn(two, cache)
-
     def test_no_cache_when_writeback_diff(self) -> None:
         mode = black.FileMode()
         with cache_dir() as workspace:
     def test_no_cache_when_writeback_diff(self) -> None:
         mode = black.FileMode()
         with cache_dir() as workspace:
@@ -1352,7 +1340,7 @@ class BlackTestCase(unittest.TestCase):
             black.write_cache({}, [], mode)
             self.assertTrue(workspace.exists())
 
             black.write_cache({}, [], mode)
             self.assertTrue(workspace.exists())
 
-    @event_loop(close=False)
+    @event_loop()
     def test_failed_formatting_does_not_get_cached(self) -> None:
         mode = black.FileMode()
         with cache_dir() as workspace, patch(
     def test_failed_formatting_does_not_get_cached(self) -> None:
         mode = black.FileMode()
         with cache_dir() as workspace, patch(
@@ -1375,7 +1363,18 @@ class BlackTestCase(unittest.TestCase):
             mock.side_effect = OSError
             black.write_cache({}, [], mode)
 
             mock.side_effect = OSError
             black.write_cache({}, [], mode)
 
-    @event_loop(close=False)
+    @event_loop()
+    @patch("black.ProcessPoolExecutor", MagicMock(side_effect=OSError))
+    def test_works_in_mono_process_only_environment(self) -> None:
+        with cache_dir() as workspace:
+            for f in [
+                (workspace / "one.py").resolve(),
+                (workspace / "two.py").resolve(),
+            ]:
+                f.write_text('print("hello")\n')
+            self.invokeBlack([str(workspace)])
+
+    @event_loop()
     def test_check_diff_use_together(self) -> None:
         with cache_dir():
             # Files which will be reformatted.
     def test_check_diff_use_together(self) -> None:
         with cache_dir():
             # Files which will be reformatted.
@@ -1438,7 +1437,7 @@ class BlackTestCase(unittest.TestCase):
             self.assertNotIn(path, normal_cache)
         self.assertEqual(actual, expected)
 
             self.assertNotIn(path, normal_cache)
         self.assertEqual(actual, expected)
 
-    @event_loop(close=False)
+    @event_loop()
     def test_multi_file_force_pyi(self) -> None:
         reg_mode = black.FileMode()
         pyi_mode = black.FileMode(is_pyi=True)
     def test_multi_file_force_pyi(self) -> None:
         reg_mode = black.FileMode()
         pyi_mode = black.FileMode(is_pyi=True)
@@ -1490,7 +1489,7 @@ class BlackTestCase(unittest.TestCase):
             self.assertNotIn(path, normal_cache)
         self.assertEqual(actual, expected)
 
             self.assertNotIn(path, normal_cache)
         self.assertEqual(actual, expected)
 
-    @event_loop(close=False)
+    @event_loop()
     def test_multi_file_force_py36(self) -> None:
         reg_mode = black.FileMode()
         py36_mode = black.FileMode(target_versions=black.PY36_VERSIONS)
     def test_multi_file_force_py36(self) -> None:
         reg_mode = black.FileMode()
         py36_mode = black.FileMode(target_versions=black.PY36_VERSIONS)
@@ -1546,8 +1545,8 @@ class BlackTestCase(unittest.TestCase):
         ]
         this_abs = THIS_DIR.resolve()
         sources.extend(
         ]
         this_abs = THIS_DIR.resolve()
         sources.extend(
-            black.gen_python_files_in_dir(
-                path, this_abs, include, exclude, report, gitignore
+            black.gen_python_files(
+                path.iterdir(), this_abs, include, [exclude], report, gitignore
             )
         )
         self.assertEqual(sorted(expected), sorted(sources))
             )
         )
         self.assertEqual(sorted(expected), sorted(sources))
@@ -1567,8 +1566,8 @@ class BlackTestCase(unittest.TestCase):
         ]
         this_abs = THIS_DIR.resolve()
         sources.extend(
         ]
         this_abs = THIS_DIR.resolve()
         sources.extend(
-            black.gen_python_files_in_dir(
-                path, this_abs, include, exclude, report, gitignore
+            black.gen_python_files(
+                path.iterdir(), this_abs, include, [exclude], report, gitignore
             )
         )
         self.assertEqual(sorted(expected), sorted(sources))
             )
         )
         self.assertEqual(sorted(expected), sorted(sources))
@@ -1592,11 +1591,11 @@ class BlackTestCase(unittest.TestCase):
         ]
         this_abs = THIS_DIR.resolve()
         sources.extend(
         ]
         this_abs = THIS_DIR.resolve()
         sources.extend(
-            black.gen_python_files_in_dir(
-                path,
+            black.gen_python_files(
+                path.iterdir(),
                 this_abs,
                 empty,
                 this_abs,
                 empty,
-                re.compile(black.DEFAULT_EXCLUDES),
+                [re.compile(black.DEFAULT_EXCLUDES)],
                 report,
                 gitignore,
             )
                 report,
                 gitignore,
             )
@@ -1619,11 +1618,11 @@ class BlackTestCase(unittest.TestCase):
         ]
         this_abs = THIS_DIR.resolve()
         sources.extend(
         ]
         this_abs = THIS_DIR.resolve()
         sources.extend(
-            black.gen_python_files_in_dir(
-                path,
+            black.gen_python_files(
+                path.iterdir(),
                 this_abs,
                 re.compile(black.DEFAULT_INCLUDES),
                 this_abs,
                 re.compile(black.DEFAULT_INCLUDES),
-                empty,
+                [empty],
                 report,
                 gitignore,
             )
                 report,
                 gitignore,
             )
@@ -1665,7 +1664,7 @@ class BlackTestCase(unittest.TestCase):
 
     def test_symlink_out_of_root_directory(self) -> None:
         path = MagicMock()
 
     def test_symlink_out_of_root_directory(self) -> None:
         path = MagicMock()
-        root = THIS_DIR
+        root = THIS_DIR.resolve()
         child = MagicMock()
         include = re.compile(black.DEFAULT_INCLUDES)
         exclude = re.compile(black.DEFAULT_EXCLUDES)
         child = MagicMock()
         include = re.compile(black.DEFAULT_INCLUDES)
         exclude = re.compile(black.DEFAULT_EXCLUDES)
@@ -1679,8 +1678,8 @@ class BlackTestCase(unittest.TestCase):
         child.is_symlink.return_value = True
         try:
             list(
         child.is_symlink.return_value = True
         try:
             list(
-                black.gen_python_files_in_dir(
-                    path, root, include, exclude, report, gitignore
+                black.gen_python_files(
+                    path.iterdir(), root, include, exclude, report, gitignore
                 )
             )
         except ValueError as ve:
                 )
             )
         except ValueError as ve:
@@ -1693,8 +1692,8 @@ class BlackTestCase(unittest.TestCase):
         child.is_symlink.return_value = False
         with self.assertRaises(ValueError):
             list(
         child.is_symlink.return_value = False
         with self.assertRaises(ValueError):
             list(
-                black.gen_python_files_in_dir(
-                    path, root, include, exclude, report, gitignore
+                black.gen_python_files(
+                    path.iterdir(), root, include, exclude, report, gitignore
                 )
             )
         path.iterdir.assert_called()
                 )
             )
         path.iterdir.assert_called()