MNT: remove unnecessary test deps + some refactoring (GH-2510)

The main goals of this commit include:

* improving consistency on how strict the test suite is -- Jelle has
  seen cases where a test did not fail to an incomplete test setup
  even though it should've
* simplifying tests for both ease of creation and reading via
  parametrization and helpers
* reorganizing the test suite by grouping more tests
* dropping test suite dependencies that aren't strictly necessary

The test suite could definitely do with more refactoring, but this is a
good first pass. Anyway it would've gotten too big to review effectively
if I did continue on this PR.

Commit history before squash merge:

* Drop parameterized dep and refactor format tests

Since the test suite is already using pytest-only features we can drop
the parameterized test dependency in favour of pytest's own offering.

I also added an utility function called assert_format that makes it
even easier to verify Black formats some code correctly. We already
have great tooling if the case is very simple in test_format.py but
any sort of complication makes it hard to use. Also if you're writing
a non-standard test case, you have to be careful to include all of
the steps so issues don't go undetected. assert_format aims to
1) improve consistency, 2) avoid wasted CPU cycles, and 3) avoid
logical errors that hide issues.

Finally, quite a few tests were either moved and/or simplified with
the new setup.

* Move file collection tests
* Add assert_collected_sources helper function

Testing source collection involves a lot of repetitive boilerplate,
something that black.files.get_sources's signature does not help with.
So to cut down on boilerplate like `report=black.Report()` I added
a convenience function to tests/test_black.py which wraps
black.get_sources. Its signature is designed to be much more lax to
make it much easier to use. Somehow this leads to cutting 100 lines!

Also IMO the test cases are much easier to read since it's more
declarative than really procedural now.

* Run isort on some test files
* Move cache tests
* Use pytest-style asserts & add parametrization
* Drop now unnecessary test dependencies

*pytest-cases might be interesting for further refactoring but I
haven't been able to wrap my head around it for the time being. We
can always revisit anyway.
This commit is contained in:
Richard Si 2021-10-02 19:37:32 -04:00 committed by GitHub
parent 0fd353f163
commit 3500e1cda5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 707 additions and 888 deletions

View File

@ -7,11 +7,8 @@ verify_ssl = true
# Testing related requirements.
coverage = ">= 5.3"
pytest = " >= 6.1.1"
pytest-mock = ">= 3.3.1"
pytest-cases = ">= 2.3.0"
pytest-xdist = ">= 2.2.1"
pytest-cov = ">= 2.11.1"
parameterized = ">= 0.7.4"
tox = "*"
# Linting related requirements.

45
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "192f075f04e702887745a3f19056b0172d83e4bc494fff4e0bcd6cfcafedd512"
"sha256": "6dbdff058fac8e6492f9d64194e98e48e062946ec4c06f9bb7df517d1dd89ce8"
},
"pipfile-spec": 6,
"requires": {},
@ -661,16 +661,8 @@
"sha256:8479067f342acf957dc82ec415d355ab5edb7e7646b90dc6e2fd1d96ad084c97"
],
"index": "pypi",
"python_version <": "3.7",
"version": "==0.8",
"version >": "0.1.3"
},
"decopatch": {
"hashes": [
"sha256:29a74d5d753423b188d5b537532da4f4b88e33ddccb95a8a20a5eff5b13265d4",
"sha256:c66b0815f15db04de7bb52b0b276432b76b7346fe7046f28033f48a14340d144"
],
"version": "==1.4.8"
"markers": "python_version < '3.7'",
"version": "==0.8"
},
"decorator": {
"hashes": [
@ -827,13 +819,6 @@
"markers": "python_version >= '3.6'",
"version": "==23.1.0"
},
"makefun": {
"hashes": [
"sha256:033eed65e2c1804fca84161a38d1fc8bb8650d32a89ac1c5dc7e54b2b2c2e88c",
"sha256:a19bddf07efb6bf92e3ccde5d593e49bc59001fd6c17cf7301d7a73a2647ae83"
],
"version": "==1.11.3"
},
"markdown-it-py": {
"hashes": [
"sha256:36be6bb3ad987bfdb839f5ba78ddf094552ca38ccbd784ae4f74a4e1419fc6e3",
@ -1028,14 +1013,6 @@
"markers": "python_version >= '3.6'",
"version": "==21.0"
},
"parameterized": {
"hashes": [
"sha256:41bbff37d6186430f77f900d777e5bb6a24928a1c46fb1de692f8b52b8833b5c",
"sha256:9cbb0b69a03e8695d68b3399a8a5825200976536fe1cb79db60ed6a4c8c9efe9"
],
"index": "pypi",
"version": "==0.8.1"
},
"parso": {
"hashes": [
"sha256:12b83492c6239ce32ff5eed6d3639d6a536170723c6f3f1506869f1ace413398",
@ -1169,14 +1146,6 @@
"index": "pypi",
"version": "==6.2.4"
},
"pytest-cases": {
"hashes": [
"sha256:13136269240615bc79041f8af8fc96e0e3e085da72dd22b18625451fda2443b8",
"sha256:a4abe0ec2b8acf8f8b5ab73060de72eac745c6ed9cfa317d59ae71b4a0bbbdf5"
],
"index": "pypi",
"version": "==3.6.3"
},
"pytest-cov": {
"hashes": [
"sha256:261bb9e47e65bd099c89c3edf92972865210c36813f80ede5277dceb77a4a62a",
@ -1193,14 +1162,6 @@
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",
"version": "==1.3.0"
},
"pytest-mock": {
"hashes": [
"sha256:30c2f2cc9759e76eee674b81ea28c9f0b94f8f0445a1b87762cadf774f0df7e3",
"sha256:40217a058c52a63f1042f0784f62009e976ba824c418cced42e88d5f40ab0e62"
],
"index": "pypi",
"version": "==3.6.1"
},
"pytest-xdist": {
"hashes": [
"sha256:e8ecde2f85d88fbcadb7d28cb33da0fa29bca5cf7d5967fa89fc0e97e5299ea5",

View File

@ -1,9 +1,6 @@
coverage >= 5.3
pre-commit
pytest >= 6.1.1
pytest-mock >= 3.3.1
pytest-cases >= 2.3.0
pytest-xdist >= 2.2.1
pytest-cov >= 2.11.1
parameterized >= 0.7.4
tox

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,17 @@
from dataclasses import replace
from typing import Any, Iterator
from unittest.mock import patch
import black
import pytest
from parameterized import parameterized
import black
from tests.util import (
BlackBaseTestCase,
fs,
DEFAULT_MODE,
PY36_VERSIONS,
THIS_DIR,
assert_format,
dump_to_stderr,
read_data,
THIS_DIR,
)
SIMPLE_CASES = [
@ -113,33 +114,121 @@
]
class TestSimpleFormat(BlackBaseTestCase):
@parameterized.expand(SIMPLE_CASES_PY2)
@pytest.mark.python2
@patch("black.dump_to_file", dump_to_stderr)
def test_simple_format_py2(self, filename: str) -> None:
self.check_file(filename, DEFAULT_MODE)
@pytest.fixture(autouse=True)
def patch_dump_to_file(request: Any) -> Iterator[None]:
with patch("black.dump_to_file", dump_to_stderr):
yield
@parameterized.expand(SIMPLE_CASES)
@patch("black.dump_to_file", dump_to_stderr)
def test_simple_format(self, filename: str) -> None:
self.check_file(filename, DEFAULT_MODE)
@parameterized.expand(EXPERIMENTAL_STRING_PROCESSING_CASES)
@patch("black.dump_to_file", dump_to_stderr)
def test_experimental_format(self, filename: str) -> None:
self.check_file(filename, black.Mode(experimental_string_processing=True))
def check_file(filename: str, mode: black.Mode, *, data: bool = True) -> None:
source, expected = read_data(filename, data=data)
assert_format(source, expected, mode, fast=False)
@parameterized.expand(SOURCES)
@patch("black.dump_to_file", dump_to_stderr)
def test_source_is_formatted(self, filename: str) -> None:
path = THIS_DIR.parent / filename
self.check_file(str(path), DEFAULT_MODE, data=False)
def check_file(self, filename: str, mode: black.Mode, *, data: bool = True) -> None:
source, expected = read_data(filename, data=data)
actual = fs(source, mode=mode)
self.assertFormatEqual(expected, actual)
if source != actual:
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, mode)
@pytest.mark.parametrize("filename", SIMPLE_CASES_PY2)
@pytest.mark.python2
def test_simple_format_py2(filename: str) -> None:
check_file(filename, DEFAULT_MODE)
@pytest.mark.parametrize("filename", SIMPLE_CASES)
def test_simple_format(filename: str) -> None:
check_file(filename, DEFAULT_MODE)
@pytest.mark.parametrize("filename", EXPERIMENTAL_STRING_PROCESSING_CASES)
def test_experimental_format(filename: str) -> None:
check_file(filename, black.Mode(experimental_string_processing=True))
@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)
# =============== #
# Complex cases
# ============= #
def test_empty() -> None:
source = expected = ""
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("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))
def test_pep_570() -> None:
source, expected = read_data("pep_570")
assert_format(source, expected, minimum_version=(3, 8))
def test_docstring_no_string_normalization() -> None:
"""Like test_docstring but with string normalization off."""
source, expected = read_data("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")
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)
@pytest.mark.python2
def test_python2_print_function() -> None:
source, expected = read_data("python2_print_function")
mode = replace(DEFAULT_MODE, target_versions={black.TargetVersion.PY27})
assert_format(source, expected, mode)
def test_stub() -> None:
mode = replace(DEFAULT_MODE, is_pyi=True)
source, expected = read_data("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))

View File

@ -1,58 +1,97 @@
import os
import sys
import unittest
from pathlib import Path
from typing import Iterator, List, Tuple, Any
from contextlib import contextmanager
from functools import partial
from pathlib import Path
from typing import Any, Iterator, List, Optional, Tuple
import black
from black.output import out, err
from black.debug import DebugVisitor
from black.mode import TargetVersion
from black.output import err, out
THIS_DIR = Path(__file__).parent
DATA_DIR = THIS_DIR / "data"
PROJECT_ROOT = THIS_DIR.parent
EMPTY_LINE = "# EMPTY LINE WITH WHITESPACE" + " (this comment will be removed)"
DETERMINISTIC_HEADER = "[Deterministic header]"
PY36_VERSIONS = {
TargetVersion.PY36,
TargetVersion.PY37,
TargetVersion.PY38,
TargetVersion.PY39,
}
DEFAULT_MODE = black.Mode()
ff = partial(black.format_file_in_place, mode=DEFAULT_MODE, fast=True)
fs = partial(black.format_str, mode=DEFAULT_MODE)
def _assert_format_equal(expected: str, actual: str) -> None:
if actual != expected and not os.environ.get("SKIP_AST_PRINT"):
bdv: DebugVisitor[Any]
out("Expected tree:", fg="green")
try:
exp_node = black.lib2to3_parse(expected)
bdv = DebugVisitor()
list(bdv.visit(exp_node))
except Exception as ve:
err(str(ve))
out("Actual tree:", fg="red")
try:
exp_node = black.lib2to3_parse(actual)
bdv = DebugVisitor()
list(bdv.visit(exp_node))
except Exception as ve:
err(str(ve))
assert actual == expected
def assert_format(
source: str,
expected: str,
mode: black.Mode = DEFAULT_MODE,
*,
fast: bool = False,
minimum_version: Optional[Tuple[int, int]] = None,
) -> None:
"""Convenience function to check that Black formats as expected.
You can pass @minimum_version if you're passing code with newer syntax to guard
safety guards so they don't just crash with a SyntaxError. Please note this is
separate from TargetVerson Mode configuration.
"""
actual = black.format_str(source, mode=mode)
_assert_format_equal(expected, actual)
# It's not useful to run safety checks if we're expecting no changes anyway. The
# assertion right above will raise if reality does actually make changes. This just
# avoids wasted CPU cycles.
if not fast and source != expected:
# Unfortunately the AST equivalence check relies on the built-in ast module
# being able to parse the code being formatted. This doesn't always work out
# when checking modern code on older versions.
if minimum_version is None or sys.version_info >= minimum_version:
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, mode=mode)
def dump_to_stderr(*output: str) -> str:
return "\n" + "\n".join(output) + "\n"
class BlackBaseTestCase(unittest.TestCase):
maxDiff = None
_diffThreshold = 2 ** 20
def assertFormatEqual(self, expected: str, actual: str) -> None:
if actual != expected and not os.environ.get("SKIP_AST_PRINT"):
bdv: DebugVisitor[Any]
out("Expected tree:", fg="green")
try:
exp_node = black.lib2to3_parse(expected)
bdv = DebugVisitor()
list(bdv.visit(exp_node))
except Exception as ve:
err(str(ve))
out("Actual tree:", fg="red")
try:
exp_node = black.lib2to3_parse(actual)
bdv = DebugVisitor()
list(bdv.visit(exp_node))
except Exception as ve:
err(str(ve))
self.assertMultiLineEqual(expected, actual)
_assert_format_equal(expected, actual)
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 = THIS_DIR / "data" if data else PROJECT_ROOT
base_dir = DATA_DIR if data else PROJECT_ROOT
return read_data_from_file(base_dir / name)