+ src2 = (THIS_DIR / "data" / "composition.py").resolve()
+ self.invokeBlack([str(src2), "--diff", "--check"])
+ # Multi file command.
+ self.invokeBlack([str(src1), str(src2), "--diff", "--check"], exit_code=1)
+
+ def test_no_files(self) -> None:
+ with cache_dir():
+ # Without an argument, black exits with error code 0.
+ self.invokeBlack([])
+
+ def test_broken_symlink(self) -> None:
+ with cache_dir() as workspace:
+ symlink = workspace / "broken_link.py"
+ try:
+ symlink.symlink_to("nonexistent.py")
+ except OSError as e:
+ self.skipTest(f"Can't create symlinks: {e}")
+ self.invokeBlack([str(workspace.resolve())])
+
+ def test_read_cache_line_lengths(self) -> None:
+ mode = black.FileMode()
+ short_mode = black.FileMode(line_length=1)
+ with cache_dir() as workspace:
+ path = (workspace / "file.py").resolve()
+ path.touch()
+ black.write_cache({}, [path], mode)
+ one = black.read_cache(mode)
+ self.assertIn(path, one)
+ two = black.read_cache(short_mode)
+ self.assertNotIn(path, two)
+
+ def test_single_file_force_pyi(self) -> None:
+ reg_mode = black.FileMode()
+ pyi_mode = black.FileMode(is_pyi=True)
+ contents, expected = read_data("force_pyi")
+ with cache_dir() as workspace:
+ path = (workspace / "file.py").resolve()
+ with open(path, "w") as fh:
+ fh.write(contents)
+ self.invokeBlack([str(path), "--pyi"])
+ with open(path, "r") as fh:
+ actual = fh.read()
+ # verify cache with --pyi is separate
+ pyi_cache = black.read_cache(pyi_mode)
+ self.assertIn(path, pyi_cache)
+ normal_cache = black.read_cache(reg_mode)
+ self.assertNotIn(path, normal_cache)
+ self.assertEqual(actual, expected)
+
+ @event_loop(close=False)
+ def test_multi_file_force_pyi(self) -> None:
+ reg_mode = black.FileMode()
+ pyi_mode = black.FileMode(is_pyi=True)
+ contents, expected = read_data("force_pyi")
+ with cache_dir() as workspace:
+ paths = [
+ (workspace / "file1.py").resolve(),
+ (workspace / "file2.py").resolve(),
+ ]
+ for path in paths:
+ with open(path, "w") as fh:
+ fh.write(contents)
+ self.invokeBlack([str(p) for p in paths] + ["--pyi"])
+ for path in paths:
+ with open(path, "r") as fh:
+ actual = fh.read()
+ self.assertEqual(actual, expected)
+ # verify cache with --pyi is separate
+ pyi_cache = black.read_cache(pyi_mode)
+ normal_cache = black.read_cache(reg_mode)
+ for path in paths:
+ self.assertIn(path, pyi_cache)
+ self.assertNotIn(path, normal_cache)
+
+ def test_pipe_force_pyi(self) -> None:
+ source, expected = read_data("force_pyi")
+ result = CliRunner().invoke(
+ black.main, ["-", "-q", "--pyi"], input=BytesIO(source.encode("utf8"))
+ )
+ self.assertEqual(result.exit_code, 0)
+ actual = result.output
+ self.assertFormatEqual(actual, expected)
+
+ def test_single_file_force_py36(self) -> None:
+ reg_mode = black.FileMode()
+ py36_mode = black.FileMode(target_versions=black.PY36_VERSIONS)
+ source, expected = read_data("force_py36")
+ with cache_dir() as workspace:
+ path = (workspace / "file.py").resolve()
+ with open(path, "w") as fh:
+ fh.write(source)
+ self.invokeBlack([str(path), *PY36_ARGS])
+ with open(path, "r") as fh:
+ actual = fh.read()
+ # verify cache with --target-version is separate
+ py36_cache = black.read_cache(py36_mode)
+ self.assertIn(path, py36_cache)
+ normal_cache = black.read_cache(reg_mode)
+ self.assertNotIn(path, normal_cache)
+ self.assertEqual(actual, expected)
+
+ @event_loop(close=False)
+ def test_multi_file_force_py36(self) -> None:
+ reg_mode = black.FileMode()
+ py36_mode = black.FileMode(target_versions=black.PY36_VERSIONS)
+ source, expected = read_data("force_py36")
+ with cache_dir() as workspace:
+ paths = [
+ (workspace / "file1.py").resolve(),
+ (workspace / "file2.py").resolve(),
+ ]
+ for path in paths:
+ with open(path, "w") as fh:
+ fh.write(source)
+ self.invokeBlack([str(p) for p in paths] + PY36_ARGS)
+ for path in paths:
+ with open(path, "r") as fh:
+ actual = fh.read()
+ self.assertEqual(actual, expected)
+ # verify cache with --target-version is separate
+ pyi_cache = black.read_cache(py36_mode)
+ normal_cache = black.read_cache(reg_mode)
+ for path in paths:
+ self.assertIn(path, pyi_cache)
+ self.assertNotIn(path, normal_cache)
+
+ def test_pipe_force_py36(self) -> None:
+ source, expected = read_data("force_py36")
+ result = CliRunner().invoke(
+ black.main,
+ ["-", "-q", "--target-version=py36"],
+ input=BytesIO(source.encode("utf8")),
+ )
+ self.assertEqual(result.exit_code, 0)
+ actual = result.output
+ self.assertFormatEqual(actual, expected)
+
+ def test_include_exclude(self) -> None:
+ path = THIS_DIR / "data" / "include_exclude_tests"
+ include = re.compile(r"\.pyi?$")
+ exclude = re.compile(r"/exclude/|/\.definitely_exclude/")
+ report = black.Report()
+ sources: List[Path] = []
+ expected = [
+ Path(path / "b/dont_exclude/a.py"),
+ Path(path / "b/dont_exclude/a.pyi"),
+ ]
+ this_abs = THIS_DIR.resolve()
+ sources.extend(
+ black.gen_python_files_in_dir(path, this_abs, include, exclude, report)
+ )
+ self.assertEqual(sorted(expected), sorted(sources))
+
+ def test_empty_include(self) -> None:
+ path = THIS_DIR / "data" / "include_exclude_tests"
+ report = black.Report()
+ empty = re.compile(r"")
+ sources: List[Path] = []
+ expected = [
+ Path(path / "b/exclude/a.pie"),
+ Path(path / "b/exclude/a.py"),
+ Path(path / "b/exclude/a.pyi"),
+ Path(path / "b/dont_exclude/a.pie"),
+ Path(path / "b/dont_exclude/a.py"),
+ Path(path / "b/dont_exclude/a.pyi"),
+ Path(path / "b/.definitely_exclude/a.pie"),
+ Path(path / "b/.definitely_exclude/a.py"),
+ Path(path / "b/.definitely_exclude/a.pyi"),
+ ]
+ this_abs = THIS_DIR.resolve()
+ sources.extend(
+ black.gen_python_files_in_dir(
+ path, this_abs, empty, re.compile(black.DEFAULT_EXCLUDES), report
+ )
+ )
+ self.assertEqual(sorted(expected), sorted(sources))
+
+ def test_empty_exclude(self) -> None:
+ path = THIS_DIR / "data" / "include_exclude_tests"
+ report = black.Report()
+ empty = re.compile(r"")
+ sources: List[Path] = []
+ expected = [
+ Path(path / "b/dont_exclude/a.py"),
+ Path(path / "b/dont_exclude/a.pyi"),
+ Path(path / "b/exclude/a.py"),
+ Path(path / "b/exclude/a.pyi"),
+ Path(path / "b/.definitely_exclude/a.py"),
+ Path(path / "b/.definitely_exclude/a.pyi"),
+ ]
+ this_abs = THIS_DIR.resolve()
+ sources.extend(
+ black.gen_python_files_in_dir(
+ path, this_abs, re.compile(black.DEFAULT_INCLUDES), empty, report
+ )
+ )
+ self.assertEqual(sorted(expected), sorted(sources))
+
+ def test_invalid_include_exclude(self) -> None:
+ for option in ["--include", "--exclude"]:
+ self.invokeBlack(["-", option, "**()(!!*)"], exit_code=2)
+
+ def test_preserves_line_endings(self) -> None:
+ with TemporaryDirectory() as workspace:
+ test_file = Path(workspace) / "test.py"
+ for nl in ["\n", "\r\n"]:
+ contents = nl.join(["def f( ):", " pass"])
+ test_file.write_bytes(contents.encode())
+ ff(test_file, write_back=black.WriteBack.YES)
+ updated_contents: bytes = test_file.read_bytes()
+ self.assertIn(nl.encode(), updated_contents)
+ if nl == "\n":
+ self.assertNotIn(b"\r\n", updated_contents)
+
+ def test_preserves_line_endings_via_stdin(self) -> None:
+ for nl in ["\n", "\r\n"]:
+ contents = nl.join(["def f( ):", " pass"])
+ runner = BlackRunner()
+ result = runner.invoke(
+ black.main, ["-", "--fast"], input=BytesIO(contents.encode("utf8"))
+ )