Skip to content

Commit 0db587b

Browse files
committed
Fix problems with missing setupcfg_examples.txt (#3239)
2 parents f54ec14 + 4b8b573 commit 0db587b

File tree

7 files changed

+96
-29
lines changed

7 files changed

+96
-29
lines changed

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ include launcher.c
1515
include msvc-build-launcher.cmd
1616
include pytest.ini
1717
include tox.ini
18+
include setuptools/tests/config/setupcfg_examples.txt

changelog.d/3233.misc.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Included missing test file ``setupcfg_examples.txt`` in ``sdist``.

changelog.d/3233.misc.2.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added script that allows developers to download ``setupcfg_examples.txt`` prior to
2+
running tests. By caching these files it should be possible to run the test suite
3+
offline.
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*
22
!.gitignore
3+
!__init__.py
4+
!preload.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import re
2+
from pathlib import Path
3+
from urllib.request import urlopen
4+
5+
__all__ = ["DOWNLOAD_DIR", "retrieve_file", "output_file", "urls_from_file"]
6+
7+
8+
NAME_REMOVE = ("http://", "https://", "github.com/", "/raw/")
9+
DOWNLOAD_DIR = Path(__file__).parent
10+
11+
12+
# ----------------------------------------------------------------------
13+
# Please update ./preload.py accordingly when modifying this file
14+
# ----------------------------------------------------------------------
15+
16+
17+
def output_file(url: str, download_dir: Path = DOWNLOAD_DIR):
18+
file_name = url.strip()
19+
for part in NAME_REMOVE:
20+
file_name = file_name.replace(part, '').strip().strip('/:').strip()
21+
return Path(download_dir, re.sub(r"[^\-_\.\w\d]+", "_", file_name))
22+
23+
24+
def retrieve_file(url: str, download_dir: Path = DOWNLOAD_DIR):
25+
path = output_file(url, download_dir)
26+
if path.exists():
27+
print(f"Skipping {url} (already exists: {path})")
28+
else:
29+
download_dir.mkdir(exist_ok=True, parents=True)
30+
print(f"Downloading {url} to {path}")
31+
download(url, path)
32+
return path
33+
34+
35+
def urls_from_file(list_file: Path):
36+
"""``list_file`` should be a text file where each line corresponds to a URL to
37+
download.
38+
"""
39+
print(f"file: {list_file}")
40+
content = list_file.read_text(encoding="utf-8")
41+
return [url for url in content.splitlines() if not url.startswith("#")]
42+
43+
44+
def download(url: str, dest: Path):
45+
with urlopen(url) as f:
46+
data = f.read()
47+
48+
with open(dest, "wb") as f:
49+
f.write(data)
50+
51+
assert Path(dest).exists()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""This file can be used to preload files needed for testing.
2+
3+
For example you can use::
4+
5+
cd setuptools/tests/config
6+
python -m downloads.preload setupcfg_examples.txt
7+
8+
to make sure the `setup.cfg` examples are downloaded before starting the tests.
9+
"""
10+
import sys
11+
from pathlib import Path
12+
13+
from . import retrieve_file, urls_from_file
14+
15+
16+
if __name__ == "__main__":
17+
urls = urls_from_file(Path(sys.argv[1]))
18+
list(map(retrieve_file, urls))

setuptools/tests/config/test_apply_pyprojecttoml.py

+20-29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Make sure that applying the configuration from pyproject.toml is equivalent to
22
applying a similar configuration from setup.cfg
3+
4+
To run these tests offline, please have a look on ``./downloads/preload.py``
35
"""
46
import io
57
import re
8+
import tarfile
69
from pathlib import Path
7-
from urllib.request import urlopen
810
from unittest.mock import Mock
11+
from zipfile import ZipFile
912

1013
import pytest
1114
from ini2toml.api import Translator
@@ -17,22 +20,23 @@
1720
from setuptools.config._apply_pyprojecttoml import _WouldIgnoreField, _some_attrgetter
1821
from setuptools.command.egg_info import write_requirements
1922

23+
from .downloads import retrieve_file, urls_from_file
24+
2025

21-
EXAMPLES = (Path(__file__).parent / "setupcfg_examples.txt").read_text()
22-
EXAMPLE_URLS = [x for x in EXAMPLES.splitlines() if not x.startswith("#")]
23-
DOWNLOAD_DIR = Path(__file__).parent / "downloads"
26+
HERE = Path(__file__).parent
27+
EXAMPLES_FILE = "setupcfg_examples.txt"
2428

2529

2630
def makedist(path, **attrs):
2731
return Distribution({"src_root": path, **attrs})
2832

2933

30-
@pytest.mark.parametrize("url", EXAMPLE_URLS)
34+
@pytest.mark.parametrize("url", urls_from_file(HERE / EXAMPLES_FILE))
3135
@pytest.mark.filterwarnings("ignore")
3236
@pytest.mark.uses_network
3337
def test_apply_pyproject_equivalent_to_setupcfg(url, monkeypatch, tmp_path):
3438
monkeypatch.setattr(expand, "read_attr", Mock(return_value="0.0.1"))
35-
setupcfg_example = retrieve_file(url, DOWNLOAD_DIR)
39+
setupcfg_example = retrieve_file(url)
3640
pyproject_example = Path(tmp_path, "pyproject.toml")
3741
toml_config = Translator().translate(setupcfg_example.read_text(), "setup.cfg")
3842
pyproject_example.write_text(toml_config)
@@ -276,32 +280,19 @@ def test_optional_dependencies_dont_remove_env_markers(self, tmp_path):
276280
assert "bar" in reqs
277281

278282

279-
# --- Auxiliary Functions ---
280-
281-
282-
NAME_REMOVE = ("http://", "https://", "github.com/", "/raw/")
283+
class TestMeta:
284+
def test_example_file_in_sdist(self, setuptools_sdist):
285+
"""Meta test to ensure tests can run from sdist"""
286+
with tarfile.open(setuptools_sdist) as tar:
287+
assert any(name.endswith(EXAMPLES_FILE) for name in tar.getnames())
283288

289+
def test_example_file_not_in_wheel(self, setuptools_wheel):
290+
"""Meta test to ensure auxiliary test files are not in wheel"""
291+
with ZipFile(setuptools_wheel) as zipfile:
292+
assert not any(name.endswith(EXAMPLES_FILE) for name in zipfile.namelist())
284293

285-
def retrieve_file(url, download_dir):
286-
file_name = url.strip()
287-
for part in NAME_REMOVE:
288-
file_name = file_name.replace(part, '').strip().strip('/:').strip()
289-
file_name = re.sub(r"[^\-_\.\w\d]+", "_", file_name)
290-
path = Path(download_dir, file_name)
291-
if not path.exists():
292-
download_dir.mkdir(exist_ok=True, parents=True)
293-
download(url, path)
294-
return path
295294

296-
297-
def download(url, dest):
298-
with urlopen(url) as f:
299-
data = f.read()
300-
301-
with open(dest, "wb") as f:
302-
f.write(data)
303-
304-
assert Path(dest).exists()
295+
# --- Auxiliary Functions ---
305296

306297

307298
def core_metadata(dist) -> str:

0 commit comments

Comments
 (0)