11import os
22import pathlib
33
4- from rsconnect .pyproject import lookup_metadata_file , parse_pyproject_python_requires
4+ from rsconnect .pyproject import (
5+ lookup_metadata_file ,
6+ parse_pyproject_python_requires ,
7+ parse_setupcfg_python_requires ,
8+ parse_pyversion_python_requires ,
9+ get_python_version_requirement_parser ,
10+ )
511
612import pytest
713
1117# Most of this tests, verify against three fixture projects that are located in PROJECTS_DIRECTORY
1218# - using_pyproject: contains a pyproject.toml file with a project.requires-python field
1319# - using_setupcfg: contains a setup.cfg file with a options.python_requires field
14- # - using_pyversion: contains a .python-version file and a pyproject.toml file without any version constraint.
20+ # - using_pyversion: contains a .python-version file and pyproject.toml, setup.cfg without any version constraint.
1521# - allofthem: contains all metadata files all with different version constraints.
1622
1723
2329 (
2430 os .path .join (PROJECTS_DIRECTORY , "using_pyversion" ),
2531 (
26- "pyproject.toml" ,
2732 ".python-version" ,
33+ "pyproject.toml" ,
34+ "setup.cfg" ,
2835 ),
2936 ),
30- (os .path .join (PROJECTS_DIRECTORY , "allofthem" ), ("pyproject.toml " , "setup.cfg " , ".python-version " )),
37+ (os .path .join (PROJECTS_DIRECTORY , "allofthem" ), (".python-version " , "pyproject.toml " , "setup.cfg " )),
3138 ],
3239 ids = ["pyproject.toml" , "setup.cfg" , ".python-version" , "allofthem" ],
3340)
@@ -37,6 +44,23 @@ def test_python_project_metadata_detect(project_dir, expected):
3744 assert lookup_metadata_file (project_dir ) == expectation
3845
3946
47+ @pytest .mark .parametrize (
48+ "filename, expected_parser" ,
49+ [
50+ ("pyproject.toml" , parse_pyproject_python_requires ),
51+ ("setup.cfg" , parse_setupcfg_python_requires ),
52+ (".python-version" , parse_pyversion_python_requires ),
53+ ("invalid.txt" , None ),
54+ ],
55+ ids = ["pyproject.toml" , "setup.cfg" , ".python-version" , "invalid" ],
56+ )
57+ def test_get_python_version_requirement_parser (filename , expected_parser ):
58+ """Test that given a metadata file name, the correct parser is returned."""
59+ metadata_file = pathlib .Path (PROJECTS_DIRECTORY ) / filename
60+ parser = get_python_version_requirement_parser (metadata_file )
61+ assert parser == expected_parser
62+
63+
4064@pytest .mark .parametrize (
4165 "project_dir" ,
4266 [
@@ -59,9 +83,43 @@ def test_python_project_metadata_missing(project_dir):
5983 ids = ["option-exists" , "option-missing" ],
6084)
6185def test_pyprojecttoml_python_requires (project_dir , expected ):
62- """Test that the python_requires field is correctly parsed from pyproject.toml.
86+ """Test that the requires-python field is correctly parsed from pyproject.toml.
6387
6488 Both when the option exists or when it missing in the pyproject.toml file.
6589 """
6690 pyproject_file = pathlib .Path (project_dir ) / "pyproject.toml"
6791 assert parse_pyproject_python_requires (pyproject_file ) == expected
92+
93+
94+ @pytest .mark .parametrize (
95+ "project_dir, expected" ,
96+ [
97+ (os .path .join (PROJECTS_DIRECTORY , "using_setupcfg" ), ">=3.8" ),
98+ (os .path .join (PROJECTS_DIRECTORY , "using_pyversion" ), None ),
99+ ],
100+ ids = ["option-exists" , "option-missing" ],
101+ )
102+ def test_setupcfg_python_requires (tmp_path , project_dir , expected ):
103+ """Test that the python_requires field is correctly parsed from setup.cfg.
104+
105+ Both when the option exists or when it missing in the file.
106+ """
107+ setupcfg_file = pathlib .Path (project_dir ) / "setup.cfg"
108+ assert parse_setupcfg_python_requires (setupcfg_file ) == expected
109+
110+
111+ @pytest .mark .parametrize (
112+ "project_dir, expected" ,
113+ [
114+ (os .path .join (PROJECTS_DIRECTORY , "using_pyversion" ), ">=3.8, <3.12" ),
115+ ],
116+ ids = ["option-exists" ],
117+ )
118+ def test_pyversion_python_requires (tmp_path , project_dir , expected ):
119+ """Test that the python version is correctly parsed from .python-version.
120+
121+ We do not test the case where the option is missing, as an empty .python-version file
122+ is not a valid case for a python project.
123+ """
124+ versionfile = pathlib .Path (project_dir ) / ".python-version"
125+ assert parse_pyversion_python_requires (versionfile ) == expected
0 commit comments