Skip to content

Commit 6113837

Browse files
author
lidong
committed
fix linux: pip error The folder you are executing pip from can no longer be found
1 parent 0da29ed commit 6113837

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

test_utils.py

+47-40
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@
1818

1919

2020
@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)
2330

2431

2532
def test_quiet_mode():
2633
# test -qqqq quiet mode
27-
_clean_paths()
34+
_clean_paths(root=False)
2835
output = subprocess.check_output([sys.executable, "-m", "zipapps", "six", "-qqqq"])
2936
assert not output, output
3037

3138

3239
def test_freeze():
3340
# test --freeze-reqs
34-
_clean_paths()
41+
_clean_paths(root=False)
3542
output = subprocess.check_output(
3643
[sys.executable, "-m", "zipapps", "--freeze-reqs", "-", "six==1.16.0"]
3744
)
@@ -40,7 +47,7 @@ def test_freeze():
4047

4148
def test_dump_load_config():
4249
# test `--dump-config` and `--load-config`
43-
_clean_paths()
50+
_clean_paths(root=False)
4451
output, _ = subprocess.Popen(
4552
[sys.executable, "-m", "zipapps", "--dump-config", "-"],
4653
stderr=subprocess.PIPE,
@@ -77,7 +84,7 @@ def test_dump_load_config():
7784

7885
def test_environ():
7986
# test os.environ
80-
_clean_paths()
87+
_clean_paths(root=False)
8188
app_path = create_app(unzip="*", unzip_path="app_cache")
8289
os.environ["CLEAR_ZIPAPPS_CACHE"] = "1"
8390
os.environ["CLEAR_ZIPAPPS_SELF"] = "1"
@@ -112,7 +119,7 @@ def test_environ():
112119

113120
def test_unzip_with_cwd_pid():
114121
# test unzip with $CWD / $PID
115-
_clean_paths()
122+
_clean_paths(root=False)
116123
app_path = create_app(
117124
unzip="bottle", pip_args=["bottle"], unzip_path="$CWD/app_cache/$PID"
118125
)
@@ -129,7 +136,7 @@ def test_unzip_with_cwd_pid():
129136

130137
def test_clear_zipapps_self():
131138
# test clear_zipapps_self
132-
_clean_paths()
139+
_clean_paths(root=False)
133140
assert not Path("app.pyz").is_file()
134141
app_path = create_app(clear_zipapps_self=True)
135142
assert Path("app.pyz").is_file()
@@ -141,13 +148,13 @@ def test_clear_zipapps_self():
141148

142149
def test_unzip_exclude():
143150
# test unzip_exclude
144-
_clean_paths()
151+
_clean_paths(root=False)
145152
app_path = create_app(unzip="*", pip_args=["six"], unzip_exclude="")
146153
stdout_output, stderr_output = subprocess.Popen(
147154
[sys.executable, str(app_path), "--activate-zipapps"]
148155
).communicate()
149156
assert Path("./zipapps_cache/app/six.py").is_file()
150-
_clean_paths()
157+
_clean_paths(root=False)
151158
app_path = create_app(unzip="*", pip_args=["six"], unzip_exclude="six")
152159
stdout_output, stderr_output = subprocess.Popen(
153160
[sys.executable, str(app_path), "--activate-zipapps"]
@@ -157,13 +164,13 @@ def test_unzip_exclude():
157164

158165
def test_clear_zip_cache():
159166
# test -czc
160-
_clean_paths()
167+
_clean_paths(root=False)
161168
app_path = create_app(clear_zipapps_cache=False, unzip="*")
162169
stdout_output, stderr_output = subprocess.Popen(
163170
[sys.executable, str(app_path), "-V"]
164171
).communicate()
165172
assert Path("./zipapps_cache").is_dir()
166-
_clean_paths()
173+
_clean_paths(root=False)
167174
app_path = create_app(clear_zipapps_cache=True, unzip="*")
168175
stdout_output, stderr_output = subprocess.Popen(
169176
[sys.executable, str(app_path), "-V"]
@@ -173,7 +180,7 @@ def test_clear_zip_cache():
173180

174181
def test_build_id_and_single_file():
175182
# test build_id
176-
_clean_paths()
183+
_clean_paths(root=False)
177184
mock_requirement = Path("_requirements.txt")
178185
mock_requirement.write_text("bottle")
179186
old_file = create_app(
@@ -200,7 +207,7 @@ def test_build_id_and_single_file():
200207

201208
def test_main_source_code():
202209
# test main: source code
203-
_clean_paths()
210+
_clean_paths(root=False)
204211
subprocess.check_output(
205212
[
206213
sys.executable,
@@ -217,7 +224,7 @@ def test_main_source_code():
217224

218225
def test_main_module():
219226
# test main module+function
220-
_clean_paths()
227+
_clean_paths(root=False)
221228
mock_main = Path("mock_main.py")
222229
mock_main.touch()
223230
mock_main.write_text("print(1)")
@@ -257,7 +264,7 @@ def test_main_module():
257264

258265
def test_includes():
259266
# test includes
260-
_clean_paths()
267+
_clean_paths(root=False)
261268
app_path = create_app(includes="")
262269
_, stderr_output = subprocess.Popen(
263270
[sys.executable, "-c", "import main"],
@@ -287,7 +294,7 @@ def test_includes():
287294

288295
def test_pip_args():
289296
# test pip_args
290-
_clean_paths()
297+
_clean_paths(root=False)
291298
stdout, _ = subprocess.Popen(
292299
[sys.executable, "-c", "import bottle;print(bottle.__file__)"],
293300
stderr=subprocess.PIPE,
@@ -305,7 +312,7 @@ def test_pip_args():
305312

306313
def test_cache_path():
307314
# test cache_path
308-
_clean_paths()
315+
_clean_paths(root=False)
309316
mock_dir = Path("mock_package")
310317
mock_dir.mkdir()
311318
create_app(cache_path=mock_dir)
@@ -314,7 +321,7 @@ def test_cache_path():
314321

315322
def test_unzip():
316323
# test unzip
317-
_clean_paths()
324+
_clean_paths(root=False)
318325
app_path = create_app(unzip="bottle", pip_args=["bottle"])
319326
output, _ = subprocess.Popen(
320327
[sys.executable, str(app_path), "-c", "import bottle;print(bottle.__file__)"],
@@ -330,7 +337,7 @@ def test_unzip():
330337

331338
def test_unzip_complete_path():
332339
# test unzip with complete path
333-
_clean_paths()
340+
_clean_paths(root=False)
334341
app_path = create_app(unzip="ensure_app,bin/bottle.py", pip_args=["bottle"])
335342
output, _ = subprocess.Popen(
336343
[sys.executable, str(app_path), "-c", "import bottle;print(bottle.__file__)"],
@@ -346,7 +353,7 @@ def test_unzip_complete_path():
346353

347354
def test_unzip_with_auto_unzip():
348355
# test unzip with `AUTO_UNZIP` and `*`
349-
_clean_paths()
356+
_clean_paths(root=False)
350357
app_path = create_app(unzip="", pip_args=["orjson"])
351358
output, _ = subprocess.Popen(
352359
[sys.executable, str(app_path), "-V"],
@@ -355,7 +362,7 @@ def test_unzip_with_auto_unzip():
355362
).communicate()
356363
orjson_unzipped = bool(list(Path("zipapps_cache").glob("**/orjson")))
357364
assert not orjson_unzipped, "test unzip failed, orjson should not be unzipped"
358-
_clean_paths()
365+
_clean_paths(root=False)
359366
app_path = create_app(unzip="AUTO_UNZIP", pip_args=["orjson"])
360367
output, _ = subprocess.Popen(
361368
[sys.executable, str(app_path), "-V"],
@@ -364,7 +371,7 @@ def test_unzip_with_auto_unzip():
364371
).communicate()
365372
orjson_unzipped = bool(list(Path("zipapps_cache").glob("**/orjson")))
366373
assert orjson_unzipped, "test unzip failed, orjson should be unzipped"
367-
_clean_paths()
374+
_clean_paths(root=False)
368375
# test auto unzip without nonsense folder
369376
app_path = create_app(unzip="AUTO_UNZIP")
370377
output, _ = subprocess.Popen(
@@ -374,7 +381,7 @@ def test_unzip_with_auto_unzip():
374381
).communicate()
375382
no_cache_dir = not Path("zipapps_cache").is_dir()
376383
assert no_cache_dir, "test unzip failed, should not unzip anything"
377-
_clean_paths()
384+
_clean_paths(root=False)
378385
app_path = create_app(unzip="AUTO", pip_args=["orjson"])
379386
output, _ = subprocess.Popen(
380387
[sys.executable, str(app_path), "-V"],
@@ -383,7 +390,7 @@ def test_unzip_with_auto_unzip():
383390
).communicate()
384391
orjson_unzipped = bool(list(Path("zipapps_cache").glob("**/orjson")))
385392
assert orjson_unzipped, "test unzip failed, orjson should be unzipped"
386-
_clean_paths()
393+
_clean_paths(root=False)
387394
app_path = create_app(unzip="*", pip_args=["orjson"])
388395
output, _ = subprocess.Popen(
389396
[sys.executable, str(app_path), "-V"],
@@ -396,7 +403,7 @@ def test_unzip_with_auto_unzip():
396403

397404
def test_env_usage():
398405
# test ensure path for venv usage
399-
_clean_paths()
406+
_clean_paths(root=False)
400407
create_app(output="bottle_env.pyz", unzip="bottle", pip_args=["bottle"])
401408
# activate sys.path and unzip cache
402409
zipimport.zipimporter("bottle_env.pyz").load_module("ensure_zipapps")
@@ -409,7 +416,7 @@ def test_env_usage():
409416

410417
def test_compiled():
411418
# test compiled
412-
_clean_paths()
419+
_clean_paths(root=False)
413420
app_path = create_app(unzip="six", compiled=True, pip_args=["six"])
414421
output, _ = subprocess.Popen(
415422
[sys.executable, str(app_path), "-c", "import six;print(six.__cached__)"],
@@ -422,7 +429,7 @@ def test_compiled():
422429

423430
def test_variable_home_self_temp():
424431
# test unzip with $HOME / $SELF / $TEMP
425-
_clean_paths()
432+
_clean_paths(root=False)
426433
app_path = create_app(
427434
unzip="bottle", pip_args=["bottle"], unzip_path="$HOME/app_cache"
428435
)
@@ -457,7 +464,7 @@ def test_variable_home_self_temp():
457464

458465
def test_runtime_zipapps_arg():
459466
# test --zipapps
460-
_clean_paths()
467+
_clean_paths(root=False)
461468
create_app(unzip="AUTO", output="orjson.pyz", pip_args=["orjson"])
462469
create_app(output="six.pyz", pip_args=["six"])
463470
cmd = (
@@ -473,7 +480,7 @@ def test_runtime_zipapps_arg():
473480

474481
def test_build_zipapps_arg():
475482
# test --zipapps while building
476-
_clean_paths()
483+
_clean_paths(root=False)
477484
# test for simple usage
478485
create_app(pip_args=["six"], output="six.pyz")
479486
Path("./entry_test.py").write_text("import six;print(six.__file__)")
@@ -502,7 +509,7 @@ def test_build_zipapps_arg():
502509
assert not error
503510
assert b"six.pyz" in output
504511

505-
_clean_paths()
512+
_clean_paths(root=False)
506513
# test for $SELF arg
507514
create_app(pip_args=["six"], output="six.pyz")
508515
Path("./entry_test.py").write_text("import six;print(six.__file__)")
@@ -530,7 +537,7 @@ def test_build_zipapps_arg():
530537
assert not error
531538
assert b"six.pyz" in output
532539

533-
_clean_paths()
540+
_clean_paths(root=False)
534541
# test for without --zipapps
535542
create_app(pip_args=["six"], output="six.pyz")
536543
Path("./entry_test.py").write_text("import six;print(six.__file__)")
@@ -550,7 +557,7 @@ def test_build_zipapps_arg():
550557

551558
def test_run_path():
552559
# test run_path
553-
_clean_paths()
560+
_clean_paths(root=False)
554561
create_app(output="app.pyz")
555562
Path("mock_main.py").write_text("import sys;print(__name__, sys.argv)")
556563
output = subprocess.check_output(
@@ -562,7 +569,7 @@ def test_run_path():
562569

563570
def test_lazy_install():
564571
# test lazy pip install
565-
_clean_paths()
572+
_clean_paths(root=False)
566573
mock_requirements = Path("_requirements.txt")
567574
mock_requirements.write_text("six")
568575
app_path = create_app(
@@ -598,7 +605,7 @@ def test_lazy_install():
598605

599606
def test_sys_paths():
600607
# test sys_paths
601-
_clean_paths()
608+
_clean_paths(root=False)
602609
# pip install by given --target
603610
args = [sys.executable, "-m", "pip", "install", "bottle", "-t", "./bottle_env"]
604611
subprocess.Popen(args=args).wait()
@@ -611,7 +618,7 @@ def test_sys_paths():
611618

612619
def test_layer_mode():
613620
# test layer-mode
614-
_clean_paths()
621+
_clean_paths(root=False)
615622
test1 = Path("setup.py")
616623
test1.touch()
617624
old_file = create_app(
@@ -631,7 +638,7 @@ def test_chmod():
631638
if os.name != "nt":
632639
# posix only
633640
# test --chmod
634-
_clean_paths()
641+
_clean_paths(root=False)
635642
app_path = create_app(unzip="*", pip_args=["six"], lazy_install=True)
636643
subprocess.Popen([sys.executable, str(app_path), "--activate-zipapps"]).wait()
637644
assert Path("app.pyz").stat().st_mode != 33279
@@ -640,7 +647,7 @@ def test_chmod():
640647
assert _path.stat().st_mode != 33279, _path.stat().st_mode
641648
break
642649

643-
_clean_paths()
650+
_clean_paths(root=False)
644651
app_path = create_app(
645652
unzip="*", pip_args=["six"], lazy_install=True, chmod="777"
646653
)
@@ -654,7 +661,7 @@ def test_chmod():
654661

655662
def test_delete_useless():
656663
# test layer-mode
657-
_clean_paths()
664+
_clean_paths(root=False)
658665
from zipfile import ZipFile
659666

660667
# test cmd mode, do not delete dist-info dir
@@ -693,7 +700,7 @@ def test_pip_install_target():
693700

694701
from zipapps import pip_install_target
695702

696-
_clean_paths()
703+
_clean_paths(root=False)
697704
# test without "insert sys.path"
698705
start_time = time.time()
699706
assert pip_install_target(

0 commit comments

Comments
 (0)