1
1
import os
2
2
import pathlib
3
3
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
+ )
5
11
6
12
import pytest
7
13
11
17
# Most of this tests, verify against three fixture projects that are located in PROJECTS_DIRECTORY
12
18
# - using_pyproject: contains a pyproject.toml file with a project.requires-python field
13
19
# - 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.
15
21
# - allofthem: contains all metadata files all with different version constraints.
16
22
17
23
23
29
(
24
30
os .path .join (PROJECTS_DIRECTORY , "using_pyversion" ),
25
31
(
26
- "pyproject.toml" ,
27
32
".python-version" ,
33
+ "pyproject.toml" ,
34
+ "setup.cfg" ,
28
35
),
29
36
),
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 " )),
31
38
],
32
39
ids = ["pyproject.toml" , "setup.cfg" , ".python-version" , "allofthem" ],
33
40
)
@@ -37,6 +44,23 @@ def test_python_project_metadata_detect(project_dir, expected):
37
44
assert lookup_metadata_file (project_dir ) == expectation
38
45
39
46
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
+
40
64
@pytest .mark .parametrize (
41
65
"project_dir" ,
42
66
[
@@ -59,9 +83,43 @@ def test_python_project_metadata_missing(project_dir):
59
83
ids = ["option-exists" , "option-missing" ],
60
84
)
61
85
def 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.
63
87
64
88
Both when the option exists or when it missing in the pyproject.toml file.
65
89
"""
66
90
pyproject_file = pathlib .Path (project_dir ) / "pyproject.toml"
67
91
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