|
1 | 1 | import os |
2 | 2 | import pathlib |
| 3 | +import tempfile |
| 4 | + |
| 5 | +import pytest |
3 | 6 |
|
4 | 7 | from rsconnect.pyproject import ( |
| 8 | + detect_python_version_requirement, |
| 9 | + get_python_version_requirement_parser, |
5 | 10 | lookup_metadata_file, |
6 | 11 | parse_pyproject_python_requires, |
7 | | - parse_setupcfg_python_requires, |
8 | 12 | parse_pyversion_python_requires, |
9 | | - get_python_version_requirement_parser, |
10 | | - detect_python_version_requirement, |
| 13 | + parse_setupcfg_python_requires, |
11 | 14 | ) |
12 | 15 |
|
13 | | -import pytest |
14 | | - |
15 | 16 | HERE = os.path.dirname(__file__) |
16 | 17 | PROJECTS_DIRECTORY = os.path.abspath(os.path.join(HERE, "testdata", "python-project")) |
17 | 18 |
|
@@ -142,3 +143,52 @@ def test_detect_python_version_requirement(): |
142 | 143 | assert detect_python_version_requirement(project_dir) == ">=3.8, <3.12" |
143 | 144 |
|
144 | 145 | assert detect_python_version_requirement(os.path.join(PROJECTS_DIRECTORY, "empty")) is None |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.parametrize( # type: ignore |
| 149 | + ["content", "expected"], |
| 150 | + [ |
| 151 | + ("3.8", "~=3.8"), |
| 152 | + ("3.8.0", "~=3.8"), |
| 153 | + ("3.8.0b1", ValueError("Invalid python version, pre-release versions are not supported: 3.8.0b1")), |
| 154 | + ("3.8.0rc1", ValueError("Invalid python version, pre-release versions are not supported: 3.8.0rc1")), |
| 155 | + ("3.8.0a1", ValueError("Invalid python version, pre-release versions are not supported: 3.8.0a1")), |
| 156 | + ("3.8.*", "==3.8.*"), |
| 157 | + ("3.*", "==3.*"), |
| 158 | + ("*", ValueError("Invalid python version: *")), |
| 159 | + # This is not perfect, but the added regex complexity doesn't seem worth it. |
| 160 | + ("invalid", ValueError("Invalid python version, pre-release versions are not supported: invalid")), |
| 161 | + ("[email protected]", ValueError("Invalid python version, python specific implementations are not supported: [email protected]")), |
| 162 | + ( |
| 163 | + "cpython-3.12.3-macos-aarch64-none", |
| 164 | + ValueError( |
| 165 | + "Invalid python version, python specific implementations are not supported: " |
| 166 | + "cpython-3.12.3-macos-aarch64-none" |
| 167 | + ), |
| 168 | + ), |
| 169 | + ( |
| 170 | + "/usr/bin/python3.8", |
| 171 | + ValueError("Invalid python version, python specific implementations are not supported: /usr/bin/python3.8"), |
| 172 | + ), |
| 173 | + ], |
| 174 | +) |
| 175 | +def test_python_version_file_adapt(content, expected): |
| 176 | + """Test that the python version is correctly converted to a PEP440 format. |
| 177 | +
|
| 178 | + Connect expects a PEP440 format, but the .python-version file can contain |
| 179 | + plain version numbers and other formats. |
| 180 | +
|
| 181 | + We should convert them to the constraints that connect expects. |
| 182 | + """ |
| 183 | + with tempfile.NamedTemporaryFile(mode="w+") as tmpfile: |
| 184 | + tmpfile.write(content) |
| 185 | + tmpfile.flush() |
| 186 | + |
| 187 | + versionfile = pathlib.Path(tmpfile.name) |
| 188 | + |
| 189 | + if isinstance(expected, Exception): |
| 190 | + with pytest.raises(expected.__class__) as excinfo: |
| 191 | + parse_pyversion_python_requires(versionfile) |
| 192 | + assert str(excinfo.value) == expected.args[0] |
| 193 | + else: |
| 194 | + assert parse_pyversion_python_requires(versionfile) == expected |
0 commit comments