Skip to content

Commit 342a623

Browse files
committed
cleanup test
1 parent 289bea7 commit 342a623

File tree

4 files changed

+27
-35
lines changed

4 files changed

+27
-35
lines changed

python/private/zipapp/zip_main_template.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import os
2525
from os.path import join
2626
import shutil
27+
import stat
2728
import subprocess
2829
import tempfile
2930
import zipfile
@@ -166,10 +167,18 @@ def extract_zip(zip_path, dest_dir):
166167
dest_dir = get_windows_path_with_unc_prefix(dest_dir)
167168
with zipfile.ZipFile(zip_path) as zf:
168169
for info in zf.infolist():
169-
zf.extract(info, dest_dir)
170-
# UNC-prefixed paths must be absolute/normalized. See
171-
# https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file#maximum-path-length-limitation
172170
file_path = os.path.abspath(os.path.join(dest_dir, info.filename))
171+
# If the file exists, it might be a symlink or read-only file from a previous extraction.
172+
# Unlink it first so zipfile.extract doesn't corrupt the symlink target or fail on read-only files.
173+
if os.path.lexists(file_path) and not os.path.isdir(file_path):
174+
try:
175+
os.unlink(file_path)
176+
except OSError:
177+
# On Windows, unlinking a read-only file fails.
178+
os.chmod(file_path, stat.S_IWRITE)
179+
os.unlink(file_path)
180+
181+
zf.extract(info, dest_dir)
173182
# The Unix st_mode bits (see "man 7 inode") are stored in the upper 16
174183
# bits of external_attr.
175184
attrs = info.external_attr >> 16

tests/py_zipapp/BUILD.bazel

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,6 @@ sh_test(
9898
toolchains = ["//python:current_py_toolchain"],
9999
)
100100

101-
sh_test(
102-
name = "extract_root_test",
103-
srcs = ["extract_root_test.sh"],
104-
data = [
105-
":system_python_zipapp",
106-
],
107-
env = {
108-
"ZIPAPP": "$(rootpath :system_python_zipapp)",
109-
},
110-
)
111-
112101
py_library(
113102
name = "some_dep",
114103
srcs = ["some_dep.py"],

tests/py_zipapp/extract_root_test.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/py_zipapp/system_python_zipapp_external_bootstrap_test.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ fi
1313
ZIPAPP="${ZIPAPP/.exe/.zip}"
1414

1515
export RULES_PYTHON_BOOTSTRAP_VERBOSE=1
16+
1617
# We're testing the invocation of `__main__.py`, so we have to
1718
# manually pass the zipapp to python.
19+
echo "Running zipapp using an automatic temp directory..."
20+
"$PYTHON" "$ZIPAPP"
21+
22+
echo "Running zipapp with extract root set..."
23+
export RULES_PYTHON_EXTRACT_ROOT="${TEST_TMPDIR:-/tmp}/extract_root_test"
24+
"$PYTHON" "$ZIPAPP"
25+
26+
# Verify that the directory was created
27+
if [[ ! -d "$RULES_PYTHON_EXTRACT_ROOT" ]]; then
28+
echo "Error: Extract root directory $RULES_PYTHON_EXTRACT_ROOT was not created!"
29+
exit 1
30+
fi
31+
32+
echo "Running zipapp with extract root set a second time..."
1833
"$PYTHON" "$ZIPAPP"

0 commit comments

Comments
 (0)