From: Sagi Shadur Date: Sun, 8 May 2022 19:27:40 +0000 (+0300) Subject: Read simple data cases automatically (#3034) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/fc2a16433e7da705793122dd0c66fcde83b305d5?hp=20d8ccb54253f8a66321f6708d53e2a05a54079b Read simple data cases automatically (#3034) Co-authored-by: Felix Hildén --- diff --git a/tests/data/attribute_access_on_number_literals.py b/tests/data/simple_cases/attribute_access_on_number_literals.py similarity index 100% rename from tests/data/attribute_access_on_number_literals.py rename to tests/data/simple_cases/attribute_access_on_number_literals.py diff --git a/tests/data/beginning_backslash.py b/tests/data/simple_cases/beginning_backslash.py similarity index 100% rename from tests/data/beginning_backslash.py rename to tests/data/simple_cases/beginning_backslash.py diff --git a/tests/data/bracketmatch.py b/tests/data/simple_cases/bracketmatch.py similarity index 100% rename from tests/data/bracketmatch.py rename to tests/data/simple_cases/bracketmatch.py diff --git a/tests/data/class_blank_parentheses.py b/tests/data/simple_cases/class_blank_parentheses.py similarity index 100% rename from tests/data/class_blank_parentheses.py rename to tests/data/simple_cases/class_blank_parentheses.py diff --git a/tests/data/class_methods_new_line.py b/tests/data/simple_cases/class_methods_new_line.py similarity index 100% rename from tests/data/class_methods_new_line.py rename to tests/data/simple_cases/class_methods_new_line.py diff --git a/tests/data/collections.py b/tests/data/simple_cases/collections.py similarity index 100% rename from tests/data/collections.py rename to tests/data/simple_cases/collections.py diff --git a/tests/data/comment_after_escaped_newline.py b/tests/data/simple_cases/comment_after_escaped_newline.py similarity index 100% rename from tests/data/comment_after_escaped_newline.py rename to tests/data/simple_cases/comment_after_escaped_newline.py diff --git a/tests/data/comments.py b/tests/data/simple_cases/comments.py similarity index 100% rename from tests/data/comments.py rename to tests/data/simple_cases/comments.py diff --git a/tests/data/comments2.py b/tests/data/simple_cases/comments2.py similarity index 100% rename from tests/data/comments2.py rename to tests/data/simple_cases/comments2.py diff --git a/tests/data/comments3.py b/tests/data/simple_cases/comments3.py similarity index 100% rename from tests/data/comments3.py rename to tests/data/simple_cases/comments3.py diff --git a/tests/data/comments4.py b/tests/data/simple_cases/comments4.py similarity index 100% rename from tests/data/comments4.py rename to tests/data/simple_cases/comments4.py diff --git a/tests/data/comments5.py b/tests/data/simple_cases/comments5.py similarity index 100% rename from tests/data/comments5.py rename to tests/data/simple_cases/comments5.py diff --git a/tests/data/comments6.py b/tests/data/simple_cases/comments6.py similarity index 100% rename from tests/data/comments6.py rename to tests/data/simple_cases/comments6.py diff --git a/tests/data/comments_non_breaking_space.py b/tests/data/simple_cases/comments_non_breaking_space.py similarity index 100% rename from tests/data/comments_non_breaking_space.py rename to tests/data/simple_cases/comments_non_breaking_space.py diff --git a/tests/data/composition.py b/tests/data/simple_cases/composition.py similarity index 100% rename from tests/data/composition.py rename to tests/data/simple_cases/composition.py diff --git a/tests/data/composition_no_trailing_comma.py b/tests/data/simple_cases/composition_no_trailing_comma.py similarity index 100% rename from tests/data/composition_no_trailing_comma.py rename to tests/data/simple_cases/composition_no_trailing_comma.py diff --git a/tests/data/docstring.py b/tests/data/simple_cases/docstring.py similarity index 100% rename from tests/data/docstring.py rename to tests/data/simple_cases/docstring.py diff --git a/tests/data/empty_lines.py b/tests/data/simple_cases/empty_lines.py similarity index 100% rename from tests/data/empty_lines.py rename to tests/data/simple_cases/empty_lines.py diff --git a/tests/data/expression.diff b/tests/data/simple_cases/expression.diff similarity index 100% rename from tests/data/expression.diff rename to tests/data/simple_cases/expression.diff diff --git a/tests/data/expression.py b/tests/data/simple_cases/expression.py similarity index 100% rename from tests/data/expression.py rename to tests/data/simple_cases/expression.py diff --git a/tests/data/fmtonoff.py b/tests/data/simple_cases/fmtonoff.py similarity index 100% rename from tests/data/fmtonoff.py rename to tests/data/simple_cases/fmtonoff.py diff --git a/tests/data/fmtonoff2.py b/tests/data/simple_cases/fmtonoff2.py similarity index 100% rename from tests/data/fmtonoff2.py rename to tests/data/simple_cases/fmtonoff2.py diff --git a/tests/data/fmtonoff3.py b/tests/data/simple_cases/fmtonoff3.py similarity index 100% rename from tests/data/fmtonoff3.py rename to tests/data/simple_cases/fmtonoff3.py diff --git a/tests/data/fmtonoff4.py b/tests/data/simple_cases/fmtonoff4.py similarity index 100% rename from tests/data/fmtonoff4.py rename to tests/data/simple_cases/fmtonoff4.py diff --git a/tests/data/fmtskip.py b/tests/data/simple_cases/fmtskip.py similarity index 100% rename from tests/data/fmtskip.py rename to tests/data/simple_cases/fmtskip.py diff --git a/tests/data/fmtskip2.py b/tests/data/simple_cases/fmtskip2.py similarity index 100% rename from tests/data/fmtskip2.py rename to tests/data/simple_cases/fmtskip2.py diff --git a/tests/data/fmtskip3.py b/tests/data/simple_cases/fmtskip3.py similarity index 100% rename from tests/data/fmtskip3.py rename to tests/data/simple_cases/fmtskip3.py diff --git a/tests/data/fmtskip4.py b/tests/data/simple_cases/fmtskip4.py similarity index 100% rename from tests/data/fmtskip4.py rename to tests/data/simple_cases/fmtskip4.py diff --git a/tests/data/fmtskip5.py b/tests/data/simple_cases/fmtskip5.py similarity index 100% rename from tests/data/fmtskip5.py rename to tests/data/simple_cases/fmtskip5.py diff --git a/tests/data/fmtskip6.py b/tests/data/simple_cases/fmtskip6.py similarity index 100% rename from tests/data/fmtskip6.py rename to tests/data/simple_cases/fmtskip6.py diff --git a/tests/data/fmtskip7.py b/tests/data/simple_cases/fmtskip7.py similarity index 100% rename from tests/data/fmtskip7.py rename to tests/data/simple_cases/fmtskip7.py diff --git a/tests/data/fstring.py b/tests/data/simple_cases/fstring.py similarity index 100% rename from tests/data/fstring.py rename to tests/data/simple_cases/fstring.py diff --git a/tests/data/function.py b/tests/data/simple_cases/function.py similarity index 100% rename from tests/data/function.py rename to tests/data/simple_cases/function.py diff --git a/tests/data/function2.py b/tests/data/simple_cases/function2.py similarity index 100% rename from tests/data/function2.py rename to tests/data/simple_cases/function2.py diff --git a/tests/data/function_trailing_comma.py b/tests/data/simple_cases/function_trailing_comma.py similarity index 100% rename from tests/data/function_trailing_comma.py rename to tests/data/simple_cases/function_trailing_comma.py diff --git a/tests/data/import_spacing.py b/tests/data/simple_cases/import_spacing.py similarity index 100% rename from tests/data/import_spacing.py rename to tests/data/simple_cases/import_spacing.py diff --git a/tests/data/power_op_spacing.py b/tests/data/simple_cases/power_op_spacing.py similarity index 100% rename from tests/data/power_op_spacing.py rename to tests/data/simple_cases/power_op_spacing.py diff --git a/tests/data/remove_parens.py b/tests/data/simple_cases/remove_parens.py similarity index 100% rename from tests/data/remove_parens.py rename to tests/data/simple_cases/remove_parens.py diff --git a/tests/data/slices.py b/tests/data/simple_cases/slices.py similarity index 100% rename from tests/data/slices.py rename to tests/data/simple_cases/slices.py diff --git a/tests/data/string_prefixes.py b/tests/data/simple_cases/string_prefixes.py similarity index 100% rename from tests/data/string_prefixes.py rename to tests/data/simple_cases/string_prefixes.py diff --git a/tests/data/torture.py b/tests/data/simple_cases/torture.py similarity index 100% rename from tests/data/torture.py rename to tests/data/simple_cases/torture.py diff --git a/tests/data/trailing_comma_optional_parens1.py b/tests/data/simple_cases/trailing_comma_optional_parens1.py similarity index 100% rename from tests/data/trailing_comma_optional_parens1.py rename to tests/data/simple_cases/trailing_comma_optional_parens1.py diff --git a/tests/data/trailing_comma_optional_parens2.py b/tests/data/simple_cases/trailing_comma_optional_parens2.py similarity index 100% rename from tests/data/trailing_comma_optional_parens2.py rename to tests/data/simple_cases/trailing_comma_optional_parens2.py diff --git a/tests/data/trailing_comma_optional_parens3.py b/tests/data/simple_cases/trailing_comma_optional_parens3.py similarity index 100% rename from tests/data/trailing_comma_optional_parens3.py rename to tests/data/simple_cases/trailing_comma_optional_parens3.py diff --git a/tests/data/tricky_unicode_symbols.py b/tests/data/simple_cases/tricky_unicode_symbols.py similarity index 100% rename from tests/data/tricky_unicode_symbols.py rename to tests/data/simple_cases/tricky_unicode_symbols.py diff --git a/tests/data/tupleassign.py b/tests/data/simple_cases/tupleassign.py similarity index 100% rename from tests/data/tupleassign.py rename to tests/data/simple_cases/tupleassign.py diff --git a/tests/test_black.py b/tests/test_black.py index 74334d2..281019a 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -179,8 +179,8 @@ class BlackTestCase(BlackBaseTestCase): 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("expression.py") - expected, _ = read_data("expression.diff") + source, _ = read_data("simple_cases/expression.py") + expected, _ = read_data("simple_cases/expression.diff") args = [ "-", "--fast", @@ -197,7 +197,7 @@ class BlackTestCase(BlackBaseTestCase): self.assertEqual(expected, actual) def test_piping_diff_with_color(self) -> None: - source, _ = read_data("expression.py") + source, _ = read_data("simple_cases/expression.py") args = [ "-", "--fast", @@ -241,7 +241,7 @@ class BlackTestCase(BlackBaseTestCase): self.assertIn(black.TargetVersion.PY38, versions) def test_expression_ff(self) -> None: - source, expected = read_data("expression") + 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 +255,8 @@ class BlackTestCase(BlackBaseTestCase): black.assert_stable(source, actual, DEFAULT_MODE) def test_expression_diff(self) -> None: - source, _ = read_data("expression.py") - expected, _ = read_data("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 +281,8 @@ class BlackTestCase(BlackBaseTestCase): self.assertEqual(expected, actual, msg) def test_expression_diff_with_color(self) -> None: - source, _ = read_data("expression.py") - expected, _ = read_data("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( @@ -320,7 +320,7 @@ class BlackTestCase(BlackBaseTestCase): black.assert_stable(source, not_normalized, mode=mode) def test_skip_magic_trailing_comma(self) -> None: - source, _ = read_data("expression.py") + source, _ = read_data("simple_cases/expression.py") expected, _ = read_data("expression_skip_magic_trailing_comma.diff") tmp_file = Path(black.dump_to_file(source)) diff_header = re.compile( @@ -755,7 +755,7 @@ class BlackTestCase(BlackBaseTestCase): 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("function") + source, expected = read_data("simple_cases/function.py") node = black.lib2to3_parse(source) expected_features = { Feature.TRAILING_COMMA_IN_CALL, @@ -765,7 +765,7 @@ class BlackTestCase(BlackBaseTestCase): 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("expression") + source, expected = read_data("simple_cases/expression.py") node = black.lib2to3_parse(source) self.assertEqual(black.get_features_used(node), set()) node = black.lib2to3_parse(expected) @@ -939,7 +939,7 @@ class BlackTestCase(BlackBaseTestCase): src1 = (THIS_DIR / "data" / "string_quotes.py").resolve() self.invokeBlack([str(src1), "--diff", "--check"], exit_code=1) # Files which will not be reformatted. - src2 = (THIS_DIR / "data" / "composition.py").resolve() + src2 = (THIS_DIR / "data" / "simple_cases" / "composition.py").resolve() self.invokeBlack([str(src2), "--diff", "--check"]) # Multi file command. self.invokeBlack([str(src1), str(src2), "--diff", "--check"], exit_code=1) @@ -1168,7 +1168,7 @@ class BlackTestCase(BlackBaseTestCase): report = MagicMock() # Even with an existing file, since we are forcing stdin, black # should output to stdout and not modify the file inplace - p = Path(str(THIS_DIR / "data/collections.py")) + p = THIS_DIR / "data" / "simple_cases" / "collections.py" # Make sure is_file actually returns True self.assertTrue(p.is_file()) path = Path(f"__BLACK_STDIN_FILENAME__{p}") diff --git a/tests/test_format.py b/tests/test_format.py index 2f08d1f..003f5bb 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -12,56 +12,9 @@ from tests.util import ( assert_format, dump_to_stderr, read_data, + all_data_cases, ) -SIMPLE_CASES: List[str] = [ - "attribute_access_on_number_literals", - "beginning_backslash", - "bracketmatch", - "class_blank_parentheses", - "class_methods_new_line", - "collections", - "comments", - "comments2", - "comments3", - "comments4", - "comments5", - "comments6", - "comments_non_breaking_space", - "comment_after_escaped_newline", - "composition", - "composition_no_trailing_comma", - "docstring", - "empty_lines", - "expression", - "fmtonoff", - "fmtonoff2", - "fmtonoff3", - "fmtonoff4", - "fmtskip", - "fmtskip2", - "fmtskip3", - "fmtskip4", - "fmtskip5", - "fmtskip6", - "fmtskip7", - "fstring", - "function", - "function2", - "function_trailing_comma", - "import_spacing", - "power_op_spacing", - "remove_parens", - "slices", - "string_prefixes", - "torture", - "trailing_comma_optional_parens1", - "trailing_comma_optional_parens2", - "trailing_comma_optional_parens3", - "tricky_unicode_symbols", - "tupleassign", -] - PY310_CASES: List[str] = [ "starred_for_target", "pattern_matching_simple", @@ -147,7 +100,7 @@ def check_file(filename: str, mode: black.Mode, *, data: bool = True) -> None: assert_format(source, expected, mode, fast=False) -@pytest.mark.parametrize("filename", SIMPLE_CASES) +@pytest.mark.parametrize("filename", all_data_cases("simple_cases")) def test_simple_format(filename: str) -> None: check_file(filename, DEFAULT_MODE) diff --git a/tests/util.py b/tests/util.py index 8755111..1d76681 100644 --- a/tests/util.py +++ b/tests/util.py @@ -90,12 +90,21 @@ class BlackBaseTestCase(unittest.TestCase): _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 + assert cases_dir.is_dir() + return [f"{dir_name}/{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 - return read_data_from_file(base_dir / name) + case_path = base_dir / name + assert case_path.is_file(), f"{case_path} is not a file." + return read_data_from_file(case_path) def read_data_from_file(file_name: Path) -> Tuple[str, str]: