X-Git-Url: https://git.madduck.net/etc/vim.git/blobdiff_plain/8d6d92aa5b5248b5ff70ebf7977f8af5cbcb10b9..0721f11731b64b1b134f3cb7c227a6477044e8fa:/tests/test_black.py diff --git a/tests/test_black.py b/tests/test_black.py index 11bba1f..6fb01f7 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -82,7 +82,7 @@ def cache_dir(exists: bool = True) -> Iterator[Path]: @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) @@ -90,8 +90,7 @@ def event_loop(close: bool) -> Iterator[None]: yield finally: - if close: - loop.close() + loop.close() @contextmanager @@ -157,9 +156,18 @@ class BlackTestCase(unittest.TestCase): ) -> 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) - 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: @@ -1251,7 +1259,7 @@ class BlackTestCase(unittest.TestCase): 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( @@ -1331,7 +1339,7 @@ class BlackTestCase(unittest.TestCase): 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( @@ -1354,7 +1362,18 @@ class BlackTestCase(unittest.TestCase): 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. @@ -1417,7 +1436,7 @@ class BlackTestCase(unittest.TestCase): 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) @@ -1469,7 +1488,7 @@ class BlackTestCase(unittest.TestCase): 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) @@ -1525,8 +1544,8 @@ class BlackTestCase(unittest.TestCase): ] 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)) @@ -1546,8 +1565,8 @@ class BlackTestCase(unittest.TestCase): ] 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)) @@ -1571,11 +1590,11 @@ class BlackTestCase(unittest.TestCase): ] this_abs = THIS_DIR.resolve() sources.extend( - black.gen_python_files_in_dir( - path, + black.gen_python_files( + path.iterdir(), this_abs, empty, - re.compile(black.DEFAULT_EXCLUDES), + [re.compile(black.DEFAULT_EXCLUDES)], report, gitignore, ) @@ -1598,11 +1617,11 @@ class BlackTestCase(unittest.TestCase): ] 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), - empty, + [empty], report, gitignore, ) @@ -1658,8 +1677,8 @@ class BlackTestCase(unittest.TestCase): 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: @@ -1672,8 +1691,8 @@ class BlackTestCase(unittest.TestCase): 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()