From: Sagi Shadur Date: Wed, 18 May 2022 19:11:37 +0000 (+0300) Subject: Remove hard coded test cases (#3062) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/2893c42176903c8b6c28c46ff9e046861328b6a8?ds=sidebyside;hp=7f033136ac5e0e5bf6cf322dd60b4a92050eedc4 Remove hard coded test cases (#3062) --- diff --git a/tests/data/pep_572_do_not_remove_parens.py b/tests/data/fast/pep_572_do_not_remove_parens.py similarity index 100% rename from tests/data/pep_572_do_not_remove_parens.py rename to tests/data/fast/pep_572_do_not_remove_parens.py diff --git a/tests/data/non_python_notebook.ipynb b/tests/data/jupyter/non_python_notebook.ipynb similarity index 100% rename from tests/data/non_python_notebook.ipynb rename to tests/data/jupyter/non_python_notebook.ipynb diff --git a/tests/data/notebook_empty_metadata.ipynb b/tests/data/jupyter/notebook_empty_metadata.ipynb similarity index 100% rename from tests/data/notebook_empty_metadata.ipynb rename to tests/data/jupyter/notebook_empty_metadata.ipynb diff --git a/tests/data/notebook_no_trailing_newline.ipynb b/tests/data/jupyter/notebook_no_trailing_newline.ipynb similarity index 100% rename from tests/data/notebook_no_trailing_newline.ipynb rename to tests/data/jupyter/notebook_no_trailing_newline.ipynb diff --git a/tests/data/notebook_trailing_newline.ipynb b/tests/data/jupyter/notebook_trailing_newline.ipynb similarity index 100% rename from tests/data/notebook_trailing_newline.ipynb rename to tests/data/jupyter/notebook_trailing_newline.ipynb diff --git a/tests/data/notebook_which_cant_be_parsed.ipynb b/tests/data/jupyter/notebook_which_cant_be_parsed.ipynb similarity index 100% rename from tests/data/notebook_which_cant_be_parsed.ipynb rename to tests/data/jupyter/notebook_which_cant_be_parsed.ipynb diff --git a/tests/data/notebook_without_changes.ipynb b/tests/data/jupyter/notebook_without_changes.ipynb similarity index 100% rename from tests/data/notebook_without_changes.ipynb rename to tests/data/jupyter/notebook_without_changes.ipynb diff --git a/tests/data/async_as_identifier.py b/tests/data/miscellaneous/async_as_identifier.py similarity index 100% rename from tests/data/async_as_identifier.py rename to tests/data/miscellaneous/async_as_identifier.py diff --git a/tests/data/blackd_diff.diff b/tests/data/miscellaneous/blackd_diff.diff similarity index 100% rename from tests/data/blackd_diff.diff rename to tests/data/miscellaneous/blackd_diff.diff diff --git a/tests/data/blackd_diff.py b/tests/data/miscellaneous/blackd_diff.py similarity index 100% rename from tests/data/blackd_diff.py rename to tests/data/miscellaneous/blackd_diff.py diff --git a/tests/data/debug_visitor.out b/tests/data/miscellaneous/debug_visitor.out similarity index 100% rename from tests/data/debug_visitor.out rename to tests/data/miscellaneous/debug_visitor.out diff --git a/tests/data/debug_visitor.py b/tests/data/miscellaneous/debug_visitor.py similarity index 100% rename from tests/data/debug_visitor.py rename to tests/data/miscellaneous/debug_visitor.py diff --git a/tests/data/decorators.py b/tests/data/miscellaneous/decorators.py similarity index 100% rename from tests/data/decorators.py rename to tests/data/miscellaneous/decorators.py diff --git a/tests/data/docstring_no_string_normalization.py b/tests/data/miscellaneous/docstring_no_string_normalization.py similarity index 100% rename from tests/data/docstring_no_string_normalization.py rename to tests/data/miscellaneous/docstring_no_string_normalization.py diff --git a/tests/data/expression_skip_magic_trailing_comma.diff b/tests/data/miscellaneous/expression_skip_magic_trailing_comma.diff similarity index 100% rename from tests/data/expression_skip_magic_trailing_comma.diff rename to tests/data/miscellaneous/expression_skip_magic_trailing_comma.diff diff --git a/tests/data/force_py36.py b/tests/data/miscellaneous/force_py36.py similarity index 100% rename from tests/data/force_py36.py rename to tests/data/miscellaneous/force_py36.py diff --git a/tests/data/force_pyi.py b/tests/data/miscellaneous/force_pyi.py similarity index 100% rename from tests/data/force_pyi.py rename to tests/data/miscellaneous/force_pyi.py diff --git a/tests/data/long_strings_flag_disabled.py b/tests/data/miscellaneous/long_strings_flag_disabled.py similarity index 100% rename from tests/data/long_strings_flag_disabled.py rename to tests/data/miscellaneous/long_strings_flag_disabled.py diff --git a/tests/data/missing_final_newline.diff b/tests/data/miscellaneous/missing_final_newline.diff similarity index 100% rename from tests/data/missing_final_newline.diff rename to tests/data/miscellaneous/missing_final_newline.diff diff --git a/tests/data/missing_final_newline.py b/tests/data/miscellaneous/missing_final_newline.py similarity index 100% rename from tests/data/missing_final_newline.py rename to tests/data/miscellaneous/missing_final_newline.py diff --git a/tests/data/pattern_matching_invalid.py b/tests/data/miscellaneous/pattern_matching_invalid.py similarity index 100% rename from tests/data/pattern_matching_invalid.py rename to tests/data/miscellaneous/pattern_matching_invalid.py diff --git a/tests/data/power_op_newline.py b/tests/data/miscellaneous/power_op_newline.py similarity index 100% rename from tests/data/power_op_newline.py rename to tests/data/miscellaneous/power_op_newline.py diff --git a/tests/data/python2_detection.py b/tests/data/miscellaneous/python2_detection.py similarity index 100% rename from tests/data/python2_detection.py rename to tests/data/miscellaneous/python2_detection.py diff --git a/tests/data/string_quotes.py b/tests/data/miscellaneous/string_quotes.py similarity index 100% rename from tests/data/string_quotes.py rename to tests/data/miscellaneous/string_quotes.py diff --git a/tests/data/stub.pyi b/tests/data/miscellaneous/stub.pyi similarity index 100% rename from tests/data/stub.pyi rename to tests/data/miscellaneous/stub.pyi diff --git a/tests/data/cantfit.py b/tests/data/preview/cantfit.py similarity index 100% rename from tests/data/cantfit.py rename to tests/data/preview/cantfit.py diff --git a/tests/data/comments7.py b/tests/data/preview/comments7.py similarity index 100% rename from tests/data/comments7.py rename to tests/data/preview/comments7.py diff --git a/tests/data/comments8.py b/tests/data/preview/comments8.py similarity index 100% rename from tests/data/comments8.py rename to tests/data/preview/comments8.py diff --git a/tests/data/docstring_preview.py b/tests/data/preview/docstring_preview.py similarity index 100% rename from tests/data/docstring_preview.py rename to tests/data/preview/docstring_preview.py diff --git a/tests/data/long_strings.py b/tests/data/preview/long_strings.py similarity index 100% rename from tests/data/long_strings.py rename to tests/data/preview/long_strings.py diff --git a/tests/data/long_strings__edge_case.py b/tests/data/preview/long_strings__edge_case.py similarity index 100% rename from tests/data/long_strings__edge_case.py rename to tests/data/preview/long_strings__edge_case.py diff --git a/tests/data/long_strings__regression.py b/tests/data/preview/long_strings__regression.py similarity index 100% rename from tests/data/long_strings__regression.py rename to tests/data/preview/long_strings__regression.py diff --git a/tests/data/one_element_subscript.py b/tests/data/preview/one_element_subscript.py similarity index 100% rename from tests/data/one_element_subscript.py rename to tests/data/preview/one_element_subscript.py diff --git a/tests/data/percent_precedence.py b/tests/data/preview/percent_precedence.py similarity index 100% rename from tests/data/percent_precedence.py rename to tests/data/preview/percent_precedence.py diff --git a/tests/data/remove_await_parens.py b/tests/data/preview/remove_await_parens.py similarity index 100% rename from tests/data/remove_await_parens.py rename to tests/data/preview/remove_await_parens.py diff --git a/tests/data/remove_except_parens.py b/tests/data/preview/remove_except_parens.py similarity index 100% rename from tests/data/remove_except_parens.py rename to tests/data/preview/remove_except_parens.py diff --git a/tests/data/remove_for_brackets.py b/tests/data/preview/remove_for_brackets.py similarity index 100% rename from tests/data/remove_for_brackets.py rename to tests/data/preview/remove_for_brackets.py diff --git a/tests/data/return_annotation_brackets.py b/tests/data/preview/return_annotation_brackets.py similarity index 100% rename from tests/data/return_annotation_brackets.py rename to tests/data/preview/return_annotation_brackets.py diff --git a/tests/data/remove_with_brackets.py b/tests/data/preview_39/remove_with_brackets.py similarity index 100% rename from tests/data/remove_with_brackets.py rename to tests/data/preview_39/remove_with_brackets.py diff --git a/tests/data/parenthesized_context_managers.py b/tests/data/py_310/parenthesized_context_managers.py similarity index 100% rename from tests/data/parenthesized_context_managers.py rename to tests/data/py_310/parenthesized_context_managers.py diff --git a/tests/data/pattern_matching_complex.py b/tests/data/py_310/pattern_matching_complex.py similarity index 100% rename from tests/data/pattern_matching_complex.py rename to tests/data/py_310/pattern_matching_complex.py diff --git a/tests/data/pattern_matching_extras.py b/tests/data/py_310/pattern_matching_extras.py similarity index 100% rename from tests/data/pattern_matching_extras.py rename to tests/data/py_310/pattern_matching_extras.py diff --git a/tests/data/pattern_matching_generic.py b/tests/data/py_310/pattern_matching_generic.py similarity index 100% rename from tests/data/pattern_matching_generic.py rename to tests/data/py_310/pattern_matching_generic.py diff --git a/tests/data/pattern_matching_simple.py b/tests/data/py_310/pattern_matching_simple.py similarity index 100% rename from tests/data/pattern_matching_simple.py rename to tests/data/py_310/pattern_matching_simple.py diff --git a/tests/data/pattern_matching_style.py b/tests/data/py_310/pattern_matching_style.py similarity index 100% rename from tests/data/pattern_matching_style.py rename to tests/data/py_310/pattern_matching_style.py diff --git a/tests/data/pep_572_py310.py b/tests/data/py_310/pep_572_py310.py similarity index 100% rename from tests/data/pep_572_py310.py rename to tests/data/py_310/pep_572_py310.py diff --git a/tests/data/starred_for_target.py b/tests/data/py_310/starred_for_target.py similarity index 100% rename from tests/data/starred_for_target.py rename to tests/data/py_310/starred_for_target.py diff --git a/tests/data/pep_654.py b/tests/data/py_311/pep_654.py similarity index 100% rename from tests/data/pep_654.py rename to tests/data/py_311/pep_654.py diff --git a/tests/data/pep_654_style.py b/tests/data/py_311/pep_654_style.py similarity index 100% rename from tests/data/pep_654_style.py rename to tests/data/py_311/pep_654_style.py diff --git a/tests/data/numeric_literals.py b/tests/data/py_36/numeric_literals.py similarity index 100% rename from tests/data/numeric_literals.py rename to tests/data/py_36/numeric_literals.py diff --git a/tests/data/numeric_literals_skip_underscores.py b/tests/data/py_36/numeric_literals_skip_underscores.py similarity index 100% rename from tests/data/numeric_literals_skip_underscores.py rename to tests/data/py_36/numeric_literals_skip_underscores.py diff --git a/tests/data/python37.py b/tests/data/py_37/python37.py similarity index 100% rename from tests/data/python37.py rename to tests/data/py_37/python37.py diff --git a/tests/data/pep_570.py b/tests/data/py_38/pep_570.py similarity index 100% rename from tests/data/pep_570.py rename to tests/data/py_38/pep_570.py diff --git a/tests/data/pep_572.py b/tests/data/py_38/pep_572.py similarity index 100% rename from tests/data/pep_572.py rename to tests/data/py_38/pep_572.py diff --git a/tests/data/pep_572_remove_parens.py b/tests/data/py_38/pep_572_remove_parens.py similarity index 100% rename from tests/data/pep_572_remove_parens.py rename to tests/data/py_38/pep_572_remove_parens.py diff --git a/tests/data/python38.py b/tests/data/py_38/python38.py similarity index 100% rename from tests/data/python38.py rename to tests/data/py_38/python38.py diff --git a/tests/data/pep_572_py39.py b/tests/data/py_39/pep_572_py39.py similarity index 100% rename from tests/data/pep_572_py39.py rename to tests/data/py_39/pep_572_py39.py diff --git a/tests/data/python39.py b/tests/data/py_39/python39.py similarity index 100% rename from tests/data/python39.py rename to tests/data/py_39/python39.py diff --git a/tests/test_black.py b/tests/test_black.py index 281019a..a633e67 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -60,6 +60,8 @@ from tests.util import ( ff, fs, read_data, + get_case_path, + read_data_from_file, ) THIS_FILE = Path(__file__) @@ -157,7 +159,7 @@ class BlackTestCase(BlackBaseTestCase): ) 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 @@ 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("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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): @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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): @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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): @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 @@ class BlackTestCase(BlackBaseTestCase): @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 @@ class BlackTestCase(BlackBaseTestCase): # 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 @@ 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("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 @@ 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("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 @@ class BlackTestCase(BlackBaseTestCase): @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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): 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 @@ class BlackTestCase(BlackBaseTestCase): # 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 " diff --git a/tests/test_blackd.py b/tests/test_blackd.py index 6174c45..75d7567 100644 --- a/tests/test_blackd.py +++ b/tests/test_blackd.py @@ -95,7 +95,7 @@ class BlackDTestCase(AioHTTPTestCase): @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 @@ class BlackDTestCase(AioHTTPTestCase): 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"} diff --git a/tests/test_format.py b/tests/test_format.py index 003f5bb..005a577 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -8,45 +8,12 @@ import black 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)) - - -def test_pep_572_remove_parens() -> None: - source, expected = read_data("pep_572_remove_parens") - assert_format(source, expected, minimum_version=(3, 8)) - - -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_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)) -@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_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_570() -> None: - source, expected = read_data("pep_570") - assert_format(source, expected, minimum_version=(3, 8)) +@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)) -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", 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)) -@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)) diff --git a/tests/test_ipynb.py b/tests/test_ipynb.py index 6f6b309..e1d7dd8 100644 --- a/tests/test_ipynb.py +++ b/tests/test_ipynb.py @@ -17,7 +17,7 @@ from black import ( 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, [ diff --git a/tests/test_no_ipynb.py b/tests/test_no_ipynb.py index b03b8e1..a3c8977 100644 --- a/tests/test_no_ipynb.py +++ b/tests/test_no_ipynb.py @@ -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 @@ runner = CliRunner() 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()) diff --git a/tests/util.py b/tests/util.py index 1d76681..d65c2e6 100644 --- a/tests/util.py +++ b/tests/util.py @@ -11,6 +11,9 @@ from black.debug import DebugVisitor 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 @@ 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 +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."""