Skip to content

Commit

Permalink
test: Add tests for inferred target version
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Aug 12, 2022
1 parent 8843159 commit 295d9d8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/data/project_metadata/both_pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = "test"
version = "1.0.0"
requires-python = ">=3.7,<3.11"

[tool.black]
line-length = 79
target-version = ["py310"]
6 changes: 6 additions & 0 deletions tests/data/project_metadata/neither_pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "test"
version = "1.0.0"

[tool.black]
line-length = 79
7 changes: 7 additions & 0 deletions tests/data/project_metadata/only_black_pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
name = "test"
version = "1.0.0"

[tool.black]
line-length = 79
target-version = ["py310"]
7 changes: 7 additions & 0 deletions tests/data/project_metadata/only_metadata_pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
name = "test"
version = "1.0.0"
requires-python = ">=3.7,<3.11"

[tool.black]
line-length = 79
29 changes: 29 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,35 @@ def test_parse_pyproject_toml(self) -> None:
self.assertEqual(config["exclude"], r"\.pyi?$")
self.assertEqual(config["include"], r"\.py?$")

def test_parse_pyproject_toml_project_metadata(self) -> None:
for test_toml, expected in [
("only_black_pyproject.toml", ["py310"]),
("only_metadata_pyproject.toml", ["py37"]),
("neither_pyproject.toml", None),
("both_pyproject.toml", ["py310"]),
]:
test_toml_file = THIS_DIR / "data" / "project_metadata" / test_toml
config = black.parse_pyproject_toml(str(test_toml_file))
self.assertEqual(config.get("target_version"), expected)

def test_infer_target_version(self) -> None:
for version, expected in [
("3.6", TargetVersion.PY36),
("3.11.0rc1", TargetVersion.PY311),
(">=3.10", TargetVersion.PY310),
(">3.6,<3.10", TargetVersion.PY37),
("==3.8.*", TargetVersion.PY38),
# (">=3.8.6", TargetVersion.PY38), # Doesn't work yet
(None, None),
("", None),
("invalid", None),
("3", None),
("3.2", None),
]:
test_toml = {"project": {"requires-python": version}}
result = black.files.infer_target_version(test_toml)
self.assertEqual(result, expected)

def test_read_pyproject_toml(self) -> None:
test_toml_file = THIS_DIR / "test.toml"
fake_ctx = FakeContext()
Expand Down

0 comments on commit 295d9d8

Please sign in to comment.