Skip to content

Commit

Permalink
new tests and changes to reach 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
gpongelli committed Jan 27, 2023
1 parent f1cb32e commit de13d7a
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/check_python_versions/sources/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
check-python-versions supports both.
"""

import io
from io import StringIO
from typing import List, Optional, TextIO, Union, cast

Expand Down Expand Up @@ -58,7 +58,7 @@ def load_toml(
if isinstance(filename, str) or isinstance(filename, StringIO):
with open_file(filename) as fp:
table = load(fp)
if isinstance(filename, TextIO):
if isinstance(filename, io.TextIOWrapper):
table = load(filename)
return table

Expand Down Expand Up @@ -170,35 +170,35 @@ def _get_pyproject_toml_classifiers(

def _get_poetry_python_requires(
table: TOMLDocument
) -> List[str]:
) -> Optional[str]:
if TOOL not in table:
return []
return None
if POETRY not in table[TOOL]:
return []
return None
if DEPENDENCIES not in \
table[TOOL][POETRY]:
return []
return None
if PYTHON not in \
table[TOOL][POETRY][DEPENDENCIES]:
return []
return cast(List[str], table[TOOL][POETRY][DEPENDENCIES][PYTHON])
return None
return cast(str, table[TOOL][POETRY][DEPENDENCIES][PYTHON])


def _get_setuptools_flit_python_requires(
table: TOMLDocument
) -> List[str]:
) -> Optional[str]:
if PROJECT not in table:
return []
return None
if PYTHON_REQUIRES not in \
table[PROJECT]:
return []
return cast(List[str], table[PROJECT][PYTHON_REQUIRES])
return None
return cast(str, table[PROJECT][PYTHON_REQUIRES])


def _get_pyproject_toml_python_requires(
filename: FileOrFilename = PYPROJECT_TOML
) -> List[str]:
_python_requires = []
) -> Optional[str]:
_python_requires = None
table = load_toml(filename)
if is_poetry_toml(table):
_python_requires = _get_poetry_python_requires(table)
Expand All @@ -214,11 +214,8 @@ def get_supported_python_versions(
"""Extract supported Python versions from classifiers in pyproject.toml."""
classifiers = _get_pyproject_toml_classifiers(filename)

if classifiers is None:
# Note: do not return None because pyproject.toml is
# not an optional source!
# We want errors to show up if pyproject.toml fails to
# declare Python versions in classifiers.
if not classifiers:
# classifier can be an empty list when nothing found
return []

if not isinstance(classifiers, list):
Expand All @@ -234,9 +231,8 @@ def get_python_requires(
"""Extract supported Python versions from python_requires in
pyproject.toml."""
python_requires = _get_pyproject_toml_python_requires(pyproject_toml)
if python_requires is None:
return None
if not isinstance(python_requires, str):
if not python_requires or not isinstance(python_requires, str):
# python_requires can be None
warn('The value specified for python dependency is not a string')
return None
return parse_python_requires(python_requires)
Expand All @@ -251,9 +247,8 @@ def update_supported_python_versions(
Does not touch the file but returns a list of lines with new file contents.
"""
classifiers = _get_pyproject_toml_classifiers(filename)
if classifiers is None:
return None
if not isinstance(classifiers, list):
# classifiers is an optional list
if not isinstance(classifiers, list) or not classifiers:
warn('The value specified for classifiers is not an array')
return None
new_classifiers = update_classifiers(classifiers, new_versions)
Expand All @@ -269,7 +264,7 @@ def update_python_requires(
Does not touch the file but returns a list of lines with new file contents.
"""
python_requires = _get_pyproject_toml_python_requires(filename)
if python_requires is None or python_requires == []:
if python_requires is None:
return None
comma = ', '
if ',' in python_requires and ', ' not in python_requires:
Expand Down Expand Up @@ -308,7 +303,7 @@ def _update_pyproject_toml_classifiers(
filename: FileOrFilename,
new_value: Union[str, List[str]],
) -> Optional[FileLines]:
_updated_table: Optional[FileLines] = []
_updated_table: Optional[FileLines] = None
table = load_toml(filename)
if is_poetry_toml(table):
_updated_table = _set_poetry_classifiers(table, new_value)
Expand Down
53 changes: 53 additions & 0 deletions tests/sources/test_pyproject_flit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
is_setuptools_toml,
load_toml,
update_python_requires,
update_supported_python_versions,
)
from check_python_versions.utils import (
FileLines,
FileOrFilename,
open_file,
)
from check_python_versions.versions import Version

Expand Down Expand Up @@ -100,6 +102,57 @@ def test_update_supported_python_versions_not_a_list(tmp_path, capsys):
)


def test_update_supported_python_versions_flit(tmp_path, capsys):
filename = tmp_path / "pyproject.toml"
filename.write_text(textwrap.dedent("""\
[project]
name='foo'
classifiers=[
'Programming Language :: Python :: 3.6'
]
[build-system]
requires = ["flit_core >=3.2,<4"]
build-backend = "flit_core.buildapi"
"""))
result = update_supported_python_versions(str(filename),
v(['3.7', '3.8']))
assert result == ['[project]',
" name='foo'",
' classifiers=['
'"Programming Language :: Python :: 3.7", '
'"Programming Language :: Python :: 3.8"]',
'[build-system]',
' requires = ["flit_core >=3.2,<4"]',
' build-backend = "flit_core.buildapi"',
'']


def test_get_python_requires_flit_no_project_table(tmp_path):
filename = tmp_path / "pyproject.toml"
filename.write_text(textwrap.dedent("""\
[fake-project]
name='foo'
[build-system]
requires = ["flit_core >=3.2,<4"]
build-backend = "flit_core.buildapi"
"""))
with open_file(str(filename)) as _file_obj:
assert get_python_requires(_file_obj) is None


def test_supported_python_versions_setuptools_no_classifier(tmp_path):
filename = tmp_path / "pyproject.toml"
filename.write_text(textwrap.dedent("""\
[project]
name='foo'
[build-system]
requires = ["flit_core >=3.2,<4"]
build-backend = "flit_core.buildapi"
"""))
with open_file(str(filename)) as _file_obj:
assert get_supported_python_versions(_file_obj) == []


def test_get_python_requires(tmp_path, fix_max_python_3_version):
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(textwrap.dedent("""\
Expand Down
Loading

0 comments on commit de13d7a

Please sign in to comment.