Debian Codegraph @

tests/test_init.py

raw
1 import builtins
2 from contextlib import contextmanager
3 from pathlib import Path
4 from tempfile import TemporaryDirectory
5 from testpath import assert_isfile
6 from unittest.mock import patch
7 import pytest
8
9 try:
10 import tomllib
11 except ImportError:
12 import tomli as tomllib
13
14 from flit import init
15
16
17 @contextmanager
18 def patch_data_dir():
19 with TemporaryDirectory() as td:
20 with patch.object(init, 'get_data_dir', lambda: Path(td)):
21 yield td
22
23 def test_store_defaults():
24 with patch_data_dir():
25 assert init.get_defaults() == {}
26 d = {'author': 'Test'}
27 init.store_defaults(d)
28 assert init.get_defaults() == d
29
30 def fake_input(entries):
31 it = iter(entries)
32 def inner(prompt):
33 try:
34 return next(it)
35 except StopIteration:
36 raise EOFError
37
38 return inner
39
40 def faking_input(entries):
41 return patch.object(builtins, 'input', fake_input(entries))
42
43 def test_prompt_options():
44 ti = init.TerminalIniter()
45 with faking_input(['4', '1']):
46 res = ti.prompt_options('Pick one', [('A', 'Apple'), ('B', 'Banana')])
47 assert res == 'A'
48
49 # Test with a default
50 with faking_input(['']):
51 res = ti.prompt_options('Pick one', [('A', 'Apple'), ('B', 'Banana')],
52 default='B')
53 assert res == 'B'
54
55 @contextmanager
56 def make_dir(files=(), dirs=()):
57 with TemporaryDirectory() as td:
58 tdp = Path(td)
59 for d in dirs:
60 (tdp / d).mkdir()
61 for f in files:
62 (tdp / f).touch()
63 yield td
64
65 def test_guess_module_name():
66 with make_dir(['foo.py', 'foo-bar.py', 'test_foo.py', 'setup.py']) as td:
67 ib = init.IniterBase(td)
68 assert ib.guess_module_name() == 'foo'
69
70 with make_dir(['baz/__init__.py', 'tests/__init__.py'], ['baz', 'tests']) as td:
71 ib = init.IniterBase(td)
72 assert ib.guess_module_name() == 'baz'
73
74 with make_dir(['src/foo.py', 'src/foo-bar.py', 'test_foo.py', 'setup.py'],
75 ['src',]) as td:
76 ib = init.IniterBase(td)
77 assert ib.guess_module_name() == 'foo'
78
79 with make_dir(['src/baz/__init__.py', 'tests/__init__.py'], ['src', 'src/baz', 'tests']) as td:
80 ib = init.IniterBase(td)
81 assert ib.guess_module_name() == 'baz'
82
83 with make_dir(['foo.py', 'bar.py']) as td:
84 ib = init.IniterBase(td)
85 assert ib.guess_module_name() is None
86
87 with make_dir(['src/foo.py', 'src/bar.py'], ['src']) as td:
88 ib = init.IniterBase(td)
89 assert ib.guess_module_name() is None
90
91 def test_write_license():
92 with TemporaryDirectory() as td:
93 ib = init.IniterBase(td)
94 ib.write_license('mit', 'Thomas Kluyver')
95 assert_isfile(Path(td, 'LICENSE'))
96
97 def test_init():
98 responses = ['foo', # Module name
99 'Test Author', # Author
100 '[email protected]', # Author email
101 'http://example.com/', # Home page
102 '1' # License (1 -> MIT)
103 ]
104 with TemporaryDirectory() as td, \
105 patch_data_dir(), \
106 faking_input(responses):
107 ti = init.TerminalIniter(td)
108 ti.initialise()
109
110 generated = Path(td) / 'pyproject.toml'
111 assert_isfile(generated)
112 with generated.open('rb') as f:
113 data = tomllib.load(f)
114 assert data['project']['authors'][0]['email'] == "[email protected]"
115 license = Path(td) / 'LICENSE'
116 assert data['project']['license'] == 'MIT'
117 assert data['project']['license-files'] == ['LICENSE']
118 assert_isfile(license)
119 with license.open() as f:
120 license_text = f.read()
121 assert license_text.startswith("Copyright (c)")
122 assert "Permission is hereby granted, free of charge" in license_text
123 assert "{year}" not in license_text
124 assert "Test Author" in license_text
125
126 def test_init_homepage_and_license_are_optional():
127 responses = ['test_module_name',
128 'Test Author',
129 '[email protected]',
130 '', # Home page omitted
131 '4', # Skip - choose a license later
132 ]
133 with TemporaryDirectory() as td, \
134 patch_data_dir(), \
135 faking_input(responses):
136 ti = init.TerminalIniter(td)
137 ti.initialise()
138 with Path(td, 'pyproject.toml').open('rb') as f:
139 data = tomllib.load(f)
140 assert not Path(td, 'LICENSE').exists()
141 assert data['project'] == {
142 'authors': [{'name': 'Test Author', 'email': '[email protected]'}],
143 'name': 'test_module_name',
144 'dynamic': ['version', 'description'],
145 }
146
147 def test_init_homepage_validator():
148 responses = ['test_module_name',
149 'Test Author',
150 '[email protected]',
151 'www.uh-oh-spagghetti-o.com', # fails validation
152 'https://www.example.org', # passes
153 '4', # Skip - choose a license later
154 ]
155 with TemporaryDirectory() as td, \
156 patch_data_dir(), \
157 faking_input(responses):
158 ti = init.TerminalIniter(td)
159 ti.initialise()
160 with Path(td, 'pyproject.toml').open('rb') as f:
161 data = tomllib.load(f)
162 assert data['project'] == {
163 'authors': [{'name': 'Test Author', 'email': '[email protected]'}],
164 'name': 'test_module_name',
165 'urls': {'Home': 'https://www.example.org'},
166 'dynamic': ['version', 'description'],
167 }
168
169 def test_author_email_field_is_optional():
170 responses = ['test_module_name',
171 'Test Author',
172 '', # Author-email field is skipped
173 'https://www.example.org',
174 '4',
175 ]
176 with TemporaryDirectory() as td, \
177 patch_data_dir(), \
178 faking_input(responses):
179 ti = init.TerminalIniter(td)
180 ti.initialise()
181 with Path(td, 'pyproject.toml').open('rb') as f:
182 data = tomllib.load(f)
183 assert not Path(td, 'LICENSE').exists()
184
185 assert data['project'] == {
186 'authors': [{'name': 'Test Author'}],
187 'name': 'test_module_name',
188 'urls': {'Home': 'https://www.example.org'},
189 'dynamic': ['version', 'description'],
190 }
191
192
193 @pytest.mark.parametrize(
194 "readme_file",
195 ["readme.md", "README.MD", "README.md",
196 "Readme.md", "readme.MD", "readme.rst",
197 "readme.txt"])
198 def test_find_readme(readme_file):
199 with make_dir([readme_file]) as td:
200 ib = init.IniterBase(td)
201 assert ib.find_readme() == readme_file
202
203
204 def test_find_readme_not_found():
205 with make_dir() as td:
206 ib = init.IniterBase(td)
207 assert ib.find_readme() is None
208
209
210 def test_init_readme_found_yes_chosen():
211 responses = ['test_module_name',
212 'Test Author',
213 '[email protected]',
214 '', # Home page omitted
215 '4', # Skip - choose a license later
216 ]
217 with make_dir(["readme.md"]) as td, \
218 patch_data_dir(), \
219 faking_input(responses):
220 ti = init.TerminalIniter(td)
221 ti.initialise()
222 with Path(td, 'pyproject.toml').open('rb') as f:
223 data = tomllib.load(f)
224
225 assert data['project'] == {
226 'authors': [{'name': 'Test Author', 'email': '[email protected]'}],
227 'name': 'test_module_name',
228 'readme': 'readme.md',
229 'dynamic': ['version', 'description'],
230 }
231
232
233 def test_init_non_ascii_author_name():
234 responses = ['foo', # Module name
235 'Test Authôr', # Author
236 '', # Author email omitted
237 '', # Home page omitted
238 '1' # License (1 -> MIT)
239 ]
240 with TemporaryDirectory() as td, \
241 patch_data_dir(), \
242 faking_input(responses):
243 ti = init.TerminalIniter(td)
244 ti.initialise()
245
246 generated = Path(td) / 'pyproject.toml'
247 assert_isfile(generated)
248 with generated.open('r', encoding='utf-8') as f:
249 raw_text = f.read()
250 print(raw_text)
251 assert "Test Authôr" in raw_text
252 assert "\\u00f4" not in raw_text
253 license = Path(td) / 'LICENSE'
254 assert_isfile(license)
255 with license.open(encoding='utf-8') as f:
256 license_text = f.read()
257 assert "Test Authôr" in license_text
258
259
260 @pytest.mark.parametrize('name', [
261 'my_package',
262 'foo',
263 'my_org.my_pkg',
264 'a.b.c',
265 '_pkg',
266 'pkg_123',
267 'a1.b2.c3',
268 ])
269 def test_validate_module_name_valid(name):
270 ib = init.IniterBase()
271 assert ib.validate_module_name(name) is True
272
273
274 @pytest.mark.parametrize('name', [
275 '',
276 '123',
277 'a..b',
278 'a.',
279 '.a',
280 'a-b',
281 'foo bar',
282 'foo/bar',
283 'foo@bar',
284 'foo.123',
285 '123.foo',
286 ])
287 def test_validate_module_name_invalid(name):
288 ib = init.IniterBase()
289 assert ib.validate_module_name(name) is False
290
291
292 def test_init_dotted_module_name():
293 responses = ['my_org.my_pkg', # Module name
294 'Test Author', # Author
295 '[email protected]', # Author email
296 '', # Home page omitted
297 '4', # Skip license
298 ]
299 with TemporaryDirectory() as td, \
300 patch_data_dir(), \
301 faking_input(responses):
302 ti = init.TerminalIniter(td)
303 ti.initialise()
304
305 generated = Path(td) / 'pyproject.toml'
306 assert_isfile(generated)
307 with generated.open('rb') as f:
308 data = tomllib.load(f)
309 assert data['project']['name'] == 'my_org.my_pkg'
310
311
312 def test_init_module_name_validator():
313 responses = ['invalid-module-name', # fails validation
314 'my_org.my_pkg', # passes validation
315 'Test Author',
316 '[email protected]',
317 '',
318 '4',
319 ]
320 with TemporaryDirectory() as td, \
321 patch_data_dir(), \
322 faking_input(responses):
323 ti = init.TerminalIniter(td)
324 ti.initialise()
325 with Path(td, 'pyproject.toml').open('rb') as f:
326 data = tomllib.load(f)
327 assert data['project']['name'] == 'my_org.my_pkg'