18
18
19
19
20
20
@atexit .register
21
- def _clean_paths ():
22
- shutil .rmtree (test_path .as_posix (), ignore_errors = True )
21
+ def _clean_paths (root = True ):
22
+ if root :
23
+ shutil .rmtree (test_path .as_posix (), ignore_errors = True )
24
+ else :
25
+ for path in test_path .iterdir ():
26
+ if path .is_file ():
27
+ path .unlink ()
28
+ else :
29
+ shutil .rmtree (path .as_posix (), ignore_errors = True )
23
30
24
31
25
32
def test_quiet_mode ():
26
33
# test -qqqq quiet mode
27
- _clean_paths ()
34
+ _clean_paths (root = False )
28
35
output = subprocess .check_output ([sys .executable , "-m" , "zipapps" , "six" , "-qqqq" ])
29
36
assert not output , output
30
37
31
38
32
39
def test_freeze ():
33
40
# test --freeze-reqs
34
- _clean_paths ()
41
+ _clean_paths (root = False )
35
42
output = subprocess .check_output (
36
43
[sys .executable , "-m" , "zipapps" , "--freeze-reqs" , "-" , "six==1.16.0" ]
37
44
)
@@ -40,7 +47,7 @@ def test_freeze():
40
47
41
48
def test_dump_load_config ():
42
49
# test `--dump-config` and `--load-config`
43
- _clean_paths ()
50
+ _clean_paths (root = False )
44
51
output , _ = subprocess .Popen (
45
52
[sys .executable , "-m" , "zipapps" , "--dump-config" , "-" ],
46
53
stderr = subprocess .PIPE ,
@@ -77,7 +84,7 @@ def test_dump_load_config():
77
84
78
85
def test_environ ():
79
86
# test os.environ
80
- _clean_paths ()
87
+ _clean_paths (root = False )
81
88
app_path = create_app (unzip = "*" , unzip_path = "app_cache" )
82
89
os .environ ["CLEAR_ZIPAPPS_CACHE" ] = "1"
83
90
os .environ ["CLEAR_ZIPAPPS_SELF" ] = "1"
@@ -112,7 +119,7 @@ def test_environ():
112
119
113
120
def test_unzip_with_cwd_pid ():
114
121
# test unzip with $CWD / $PID
115
- _clean_paths ()
122
+ _clean_paths (root = False )
116
123
app_path = create_app (
117
124
unzip = "bottle" , pip_args = ["bottle" ], unzip_path = "$CWD/app_cache/$PID"
118
125
)
@@ -129,7 +136,7 @@ def test_unzip_with_cwd_pid():
129
136
130
137
def test_clear_zipapps_self ():
131
138
# test clear_zipapps_self
132
- _clean_paths ()
139
+ _clean_paths (root = False )
133
140
assert not Path ("app.pyz" ).is_file ()
134
141
app_path = create_app (clear_zipapps_self = True )
135
142
assert Path ("app.pyz" ).is_file ()
@@ -141,13 +148,13 @@ def test_clear_zipapps_self():
141
148
142
149
def test_unzip_exclude ():
143
150
# test unzip_exclude
144
- _clean_paths ()
151
+ _clean_paths (root = False )
145
152
app_path = create_app (unzip = "*" , pip_args = ["six" ], unzip_exclude = "" )
146
153
stdout_output , stderr_output = subprocess .Popen (
147
154
[sys .executable , str (app_path ), "--activate-zipapps" ]
148
155
).communicate ()
149
156
assert Path ("./zipapps_cache/app/six.py" ).is_file ()
150
- _clean_paths ()
157
+ _clean_paths (root = False )
151
158
app_path = create_app (unzip = "*" , pip_args = ["six" ], unzip_exclude = "six" )
152
159
stdout_output , stderr_output = subprocess .Popen (
153
160
[sys .executable , str (app_path ), "--activate-zipapps" ]
@@ -157,13 +164,13 @@ def test_unzip_exclude():
157
164
158
165
def test_clear_zip_cache ():
159
166
# test -czc
160
- _clean_paths ()
167
+ _clean_paths (root = False )
161
168
app_path = create_app (clear_zipapps_cache = False , unzip = "*" )
162
169
stdout_output , stderr_output = subprocess .Popen (
163
170
[sys .executable , str (app_path ), "-V" ]
164
171
).communicate ()
165
172
assert Path ("./zipapps_cache" ).is_dir ()
166
- _clean_paths ()
173
+ _clean_paths (root = False )
167
174
app_path = create_app (clear_zipapps_cache = True , unzip = "*" )
168
175
stdout_output , stderr_output = subprocess .Popen (
169
176
[sys .executable , str (app_path ), "-V" ]
@@ -173,7 +180,7 @@ def test_clear_zip_cache():
173
180
174
181
def test_build_id_and_single_file ():
175
182
# test build_id
176
- _clean_paths ()
183
+ _clean_paths (root = False )
177
184
mock_requirement = Path ("_requirements.txt" )
178
185
mock_requirement .write_text ("bottle" )
179
186
old_file = create_app (
@@ -200,7 +207,7 @@ def test_build_id_and_single_file():
200
207
201
208
def test_main_source_code ():
202
209
# test main: source code
203
- _clean_paths ()
210
+ _clean_paths (root = False )
204
211
subprocess .check_output (
205
212
[
206
213
sys .executable ,
@@ -217,7 +224,7 @@ def test_main_source_code():
217
224
218
225
def test_main_module ():
219
226
# test main module+function
220
- _clean_paths ()
227
+ _clean_paths (root = False )
221
228
mock_main = Path ("mock_main.py" )
222
229
mock_main .touch ()
223
230
mock_main .write_text ("print(1)" )
@@ -257,7 +264,7 @@ def test_main_module():
257
264
258
265
def test_includes ():
259
266
# test includes
260
- _clean_paths ()
267
+ _clean_paths (root = False )
261
268
app_path = create_app (includes = "" )
262
269
_ , stderr_output = subprocess .Popen (
263
270
[sys .executable , "-c" , "import main" ],
@@ -287,7 +294,7 @@ def test_includes():
287
294
288
295
def test_pip_args ():
289
296
# test pip_args
290
- _clean_paths ()
297
+ _clean_paths (root = False )
291
298
stdout , _ = subprocess .Popen (
292
299
[sys .executable , "-c" , "import bottle;print(bottle.__file__)" ],
293
300
stderr = subprocess .PIPE ,
@@ -305,7 +312,7 @@ def test_pip_args():
305
312
306
313
def test_cache_path ():
307
314
# test cache_path
308
- _clean_paths ()
315
+ _clean_paths (root = False )
309
316
mock_dir = Path ("mock_package" )
310
317
mock_dir .mkdir ()
311
318
create_app (cache_path = mock_dir )
@@ -314,7 +321,7 @@ def test_cache_path():
314
321
315
322
def test_unzip ():
316
323
# test unzip
317
- _clean_paths ()
324
+ _clean_paths (root = False )
318
325
app_path = create_app (unzip = "bottle" , pip_args = ["bottle" ])
319
326
output , _ = subprocess .Popen (
320
327
[sys .executable , str (app_path ), "-c" , "import bottle;print(bottle.__file__)" ],
@@ -330,7 +337,7 @@ def test_unzip():
330
337
331
338
def test_unzip_complete_path ():
332
339
# test unzip with complete path
333
- _clean_paths ()
340
+ _clean_paths (root = False )
334
341
app_path = create_app (unzip = "ensure_app,bin/bottle.py" , pip_args = ["bottle" ])
335
342
output , _ = subprocess .Popen (
336
343
[sys .executable , str (app_path ), "-c" , "import bottle;print(bottle.__file__)" ],
@@ -346,7 +353,7 @@ def test_unzip_complete_path():
346
353
347
354
def test_unzip_with_auto_unzip ():
348
355
# test unzip with `AUTO_UNZIP` and `*`
349
- _clean_paths ()
356
+ _clean_paths (root = False )
350
357
app_path = create_app (unzip = "" , pip_args = ["orjson" ])
351
358
output , _ = subprocess .Popen (
352
359
[sys .executable , str (app_path ), "-V" ],
@@ -355,7 +362,7 @@ def test_unzip_with_auto_unzip():
355
362
).communicate ()
356
363
orjson_unzipped = bool (list (Path ("zipapps_cache" ).glob ("**/orjson" )))
357
364
assert not orjson_unzipped , "test unzip failed, orjson should not be unzipped"
358
- _clean_paths ()
365
+ _clean_paths (root = False )
359
366
app_path = create_app (unzip = "AUTO_UNZIP" , pip_args = ["orjson" ])
360
367
output , _ = subprocess .Popen (
361
368
[sys .executable , str (app_path ), "-V" ],
@@ -364,7 +371,7 @@ def test_unzip_with_auto_unzip():
364
371
).communicate ()
365
372
orjson_unzipped = bool (list (Path ("zipapps_cache" ).glob ("**/orjson" )))
366
373
assert orjson_unzipped , "test unzip failed, orjson should be unzipped"
367
- _clean_paths ()
374
+ _clean_paths (root = False )
368
375
# test auto unzip without nonsense folder
369
376
app_path = create_app (unzip = "AUTO_UNZIP" )
370
377
output , _ = subprocess .Popen (
@@ -374,7 +381,7 @@ def test_unzip_with_auto_unzip():
374
381
).communicate ()
375
382
no_cache_dir = not Path ("zipapps_cache" ).is_dir ()
376
383
assert no_cache_dir , "test unzip failed, should not unzip anything"
377
- _clean_paths ()
384
+ _clean_paths (root = False )
378
385
app_path = create_app (unzip = "AUTO" , pip_args = ["orjson" ])
379
386
output , _ = subprocess .Popen (
380
387
[sys .executable , str (app_path ), "-V" ],
@@ -383,7 +390,7 @@ def test_unzip_with_auto_unzip():
383
390
).communicate ()
384
391
orjson_unzipped = bool (list (Path ("zipapps_cache" ).glob ("**/orjson" )))
385
392
assert orjson_unzipped , "test unzip failed, orjson should be unzipped"
386
- _clean_paths ()
393
+ _clean_paths (root = False )
387
394
app_path = create_app (unzip = "*" , pip_args = ["orjson" ])
388
395
output , _ = subprocess .Popen (
389
396
[sys .executable , str (app_path ), "-V" ],
@@ -396,7 +403,7 @@ def test_unzip_with_auto_unzip():
396
403
397
404
def test_env_usage ():
398
405
# test ensure path for venv usage
399
- _clean_paths ()
406
+ _clean_paths (root = False )
400
407
create_app (output = "bottle_env.pyz" , unzip = "bottle" , pip_args = ["bottle" ])
401
408
# activate sys.path and unzip cache
402
409
zipimport .zipimporter ("bottle_env.pyz" ).load_module ("ensure_zipapps" )
@@ -409,7 +416,7 @@ def test_env_usage():
409
416
410
417
def test_compiled ():
411
418
# test compiled
412
- _clean_paths ()
419
+ _clean_paths (root = False )
413
420
app_path = create_app (unzip = "six" , compiled = True , pip_args = ["six" ])
414
421
output , _ = subprocess .Popen (
415
422
[sys .executable , str (app_path ), "-c" , "import six;print(six.__cached__)" ],
@@ -422,7 +429,7 @@ def test_compiled():
422
429
423
430
def test_variable_home_self_temp ():
424
431
# test unzip with $HOME / $SELF / $TEMP
425
- _clean_paths ()
432
+ _clean_paths (root = False )
426
433
app_path = create_app (
427
434
unzip = "bottle" , pip_args = ["bottle" ], unzip_path = "$HOME/app_cache"
428
435
)
@@ -457,7 +464,7 @@ def test_variable_home_self_temp():
457
464
458
465
def test_runtime_zipapps_arg ():
459
466
# test --zipapps
460
- _clean_paths ()
467
+ _clean_paths (root = False )
461
468
create_app (unzip = "AUTO" , output = "orjson.pyz" , pip_args = ["orjson" ])
462
469
create_app (output = "six.pyz" , pip_args = ["six" ])
463
470
cmd = (
@@ -473,7 +480,7 @@ def test_runtime_zipapps_arg():
473
480
474
481
def test_build_zipapps_arg ():
475
482
# test --zipapps while building
476
- _clean_paths ()
483
+ _clean_paths (root = False )
477
484
# test for simple usage
478
485
create_app (pip_args = ["six" ], output = "six.pyz" )
479
486
Path ("./entry_test.py" ).write_text ("import six;print(six.__file__)" )
@@ -502,7 +509,7 @@ def test_build_zipapps_arg():
502
509
assert not error
503
510
assert b"six.pyz" in output
504
511
505
- _clean_paths ()
512
+ _clean_paths (root = False )
506
513
# test for $SELF arg
507
514
create_app (pip_args = ["six" ], output = "six.pyz" )
508
515
Path ("./entry_test.py" ).write_text ("import six;print(six.__file__)" )
@@ -530,7 +537,7 @@ def test_build_zipapps_arg():
530
537
assert not error
531
538
assert b"six.pyz" in output
532
539
533
- _clean_paths ()
540
+ _clean_paths (root = False )
534
541
# test for without --zipapps
535
542
create_app (pip_args = ["six" ], output = "six.pyz" )
536
543
Path ("./entry_test.py" ).write_text ("import six;print(six.__file__)" )
@@ -550,7 +557,7 @@ def test_build_zipapps_arg():
550
557
551
558
def test_run_path ():
552
559
# test run_path
553
- _clean_paths ()
560
+ _clean_paths (root = False )
554
561
create_app (output = "app.pyz" )
555
562
Path ("mock_main.py" ).write_text ("import sys;print(__name__, sys.argv)" )
556
563
output = subprocess .check_output (
@@ -562,7 +569,7 @@ def test_run_path():
562
569
563
570
def test_lazy_install ():
564
571
# test lazy pip install
565
- _clean_paths ()
572
+ _clean_paths (root = False )
566
573
mock_requirements = Path ("_requirements.txt" )
567
574
mock_requirements .write_text ("six" )
568
575
app_path = create_app (
@@ -598,7 +605,7 @@ def test_lazy_install():
598
605
599
606
def test_sys_paths ():
600
607
# test sys_paths
601
- _clean_paths ()
608
+ _clean_paths (root = False )
602
609
# pip install by given --target
603
610
args = [sys .executable , "-m" , "pip" , "install" , "bottle" , "-t" , "./bottle_env" ]
604
611
subprocess .Popen (args = args ).wait ()
@@ -611,7 +618,7 @@ def test_sys_paths():
611
618
612
619
def test_layer_mode ():
613
620
# test layer-mode
614
- _clean_paths ()
621
+ _clean_paths (root = False )
615
622
test1 = Path ("setup.py" )
616
623
test1 .touch ()
617
624
old_file = create_app (
@@ -631,7 +638,7 @@ def test_chmod():
631
638
if os .name != "nt" :
632
639
# posix only
633
640
# test --chmod
634
- _clean_paths ()
641
+ _clean_paths (root = False )
635
642
app_path = create_app (unzip = "*" , pip_args = ["six" ], lazy_install = True )
636
643
subprocess .Popen ([sys .executable , str (app_path ), "--activate-zipapps" ]).wait ()
637
644
assert Path ("app.pyz" ).stat ().st_mode != 33279
@@ -640,7 +647,7 @@ def test_chmod():
640
647
assert _path .stat ().st_mode != 33279 , _path .stat ().st_mode
641
648
break
642
649
643
- _clean_paths ()
650
+ _clean_paths (root = False )
644
651
app_path = create_app (
645
652
unzip = "*" , pip_args = ["six" ], lazy_install = True , chmod = "777"
646
653
)
@@ -654,7 +661,7 @@ def test_chmod():
654
661
655
662
def test_delete_useless ():
656
663
# test layer-mode
657
- _clean_paths ()
664
+ _clean_paths (root = False )
658
665
from zipfile import ZipFile
659
666
660
667
# test cmd mode, do not delete dist-info dir
@@ -693,7 +700,7 @@ def test_pip_install_target():
693
700
694
701
from zipapps import pip_install_target
695
702
696
- _clean_paths ()
703
+ _clean_paths (root = False )
697
704
# test without "insert sys.path"
698
705
start_time = time .time ()
699
706
assert pip_install_target (
0 commit comments