-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
test_editable_builder.py
235 lines (177 loc) · 6.48 KB
/
test_editable_builder.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
import shutil
from pathlib import Path
import pytest
from cleo.io.null_io import NullIO
from poetry.factory import Factory
from poetry.masonry.builders.editable import EditableBuilder
from poetry.utils.env import EnvManager
from poetry.utils.env import MockEnv
from poetry.utils.env import VirtualEnv
@pytest.fixture()
def simple_poetry():
poetry = Factory().create_poetry(
Path(__file__).parent.parent.parent / "fixtures" / "simple_project"
)
return poetry
@pytest.fixture()
def project_with_include():
poetry = Factory().create_poetry(
Path(__file__).parent.parent.parent / "fixtures" / "with-include"
)
return poetry
@pytest.fixture()
def extended_poetry():
poetry = Factory().create_poetry(
Path(__file__).parent.parent.parent / "fixtures" / "extended_project"
)
return poetry
@pytest.fixture()
def extended_without_setup_poetry():
poetry = Factory().create_poetry(
Path(__file__).parent.parent.parent
/ "fixtures"
/ "extended_project_without_setup"
)
return poetry
@pytest.fixture()
def env_manager(simple_poetry):
return EnvManager(simple_poetry)
@pytest.fixture
def tmp_venv(tmp_dir, env_manager):
venv_path = Path(tmp_dir) / "venv"
env_manager.build_venv(str(venv_path))
venv = VirtualEnv(venv_path)
yield venv
shutil.rmtree(str(venv.path))
def test_builder_installs_proper_files_for_standard_packages(simple_poetry, tmp_venv):
builder = EditableBuilder(simple_poetry, tmp_venv, NullIO())
builder.build()
assert tmp_venv._bin_dir.joinpath("foo").exists()
pth_file = "simple_project.pth"
assert tmp_venv.site_packages.exists(pth_file)
assert (
simple_poetry.file.parent.resolve().as_posix()
== tmp_venv.site_packages.find(pth_file)[0].read_text().strip(os.linesep)
)
dist_info = "simple_project-1.2.3.dist-info"
assert tmp_venv.site_packages.exists(dist_info)
dist_info = tmp_venv.site_packages.find(dist_info)[0]
assert dist_info.joinpath("INSTALLER").exists()
assert dist_info.joinpath("METADATA").exists()
assert dist_info.joinpath("RECORD").exists()
assert dist_info.joinpath("entry_points.txt").exists()
assert "poetry" == dist_info.joinpath("INSTALLER").read_text()
assert (
"[console_scripts]\nbaz=bar:baz.boom.bim\nfoo=foo:bar\nfox=fuz.foo:bar.baz\n\n"
== dist_info.joinpath("entry_points.txt").read_text()
)
metadata = """\
Metadata-Version: 2.1
Name: simple-project
Version: 1.2.3
Summary: Some description.
Home-page: https://python-poetry.org
License: MIT
Keywords: packaging,dependency,poetry
Author: Sébastien Eustace
Author-email: sebastien@eustace.io
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Project-URL: Documentation, https://python-poetry.org/docs
Project-URL: Repository, https://github.com/python-poetry/poetry
Description-Content-Type: text/x-rst
My Package
==========
"""
assert metadata == dist_info.joinpath("METADATA").read_text(encoding="utf-8")
records = dist_info.joinpath("RECORD").read_text()
pth_file = "simple_project.pth"
assert tmp_venv.site_packages.exists(pth_file)
assert str(tmp_venv.site_packages.find(pth_file)[0]) in records
assert str(tmp_venv._bin_dir.joinpath("foo")) in records
assert str(tmp_venv._bin_dir.joinpath("baz")) in records
assert str(dist_info.joinpath("METADATA")) in records
assert str(dist_info.joinpath("INSTALLER")) in records
assert str(dist_info.joinpath("entry_points.txt")) in records
assert str(dist_info.joinpath("RECORD")) in records
baz_script = """\
#!{python}
import sys
from bar import baz
if __name__ == '__main__':
sys.exit(baz.boom.bim())
""".format(
python=tmp_venv.python
)
assert baz_script == tmp_venv._bin_dir.joinpath("baz").read_text()
foo_script = """\
#!{python}
import sys
from foo import bar
if __name__ == '__main__':
sys.exit(bar())
""".format(
python=tmp_venv.python
)
assert foo_script == tmp_venv._bin_dir.joinpath("foo").read_text()
fox_script = """\
#!{python}
import sys
from fuz.foo import bar
if __name__ == '__main__':
sys.exit(bar.baz())
""".format(
python=tmp_venv.python
)
assert fox_script == tmp_venv._bin_dir.joinpath("fox").read_text()
def test_builder_falls_back_on_setup_and_pip_for_packages_with_build_scripts(
mocker, extended_poetry, tmp_dir
):
pip_editable_install = mocker.patch(
"poetry.masonry.builders.editable.pip_editable_install"
)
env = MockEnv(path=Path(tmp_dir) / "foo")
builder = EditableBuilder(extended_poetry, env, NullIO())
builder.build()
pip_editable_install.assert_called_once_with(
extended_poetry.pyproject.file.path.parent, env
)
assert [] == env.executed
def test_builder_installs_proper_files_when_packages_configured(
project_with_include, tmp_venv
):
builder = EditableBuilder(project_with_include, tmp_venv, NullIO())
builder.build()
pth_file = "with_include.pth"
assert tmp_venv.site_packages.exists(pth_file)
pth_file = tmp_venv.site_packages.find(pth_file)[0]
paths = set()
with pth_file.open() as f:
for line in f.readlines():
line = line.strip(os.linesep)
if line:
paths.add(line)
project_root = project_with_include.file.parent.resolve()
expected = {project_root.as_posix(), project_root.joinpath("src").as_posix()}
assert paths.issubset(expected)
assert len(paths) == len(expected)
def test_builder_should_execute_build_scripts(extended_without_setup_poetry, tmp_dir):
env = MockEnv(path=Path(tmp_dir) / "foo")
builder = EditableBuilder(extended_without_setup_poetry, env, NullIO())
builder.build()
assert [
["python", str(extended_without_setup_poetry.file.parent / "build.py")]
] == env.executed