import types
import unittest
from concurrent.futures import ThreadPoolExecutor
-from contextlib import contextmanager
+from contextlib import contextmanager, redirect_stderr
from dataclasses import replace
from io import BytesIO
from pathlib import Path
)
THIS_FILE = Path(__file__)
+EMPTY_CONFIG = THIS_DIR / "data" / "empty_pyproject.toml"
PY36_ARGS = [f"--target-version={version.name.lower()}" for version in PY36_VERSIONS]
DEFAULT_EXCLUDE = black.re_compile_maybe_verbose(black.const.DEFAULT_EXCLUDES)
DEFAULT_INCLUDE = black.re_compile_maybe_verbose(black.const.DEFAULT_INCLUDES)
source, expected = read_data("src/black/__init__", data=False)
result = BlackRunner().invoke(
black.main,
- ["-", "--fast", f"--line-length={black.DEFAULT_LINE_LENGTH}"],
+ [
+ "-",
+ "--fast",
+ f"--line-length={black.DEFAULT_LINE_LENGTH}",
+ f"--config={EMPTY_CONFIG}",
+ ],
input=BytesIO(source.encode("utf8")),
)
self.assertEqual(result.exit_code, 0)
)
source, _ = read_data("expression.py")
expected, _ = read_data("expression.diff")
- config = THIS_DIR / "data" / "empty_pyproject.toml"
args = [
"-",
"--fast",
f"--line-length={black.DEFAULT_LINE_LENGTH}",
"--diff",
- f"--config={config}",
+ f"--config={EMPTY_CONFIG}",
]
result = BlackRunner().invoke(
black.main, args, input=BytesIO(source.encode("utf8"))
def test_piping_diff_with_color(self) -> None:
source, _ = read_data("expression.py")
- config = THIS_DIR / "data" / "empty_pyproject.toml"
args = [
"-",
"--fast",
f"--line-length={black.DEFAULT_LINE_LENGTH}",
"--diff",
"--color",
- f"--config={config}",
+ f"--config={EMPTY_CONFIG}",
]
result = BlackRunner().invoke(
black.main, args, input=BytesIO(source.encode("utf8"))
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())
- @unittest.expectedFailure
- @patch("black.dump_to_file", dump_to_stderr)
- def test_trailing_comma_optional_parens_stability1(self) -> None:
- source, _expected = read_data("trailing_comma_optional_parens1")
- actual = fs(source)
- black.assert_stable(source, actual, DEFAULT_MODE)
-
- @unittest.expectedFailure
- @patch("black.dump_to_file", dump_to_stderr)
- def test_trailing_comma_optional_parens_stability2(self) -> None:
- source, _expected = read_data("trailing_comma_optional_parens2")
- actual = fs(source)
- black.assert_stable(source, actual, DEFAULT_MODE)
-
- @unittest.expectedFailure
- @patch("black.dump_to_file", dump_to_stderr)
- def test_trailing_comma_optional_parens_stability3(self) -> None:
- source, _expected = read_data("trailing_comma_optional_parens3")
- actual = fs(source)
- black.assert_stable(source, actual, DEFAULT_MODE)
-
- @patch("black.dump_to_file", dump_to_stderr)
- def test_trailing_comma_optional_parens_stability1_pass2(self) -> None:
- source, _expected = read_data("trailing_comma_optional_parens1")
- actual = fs(fs(source)) # this is what `format_file_contents` does with --safe
- black.assert_stable(source, actual, DEFAULT_MODE)
-
- @patch("black.dump_to_file", dump_to_stderr)
- def test_trailing_comma_optional_parens_stability2_pass2(self) -> None:
- source, _expected = read_data("trailing_comma_optional_parens2")
- actual = fs(fs(source)) # this is what `format_file_contents` does with --safe
- black.assert_stable(source, actual, DEFAULT_MODE)
-
- @patch("black.dump_to_file", dump_to_stderr)
- def test_trailing_comma_optional_parens_stability3_pass2(self) -> None:
- source, _expected = read_data("trailing_comma_optional_parens3")
- actual = fs(fs(source)) # this is what `format_file_contents` does with --safe
- black.assert_stable(source, actual, DEFAULT_MODE)
-
def test_pep_572_version_detection(self) -> None:
source, _ = read_data("pep_572")
root = black.lib2to3_parse(source)
def test_expression_diff(self) -> None:
source, _ = read_data("expression.py")
- config = THIS_DIR / "data" / "empty_pyproject.toml"
expected, _ = read_data("expression.diff")
tmp_file = Path(black.dump_to_file(source))
diff_header = re.compile(
)
try:
result = BlackRunner().invoke(
- black.main, ["--diff", str(tmp_file), f"--config={config}"]
+ black.main, ["--diff", str(tmp_file), f"--config={EMPTY_CONFIG}"]
)
self.assertEqual(result.exit_code, 0)
finally:
def test_expression_diff_with_color(self) -> None:
source, _ = read_data("expression.py")
- config = THIS_DIR / "data" / "empty_pyproject.toml"
expected, _ = read_data("expression.diff")
tmp_file = Path(black.dump_to_file(source))
try:
result = BlackRunner().invoke(
- black.main, ["--diff", "--color", str(tmp_file), f"--config={config}"]
+ black.main,
+ ["--diff", "--color", str(tmp_file), f"--config={EMPTY_CONFIG}"],
)
finally:
os.unlink(tmp_file)
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)])
+ result = BlackRunner().invoke(
+ black.main, ["-C", "--diff", str(tmp_file), f"--config={EMPTY_CONFIG}"]
+ )
self.assertEqual(result.exit_code, 0)
finally:
os.unlink(tmp_file)
# Multi file command.
self.invokeBlack([str(src1), str(src2), "--diff", "--check"], exit_code=1)
- def test_no_files(self) -> None:
+ def test_no_src_fails(self) -> None:
with cache_dir():
- # Without an argument, black exits with error code 0.
- self.invokeBlack([])
+ self.invokeBlack([], exit_code=1)
+
+ def test_src_and_code_fails(self) -> None:
+ with cache_dir():
+ self.invokeBlack([".", "-c", "0"], exit_code=1)
def test_broken_symlink(self) -> None:
with cache_dir() as workspace:
def test_required_version_matches_version(self) -> None:
self.invokeBlack(
- ["--required-version", black.__version__], exit_code=0, ignore_config=True
+ ["--required-version", black.__version__, "-c", "0"],
+ exit_code=0,
+ ignore_config=True,
)
- def test_required_version_does_not_match_version(self) -> None:
+ def test_required_version_matches_partial_version(self) -> None:
self.invokeBlack(
- ["--required-version", "20.99b"], exit_code=1, ignore_config=True
+ ["--required-version", black.__version__.split(".")[0], "-c", "0"],
+ exit_code=0,
+ ignore_config=True,
+ )
+
+ def test_required_version_does_not_match_on_minor_version(self) -> None:
+ self.invokeBlack(
+ ["--required-version", black.__version__.split(".")[0] + ".999", "-c", "0"],
+ exit_code=1,
+ ignore_config=True,
+ )
+
+ def test_required_version_does_not_match_version(self) -> None:
+ result = BlackRunner().invoke(
+ black.main,
+ ["--required-version", "20.99b", "-c", "0"],
)
+ self.assertEqual(result.exit_code, 1)
+ self.assertIn("required version", result.stderr)
def test_preserves_line_endings(self) -> None:
with TemporaryDirectory() as workspace:
(src_dir.resolve(), "pyproject.toml"),
)
+ @patch(
+ "black.files.find_user_pyproject_toml",
+ )
+ def test_find_pyproject_toml(self, find_user_pyproject_toml: MagicMock) -> None:
+ find_user_pyproject_toml.side_effect = RuntimeError()
+
+ with redirect_stderr(io.StringIO()) as stderr:
+ result = black.files.find_pyproject_toml(
+ path_search_start=(str(Path.cwd().root),)
+ )
+
+ assert result is None
+ err = stderr.getvalue()
+ assert "Ignoring user configuration" in err
+
@patch(
"black.files.find_user_pyproject_toml",
black.files.find_user_pyproject_toml.__wrapped__,