|
1 | 1 | """Test .dist-info style distributions.
|
2 | 2 | """
|
| 3 | +import pathlib |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from functools import partial |
3 | 8 |
|
4 | 9 | import pytest
|
5 | 10 |
|
6 | 11 | import pkg_resources
|
| 12 | +from setuptools.archive_util import unpack_archive |
7 | 13 | from .textwrap import DALS
|
8 | 14 |
|
9 | 15 |
|
| 16 | +read = partial(pathlib.Path.read_text, encoding="utf-8") |
| 17 | + |
| 18 | + |
10 | 19 | class TestDistInfo:
|
11 | 20 |
|
12 | 21 | metadata_base = DALS("""
|
@@ -72,3 +81,78 @@ def test_conditional_dependencies(self, metadata):
|
72 | 81 | pkg_resources.Requirement.parse('quux>=1.1;extra=="baz"'),
|
73 | 82 | ]
|
74 | 83 | assert d.extras == ['baz']
|
| 84 | + |
| 85 | + def test_invalid_version(self, tmp_path): |
| 86 | + config = "[metadata]\nname=proj\nversion=42\n[egg_info]\ntag_build=invalid!!!\n" |
| 87 | + (tmp_path / "setup.cfg").write_text(config, encoding="utf-8") |
| 88 | + msg = re.compile("invalid version", re.M | re.I) |
| 89 | + output = run_command("dist_info", cwd=tmp_path) |
| 90 | + assert msg.search(output) |
| 91 | + dist_info = next(tmp_path.glob("*.dist-info")) |
| 92 | + assert dist_info.name.startswith("proj-42") |
| 93 | + |
| 94 | + |
| 95 | +class TestWheelCompatibility: |
| 96 | + """Make sure the .dist-info directory produced with the ``dist_info`` command |
| 97 | + is the same as the one produced by ``bdist_wheel``. |
| 98 | + """ |
| 99 | + SETUPCFG = DALS(""" |
| 100 | + [metadata] |
| 101 | + name = {name} |
| 102 | + version = {version} |
| 103 | +
|
| 104 | + [options] |
| 105 | + install_requires = foo>=12; sys_platform != "linux" |
| 106 | +
|
| 107 | + [options.extras_require] |
| 108 | + test = pytest |
| 109 | +
|
| 110 | + [options.entry_points] |
| 111 | + console_scripts = |
| 112 | + executable-name = my_package.module:function |
| 113 | + discover = |
| 114 | + myproj = my_package.other_module:function |
| 115 | + """) |
| 116 | + |
| 117 | + EGG_INFO_OPTS = [ |
| 118 | + # Related: #3088 #2872 |
| 119 | + ("", ""), |
| 120 | + (".post", "[egg_info]\ntag_build = post\n"), |
| 121 | + (".post", "[egg_info]\ntag_build = .post\n"), |
| 122 | + (".post", "[egg_info]\ntag_build = post\ntag_date = 1\n"), |
| 123 | + (".dev", "[egg_info]\ntag_build = .dev\n"), |
| 124 | + (".dev", "[egg_info]\ntag_build = .dev\ntag_date = 1\n"), |
| 125 | + ("a1", "[egg_info]\ntag_build = .a1\n"), |
| 126 | + ("+local", "[egg_info]\ntag_build = +local\n"), |
| 127 | + ] |
| 128 | + |
| 129 | + @pytest.mark.parametrize("name", "my-proj my_proj my.proj My.Proj".split()) |
| 130 | + @pytest.mark.parametrize("version", ["0.42.13"]) |
| 131 | + @pytest.mark.parametrize("suffix, cfg", EGG_INFO_OPTS) |
| 132 | + def test_dist_info_is_the_same_as_in_wheel( |
| 133 | + self, name, version, tmp_path, suffix, cfg |
| 134 | + ): |
| 135 | + config = self.SETUPCFG.format(name=name, version=version) + cfg |
| 136 | + |
| 137 | + for i in "dir_wheel", "dir_dist": |
| 138 | + (tmp_path / i).mkdir() |
| 139 | + (tmp_path / i / "setup.cfg").write_text(config, encoding="utf-8") |
| 140 | + |
| 141 | + run_command("bdist_wheel", cwd=tmp_path / "dir_wheel") |
| 142 | + wheel = next(tmp_path.glob("dir_wheel/dist/*.whl")) |
| 143 | + unpack_archive(wheel, tmp_path / "unpack") |
| 144 | + wheel_dist_info = next(tmp_path.glob("unpack/*.dist-info")) |
| 145 | + |
| 146 | + run_command("dist_info", cwd=tmp_path / "dir_dist") |
| 147 | + dist_info = next(tmp_path.glob("dir_dist/*.dist-info")) |
| 148 | + |
| 149 | + assert dist_info.name == wheel_dist_info.name |
| 150 | + assert dist_info.name.startswith(f"{name.replace('-', '_')}-{version}{suffix}") |
| 151 | + for file in "METADATA", "entry_points.txt": |
| 152 | + assert read(dist_info / file) == read(wheel_dist_info / file) |
| 153 | + |
| 154 | + |
| 155 | +def run_command(*cmd, **kwargs): |
| 156 | + opts = {"stderr": subprocess.STDOUT, "text": True, **kwargs} |
| 157 | + cmd = [sys.executable, "-c", "__import__('setuptools').setup()", *cmd] |
| 158 | + return subprocess.check_output(cmd, **opts) |
0 commit comments