-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_playwrightprovider.py
More file actions
413 lines (366 loc) · 16 KB
/
test_playwrightprovider.py
File metadata and controls
413 lines (366 loc) · 16 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import os
import shutil
import tempfile
from pathlib import Path
import pytest
from abxpkg import Binary, PlaywrightProvider
@pytest.fixture(scope="module")
def seeded_playwright_root():
with tempfile.TemporaryDirectory() as temp_dir:
install_root = Path(temp_dir) / "seeded-playwright-root"
provider = PlaywrightProvider(install_root=install_root)
installed = provider.install("chromium", no_cache=True)
assert installed is not None
assert installed.loaded_abspath is not None
assert installed.loaded_abspath.exists()
yield install_root
class TestPlaywrightProvider:
@staticmethod
def copy_seeded_playwright_root(
seeded_playwright_root: Path,
install_root: Path,
) -> None:
shutil.copytree(
seeded_playwright_root,
install_root,
symlinks=True,
copy_function=os.link,
)
copied_bin_dir = install_root / "bin"
if not copied_bin_dir.is_dir():
return
for link_path in copied_bin_dir.iterdir():
if not link_path.is_symlink():
continue
link_target = link_path.resolve(strict=False)
if seeded_playwright_root.resolve() not in link_target.parents:
continue
relative_target = link_target.relative_to(seeded_playwright_root.resolve())
link_path.unlink()
link_path.symlink_to(install_root / relative_target)
def test_chromium_install_puts_real_browser_into_managed_bin_dir(
self,
test_machine,
seeded_playwright_root,
):
test_machine.require_tool("node")
test_machine.require_tool("npm")
with tempfile.TemporaryDirectory() as temp_dir:
playwright_root = Path(temp_dir) / "playwright-root"
self.copy_seeded_playwright_root(seeded_playwright_root, playwright_root)
provider = PlaywrightProvider(install_root=playwright_root)
installed = provider.load("chromium", no_cache=True)
assert installed is not None
test_machine.assert_shallow_binary_loaded(
installed,
assert_version_command=False,
)
assert installed.name == "chromium"
assert installed.loaded_abspath is not None
assert installed.loaded_abspath.exists()
assert provider.bin_dir is not None
assert installed.loaded_abspath.parent == provider.bin_dir
assert installed.loaded_abspath == provider.bin_dir / "chromium"
# The symlink resolves into playwright_root (which is also
# PLAYWRIGHT_BROWSERS_PATH for this provider).
real_target = installed.loaded_abspath.resolve()
assert playwright_root.resolve() in real_target.parents
# Playwright lays out chromium builds as chromium-<build>/.
assert any(
child.name.startswith("chromium-")
for child in playwright_root.iterdir()
if child.is_dir()
)
loaded = provider.load("chromium", no_cache=True)
test_machine.assert_shallow_binary_loaded(
loaded,
assert_version_command=False,
)
assert loaded is not None
assert loaded.loaded_abspath is not None
assert loaded.loaded_abspath.resolve() == installed.loaded_abspath.resolve()
loaded_or_installed = provider.install("chromium")
test_machine.assert_shallow_binary_loaded(
loaded_or_installed,
assert_version_command=False,
)
assert loaded_or_installed is not None
assert loaded_or_installed.loaded_abspath is not None
assert (
loaded_or_installed.loaded_abspath.resolve()
== installed.loaded_abspath.resolve()
)
def test_install_root_alias_without_explicit_bin_dir_uses_root_bin(
self,
test_machine,
seeded_playwright_root,
):
test_machine.require_tool("node")
test_machine.require_tool("npm")
with tempfile.TemporaryDirectory() as temp_dir:
install_root = Path(temp_dir) / "pw-root"
self.copy_seeded_playwright_root(seeded_playwright_root, install_root)
provider = PlaywrightProvider.model_validate(
{
"install_root": install_root,
},
)
installed = provider.install("chromium")
test_machine.assert_shallow_binary_loaded(
installed,
assert_version_command=False,
)
assert installed is not None
assert provider.install_root == install_root
assert provider.bin_dir == install_root / "bin"
assert installed.loaded_abspath is not None
assert installed.loaded_abspath.parent == provider.bin_dir
# Chromium build landed directly under install_root (which is
# the effective PLAYWRIGHT_BROWSERS_PATH for this provider).
assert any(
child.name.startswith("chromium-")
for child in install_root.iterdir()
if child.is_dir()
)
def test_install_root_and_bin_dir_aliases_install_into_the_requested_paths(
self,
test_machine,
seeded_playwright_root,
):
test_machine.require_tool("node")
test_machine.require_tool("npm")
with tempfile.TemporaryDirectory() as temp_dir:
install_root = Path(temp_dir) / "pw-root"
bin_dir = Path(temp_dir) / "custom-bin"
self.copy_seeded_playwright_root(seeded_playwright_root, install_root)
provider = PlaywrightProvider.model_validate(
{
"install_root": install_root,
"bin_dir": bin_dir,
},
)
installed = provider.install("chromium")
test_machine.assert_shallow_binary_loaded(
installed,
assert_version_command=False,
)
assert installed is not None
assert provider.install_root == install_root
assert provider.bin_dir == bin_dir
assert installed.loaded_abspath is not None
assert installed.loaded_abspath.parent == bin_dir
# Browser tree still landed in install_root, not bin_dir.
assert any(
child.name.startswith("chromium-")
for child in install_root.iterdir()
if child.is_dir()
)
def test_explicit_bin_dir_takes_precedence_over_existing_PATH_entries(
self,
test_machine,
seeded_playwright_root,
):
test_machine.require_tool("node")
test_machine.require_tool("npm")
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
ambient_root = temp_dir_path / "ambient-root"
self.copy_seeded_playwright_root(seeded_playwright_root, ambient_root)
ambient_provider = PlaywrightProvider(
install_root=ambient_root,
bin_dir=ambient_root / "bin",
)
ambient_installed = ambient_provider.install("chromium")
assert ambient_installed is not None
assert ambient_installed.loaded_abspath is not None
assert ambient_installed.loaded_abspath.parent == ambient_provider.bin_dir
install_root = temp_dir_path / "playwright-root"
self.copy_seeded_playwright_root(seeded_playwright_root, install_root)
provider = PlaywrightProvider(
PATH=str(ambient_provider.bin_dir),
install_root=install_root,
bin_dir=temp_dir_path / "custom-bin",
)
installed = provider.install("chromium")
test_machine.assert_shallow_binary_loaded(
installed,
assert_version_command=False,
)
assert installed is not None
assert provider.bin_dir == temp_dir_path / "custom-bin"
assert installed.loaded_abspath is not None
assert installed.loaded_abspath.parent == provider.bin_dir
# The two providers resolve to different on-disk symlinks.
assert installed.loaded_abspath != ambient_installed.loaded_abspath
def test_provider_install_args_are_passed_through_to_playwright_install(
self,
test_machine,
):
test_machine.require_tool("node")
test_machine.require_tool("npm")
with tempfile.TemporaryDirectory() as temp_dir:
playwright_root = Path(temp_dir) / "playwright-root"
# Use ``--no-shell`` to skip the headless shell download, which
# is a real playwright install flag we can verify by checking
# that ``chromium_headless_shell-*`` does NOT end up on disk.
provider = PlaywrightProvider(
install_root=playwright_root,
).get_provider_with_overrides(
overrides={"chromium": {"install_args": ["chromium", "--no-shell"]}},
)
installed = provider.install("chromium")
test_machine.assert_shallow_binary_loaded(
installed,
assert_version_command=False,
)
assert installed is not None
assert installed.loaded_abspath is not None
assert installed.loaded_abspath.exists()
chromium_dirs = [
child
for child in playwright_root.iterdir()
if child.is_dir()
and child.name.startswith("chromium-")
and not child.name.startswith("chromium_headless_shell")
]
assert chromium_dirs, "chromium-<build> dir should exist on disk"
# ``--no-shell`` should have skipped the headless shell download.
headless_shell_dirs = [
child
for child in playwright_root.iterdir()
if child.is_dir() and child.name.startswith("chromium_headless_shell")
]
assert not headless_shell_dirs, (
f"--no-shell should have skipped chromium_headless_shell, "
f"but found: {[p.name for p in headless_shell_dirs]}"
)
def test_provider_direct_methods_exercise_real_lifecycle(
self,
test_machine,
seeded_playwright_root,
):
test_machine.require_tool("node")
test_machine.require_tool("npm")
with tempfile.TemporaryDirectory() as temp_dir:
install_root = Path(temp_dir) / "playwright-root"
self.copy_seeded_playwright_root(seeded_playwright_root, install_root)
provider = PlaywrightProvider(install_root=install_root)
loaded = provider.load("chromium", no_cache=True)
test_machine.assert_shallow_binary_loaded(
loaded,
assert_version_command=False,
)
loaded_or_installed = provider.install("chromium")
test_machine.assert_shallow_binary_loaded(
loaded_or_installed,
assert_version_command=False,
)
updated = provider.update("chromium", no_cache=True)
test_machine.assert_shallow_binary_loaded(
updated,
assert_version_command=False,
)
assert provider.uninstall("chromium", no_cache=True) is True
test_machine.assert_provider_missing(provider, "chromium")
def test_binary_direct_methods_exercise_real_lifecycle(
self,
test_machine,
seeded_playwright_root,
):
test_machine.require_tool("node")
test_machine.require_tool("npm")
with tempfile.TemporaryDirectory() as temp_dir:
install_root = Path(temp_dir) / "playwright-root"
self.copy_seeded_playwright_root(seeded_playwright_root, install_root)
binary = Binary(
name="chromium",
binproviders=[
PlaywrightProvider(
install_root=install_root,
),
],
)
loaded = binary.load(no_cache=True)
test_machine.assert_shallow_binary_loaded(
loaded,
assert_version_command=False,
)
loaded_or_installed = test_machine.unloaded_binary(binary).install()
test_machine.assert_shallow_binary_loaded(
loaded_or_installed,
assert_version_command=False,
)
updated = loaded.update()
test_machine.assert_shallow_binary_loaded(
updated,
assert_version_command=False,
)
removed = updated.uninstall()
assert not removed.is_valid
assert removed.loaded_binprovider is None
assert removed.loaded_abspath is None
assert removed.loaded_version is None
assert removed.loaded_sha256 is None
test_machine.assert_binary_missing(binary)
def test_update_refreshes_chromium_in_place(
self,
test_machine,
seeded_playwright_root,
):
test_machine.require_tool("node")
test_machine.require_tool("npm")
with tempfile.TemporaryDirectory() as temp_dir:
playwright_root = Path(temp_dir) / "playwright-root"
self.copy_seeded_playwright_root(seeded_playwright_root, playwright_root)
provider = PlaywrightProvider(install_root=playwright_root)
installed = provider.load("chromium", no_cache=True)
test_machine.assert_shallow_binary_loaded(
installed,
assert_version_command=False,
)
assert installed is not None
assert installed.loaded_abspath is not None
original_target = installed.loaded_abspath.resolve()
assert original_target.exists()
updated = provider.update("chromium", no_cache=True)
test_machine.assert_shallow_binary_loaded(
updated,
assert_version_command=False,
)
assert updated is not None
assert updated.loaded_abspath is not None
# The symlink resolves to a chromium build that actually
# exists on disk after update (whether the build-id moved
# depends on the current playwright release, but the
# resolved target must always exist and still live inside
# ``playwright_root``).
updated_target = updated.loaded_abspath.resolve()
assert updated_target.exists()
assert playwright_root.resolve() in updated_target.parents
assert any(
child.name.startswith("chromium-")
for child in playwright_root.iterdir()
if child.is_dir()
)
def test_provider_dry_run_does_not_install_chromium(self, test_machine):
test_machine.require_tool("node")
test_machine.require_tool("npm")
with tempfile.TemporaryDirectory() as temp_dir:
playwright_root = Path(temp_dir) / "playwright-root"
provider = PlaywrightProvider(install_root=playwright_root)
test_machine.exercise_provider_dry_run(provider, bin_name="chromium")
# dry_run must not have actually downloaded any browsers.
browser_dirs = (
[
p
for p in playwright_root.iterdir()
if p.is_dir()
and p.name.startswith(("chromium-", "firefox-", "webkit-"))
]
if playwright_root.is_dir()
else []
)
assert not browser_dirs, (
f"dry_run should not have created any browser dirs, got: "
f"{[p.name for p in browser_dirs]}"
)