Debian Codegraph @

tests/test_install.py

raw
1 import json
2 import os
3 import pathlib
4 import sys
5 import tempfile
6 from unittest import TestCase, SkipTest, skipIf
7 from unittest.mock import patch
8
9 import pytest
10 from testpath import (
11 assert_isfile, assert_isdir, assert_islink, assert_not_path_exists, MockCommand
12 )
13
14 from flit import install
15 from flit.install import Installer, _requires_dist_to_pip_requirement, DependencyError
16
17 tests_dir = pathlib.Path(__file__).parent
18 samples_dir = tests_dir / 'samples'
19 core_samples_dir = tests_dir.parent / 'flit_core' / 'tests_core' / 'samples'
20
21 class InstallTests(TestCase):
22 def setUp(self):
23 td = tempfile.TemporaryDirectory()
24 self.addCleanup(td.cleanup)
25 self.get_dirs_patch = patch('flit.install.get_dirs',
26 return_value={
27 'scripts': os.path.join(td.name, 'scripts'),
28 'purelib': os.path.join(td.name, 'site-packages'),
29 'data': os.path.join(td.name, 'data'),
30 })
31 self.get_dirs_patch.start()
32 self.tmpdir = pathlib.Path(td.name)
33
34 def tearDown(self):
35 self.get_dirs_patch.stop()
36
37 def _assert_direct_url(self, directory, package, version, expected_editable):
38 direct_url_file = (
39 self.tmpdir
40 / 'site-packages'
41 / f'{package}-{version}.dist-info'
42 / 'direct_url.json'
43 )
44 assert_isfile(direct_url_file)
45 with direct_url_file.open() as f:
46 direct_url = json.load(f)
47 assert direct_url['url'].startswith('file:///')
48 assert direct_url['url'] == directory.as_uri()
49 assert direct_url['dir_info'].get('editable') is expected_editable
50
51 def test_install_module(self):
52 Installer.from_ini_path(samples_dir / 'module1_toml' / 'pyproject.toml').install_directly()
53 assert_isfile(self.tmpdir / 'site-packages' / 'module1.py')
54 assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.1.dist-info')
55 self._assert_direct_url(
56 samples_dir / 'module1_toml', 'module1', '0.1', expected_editable=False
57 )
58
59 @skipIf(not core_samples_dir.is_dir(), "Missing flit_core samples")
60 def test_install_module_pep621(self):
61 Installer.from_ini_path(
62 core_samples_dir / 'pep621_nodynamic' / 'pyproject.toml',
63 ).install_directly()
64 assert_isfile(self.tmpdir / 'site-packages' / 'module1.py')
65 assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.3.dist-info')
66 self._assert_direct_url(
67 core_samples_dir / 'pep621_nodynamic', 'module1', '0.3',
68 expected_editable=False
69 )
70
71 def test_install_package(self):
72 oldcwd = os.getcwd()
73 os.chdir(str(samples_dir / 'package1'))
74 try:
75 Installer.from_ini_path(pathlib.Path('pyproject.toml')).install_directly()
76 finally:
77 os.chdir(oldcwd)
78 assert_isdir(self.tmpdir / 'site-packages' / 'package1')
79 assert_isdir(self.tmpdir / 'site-packages' / 'package1-0.1.dist-info')
80 assert_isfile(self.tmpdir / 'scripts' / 'pkg_script')
81 with (self.tmpdir / 'scripts' / 'pkg_script').open() as f:
82 assert f.readline().strip() == "#!" + sys.executable
83 if sys.platform == 'win32':
84 cmd_path = self.tmpdir / 'scripts' / 'pkg_script.cmd'
85 assert_isfile(cmd_path)
86 raw = cmd_path.read_bytes()
87 assert b'\r\r\n' not in raw
88 assert raw.startswith(b'@echo off\r\n')
89 assert sys.executable.encode('utf-8') in raw
90 self._assert_direct_url(
91 samples_dir / 'package1', 'package1', '0.1', expected_editable=False
92 )
93
94 def test_install_module_in_src(self):
95 oldcwd = os.getcwd()
96 os.chdir(samples_dir / 'packageinsrc')
97 try:
98 Installer.from_ini_path(pathlib.Path('pyproject.toml')).install_directly()
99 finally:
100 os.chdir(oldcwd)
101 assert_isfile(self.tmpdir / 'site-packages' / 'module1.py')
102 assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.1.dist-info')
103
104 def test_install_ns_package_native(self):
105 Installer.from_ini_path(samples_dir / 'ns1-pkg' / 'pyproject.toml').install_directly()
106 assert_isdir(self.tmpdir / 'site-packages' / 'ns1')
107 assert_isfile(self.tmpdir / 'site-packages' / 'ns1' / 'pkg' / '__init__.py')
108 assert_not_path_exists(self.tmpdir / 'site-packages' / 'ns1' / '__init__.py')
109 assert_isdir(self.tmpdir / 'site-packages' / 'ns1_pkg-0.1.dist-info')
110
111 def test_install_ns_package_module_native(self):
112 Installer.from_ini_path(samples_dir / 'ns1-pkg-mod' / 'pyproject.toml').install_directly()
113 assert_isfile(self.tmpdir / 'site-packages' / 'ns1' / 'module.py')
114 assert_not_path_exists(self.tmpdir / 'site-packages' / 'ns1' / '__init__.py')
115
116 def test_install_ns_package_native_symlink(self):
117 if os.name == 'nt':
118 raise SkipTest('symlink')
119 Installer.from_ini_path(
120 samples_dir / 'ns1-pkg' / 'pyproject.toml', symlink=True
121 ).install_directly()
122 Installer.from_ini_path(
123 samples_dir / 'ns1-pkg2' / 'pyproject.toml', symlink=True
124 ).install_directly()
125 Installer.from_ini_path(
126 samples_dir / 'ns1-pkg-mod' / 'pyproject.toml', symlink=True
127 ).install_directly()
128 assert_isdir(self.tmpdir / 'site-packages' / 'ns1')
129 assert_isdir(self.tmpdir / 'site-packages' / 'ns1' / 'pkg')
130 assert_islink(self.tmpdir / 'site-packages' / 'ns1' / 'pkg',
131 to=str(samples_dir / 'ns1-pkg' / 'ns1' / 'pkg'))
132 assert_isdir(self.tmpdir / 'site-packages' / 'ns1_pkg-0.1.dist-info')
133
134 assert_isdir(self.tmpdir / 'site-packages' / 'ns1' / 'pkg2')
135 assert_islink(self.tmpdir / 'site-packages' / 'ns1' / 'pkg2',
136 to=str(samples_dir / 'ns1-pkg2' / 'ns1' / 'pkg2'))
137 assert_isdir(self.tmpdir / 'site-packages' / 'ns1_pkg2-0.1.dist-info')
138
139 assert_islink(self.tmpdir / 'site-packages' / 'ns1' / 'module.py',
140 to=samples_dir / 'ns1-pkg-mod' / 'ns1' / 'module.py')
141 assert_isdir(self.tmpdir / 'site-packages' / 'ns1_module-0.1.dist-info')
142
143 def test_install_ns_package_pth_file(self):
144 Installer.from_ini_path(
145 samples_dir / 'ns1-pkg' / 'pyproject.toml', pth=True
146 ).install_directly()
147
148 pth_file = self.tmpdir / 'site-packages' / 'ns1.pkg.pth'
149 assert_isfile(pth_file)
150 assert pth_file.read_text('utf-8').strip() == str(samples_dir / 'ns1-pkg')
151
152 def test_symlink_package(self):
153 if os.name == 'nt':
154 raise SkipTest("symlink")
155 Installer.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', symlink=True).install()
156 assert_islink(self.tmpdir / 'site-packages' / 'package1',
157 to=samples_dir / 'package1' / 'package1')
158 assert_isfile(self.tmpdir / 'scripts' / 'pkg_script')
159 with (self.tmpdir / 'scripts' / 'pkg_script').open() as f:
160 assert f.readline().strip() == "#!" + sys.executable
161 self._assert_direct_url(
162 samples_dir / 'package1', 'package1', '0.1', expected_editable=True
163 )
164
165 @skipIf(not core_samples_dir.is_dir(), "Missing flit_core samples")
166 def test_symlink_module_pep621(self):
167 if os.name == 'nt':
168 raise SkipTest("symlink")
169 Installer.from_ini_path(
170 core_samples_dir / 'pep621_nodynamic' / 'pyproject.toml', symlink=True
171 ).install_directly()
172 assert_islink(self.tmpdir / 'site-packages' / 'module1.py',
173 to=core_samples_dir / 'pep621_nodynamic' / 'module1.py')
174 assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.3.dist-info')
175 self._assert_direct_url(
176 core_samples_dir / 'pep621_nodynamic', 'module1', '0.3',
177 expected_editable=True
178 )
179
180 def test_symlink_module_in_src(self):
181 if os.name == 'nt':
182 raise SkipTest("symlink")
183 oldcwd = os.getcwd()
184 os.chdir(samples_dir / 'packageinsrc')
185 try:
186 Installer.from_ini_path(
187 pathlib.Path('pyproject.toml'), symlink=True
188 ).install_directly()
189 finally:
190 os.chdir(oldcwd)
191 assert_islink(self.tmpdir / 'site-packages' / 'module1.py',
192 to=(samples_dir / 'packageinsrc' / 'src' / 'module1.py'))
193 assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.1.dist-info')
194
195 def test_pth_package(self):
196 Installer.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', pth=True).install()
197 assert_isfile(self.tmpdir / 'site-packages' / 'package1.pth')
198 with open(str(self.tmpdir / 'site-packages' / 'package1.pth')) as f:
199 assert f.read() == str(samples_dir / 'package1')
200 assert_isfile(self.tmpdir / 'scripts' / 'pkg_script')
201 self._assert_direct_url(
202 samples_dir / 'package1', 'package1', '0.1', expected_editable=True
203 )
204
205 def test_pth_module_in_src(self):
206 oldcwd = os.getcwd()
207 os.chdir(samples_dir / 'packageinsrc')
208 try:
209 Installer.from_ini_path(
210 pathlib.Path('pyproject.toml'), pth=True
211 ).install_directly()
212 finally:
213 os.chdir(oldcwd)
214 pth_path = self.tmpdir / 'site-packages' / 'module1.pth'
215 assert_isfile(pth_path)
216 assert pth_path.read_text('utf-8').strip() == str(
217 samples_dir / 'packageinsrc' / 'src'
218 )
219 assert_isdir(self.tmpdir / 'site-packages' / 'module1-0.1.dist-info')
220
221 def test_dist_name(self):
222 Installer.from_ini_path(samples_dir / 'altdistname' / 'pyproject.toml').install_directly()
223 assert_isdir(self.tmpdir / 'site-packages' / 'package1')
224 assert_isdir(self.tmpdir / 'site-packages' / 'package_dist1-0.1.dist-info')
225
226 def test_entry_points(self):
227 Installer.from_ini_path(samples_dir / 'entrypoints_valid' / 'pyproject.toml').install_directly()
228 assert_isfile(self.tmpdir / 'site-packages' / 'package1-0.1.dist-info' / 'entry_points.txt')
229
230 def test_pip_install(self):
231 ins = Installer.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', python='mock_python',
232 user=False)
233
234 with MockCommand('mock_python') as mock_py:
235 ins.install()
236
237 calls = mock_py.get_calls()
238 assert len(calls) == 1
239 cmd = calls[0]['argv']
240 assert cmd[1:4] == ['-m', 'pip', 'install']
241 assert cmd[4].endswith('package1')
242
243 def test_symlink_other_python(self):
244 if os.name == 'nt':
245 raise SkipTest('symlink')
246 (self.tmpdir / 'site-packages2').mkdir()
247 (self.tmpdir / 'scripts2').mkdir()
248
249 # Called by Installer._auto_user() :
250 script1 = ("#!{python}\n"
251 "import sysconfig\n"
252 "print(True)\n" # site.ENABLE_USER_SITE
253 "print({purelib!r})" # sysconfig.get_path('purelib')
254 ).format(python=sys.executable,
255 purelib=str(self.tmpdir / 'site-packages2'))
256
257 # Called by Installer._get_dirs() :
258 script2 = ("#!{python}\n"
259 "import json, sys\n"
260 "json.dump({{'purelib': {purelib!r}, 'scripts': {scripts!r}, 'data': {data!r} }}, "
261 "sys.stdout)"
262 ).format(python=sys.executable,
263 purelib=str(self.tmpdir / 'site-packages2'),
264 scripts=str(self.tmpdir / 'scripts2'),
265 data=str(self.tmpdir / 'data'),
266 )
267
268 with MockCommand('mock_python', content=script1):
269 ins = Installer.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', python='mock_python',
270 symlink=True)
271 with MockCommand('mock_python', content=script2):
272 ins.install()
273
274 assert_islink(self.tmpdir / 'site-packages2' / 'package1',
275 to=samples_dir / 'package1' / 'package1')
276 assert_isfile(self.tmpdir / 'scripts2' / 'pkg_script')
277 with (self.tmpdir / 'scripts2' / 'pkg_script').open() as f:
278 assert f.readline().strip() == "#!mock_python"
279
280 def test_install_requires(self):
281 ins = Installer.from_ini_path(samples_dir / 'requires-requests.toml',
282 user=False, python='mock_python')
283
284 with MockCommand('mock_python') as mockpy:
285 ins.install_requirements()
286 calls = mockpy.get_calls()
287 assert len(calls) == 1
288 assert calls[0]['argv'][1:5] == ['-m', 'pip', 'install', '-r']
289
290 @skipIf(not core_samples_dir.is_dir(), "Missing flit_core samples")
291 def test_install_reqs_my_python_if_needed_pep621(self):
292 ins = Installer.from_ini_path(
293 core_samples_dir / 'pep621_nodynamic' / 'pyproject.toml',
294 deps='none',
295 )
296
297 # This shouldn't try to get version & docstring from the module
298 ins.install_reqs_my_python_if_needed()
299
300 def test_extras_error(self):
301 with pytest.raises(DependencyError):
302 Installer.from_ini_path(samples_dir / 'requires-requests.toml',
303 user=False, deps='none', extras='dev')
304
305 @skipIf(not core_samples_dir.is_dir(), "Missing flit_core samples")
306 def test_install_data_dir(self):
307 Installer.from_ini_path(
308 core_samples_dir / 'with_data_dir' / 'pyproject.toml',
309 ).install_directly()
310 assert_isfile(self.tmpdir / 'site-packages' / 'module1.py')
311 assert_isfile(self.tmpdir / 'data' / 'share' / 'man' / 'man1' / 'foo.1')
312
313 @skipIf(not core_samples_dir.is_dir(), "Missing flit_core samples")
314 def test_symlink_data_dir(self):
315 if os.name == 'nt':
316 raise SkipTest("symlink")
317 Installer.from_ini_path(
318 core_samples_dir / 'with_data_dir' / 'pyproject.toml', symlink=True
319 ).install_directly()
320 assert_isfile(self.tmpdir / 'site-packages' / 'module1.py')
321 assert_islink(
322 self.tmpdir / 'data' / 'share' / 'man' / 'man1' / 'foo.1',
323 to=core_samples_dir / 'with_data_dir' / 'data' / 'share' / 'man' / 'man1' / 'foo.1'
324 )
325
326 @pytest.mark.parametrize(('deps', 'extras', 'installed'), [
327 ('none', [], set()),
328 ('develop', [], {'pytest ;', 'toml ;'}),
329 ('production', [], {'toml ;'}),
330 ('all', [], {'toml ;', 'pytest ;', 'requests ;'}),
331 ])
332 def test_install_requires_extra(deps, extras, installed):
333 it = InstallTests()
334 try:
335 it.setUp()
336 ins = Installer.from_ini_path(samples_dir / 'extras' / 'pyproject.toml', python='mock_python',
337 user=False, deps=deps, extras=extras)
338
339 cmd = MockCommand('mock_python')
340 get_reqs = (
341 f"#!{sys.executable}\n"
342 "import sys\n"
343 f"with open({cmd.recording_file!r}, 'wb') as w, open(sys.argv[-1], 'rb') as r:\n"
344 " w.write(r.read())"
345 )
346 cmd.content = get_reqs
347
348 with cmd as mock_py:
349 ins.install_requirements()
350 with open(mock_py.recording_file) as f:
351 str_deps = f.read()
352 deps = str_deps.split('\n') if str_deps else []
353
354 assert set(deps) == installed
355 finally:
356 it.tearDown()
357
358 def test_requires_dist_to_pip_requirement():
359 rd = 'pathlib2 (>=2.3); python_version == "2.7"'
360 assert _requires_dist_to_pip_requirement(rd) == \
361 'pathlib2>=2.3 ; python_version == "2.7"'
362
363 def test_test_writable_dir_win():
364 with tempfile.TemporaryDirectory() as td:
365 assert install._test_writable_dir_win(td) is True
366
367 # Ironically, I don't know how to make a non-writable dir on Windows,
368 # so although the functionality is for Windows, the test is for Posix
369 if os.name != 'posix':
370 return
371
372 # Remove write permissions from the directory
373 os.chmod(td, 0o444)
374 try:
375 assert install._test_writable_dir_win(td) is False
376 finally:
377 os.chmod(td, 0o644)