Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hard coded test cases #3062

Merged
merged 16 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
74 changes: 40 additions & 34 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
ff,
fs,
read_data,
get_case_path,
read_data_from_file,
)

THIS_FILE = Path(__file__)
Expand Down Expand Up @@ -157,7 +159,9 @@ 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(
Path(PROJECT_ROOT / "src/black/__init__.py")
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
)
result = BlackRunner().invoke(
black.main,
[
Expand All @@ -179,8 +183,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",
Expand All @@ -197,7 +201,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",
Expand All @@ -219,7 +223,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,
Expand All @@ -233,15 +237,15 @@ 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)
versions = black.detect_target_versions(root)
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))
Expand All @@ -255,8 +259,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 "
Expand All @@ -281,8 +285,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(
Expand All @@ -301,7 +305,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)
Expand All @@ -310,7 +314,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)
Expand All @@ -320,8 +324,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.py")
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
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 "
Expand All @@ -348,8 +354,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]
Expand All @@ -363,8 +369,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]
Expand Down Expand Up @@ -712,7 +718,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)
Expand Down Expand Up @@ -755,7 +761,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.py")
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
node = black.lib2to3_parse(source)
expected_features = {
Feature.TRAILING_COMMA_IN_CALL,
Expand All @@ -765,7 +771,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.py")
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
node = black.lib2to3_parse(source)
self.assertEqual(black.get_features_used(node), set())
node = black.lib2to3_parse(expected)
Expand Down Expand Up @@ -851,8 +857,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.py")
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
expected, _ = read_data("miscellaneous", "debug_visitor.out")
out_lines = []
err_lines = []

Expand Down Expand Up @@ -936,10 +942,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)
Expand All @@ -963,7 +969,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:
Expand All @@ -984,7 +990,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(),
Expand All @@ -1006,7 +1012,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"))
)
Expand All @@ -1017,7 +1023,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:
Expand All @@ -1036,7 +1042,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(),
Expand All @@ -1058,7 +1064,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"],
Expand Down Expand Up @@ -1454,10 +1460,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.py")
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
# 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 "
Expand Down
6 changes: 3 additions & 3 deletions tests/test_blackd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
)
Expand All @@ -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.py")
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
expected, _ = read_data("miscellaneous", "blackd_diff.diff")

response = await self.client.post(
"/", data=source, headers={blackd.DIFF_HEADER: "true"}
Expand Down
Loading