Remove hard coded test cases (#3062)
This commit is contained in:
parent
7f033136ac
commit
2893c42176
@ -60,6 +60,8 @@
|
||||
ff,
|
||||
fs,
|
||||
read_data,
|
||||
get_case_path,
|
||||
read_data_from_file,
|
||||
)
|
||||
|
||||
THIS_FILE = Path(__file__)
|
||||
@ -157,7 +159,7 @@ def test_experimental_string_processing_warns(self) -> None:
|
||||
)
|
||||
|
||||
def test_piping(self) -> None:
|
||||
source, expected = read_data("src/black/__init__", data=False)
|
||||
source, expected = read_data_from_file(PROJECT_ROOT / "src/black/__init__.py")
|
||||
result = BlackRunner().invoke(
|
||||
black.main,
|
||||
[
|
||||
@ -179,8 +181,8 @@ def test_piping_diff(self) -> None:
|
||||
r"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d "
|
||||
r"\+\d\d\d\d"
|
||||
)
|
||||
source, _ = read_data("simple_cases/expression.py")
|
||||
expected, _ = read_data("simple_cases/expression.diff")
|
||||
source, _ = read_data("simple_cases", "expression.py")
|
||||
expected, _ = read_data("simple_cases", "expression.diff")
|
||||
args = [
|
||||
"-",
|
||||
"--fast",
|
||||
@ -197,7 +199,7 @@ def test_piping_diff(self) -> None:
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_piping_diff_with_color(self) -> None:
|
||||
source, _ = read_data("simple_cases/expression.py")
|
||||
source, _ = read_data("simple_cases", "expression.py")
|
||||
args = [
|
||||
"-",
|
||||
"--fast",
|
||||
@ -219,7 +221,7 @@ def test_piping_diff_with_color(self) -> None:
|
||||
|
||||
@patch("black.dump_to_file", dump_to_stderr)
|
||||
def _test_wip(self) -> None:
|
||||
source, expected = read_data("wip")
|
||||
source, expected = read_data("miscellaneous", "wip")
|
||||
sys.settrace(tracefunc)
|
||||
mode = replace(
|
||||
DEFAULT_MODE,
|
||||
@ -233,7 +235,7 @@ def _test_wip(self) -> None:
|
||||
black.assert_stable(source, actual, black.FileMode())
|
||||
|
||||
def test_pep_572_version_detection(self) -> None:
|
||||
source, _ = read_data("pep_572")
|
||||
source, _ = read_data("py_38", "pep_572")
|
||||
root = black.lib2to3_parse(source)
|
||||
features = black.get_features_used(root)
|
||||
self.assertIn(black.Feature.ASSIGNMENT_EXPRESSIONS, features)
|
||||
@ -241,7 +243,7 @@ def test_pep_572_version_detection(self) -> None:
|
||||
self.assertIn(black.TargetVersion.PY38, versions)
|
||||
|
||||
def test_expression_ff(self) -> None:
|
||||
source, expected = read_data("simple_cases/expression.py")
|
||||
source, expected = read_data("simple_cases", "expression.py")
|
||||
tmp_file = Path(black.dump_to_file(source))
|
||||
try:
|
||||
self.assertTrue(ff(tmp_file, write_back=black.WriteBack.YES))
|
||||
@ -255,8 +257,8 @@ def test_expression_ff(self) -> None:
|
||||
black.assert_stable(source, actual, DEFAULT_MODE)
|
||||
|
||||
def test_expression_diff(self) -> None:
|
||||
source, _ = read_data("simple_cases/expression.py")
|
||||
expected, _ = read_data("simple_cases/expression.diff")
|
||||
source, _ = read_data("simple_cases", "expression.py")
|
||||
expected, _ = read_data("simple_cases", "expression.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 "
|
||||
@ -281,8 +283,8 @@ def test_expression_diff(self) -> None:
|
||||
self.assertEqual(expected, actual, msg)
|
||||
|
||||
def test_expression_diff_with_color(self) -> None:
|
||||
source, _ = read_data("simple_cases/expression.py")
|
||||
expected, _ = read_data("simple_cases/expression.diff")
|
||||
source, _ = read_data("simple_cases", "expression.py")
|
||||
expected, _ = read_data("simple_cases", "expression.diff")
|
||||
tmp_file = Path(black.dump_to_file(source))
|
||||
try:
|
||||
result = BlackRunner().invoke(
|
||||
@ -301,7 +303,7 @@ def test_expression_diff_with_color(self) -> None:
|
||||
self.assertIn("\033[0m", actual)
|
||||
|
||||
def test_detect_pos_only_arguments(self) -> None:
|
||||
source, _ = read_data("pep_570")
|
||||
source, _ = read_data("py_38", "pep_570")
|
||||
root = black.lib2to3_parse(source)
|
||||
features = black.get_features_used(root)
|
||||
self.assertIn(black.Feature.POS_ONLY_ARGUMENTS, features)
|
||||
@ -310,7 +312,7 @@ def test_detect_pos_only_arguments(self) -> None:
|
||||
|
||||
@patch("black.dump_to_file", dump_to_stderr)
|
||||
def test_string_quotes(self) -> None:
|
||||
source, expected = read_data("string_quotes")
|
||||
source, expected = read_data("miscellaneous", "string_quotes")
|
||||
mode = black.Mode(preview=True)
|
||||
assert_format(source, expected, mode)
|
||||
mode = replace(mode, string_normalization=False)
|
||||
@ -320,8 +322,10 @@ def test_string_quotes(self) -> None:
|
||||
black.assert_stable(source, not_normalized, mode=mode)
|
||||
|
||||
def test_skip_magic_trailing_comma(self) -> None:
|
||||
source, _ = read_data("simple_cases/expression.py")
|
||||
expected, _ = read_data("expression_skip_magic_trailing_comma.diff")
|
||||
source, _ = read_data("simple_cases", "expression")
|
||||
expected, _ = read_data(
|
||||
"miscellaneous", "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 "
|
||||
@ -348,8 +352,8 @@ def test_skip_magic_trailing_comma(self) -> None:
|
||||
|
||||
@patch("black.dump_to_file", dump_to_stderr)
|
||||
def test_async_as_identifier(self) -> None:
|
||||
source_path = (THIS_DIR / "data" / "async_as_identifier.py").resolve()
|
||||
source, expected = read_data("async_as_identifier")
|
||||
source_path = get_case_path("miscellaneous", "async_as_identifier")
|
||||
source, expected = read_data_from_file(source_path)
|
||||
actual = fs(source)
|
||||
self.assertFormatEqual(expected, actual)
|
||||
major, minor = sys.version_info[:2]
|
||||
@ -363,8 +367,8 @@ def test_async_as_identifier(self) -> None:
|
||||
|
||||
@patch("black.dump_to_file", dump_to_stderr)
|
||||
def test_python37(self) -> None:
|
||||
source_path = (THIS_DIR / "data" / "python37.py").resolve()
|
||||
source, expected = read_data("python37")
|
||||
source_path = get_case_path("py_37", "python37")
|
||||
source, expected = read_data_from_file(source_path)
|
||||
actual = fs(source)
|
||||
self.assertFormatEqual(expected, actual)
|
||||
major, minor = sys.version_info[:2]
|
||||
@ -712,7 +716,7 @@ def test_get_features_used_decorator(self) -> None:
|
||||
# since this makes some test cases of test_get_features_used()
|
||||
# fails if it fails, this is tested first so that a useful case
|
||||
# is identified
|
||||
simples, relaxed = read_data("decorators")
|
||||
simples, relaxed = read_data("miscellaneous", "decorators")
|
||||
# skip explanation comments at the top of the file
|
||||
for simple_test in simples.split("##")[1:]:
|
||||
node = black.lib2to3_parse(simple_test)
|
||||
@ -755,7 +759,7 @@ def test_get_features_used(self) -> None:
|
||||
self.assertEqual(black.get_features_used(node), {Feature.NUMERIC_UNDERSCORES})
|
||||
node = black.lib2to3_parse("123456\n")
|
||||
self.assertEqual(black.get_features_used(node), set())
|
||||
source, expected = read_data("simple_cases/function.py")
|
||||
source, expected = read_data("simple_cases", "function")
|
||||
node = black.lib2to3_parse(source)
|
||||
expected_features = {
|
||||
Feature.TRAILING_COMMA_IN_CALL,
|
||||
@ -765,7 +769,7 @@ def test_get_features_used(self) -> None:
|
||||
self.assertEqual(black.get_features_used(node), expected_features)
|
||||
node = black.lib2to3_parse(expected)
|
||||
self.assertEqual(black.get_features_used(node), expected_features)
|
||||
source, expected = read_data("simple_cases/expression.py")
|
||||
source, expected = read_data("simple_cases", "expression")
|
||||
node = black.lib2to3_parse(source)
|
||||
self.assertEqual(black.get_features_used(node), set())
|
||||
node = black.lib2to3_parse(expected)
|
||||
@ -851,8 +855,8 @@ def test_get_future_imports(self) -> None:
|
||||
|
||||
@pytest.mark.incompatible_with_mypyc
|
||||
def test_debug_visitor(self) -> None:
|
||||
source, _ = read_data("debug_visitor.py")
|
||||
expected, _ = read_data("debug_visitor.out")
|
||||
source, _ = read_data("miscellaneous", "debug_visitor")
|
||||
expected, _ = read_data("miscellaneous", "debug_visitor.out")
|
||||
out_lines = []
|
||||
err_lines = []
|
||||
|
||||
@ -936,10 +940,10 @@ def test_works_in_mono_process_only_environment(self) -> None:
|
||||
def test_check_diff_use_together(self) -> None:
|
||||
with cache_dir():
|
||||
# Files which will be reformatted.
|
||||
src1 = (THIS_DIR / "data" / "string_quotes.py").resolve()
|
||||
src1 = get_case_path("miscellaneous", "string_quotes")
|
||||
self.invokeBlack([str(src1), "--diff", "--check"], exit_code=1)
|
||||
# Files which will not be reformatted.
|
||||
src2 = (THIS_DIR / "data" / "simple_cases" / "composition.py").resolve()
|
||||
src2 = get_case_path("simple_cases", "composition")
|
||||
self.invokeBlack([str(src2), "--diff", "--check"])
|
||||
# Multi file command.
|
||||
self.invokeBlack([str(src1), str(src2), "--diff", "--check"], exit_code=1)
|
||||
@ -963,7 +967,7 @@ def test_broken_symlink(self) -> None:
|
||||
|
||||
def test_single_file_force_pyi(self) -> None:
|
||||
pyi_mode = replace(DEFAULT_MODE, is_pyi=True)
|
||||
contents, expected = read_data("force_pyi")
|
||||
contents, expected = read_data("miscellaneous", "force_pyi")
|
||||
with cache_dir() as workspace:
|
||||
path = (workspace / "file.py").resolve()
|
||||
with open(path, "w") as fh:
|
||||
@ -984,7 +988,7 @@ def test_single_file_force_pyi(self) -> None:
|
||||
def test_multi_file_force_pyi(self) -> None:
|
||||
reg_mode = DEFAULT_MODE
|
||||
pyi_mode = replace(DEFAULT_MODE, is_pyi=True)
|
||||
contents, expected = read_data("force_pyi")
|
||||
contents, expected = read_data("miscellaneous", "force_pyi")
|
||||
with cache_dir() as workspace:
|
||||
paths = [
|
||||
(workspace / "file1.py").resolve(),
|
||||
@ -1006,7 +1010,7 @@ def test_multi_file_force_pyi(self) -> None:
|
||||
self.assertNotIn(str(path), normal_cache)
|
||||
|
||||
def test_pipe_force_pyi(self) -> None:
|
||||
source, expected = read_data("force_pyi")
|
||||
source, expected = read_data("miscellaneous", "force_pyi")
|
||||
result = CliRunner().invoke(
|
||||
black.main, ["-", "-q", "--pyi"], input=BytesIO(source.encode("utf8"))
|
||||
)
|
||||
@ -1017,7 +1021,7 @@ def test_pipe_force_pyi(self) -> None:
|
||||
def test_single_file_force_py36(self) -> None:
|
||||
reg_mode = DEFAULT_MODE
|
||||
py36_mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
|
||||
source, expected = read_data("force_py36")
|
||||
source, expected = read_data("miscellaneous", "force_py36")
|
||||
with cache_dir() as workspace:
|
||||
path = (workspace / "file.py").resolve()
|
||||
with open(path, "w") as fh:
|
||||
@ -1036,7 +1040,7 @@ def test_single_file_force_py36(self) -> None:
|
||||
def test_multi_file_force_py36(self) -> None:
|
||||
reg_mode = DEFAULT_MODE
|
||||
py36_mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
|
||||
source, expected = read_data("force_py36")
|
||||
source, expected = read_data("miscellaneous", "force_py36")
|
||||
with cache_dir() as workspace:
|
||||
paths = [
|
||||
(workspace / "file1.py").resolve(),
|
||||
@ -1058,7 +1062,7 @@ def test_multi_file_force_py36(self) -> None:
|
||||
self.assertNotIn(str(path), normal_cache)
|
||||
|
||||
def test_pipe_force_py36(self) -> None:
|
||||
source, expected = read_data("force_py36")
|
||||
source, expected = read_data("miscellaneous", "force_py36")
|
||||
result = CliRunner().invoke(
|
||||
black.main,
|
||||
["-", "-q", "--target-version=py36"],
|
||||
@ -1454,10 +1458,10 @@ def test_bpo_2142_workaround(self) -> None:
|
||||
|
||||
# https://bugs.python.org/issue2142
|
||||
|
||||
source, _ = read_data("missing_final_newline.py")
|
||||
source, _ = read_data("miscellaneous", "missing_final_newline")
|
||||
# read_data adds a trailing newline
|
||||
source = source.rstrip()
|
||||
expected, _ = read_data("missing_final_newline.diff")
|
||||
expected, _ = read_data("miscellaneous", "missing_final_newline.diff")
|
||||
tmp_file = Path(black.dump_to_file(source, ensure_final_newline=False))
|
||||
diff_header = re.compile(
|
||||
rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d "
|
||||
|
@ -95,7 +95,7 @@ async def check(header_value: str, expected_status: int = 400) -> None:
|
||||
|
||||
@unittest_run_loop
|
||||
async def test_blackd_pyi(self) -> None:
|
||||
source, expected = read_data("stub.pyi")
|
||||
source, expected = read_data("miscellaneous", "stub.pyi")
|
||||
response = await self.client.post(
|
||||
"/", data=source, headers={blackd.PYTHON_VARIANT_HEADER: "pyi"}
|
||||
)
|
||||
@ -108,8 +108,8 @@ async def test_blackd_diff(self) -> None:
|
||||
r"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d"
|
||||
)
|
||||
|
||||
source, _ = read_data("blackd_diff.py")
|
||||
expected, _ = read_data("blackd_diff.diff")
|
||||
source, _ = read_data("miscellaneous", "blackd_diff")
|
||||
expected, _ = read_data("miscellaneous", "blackd_diff.diff")
|
||||
|
||||
response = await self.client.post(
|
||||
"/", data=source, headers={blackd.DIFF_HEADER: "true"}
|
||||
|
@ -8,45 +8,12 @@
|
||||
from tests.util import (
|
||||
DEFAULT_MODE,
|
||||
PY36_VERSIONS,
|
||||
THIS_DIR,
|
||||
assert_format,
|
||||
dump_to_stderr,
|
||||
read_data,
|
||||
all_data_cases,
|
||||
)
|
||||
|
||||
PY310_CASES: List[str] = [
|
||||
"starred_for_target",
|
||||
"pattern_matching_simple",
|
||||
"pattern_matching_complex",
|
||||
"pattern_matching_extras",
|
||||
"pattern_matching_style",
|
||||
"pattern_matching_generic",
|
||||
"parenthesized_context_managers",
|
||||
]
|
||||
|
||||
PY311_CASES: List[str] = [
|
||||
"pep_654",
|
||||
"pep_654_style",
|
||||
]
|
||||
|
||||
PREVIEW_CASES: List[str] = [
|
||||
# string processing
|
||||
"cantfit",
|
||||
"comments7",
|
||||
"comments8",
|
||||
"long_strings",
|
||||
"long_strings__edge_case",
|
||||
"long_strings__regression",
|
||||
"percent_precedence",
|
||||
"remove_except_parens",
|
||||
"remove_for_brackets",
|
||||
"one_element_subscript",
|
||||
"remove_await_parens",
|
||||
"return_annotation_brackets",
|
||||
"docstring_preview",
|
||||
]
|
||||
|
||||
SOURCES: List[str] = [
|
||||
"src/black/__init__.py",
|
||||
"src/black/__main__.py",
|
||||
@ -95,25 +62,33 @@ def patch_dump_to_file(request: Any) -> Iterator[None]:
|
||||
yield
|
||||
|
||||
|
||||
def check_file(filename: str, mode: black.Mode, *, data: bool = True) -> None:
|
||||
source, expected = read_data(filename, data=data)
|
||||
def check_file(
|
||||
subdir: str, filename: str, mode: black.Mode, *, data: bool = True
|
||||
) -> None:
|
||||
source, expected = read_data(subdir, filename, data=data)
|
||||
assert_format(source, expected, mode, fast=False)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("filename", all_data_cases("simple_cases"))
|
||||
def test_simple_format(filename: str) -> None:
|
||||
check_file(filename, DEFAULT_MODE)
|
||||
check_file("simple_cases", filename, DEFAULT_MODE)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("filename", PREVIEW_CASES)
|
||||
@pytest.mark.parametrize("filename", all_data_cases("preview"))
|
||||
def test_preview_format(filename: str) -> None:
|
||||
check_file(filename, black.Mode(preview=True))
|
||||
check_file("preview", filename, black.Mode(preview=True))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("filename", all_data_cases("preview_39"))
|
||||
def test_preview_minimum_python_39_format(filename: str) -> None:
|
||||
source, expected = read_data("preview_39", filename)
|
||||
mode = black.Mode(preview=True)
|
||||
assert_format(source, expected, mode, minimum_version=(3, 9))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("filename", SOURCES)
|
||||
def test_source_is_formatted(filename: str) -> None:
|
||||
path = THIS_DIR.parent / filename
|
||||
check_file(str(path), DEFAULT_MODE, data=False)
|
||||
check_file("", filename, DEFAULT_MODE, data=False)
|
||||
|
||||
|
||||
# =============== #
|
||||
@ -126,59 +101,50 @@ def test_empty() -> None:
|
||||
assert_format(source, expected)
|
||||
|
||||
|
||||
def test_pep_572() -> None:
|
||||
source, expected = read_data("pep_572")
|
||||
assert_format(source, expected, minimum_version=(3, 8))
|
||||
@pytest.mark.parametrize("filename", all_data_cases("py_36"))
|
||||
def test_python_36(filename: str) -> None:
|
||||
source, expected = read_data("py_36", filename)
|
||||
mode = black.Mode(target_versions=PY36_VERSIONS)
|
||||
assert_format(source, expected, mode, minimum_version=(3, 6))
|
||||
|
||||
|
||||
def test_pep_572_remove_parens() -> None:
|
||||
source, expected = read_data("pep_572_remove_parens")
|
||||
assert_format(source, expected, minimum_version=(3, 8))
|
||||
@pytest.mark.parametrize("filename", all_data_cases("py_37"))
|
||||
def test_python_37(filename: str) -> None:
|
||||
source, expected = read_data("py_37", filename)
|
||||
mode = black.Mode(target_versions={black.TargetVersion.PY37})
|
||||
assert_format(source, expected, mode, minimum_version=(3, 7))
|
||||
|
||||
|
||||
def test_pep_572_do_not_remove_parens() -> None:
|
||||
source, expected = read_data("pep_572_do_not_remove_parens")
|
||||
# the AST safety checks will fail, but that's expected, just make sure no
|
||||
# parentheses are touched
|
||||
assert_format(source, expected, fast=True)
|
||||
@pytest.mark.parametrize("filename", all_data_cases("py_38"))
|
||||
def test_python_38(filename: str) -> None:
|
||||
source, expected = read_data("py_38", filename)
|
||||
mode = black.Mode(target_versions={black.TargetVersion.PY38})
|
||||
assert_format(source, expected, mode, minimum_version=(3, 8))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("major, minor", [(3, 9), (3, 10)])
|
||||
def test_pep_572_newer_syntax(major: int, minor: int) -> None:
|
||||
source, expected = read_data(f"pep_572_py{major}{minor}")
|
||||
assert_format(source, expected, minimum_version=(major, minor))
|
||||
@pytest.mark.parametrize("filename", all_data_cases("py_39"))
|
||||
def test_python_39(filename: str) -> None:
|
||||
source, expected = read_data("py_39", filename)
|
||||
mode = black.Mode(target_versions={black.TargetVersion.PY39})
|
||||
assert_format(source, expected, mode, minimum_version=(3, 9))
|
||||
|
||||
|
||||
def test_pep_570() -> None:
|
||||
source, expected = read_data("pep_570")
|
||||
assert_format(source, expected, minimum_version=(3, 8))
|
||||
|
||||
|
||||
def test_remove_with_brackets() -> None:
|
||||
source, expected = read_data("remove_with_brackets")
|
||||
assert_format(
|
||||
source,
|
||||
expected,
|
||||
black.Mode(preview=True),
|
||||
minimum_version=(3, 9),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("filename", PY310_CASES)
|
||||
@pytest.mark.parametrize("filename", all_data_cases("py_310"))
|
||||
def test_python_310(filename: str) -> None:
|
||||
source, expected = read_data(filename)
|
||||
source, expected = read_data("py_310", filename)
|
||||
mode = black.Mode(target_versions={black.TargetVersion.PY310})
|
||||
assert_format(source, expected, mode, minimum_version=(3, 10))
|
||||
|
||||
|
||||
def test_python_310_without_target_version() -> None:
|
||||
source, expected = read_data("pattern_matching_simple")
|
||||
@pytest.mark.parametrize("filename", all_data_cases("py_310"))
|
||||
def test_python_310_without_target_version(filename: str) -> None:
|
||||
source, expected = read_data("py_310", filename)
|
||||
mode = black.Mode()
|
||||
assert_format(source, expected, mode, minimum_version=(3, 10))
|
||||
|
||||
|
||||
def test_patma_invalid() -> None:
|
||||
source, expected = read_data("pattern_matching_invalid")
|
||||
source, expected = read_data("miscellaneous", "pattern_matching_invalid")
|
||||
mode = black.Mode(target_versions={black.TargetVersion.PY310})
|
||||
with pytest.raises(black.parsing.InvalidInput) as exc_info:
|
||||
assert_format(source, expected, mode, minimum_version=(3, 10))
|
||||
@ -186,13 +152,19 @@ def test_patma_invalid() -> None:
|
||||
exc_info.match("Cannot parse: 10:11")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("filename", PY311_CASES)
|
||||
@pytest.mark.parametrize("filename", all_data_cases("py_311"))
|
||||
def test_python_311(filename: str) -> None:
|
||||
source, expected = read_data(filename)
|
||||
source, expected = read_data("py_311", filename)
|
||||
mode = black.Mode(target_versions={black.TargetVersion.PY311})
|
||||
assert_format(source, expected, mode, minimum_version=(3, 11))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("filename", all_data_cases("fast"))
|
||||
def test_fast_cases(filename: str) -> None:
|
||||
source, expected = read_data("fast", filename)
|
||||
assert_format(source, expected, fast=True)
|
||||
|
||||
|
||||
def test_python_2_hint() -> None:
|
||||
with pytest.raises(black.parsing.InvalidInput) as exc_info:
|
||||
assert_format("print 'daylily'", "print 'daylily'")
|
||||
@ -201,47 +173,25 @@ def test_python_2_hint() -> None:
|
||||
|
||||
def test_docstring_no_string_normalization() -> None:
|
||||
"""Like test_docstring but with string normalization off."""
|
||||
source, expected = read_data("docstring_no_string_normalization")
|
||||
source, expected = read_data("miscellaneous", "docstring_no_string_normalization")
|
||||
mode = replace(DEFAULT_MODE, string_normalization=False)
|
||||
assert_format(source, expected, mode)
|
||||
|
||||
|
||||
def test_long_strings_flag_disabled() -> None:
|
||||
"""Tests for turning off the string processing logic."""
|
||||
source, expected = read_data("long_strings_flag_disabled")
|
||||
source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
|
||||
mode = replace(DEFAULT_MODE, experimental_string_processing=False)
|
||||
assert_format(source, expected, mode)
|
||||
|
||||
|
||||
def test_numeric_literals() -> None:
|
||||
source, expected = read_data("numeric_literals")
|
||||
mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
|
||||
assert_format(source, expected, mode)
|
||||
|
||||
|
||||
def test_numeric_literals_ignoring_underscores() -> None:
|
||||
source, expected = read_data("numeric_literals_skip_underscores")
|
||||
mode = replace(DEFAULT_MODE, target_versions=PY36_VERSIONS)
|
||||
assert_format(source, expected, mode)
|
||||
|
||||
|
||||
def test_stub() -> None:
|
||||
mode = replace(DEFAULT_MODE, is_pyi=True)
|
||||
source, expected = read_data("stub.pyi")
|
||||
source, expected = read_data("miscellaneous", "stub.pyi")
|
||||
assert_format(source, expected, mode)
|
||||
|
||||
|
||||
def test_python38() -> None:
|
||||
source, expected = read_data("python38")
|
||||
assert_format(source, expected, minimum_version=(3, 8))
|
||||
|
||||
|
||||
def test_python39() -> None:
|
||||
source, expected = read_data("python39")
|
||||
assert_format(source, expected, minimum_version=(3, 9))
|
||||
|
||||
|
||||
def test_power_op_newline() -> None:
|
||||
# requires line_length=0
|
||||
source, expected = read_data("power_op_newline")
|
||||
source, expected = read_data("miscellaneous", "power_op_newline")
|
||||
assert_format(source, expected, mode=black.Mode(line_length=0))
|
||||
|
@ -17,7 +17,7 @@
|
||||
import pytest
|
||||
from black import Mode
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
from tests.util import DATA_DIR
|
||||
from tests.util import DATA_DIR, read_jupyter_notebook, get_case_path
|
||||
|
||||
with contextlib.suppress(ModuleNotFoundError):
|
||||
import IPython
|
||||
@ -252,9 +252,7 @@ def test_empty_cell() -> None:
|
||||
|
||||
|
||||
def test_entire_notebook_empty_metadata() -> None:
|
||||
with open(DATA_DIR / "notebook_empty_metadata.ipynb", "rb") as fd:
|
||||
content_bytes = fd.read()
|
||||
content = content_bytes.decode()
|
||||
content = read_jupyter_notebook("jupyter", "notebook_empty_metadata")
|
||||
result = format_file_contents(content, fast=True, mode=JUPYTER_MODE)
|
||||
expected = (
|
||||
"{\n"
|
||||
@ -289,9 +287,7 @@ def test_entire_notebook_empty_metadata() -> None:
|
||||
|
||||
|
||||
def test_entire_notebook_trailing_newline() -> None:
|
||||
with open(DATA_DIR / "notebook_trailing_newline.ipynb", "rb") as fd:
|
||||
content_bytes = fd.read()
|
||||
content = content_bytes.decode()
|
||||
content = read_jupyter_notebook("jupyter", "notebook_trailing_newline")
|
||||
result = format_file_contents(content, fast=True, mode=JUPYTER_MODE)
|
||||
expected = (
|
||||
"{\n"
|
||||
@ -338,9 +334,7 @@ def test_entire_notebook_trailing_newline() -> None:
|
||||
|
||||
|
||||
def test_entire_notebook_no_trailing_newline() -> None:
|
||||
with open(DATA_DIR / "notebook_no_trailing_newline.ipynb", "rb") as fd:
|
||||
content_bytes = fd.read()
|
||||
content = content_bytes.decode()
|
||||
content = read_jupyter_notebook("jupyter", "notebook_no_trailing_newline")
|
||||
result = format_file_contents(content, fast=True, mode=JUPYTER_MODE)
|
||||
expected = (
|
||||
"{\n"
|
||||
@ -387,17 +381,14 @@ def test_entire_notebook_no_trailing_newline() -> None:
|
||||
|
||||
|
||||
def test_entire_notebook_without_changes() -> None:
|
||||
with open(DATA_DIR / "notebook_without_changes.ipynb", "rb") as fd:
|
||||
content_bytes = fd.read()
|
||||
content = content_bytes.decode()
|
||||
content = read_jupyter_notebook("jupyter", "notebook_without_changes")
|
||||
with pytest.raises(NothingChanged):
|
||||
format_file_contents(content, fast=True, mode=JUPYTER_MODE)
|
||||
|
||||
|
||||
def test_non_python_notebook() -> None:
|
||||
with open(DATA_DIR / "non_python_notebook.ipynb", "rb") as fd:
|
||||
content_bytes = fd.read()
|
||||
content = content_bytes.decode()
|
||||
content = read_jupyter_notebook("jupyter", "non_python_notebook")
|
||||
|
||||
with pytest.raises(NothingChanged):
|
||||
format_file_contents(content, fast=True, mode=JUPYTER_MODE)
|
||||
|
||||
@ -408,7 +399,7 @@ def test_empty_string() -> None:
|
||||
|
||||
|
||||
def test_unparseable_notebook() -> None:
|
||||
path = DATA_DIR / "notebook_which_cant_be_parsed.ipynb"
|
||||
path = get_case_path("jupyter", "notebook_which_cant_be_parsed.ipynb")
|
||||
msg = rf"File '{re.escape(str(path))}' cannot be parsed as valid Jupyter notebook\."
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
format_file_in_place(path, fast=True, mode=JUPYTER_MODE)
|
||||
@ -418,7 +409,7 @@ def test_ipynb_diff_with_change() -> None:
|
||||
result = runner.invoke(
|
||||
main,
|
||||
[
|
||||
str(DATA_DIR / "notebook_trailing_newline.ipynb"),
|
||||
str(get_case_path("jupyter", "notebook_trailing_newline.ipynb")),
|
||||
"--diff",
|
||||
f"--config={EMPTY_CONFIG}",
|
||||
],
|
||||
@ -431,7 +422,7 @@ def test_ipynb_diff_with_no_change() -> None:
|
||||
result = runner.invoke(
|
||||
main,
|
||||
[
|
||||
str(DATA_DIR / "notebook_without_changes.ipynb"),
|
||||
str(get_case_path("jupyter", "notebook_without_changes.ipynb")),
|
||||
"--diff",
|
||||
f"--config={EMPTY_CONFIG}",
|
||||
],
|
||||
@ -445,7 +436,7 @@ def test_cache_isnt_written_if_no_jupyter_deps_single(
|
||||
) -> None:
|
||||
# Check that the cache isn't written to if Jupyter dependencies aren't installed.
|
||||
jupyter_dependencies_are_installed.cache_clear()
|
||||
nb = DATA_DIR / "notebook_trailing_newline.ipynb"
|
||||
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
|
||||
tmp_nb = tmp_path / "notebook.ipynb"
|
||||
with open(nb) as src, open(tmp_nb, "w") as dst:
|
||||
dst.write(src.read())
|
||||
@ -471,7 +462,7 @@ def test_cache_isnt_written_if_no_jupyter_deps_dir(
|
||||
) -> None:
|
||||
# Check that the cache isn't written to if Jupyter dependencies aren't installed.
|
||||
jupyter_dependencies_are_installed.cache_clear()
|
||||
nb = DATA_DIR / "notebook_trailing_newline.ipynb"
|
||||
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
|
||||
tmp_nb = tmp_path / "notebook.ipynb"
|
||||
with open(nb) as src, open(tmp_nb, "w") as dst:
|
||||
dst.write(src.read())
|
||||
@ -489,7 +480,7 @@ def test_cache_isnt_written_if_no_jupyter_deps_dir(
|
||||
|
||||
|
||||
def test_ipynb_flag(tmp_path: pathlib.Path) -> None:
|
||||
nb = DATA_DIR / "notebook_trailing_newline.ipynb"
|
||||
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
|
||||
tmp_nb = tmp_path / "notebook.a_file_extension_which_is_definitely_not_ipynb"
|
||||
with open(nb) as src, open(tmp_nb, "w") as dst:
|
||||
dst.write(src.read())
|
||||
@ -507,7 +498,7 @@ def test_ipynb_flag(tmp_path: pathlib.Path) -> None:
|
||||
|
||||
|
||||
def test_ipynb_and_pyi_flags() -> None:
|
||||
nb = DATA_DIR / "notebook_trailing_newline.ipynb"
|
||||
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
|
||||
result = runner.invoke(
|
||||
main,
|
||||
[
|
||||
|
@ -1,8 +1,7 @@
|
||||
import pytest
|
||||
import os
|
||||
import pathlib
|
||||
|
||||
from tests.util import THIS_DIR
|
||||
from tests.util import get_case_path
|
||||
from black import main, jupyter_dependencies_are_installed
|
||||
from click.testing import CliRunner
|
||||
|
||||
@ -13,7 +12,7 @@
|
||||
|
||||
def test_ipynb_diff_with_no_change_single() -> None:
|
||||
jupyter_dependencies_are_installed.cache_clear()
|
||||
path = THIS_DIR / "data/notebook_trailing_newline.ipynb"
|
||||
path = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
|
||||
result = runner.invoke(main, [str(path)])
|
||||
expected_output = (
|
||||
"Skipping .ipynb files as Jupyter dependencies are not installed.\n"
|
||||
@ -25,7 +24,7 @@ def test_ipynb_diff_with_no_change_single() -> None:
|
||||
def test_ipynb_diff_with_no_change_dir(tmp_path: pathlib.Path) -> None:
|
||||
jupyter_dependencies_are_installed.cache_clear()
|
||||
runner = CliRunner()
|
||||
nb = os.path.join("tests", "data", "notebook_trailing_newline.ipynb")
|
||||
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
|
||||
tmp_nb = tmp_path / "notebook.ipynb"
|
||||
with open(nb) as src, open(tmp_nb, "w") as dst:
|
||||
dst.write(src.read())
|
||||
|
@ -11,6 +11,9 @@
|
||||
from black.mode import TargetVersion
|
||||
from black.output import diff, err, out
|
||||
|
||||
PYTHON_SUFFIX = ".py"
|
||||
ALLOWED_SUFFIXES = (PYTHON_SUFFIX, ".pyi", ".out", ".diff", ".ipynb")
|
||||
|
||||
THIS_DIR = Path(__file__).parent
|
||||
DATA_DIR = THIS_DIR / "data"
|
||||
PROJECT_ROOT = THIS_DIR.parent
|
||||
@ -90,21 +93,30 @@ def assertFormatEqual(self, expected: str, actual: str) -> None:
|
||||
_assert_format_equal(expected, actual)
|
||||
|
||||
|
||||
def all_data_cases(dir_name: str, data: bool = True) -> List[str]:
|
||||
base_dir = DATA_DIR if data else PROJECT_ROOT
|
||||
cases_dir = base_dir / dir_name
|
||||
def get_base_dir(data: bool) -> Path:
|
||||
return DATA_DIR if data else PROJECT_ROOT
|
||||
|
||||
|
||||
def all_data_cases(subdir_name: str, data: bool = True) -> List[str]:
|
||||
cases_dir = get_base_dir(data) / subdir_name
|
||||
assert cases_dir.is_dir()
|
||||
return [f"{dir_name}/{case_path.stem}" for case_path in cases_dir.iterdir()]
|
||||
return [case_path.stem for case_path in cases_dir.iterdir()]
|
||||
|
||||
|
||||
def read_data(name: str, data: bool = True) -> Tuple[str, str]:
|
||||
"""read_data('test_name') -> 'input', 'output'"""
|
||||
if not name.endswith((".py", ".pyi", ".out", ".diff")):
|
||||
name += ".py"
|
||||
base_dir = DATA_DIR if data else PROJECT_ROOT
|
||||
case_path = base_dir / name
|
||||
def get_case_path(
|
||||
subdir_name: str, name: str, data: bool = True, suffix: str = PYTHON_SUFFIX
|
||||
) -> Path:
|
||||
"""Get case path from name"""
|
||||
case_path = get_base_dir(data) / subdir_name / name
|
||||
if not name.endswith(ALLOWED_SUFFIXES):
|
||||
case_path = case_path.with_suffix(suffix)
|
||||
assert case_path.is_file(), f"{case_path} is not a file."
|
||||
return read_data_from_file(case_path)
|
||||
return case_path
|
||||
|
||||
|
||||
def read_data(subdir_name: str, name: str, data: bool = True) -> Tuple[str, str]:
|
||||
"""read_data('test_name') -> 'input', 'output'"""
|
||||
return read_data_from_file(get_case_path(subdir_name, name, data))
|
||||
|
||||
|
||||
def read_data_from_file(file_name: Path) -> Tuple[str, str]:
|
||||
@ -126,6 +138,18 @@ def read_data_from_file(file_name: Path) -> Tuple[str, str]:
|
||||
return "".join(_input).strip() + "\n", "".join(_output).strip() + "\n"
|
||||
|
||||
|
||||
def read_jupyter_notebook(subdir_name: str, name: str, data: bool = True) -> str:
|
||||
return read_jupyter_notebook_from_file(
|
||||
get_case_path(subdir_name, name, data, suffix=".ipynb")
|
||||
)
|
||||
|
||||
|
||||
def read_jupyter_notebook_from_file(file_name: Path) -> str:
|
||||
with open(file_name, mode="rb") as fd:
|
||||
content_bytes = fd.read()
|
||||
return content_bytes.decode()
|
||||
|
||||
|
||||
@contextmanager
|
||||
def change_directory(path: Path) -> Iterator[None]:
|
||||
"""Context manager to temporarily chdir to a different directory."""
|
||||
|
Loading…
Reference in New Issue
Block a user