FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pymanager/tests/test_install_command.py at main · python/pymanager · GitHub
python
pymanager
Repository navigation
Code
Issues
23
(23)
Pull requests
7
(7)
Actions
Security and quality
1
(1)
Insights
Expand file tree
Breadcrumbs
pymanager
/
tests
/
test_install_command.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
749 lines (607 loc) · 24.3 KB
Breadcrumbs
pymanager
/
tests
/
test_install_command.py
Copy path
File metadata and controls
749 lines (607 loc) · 24.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
import
json
import
os
import
pytest
from
pathlib
import
Path
,
PurePath
from
manage
import
install_command
as
IC
from
manage
import
installs
from
manage
.
commands
import
BaseCommand
from
manage
.
exceptions
import
NoInstallFoundError
from
manage
.
logging
import
LOGGER
@
pytest
.
mark
.
parametrize
(
"suffix"
, [
".zip"
,
".nupkg"
])
@
pytest
.
mark
.
parametrize
(
"repair"
, [
False
,
True
])
def
test_extract_package_streaming
(
tmp_path
,
monkeypatch
,
suffix
,
repair
):
"""Extract in bounded reads while preserving overwrite and repair behavior."""
import
zipfile
package
=
tmp_path
/
(
"package"
+
suffix
)
prefix
=
tmp_path
/
"install"
prefix
.
mkdir
()
existing
=
prefix
/
"existing.txt"
existing
.
write_bytes
(
b"original"
)
chunk_size
=
10
*
1024
*
1024
data
=
bytes
(
range
(
256
))
*
(
chunk_size
//
256
+
1
)
archive_prefix
=
"tools/"
if
suffix
==
".nupkg"
else
""
with
zipfile
.
ZipFile
(
package
,
"w"
,
zipfile
.
ZIP_DEFLATED
)
as
zf
:
zf
.
writestr
(
archive_prefix
+
"nested/data.bin"
,
data
)
zf
.
writestr
(
archive_prefix
+
"empty.txt"
,
b""
)
zf
.
writestr
(
archive_prefix
+
"existing.txt"
,
b"replacement"
)
if
suffix
==
".nupkg"
:
zf
.
writestr
(
"metadata.txt"
,
b"ignored"
)
reads
=
[]
original_read
=
zipfile
.
ZipExtFile
.
read
def
bounded_read
(
self
,
n
=
-
1
):
assert
n
==
chunk_size
result
=
original_read
(
self
,
n
)
if
self
.
name
==
archive_prefix
+
"nested/data.bin"
:
reads
.
append
(
len
(
result
))
return
result
monkeypatch
.
setattr
(
zipfile
.
ZipExtFile
,
"read"
,
bounded_read
)
progress
=
[]
IC
.
extract_package
(
package
,
prefix
,
calculate_dest
=
Path
,
on_progress
=
progress
.
append
,
repair
=
repair
)
assert
(
prefix
/
"nested/data.bin"
).
read_bytes
()
==
data
assert
(
prefix
/
"empty.txt"
).
read_bytes
()
==
b""
assert
existing
.
read_bytes
()
==
(
b"replacement"
if
repair
else
b"original"
)
assert
not
(
prefix
/
"metadata.txt"
).
exists
()
assert
reads
==
[
chunk_size
,
256
,
0
]
assert
progress
[
0
]
==
0
assert
100
in
progress
assert
(
None
in
progress
)
==
(
not
repair
)
def
test_print_cli_shortcuts
(
patched_installs
,
assert_log
,
monkeypatch
,
tmp_path
):
class
Cmd
:
scratch
=
{}
global_dir
=
Path
(
tmp_path
)
default_platform
=
"-64"
def
get_installs
(
self
):
return
installs
.
get_installs
(
None
)
(
tmp_path
/
"fake.exe"
).
write_bytes
(
b""
)
monkeypatch
.
setitem
(
os
.
environ
,
"PATH"
,
f"
{
os
.
environ
[
'PATH'
]
}
;
{
Cmd
.
global_dir
}
"
)
IC
.
print_cli_shortcuts
(
Cmd
())
assert_log
(
assert_log
.
skip_until
(
"Installed %s"
, [
"Python 2.0-64"
,
PurePath
(
"C:
\\
2.0-64"
)]),
assert_log
.
skip_until
(
"%s will be launched by %s"
, [
"Python 1.0-64"
,
"py1.0[-64].exe"
]),
(
"%s will be launched by %s"
, [
"Python 1.0-32"
,
"py1.0-32.exe"
]),
)
def
test_print_path_warning
(
patched_installs
,
assert_log
,
tmp_path
):
class
Cmd
:
scratch
=
{}
global_dir
=
Path
(
tmp_path
)
def
get_installs
(
self
):
return
installs
.
get_installs
(
None
)
(
tmp_path
/
"fake.exe"
).
write_bytes
(
b""
)
IC
.
print_cli_shortcuts
(
Cmd
())
assert_log
(
assert_log
.
skip_until
(
".*Global shortcuts directory is not on PATH"
)
)
def
test_merge_existing_index
(
tmp_path
):
# This function is for multiple downloaded index.jsons, so it merges based
# on the url property, which should usually be a local file.
existing
=
tmp_path
/
"index.json"
with
open
(
existing
,
"w"
,
encoding
=
"utf-8"
)
as
f
:
json
.
dump
({
"versions"
: [
{
"id"
:
"test-1"
,
"url"
:
"test-file-1.zip"
},
{
"id"
:
"test-2"
,
"url"
:
"test-file-2.zip"
},
{
"id"
:
"test-3"
,
"url"
:
"test-file-3.zip"
},
]},
f
)
new
=
[
# Ensure new versions appear first
{
"id"
:
"test-4"
,
"url"
:
"test-file-4.zip"
},
# Ensure matching ID doesn't result in overwrite
{
"id"
:
"test-1"
,
"url"
:
"test-file-1b.zip"
},
# Ensure matching URL excludes original entry
{
"id"
:
"test-2b"
,
"url"
:
"test-file-2.zip"
},
]
IC
.
_merge_existing_index
(
new
,
existing
)
assert
new
==
[
{
"id"
:
"test-4"
,
"url"
:
"test-file-4.zip"
},
{
"id"
:
"test-1"
,
"url"
:
"test-file-1b.zip"
},
{
"id"
:
"test-2b"
,
"url"
:
"test-file-2.zip"
},
{
"id"
:
"test-1"
,
"url"
:
"test-file-1.zip"
},
{
"id"
:
"test-3"
,
"url"
:
"test-file-3.zip"
},
]
def
test_download_package_yes_aborts_background_download
(
tmp_path
):
class
Cmd
:
force
=
False
bundled_dir
=
None
source
=
"https://example.com/index.json"
confirm
=
False
_ask
=
BaseCommand
.
_ask
ask_yn
=
BaseCommand
.
ask_yn
def
urlretrieve
(
url
,
dest
,
**
kwargs
):
assert
url
==
"https://example.com/download.zip"
assert
kwargs
[
"on_cancel"
]()
is
True
dest
.
write_bytes
(
b"download"
)
dest
=
tmp_path
/
"download.zip"
result
=
IC
.
download_package
(
Cmd
(),
{
"url"
:
"https://example.com/download.zip"
},
dest
,
{},
urlretrieve
=
urlretrieve
,
)
assert
result
==
dest
assert
dest
.
read_bytes
()
==
b"download"
def
test_merge_existing_index_not_found
(
tmp_path
):
existing
=
tmp_path
/
"index.json"
try
:
existing
.
unlink
()
except
FileNotFoundError
:
pass
# Expect no failure and no change
new
=
[
1
,
2
,
3
]
IC
.
_merge_existing_index
(
new
,
existing
)
assert
new
==
[
1
,
2
,
3
]
def
test_merge_existing_index_not_valid
(
tmp_path
):
existing
=
tmp_path
/
"index.json"
with
open
(
existing
,
"w"
,
encoding
=
"utf-8"
)
as
f
:
print
(
"It's not a list of installs"
,
file
=
f
)
print
(
"But more importantly,"
,
file
=
f
)
print
(
"it's not valid JSON!"
,
file
=
f
)
# Expect no failure and no change
new
=
[
1
,
2
,
3
]
IC
.
_merge_existing_index
(
new
,
existing
)
assert
new
==
[
1
,
2
,
3
]
def
test_preserve_site
(
tmp_path
):
root
=
tmp_path
/
"root"
preserved
=
tmp_path
/
"_root"
site
=
root
/
"site-packages"
not_site
=
root
/
"site-not-packages"
not_site
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
A
=
site
/
"A"
B
=
site
/
"B.txt"
C
=
site
/
"C.txt"
A
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
B
.
write_bytes
(
b""
)
C
.
write_bytes
(
b"original"
)
class
Cmd
:
preserve_site_on_upgrade
=
False
force
=
False
repair
=
False
install
=
{
"shortcuts"
: [
{
"kind"
:
"site-dirs"
,
"dirs"
: [
"site-packages"
]},
],
}
state
=
IC
.
_preserve_site
(
Cmd
,
root
,
install
)
assert
not
state
assert
not
preserved
.
exists
()
Cmd
.
preserve_site_on_upgrade
=
True
Cmd
.
force
=
True
state
=
IC
.
_preserve_site
(
Cmd
,
root
,
install
)
assert
not
state
assert
not
preserved
.
exists
()
Cmd
.
force
=
False
Cmd
.
repair
=
True
state
=
IC
.
_preserve_site
(
Cmd
,
root
,
install
)
assert
not
state
assert
not
preserved
.
exists
()
Cmd
.
repair
=
False
state
=
IC
.
_preserve_site
(
Cmd
,
root
,
install
)
assert
state
==
[(
site
,
preserved
/
"0"
), (
None
,
preserved
)]
assert
preserved
.
is_dir
()
root
.
rename
(
root
.
parent
/
"ex_root_1"
)
IC
.
_restore_site
(
Cmd
,
state
)
assert
root
.
is_dir
()
assert
A
.
is_dir
()
assert
B
.
is_file
()
assert
C
.
is_file
()
assert
b"original"
==
C
.
read_bytes
()
assert
not
preserved
.
exists
()
state
=
IC
.
_preserve_site
(
Cmd
,
root
,
install
)
assert
state
==
[(
site
,
preserved
/
"0"
), (
None
,
preserved
)]
assert
not
C
.
exists
()
C
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
C
.
write_bytes
(
b"updated"
)
IC
.
_restore_site
(
Cmd
,
state
)
assert
A
.
is_dir
()
assert
B
.
is_file
()
assert
C
.
is_file
()
assert
b"updated"
==
C
.
read_bytes
()
assert
not
preserved
.
exists
()
@
pytest
.
mark
.
parametrize
(
"default"
, [
1
,
0
])
def
test_write_alias_default
(
monkeypatch
,
tmp_path
,
default
):
prefix
=
Path
(
tmp_path
)
/
"runtime"
class
Cmd
:
global_dir
=
Path
(
tmp_path
)
/
"bin"
launcher_exe
=
None
scratch
=
{}
enable_shortcut_kinds
=
disable_shortcut_kinds
=
None
enable_entrypoints
=
True
def
get_installs
(
self
):
return
[
{
"id"
:
"test"
,
"alias"
: [
{
"name"
:
"python3.exe"
,
"target"
:
"p.exe"
},
{
"name"
:
"pythonw3.exe"
,
"target"
:
"pw.exe"
,
"windowed"
:
1
},
],
"default"
:
default
,
"prefix"
:
prefix
,
}
]
prefix
.
mkdir
(
exist_ok
=
True
,
parents
=
True
)
(
prefix
/
"p.exe"
).
write_bytes
(
b""
)
(
prefix
/
"pw.exe"
).
write_bytes
(
b""
)
created
=
[]
class
AliasUtils
:
import
manage
.
aliasutils
as
AU
calculate_aliases
=
staticmethod
(
AU
.
calculate_aliases
)
@
staticmethod
def
create_aliases
(
cmd
,
aliases
,
*
,
allow_link
=
True
):
created
.
extend
(
aliases
)
@
staticmethod
def
cleanup_aliases
(
cmd
,
preserve
):
pass
IC
.
update_all_shortcuts
(
Cmd
(),
_aliasutils
=
AliasUtils
)
if
default
:
# Main test: python.exe and pythonw.exe are added in automatically
assert
sorted
(
a
.
name
for
a
in
created
)
==
[
"python"
,
"python3.exe"
,
"pythonw"
,
"pythonw3.exe"
]
else
:
assert
sorted
(
a
.
name
for
a
in
created
)
==
[
"python3.exe"
,
"pythonw3.exe"
]
# Ensure we still only have the two targets
assert
set
(
a
.
target
for
a
in
created
)
==
{
"p.exe"
,
"pw.exe"
}
def
test_update_all_shortcuts_default_first
(
tmp_path
):
# Regression test for https://github.com/python/pymanager/issues/413
# When the default install is not first in precedence order (e.g. via
# PYTHON_MANAGER_DEFAULT), its entrypoints must still win over the
# first install's, so pip and python resolve to the same runtime.
prefix_new
=
Path
(
tmp_path
)
/
"new"
prefix_default
=
Path
(
tmp_path
)
/
"default"
for
prefix
in
(
prefix_new
,
prefix_default
):
prefix
.
mkdir
(
exist_ok
=
True
,
parents
=
True
)
(
prefix
/
"python.exe"
).
write_bytes
(
b""
)
(
prefix
/
"shared-target.exe"
).
write_bytes
(
b""
)
class
Cmd
:
global_dir
=
Path
(
tmp_path
)
/
"bin"
launcher_exe
=
None
scratch
=
{}
enable_shortcut_kinds
=
disable_shortcut_kinds
=
None
enable_entrypoints
=
False
def
get_installs
(
self
):
return
[
{
"id"
:
"new"
,
"alias"
: [
{
"name"
:
"python3.14.exe"
,
"target"
:
"python.exe"
},
{
"name"
:
"shared.exe"
,
"target"
:
"shared-target.exe"
},
],
"prefix"
:
prefix_new
,
},
{
"id"
:
"default"
,
"alias"
: [
{
"name"
:
"python3.13.exe"
,
"target"
:
"python.exe"
},
{
"name"
:
"shared.exe"
,
"target"
:
"shared-target.exe"
},
],
"default"
:
True
,
"prefix"
:
prefix_default
,
},
]
created
=
[]
class
AliasUtils
:
import
manage
.
aliasutils
as
AU
calculate_aliases
=
staticmethod
(
AU
.
calculate_aliases
)
@
staticmethod
def
create_aliases
(
cmd
,
aliases
,
*
,
allow_link
=
True
):
created
.
extend
(
aliases
)
@
staticmethod
def
cleanup_aliases
(
cmd
,
preserve
):
pass
IC
.
update_all_shortcuts
(
Cmd
(),
_aliasutils
=
AliasUtils
)
shared
=
[
a
for
a
in
created
if
a
.
name
.
casefold
()
==
"shared.exe"
]
assert
len
(
shared
)
==
2
# Default install must come first so first-match-wins picks it
assert
shared
[
0
].
install
[
"id"
]
==
"default"
assert
shared
[
1
].
install
[
"id"
]
==
"new"
python_aliases
=
[
a
for
a
in
created
if
a
.
name
in
(
"python"
,
"pythonw"
)]
assert
python_aliases
assert
all
(
a
.
install
[
"id"
]
==
"default"
for
a
in
python_aliases
)
class
InstallCommandTestCmd
:
def
__init__
(
self
,
tmp_path
,
*
args
,
**
kwargs
):
self
.
args
=
args
self
.
tags
=
None
self
.
download_cache
=
{}
self
.
scratch
=
{
"install_command.download_cache"
:
self
.
download_cache
,
}
self
.
automatic
=
kwargs
.
pop
(
"automatic"
,
False
)
self
.
by_id
=
kwargs
.
pop
(
"by_id"
,
False
)
self
.
default_install_tag
=
kwargs
.
pop
(
"default_install_tag"
,
"1"
)
self
.
default_platform
=
kwargs
.
pop
(
"default_platform"
,
"-32"
)
self
.
default_tag
=
kwargs
.
pop
(
"default_tag"
,
"1"
)
self
.
download
=
kwargs
.
pop
(
"download"
,
None
)
if
self
.
download
:
self
.
download
=
tmp_path
/
self
.
download
self
.
download_dir
=
tmp_path
/
kwargs
.
pop
(
"download_dir"
,
"_cache"
)
self
.
dry_run
=
kwargs
.
pop
(
"dry_run"
,
True
)
self
.
fallback_source
=
kwargs
.
pop
(
"fallback_source"
,
None
)
self
.
force
=
kwargs
.
pop
(
"force"
,
True
)
self
.
from_script
=
kwargs
.
pop
(
"from_script"
,
None
)
self
.
log_file
=
kwargs
.
pop
(
"log_file"
,
None
)
self
.
refresh
=
kwargs
.
pop
(
"refresh"
,
False
)
self
.
repair
=
kwargs
.
pop
(
"repair"
,
False
)
self
.
shebang_can_run_anything
=
kwargs
.
pop
(
"shebang_can_run_anything"
,
False
)
self
.
shebang_can_run_anything_silently
=
kwargs
.
pop
(
"shebang_can_run_anything_silently"
,
False
)
self
.
shebang_templates
=
{}
self
.
source
=
kwargs
.
pop
(
"source"
,
"http://example.com/index.json"
)
self
.
target
=
kwargs
.
pop
(
"target"
,
None
)
if
self
.
target
:
self
.
target
=
tmp_path
/
self
.
target
self
.
update
=
kwargs
.
pop
(
"update"
,
False
)
self
.
virtual_env
=
kwargs
.
pop
(
"virtual_env"
,
None
)
self
.
index_installs
=
[
*
kwargs
.
pop
(
"index_installs"
, ()),
{
"schema"
:
1
,
"id"
:
"test-32"
,
"sort-version"
:
"1.1"
,
"company"
:
"Test"
,
"tag"
:
"1.1-32"
,
"install-for"
: [
"1-32"
,
"1.1-32"
,
"1.1-32"
],
"display-name"
:
"Test 1.1 (32)"
,
"executable"
:
"test.exe"
,
"url"
:
"about:blank"
,
},
{
"schema"
:
1
,
"id"
:
"test-32"
,
"sort-version"
:
"1.0"
,
"company"
:
"Test"
,
"tag"
:
"1.0-32"
,
"install-for"
: [
"1-32"
,
"1.0-32"
,
"1.0-32"
],
"display-name"
:
"Test 1.0 (32)"
,
"executable"
:
"test.exe"
,
"url"
:
"about:blank"
,
},
]
self
.
download_cache
[
"http://example.com/index.json"
]
=
json
.
dumps
({
"versions"
:
self
.
index_installs
,
})
self
.
installs
=
[{
**
self
.
index_installs
[
-
1
],
"source"
:
self
.
source
,
"prefix"
:
tmp_path
/
"test-32"
,
}]
assert
not
kwargs
def
ask_yn
(
self
,
prompt
,
*
args
):
LOGGER
.
info
(
prompt
,
*
args
)
return
True
def
get_log_file
(
self
):
return
self
.
log_file
def
get_installs
(
self
):
return
self
.
installs
def
get_install_to_run
(
self
,
tag
=
None
,
script
=
None
,
*
,
windowed
=
False
):
for
i
in
self
.
installs
:
if
i
[
"tag"
]
==
tag
or
f"
{
i
[
'company'
]
}
/
{
i
[
'tag'
]
}
"
==
tag
:
return
i
raise
NoInstallFoundError
(
tag
)
def
test_install_simple
(
tmp_path
,
assert_log
):
cmd
=
InstallCommandTestCmd
(
tmp_path
,
"1.1"
,
force
=
False
)
IC
.
execute
(
cmd
)
assert_log
(
assert_log
.
skip_until
(
"Searching for Python matching %s"
, [
"1.1"
]),
assert_log
.
skip_until
(
".*Your existing %s install.*"
, [
"Test 1.0 (32)"
,
"Test 1.1 (32)"
]),
assert_log
.
skip_until
(
"Installing %s"
, [
"Test 1.1 (32)"
]),
(
"Tag: %s
\\
\\
%s"
, [
"Test"
,
"1.1-32"
]),
)
def
test_install_already_installed
(
tmp_path
,
assert_log
):
cmd
=
InstallCommandTestCmd
(
tmp_path
,
"1.0"
,
force
=
False
)
IC
.
execute
(
cmd
)
assert_log
(
assert_log
.
skip_until
(
"Searching for Python matching %s"
, [
"1.0"
]),
assert_log
.
skip_until
(
"%s is already installed"
, [
"Test 1.0 (32)"
]),
)
def
test_install_update
(
tmp_path
,
assert_log
):
cmd
=
InstallCommandTestCmd
(
tmp_path
,
update
=
True
,
force
=
False
,
index_installs
=
[
{
"schema"
:
1
,
"id"
:
"test-32"
,
"sort-version"
:
"1.2rc1"
,
"display-name"
:
"Test 1.2rc1 (32)"
,
"company"
:
"Test"
,
"tag"
:
"1.2rc-32"
,
"install-for"
: [
"1-32"
,
"1.2-32"
,
"1.2rc1-32"
]}
],
)
IC
.
execute
(
cmd
)
assert_log
(
assert_log
.
skip_until
(
"Searching for Python with ID %s"
, [
"test-32"
]),
assert_log
.
skip_until
(
"Updating to %s"
, [
"Test 1.1 (32)"
]),
)
def
test_install_update_explicit
(
tmp_path
,
assert_log
):
cmd
=
InstallCommandTestCmd
(
tmp_path
,
"1"
,
update
=
True
,
force
=
False
,
index_installs
=
[
{
"schema"
:
1
,
"id"
:
"test-32"
,
"sort-version"
:
"1.2rc1"
,
"display-name"
:
"Test 1.2rc1 (32)"
,
"company"
:
"Test"
,
"tag"
:
"1.2rc-32"
,
"install-for"
: [
"1-32"
,
"1.2-32"
,
"1.2rc1-32"
]}
],
)
IC
.
execute
(
cmd
)
assert_log
(
assert_log
.
skip_until
(
"Searching for Python matching %s"
, [
"1"
]),
assert_log
.
skip_until
(
"Updating to %s"
, [
"Test 1.1 (32)"
]),
)
def
test_install_update_explicit_pre
(
tmp_path
,
assert_log
):
cmd
=
InstallCommandTestCmd
(
tmp_path
,
"1rc"
,
update
=
True
,
force
=
False
,
index_installs
=
[
{
"schema"
:
1
,
"id"
:
"test-32"
,
"sort-version"
:
"1.2rc1"
,
"display-name"
:
"Test 1.2rc1 (32)"
,
"company"
:
"Test"
,
"tag"
:
"1.2rc-32"
,
"install-for"
: [
"1-32"
,
"1.2-32"
,
"1.2rc1-32"
]}
],
)
IC
.
execute
(
cmd
)
assert_log
(
assert_log
.
skip_until
(
"Searching for Python matching %s"
, [
"1rc"
]),
assert_log
.
skip_until
(
"Updating to %s"
, [
"Test 1.2rc1 (32)"
]),
)
def
test_install_from_script
(
tmp_path
,
assert_log
):
cmd
=
InstallCommandTestCmd
(
tmp_path
,
from_script
=
tmp_path
/
"t.py"
)
cmd
.
from_script
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
cmd
.
from_script
.
write_text
(
"#! python1.1.exe"
)
IC
.
execute
(
cmd
)
assert_log
(
assert_log
.
skip_until
(
"Searching for Python matching"
),
assert_log
.
skip_until
(
"Installing %s"
, [
"Test 1.1 (32)"
]),
(
"Tag: %s
\\
\\
%s"
, [
"Test"
,
"1.1-32"
]),
)
def
test_failed_install_unwind
(
tmp_path
,
monkeypatch
,
assert_log
):
cmd
=
InstallCommandTestCmd
(
tmp_path
,
"1.0"
,
force
=
False
)
cmd
.
dry_run
=
False
cmd
.
preserve_site_on_upgrade
=
True
inst
=
cmd
.
installs
[
0
]
inst
.
setdefault
(
"shortcuts"
, []).
append
({
"kind"
:
"site-dirs"
,
"dirs"
: [
"test-site"
],
})
target
=
tmp_path
/
"target"
test_file
=
target
/
"test-site/file.txt"
test_file
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
test_file
.
write_text
(
"Before"
)
def
remove_existing
(
*
args
):
pass
def
download_one
(
*
args
,
**
kwargs
):
return
"<package>"
def
extract_package
(
package
,
dest
,
*
args
,
**
kwargs
):
# site dir should be gone
assert
not
test_file
.
is_file
()
# create the target directory
dest
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
# interrupt the install process
raise
RuntimeError
(
"Failed to extract for test reasons"
)
monkeypatch
.
setattr
(
IC
,
"_remove_existing"
,
remove_existing
)
monkeypatch
.
setattr
(
IC
,
"_download_one"
,
download_one
)
monkeypatch
.
setattr
(
IC
,
"extract_package"
,
extract_package
)
with
pytest
.
raises
(
RuntimeError
):
IC
.
_install_one
(
cmd
,
"<source>"
,
inst
,
target
=
target
)
# site dir should be back
assert
test_file
.
is_file
()
assert
test_file
.
read_text
()
==
"Before"
def
test_failed_install_unwind_dont_clobber
(
tmp_path
,
monkeypatch
,
assert_log
):
cmd
=
InstallCommandTestCmd
(
tmp_path
,
"1.0"
,
force
=
False
)
cmd
.
dry_run
=
False
cmd
.
preserve_site_on_upgrade
=
True
inst
=
cmd
.
installs
[
0
]
inst
.
setdefault
(
"shortcuts"
, []).
append
({
"kind"
:
"site-dirs"
,
"dirs"
: [
"test-site"
],
})
target
=
tmp_path
/
"test-site"
test_file
=
target
/
"file.txt"
test_file
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
test_file
.
write_text
(
"Before"
)
def
download_one
(
*
args
,
**
kwargs
):
return
"<package>"
def
extract_package
(
package
,
dest
,
*
args
,
**
kwargs
):
# site dir should be gone
assert
not
test_file
.
is_file
()
# create a new one - it should be preserved
test_file
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
test_file
.
write_text
(
"After"
)
# interrupt the install process
raise
RuntimeError
(
"Failed to extract for test reasons"
)
monkeypatch
.
setattr
(
IC
,
"_download_one"
,
download_one
)
monkeypatch
.
setattr
(
IC
,
"extract_package"
,
extract_package
)
with
pytest
.
raises
(
RuntimeError
):
IC
.
_install_one
(
cmd
,
"<source>"
,
inst
,
target
=
target
)
# Ensure file we extracted is still there
assert
test_file
.
read_text
()
==
"After"
def
test_finalize_metadata_urls
():
class
Cmd
:
enable_shortcut_kinds
=
[]
disable_shortcut_kinds
=
[]
fallback_source
=
None
i
=
{
"url"
:
"http://user:placeholder@example.com/package.zip"
,
"source"
:
"http://user:placeholder@example.com/index.json"
,
}
IC
.
_finalize_metadata
(
Cmd
,
i
)
assert
i
[
"url"
]
==
"http://example.com/package.zip"
assert
i
[
"source"
]
==
"http://example.com/index.json"
def
test_finalize_metadata_fallback_urls
():
class
Cmd
:
enable_shortcut_kinds
=
[]
disable_shortcut_kinds
=
[]
fallback_source
=
"http://user:placeholder@example.com/index.json"
i
=
{
"url"
:
"http://user:placeholder@example.com/package.zip"
,
"source"
:
"http://user:placeholder@example.com/index.json"
,
}
IC
.
_finalize_metadata
(
Cmd
,
i
)
assert
i
[
"url"
]
==
"http://example.com/package.zip"
assert
i
[
"source"
]
==
"http://user:placeholder@example.com/index.json"
def
test_finalize_metadata_shortcuts
():
class
Cmd
:
enable_shortcut_kinds
=
[]
disable_shortcut_kinds
=
[]
fallback_source
=
None
i
=
{
"url"
:
""
,
"shortcuts"
: [
dict
(
kind
=
a
)
for
a
in
"abc"
],
}
IC
.
_finalize_metadata
(
Cmd
,
i
)
assert
[
j
[
"kind"
]
for
j
in
i
[
"shortcuts"
]]
==
[
"a"
,
"b"
,
"c"
]
assert
[
j
[
"kind"
]
for
j
in
i
[
"__original-shortcuts"
]]
==
[
"a"
,
"b"
,
"c"
]
def
test_finalize_metadata_shortcuts_disable
():
class
Cmd
:
enable_shortcut_kinds
=
[]
disable_shortcut_kinds
=
[
"b"
]
fallback_source
=
None
i
=
{
"url"
:
""
,
"shortcuts"
: [
dict
(
kind
=
a
)
for
a
in
"abc"
],
}
IC
.
_finalize_metadata
(
Cmd
,
i
)
assert
[
j
[
"kind"
]
for
j
in
i
[
"shortcuts"
]]
==
[
"a"
,
"c"
]
assert
[
j
[
"kind"
]
for
j
in
i
[
"__original-shortcuts"
]]
==
[
"a"
,
"b"
,
"c"
]
def
test_finalize_metadata_shortcuts_enable
():
class
Cmd
:
enable_shortcut_kinds
=
[
"b"
]
disable_shortcut_kinds
=
[]
fallback_source
=
None
i
=
{
"url"
:
""
,
"shortcuts"
: [
dict
(
kind
=
a
)
for
a
in
"abc"
],
}
IC
.
_finalize_metadata
(
Cmd
,
i
)
assert
[
j
[
"kind"
]
for
j
in
i
[
"shortcuts"
]]
==
[
"b"
]
assert
[
j
[
"kind"
]
for
j
in
i
[
"__original-shortcuts"
]]
==
[
"a"
,
"b"
,
"c"
]
def
test_finalize_metadata_merge_from
(
tmp_path
):
class
Cmd
:
enable_shortcut_kinds
=
[]
disable_shortcut_kinds
=
[]
fallback_source
=
None
merge_from
=
tmp_path
/
"file.json"
test_url_1
=
"https://example.com/"
test_url_2
=
"https://example.com/path2"
# merge_from does not exist, but we shouldn't fail
i
=
{
"url"
:
test_url_1
}
IC
.
_finalize_metadata
(
Cmd
,
i
,
merge_from
)
assert
i
[
"url"
]
==
test_url_1
# Update missing fields from merge_from, but don't touch existing ones
with
open
(
merge_from
,
"w"
,
encoding
=
"utf-8"
)
as
f
:
json
.
dump
({
"url"
:
test_url_1
,
"data1"
:
"b"
,
"data2"
:
"c"
},
f
)
i
=
{
"url"
:
test_url_2
,
"data1"
:
"a"
}
IC
.
_finalize_metadata
(
Cmd
,
i
,
merge_from
)
assert
i
[
"url"
]
==
test_url_2
assert
i
[
"data1"
]
==
"a"
assert
i
[
"data2"
]
==
"c"
def
test_user_provided_source_retained
(
tmp_path
,
assert_log
,
monkeypatch
):
cmd
=
InstallCommandTestCmd
(
tmp_path
,
"1.1"
,
force
=
False
)
source1
=
cmd
.
source
source2
=
"http://example.com/index-2.json"
cmd
.
source
=
source2
cmd
.
download_cache
[
source2
]
=
json
.
dumps
({
"versions"
: [],
"next"
:
source1
,
})
def
find_one
(
*
args
,
_orig
=
IC
.
_find_one
,
**
kwargs
):
i
=
_orig
(
*
args
,
**
kwargs
)
assert
i
[
"source"
]
==
cmd
.
source
return
i
monkeypatch
.
setattr
(
IC
,
"_find_one"
,
find_one
)
IC
.
execute
(
cmd
)
assert_log
(
assert_log
.
skip_until
(
"Searching for Python matching %s"
, [
"1.1"
]),
assert_log
.
skip_until
(
"Fetching: %s"
, [
source2
]),
assert_log
.
skip_until
(
"No install found.+"
),
assert_log
.
skip_until
(
"Fetching: %s"
, [
source1
]),
assert_log
.
skip_until
(
"Storing %s as source of package"
, [
source2
]),
assert_log
.
skip_until
(
"Installing %s"
, [
"Test 1.1 (32)"
]),
(
"Tag: %s
\\
\\
%s"
, [
"Test"
,
"1.1-32"
]),
)
Back
|
FazBrowse Home
|
New Git URL