/
test_expand.py
215 lines (184 loc) · 7.5 KB
/
test_expand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import pytest
from distutils.errors import DistutilsOptionError
from setuptools.config import expand
from setuptools.discovery import find_package_path
def write_files(files, root_dir):
for file, content in files.items():
path = root_dir / file
path.parent.mkdir(exist_ok=True, parents=True)
path.write_text(content)
def test_glob_relative(tmp_path, monkeypatch):
files = {
"dir1/dir2/dir3/file1.txt",
"dir1/dir2/file2.txt",
"dir1/file3.txt",
"a.ini",
"b.ini",
"dir1/c.ini",
"dir1/dir2/a.ini",
}
write_files({k: "" for k in files}, tmp_path)
patterns = ["**/*.txt", "[ab].*", "**/[ac].ini"]
monkeypatch.chdir(tmp_path)
assert set(expand.glob_relative(patterns)) == files
# Make sure the same APIs work outside cwd
assert set(expand.glob_relative(patterns, tmp_path)) == files
def test_read_files(tmp_path, monkeypatch):
dir_ = tmp_path / "dir_"
(tmp_path / "_dir").mkdir(exist_ok=True)
(tmp_path / "a.txt").touch()
files = {
"a.txt": "a",
"dir1/b.txt": "b",
"dir1/dir2/c.txt": "c"
}
write_files(files, dir_)
with monkeypatch.context() as m:
m.chdir(dir_)
assert expand.read_files(list(files)) == "a\nb\nc"
cannot_access_msg = r"Cannot access '.*\.\..a\.txt'"
with pytest.raises(DistutilsOptionError, match=cannot_access_msg):
expand.read_files(["../a.txt"])
# Make sure the same APIs work outside cwd
assert expand.read_files(list(files), dir_) == "a\nb\nc"
with pytest.raises(DistutilsOptionError, match=cannot_access_msg):
expand.read_files(["../a.txt"], dir_)
class TestReadAttr:
@pytest.mark.parametrize(
"example",
[
# No cookie means UTF-8:
b"__version__ = '\xc3\xa9'\nraise SystemExit(1)\n",
# If a cookie is present, honor it:
b"# -*- coding: utf-8 -*-\n__version__ = '\xc3\xa9'\nraise SystemExit(1)\n",
b"# -*- coding: latin1 -*-\n__version__ = '\xe9'\nraise SystemExit(1)\n",
]
)
def test_read_attr_encoding_cookie(self, example, tmp_path):
(tmp_path / "mod.py").write_bytes(example)
assert expand.read_attr('mod.__version__', root_dir=tmp_path) == 'é'
def test_read_attr(self, tmp_path, monkeypatch):
files = {
"pkg/__init__.py": "",
"pkg/sub/__init__.py": "VERSION = '0.1.1'",
"pkg/sub/mod.py": (
"VALUES = {'a': 0, 'b': {42}, 'c': (0, 1, 1)}\n"
"raise SystemExit(1)"
),
}
write_files(files, tmp_path)
with monkeypatch.context() as m:
m.chdir(tmp_path)
# Make sure it can read the attr statically without evaluating the module
assert expand.read_attr('pkg.sub.VERSION') == '0.1.1'
values = expand.read_attr('lib.mod.VALUES', {'lib': 'pkg/sub'})
assert values['a'] == 0
assert values['b'] == {42}
# Make sure the same APIs work outside cwd
assert expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path) == '0.1.1'
values = expand.read_attr('lib.mod.VALUES', {'lib': 'pkg/sub'}, tmp_path)
assert values['c'] == (0, 1, 1)
@pytest.mark.parametrize(
"example",
[
"VERSION: str\nVERSION = '0.1.1'\nraise SystemExit(1)\n",
"VERSION: str = '0.1.1'\nraise SystemExit(1)\n",
]
)
def test_read_annotated_attr(self, tmp_path, example):
files = {
"pkg/__init__.py": "",
"pkg/sub/__init__.py": example,
}
write_files(files, tmp_path)
# Make sure this attribute can be read statically
assert expand.read_attr('pkg.sub.VERSION', root_dir=tmp_path) == '0.1.1'
def test_import_order(self, tmp_path):
"""
Sometimes the import machinery will import the parent package of a nested
module, which triggers side-effects and might create problems (see issue #3176)
``read_attr`` should bypass these limitations by resolving modules statically
(via ast.literal_eval).
"""
files = {
"src/pkg/__init__.py": "from .main import func\nfrom .about import version",
"src/pkg/main.py": "import super_complicated_dep\ndef func(): return 42",
"src/pkg/about.py": "version = '42'",
}
write_files(files, tmp_path)
attr_desc = "pkg.about.version"
package_dir = {"": "src"}
# `import super_complicated_dep` should not run, otherwise the build fails
assert expand.read_attr(attr_desc, package_dir, tmp_path) == "42"
@pytest.mark.parametrize(
'package_dir, file, module, return_value',
[
({"": "src"}, "src/pkg/main.py", "pkg.main", 42),
({"pkg": "lib"}, "lib/main.py", "pkg.main", 13),
({}, "single_module.py", "single_module", 70),
({}, "flat_layout/pkg.py", "flat_layout.pkg", 836),
]
)
def test_resolve_class(tmp_path, package_dir, file, module, return_value):
files = {file: f"class Custom:\n def testing(self): return {return_value}"}
write_files(files, tmp_path)
cls = expand.resolve_class(f"{module}.Custom", package_dir, tmp_path)
assert cls().testing() == return_value
@pytest.mark.parametrize(
'args, pkgs',
[
({"where": ["."], "namespaces": False}, {"pkg", "other"}),
({"where": [".", "dir1"], "namespaces": False}, {"pkg", "other", "dir2"}),
({"namespaces": True}, {"pkg", "other", "dir1", "dir1.dir2"}),
({}, {"pkg", "other", "dir1", "dir1.dir2"}), # default value for `namespaces`
]
)
def test_find_packages(tmp_path, args, pkgs):
files = {
"pkg/__init__.py",
"other/__init__.py",
"dir1/dir2/__init__.py",
}
write_files({k: "" for k in files}, tmp_path)
package_dir = {}
kwargs = {"root_dir": tmp_path, "fill_package_dir": package_dir, **args}
where = kwargs.get("where", ["."])
assert set(expand.find_packages(**kwargs)) == pkgs
for pkg in pkgs:
pkg_path = find_package_path(pkg, package_dir, tmp_path)
assert os.path.exists(pkg_path)
# Make sure the same APIs work outside cwd
where = [
str((tmp_path / p).resolve()).replace(os.sep, "/") # ensure posix-style paths
for p in args.pop("where", ["."])
]
assert set(expand.find_packages(where=where, **args)) == pkgs
@pytest.mark.parametrize(
"files, where, expected_package_dir",
[
(["pkg1/__init__.py", "pkg1/other.py"], ["."], {}),
(["pkg1/__init__.py", "pkg2/__init__.py"], ["."], {}),
(["src/pkg1/__init__.py", "src/pkg1/other.py"], ["src"], {"": "src"}),
(["src/pkg1/__init__.py", "src/pkg2/__init__.py"], ["src"], {"": "src"}),
(
["src1/pkg1/__init__.py", "src2/pkg2/__init__.py"],
["src1", "src2"],
{"pkg1": "src1/pkg1", "pkg2": "src2/pkg2"},
),
(
["src/pkg1/__init__.py", "pkg2/__init__.py"],
["src", "."],
{"pkg1": "src/pkg1"},
),
],
)
def test_fill_package_dir(tmp_path, files, where, expected_package_dir):
write_files({k: "" for k in files}, tmp_path)
pkg_dir = {}
kwargs = {"root_dir": tmp_path, "fill_package_dir": pkg_dir, "namespaces": False}
pkgs = expand.find_packages(where=where, **kwargs)
assert set(pkg_dir.items()) == set(expected_package_dir.items())
for pkg in pkgs:
pkg_path = find_package_path(pkg, pkg_dir, tmp_path)
assert os.path.exists(pkg_path)