Skip to content

Commit 754790c

Browse files
committed
fix zipper test
1 parent 58307f5 commit 754790c

3 files changed

Lines changed: 26 additions & 18 deletions

File tree

python/private/zipapp/py_zipapp_rule.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def _create_zip(ctx, py_runtime, py_executable, stage2_bootstrap):
162162
zipper_args.add("--runfiles-dir=runfiles")
163163

164164
is_windows = target_platform_has_any_constraint(ctx, ctx.attr._windows_constraints)
165-
zipper_args.add("\\" if is_windows else "/", format = "--pathsep=%s")
165+
zipper_args.add("\\" if is_windows else "/", format = "--target-platform-pathsep=%s")
166166

167167
actions_run(
168168
ctx,

tests/tools/zipapp/zipper_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_create_zip_with_direct_symlink(self):
106106
zf,
107107
"runfiles/path/to/link",
108108
is_symlink=True,
109-
target="../../../target/path",
109+
target="../../target/path",
110110
)
111111

112112
def test_pathsep_normalization(self):
@@ -128,14 +128,14 @@ def test_pathsep_normalization(self):
128128
# But the content of the symlink should be normalized if it was passed through path_norm
129129
self.assertEqual(
130130
set(zf.namelist()),
131-
{"dir\\file.txt", "runfiles\\link\\path"},
131+
{"dir/file.txt", "runfiles/link/path"},
132132
)
133133
# The target of the symlink should have backslashes
134134
self.assertZipFileContent(
135135
zf,
136-
"runfiles\\link\\path",
136+
"runfiles/link/path",
137137
is_symlink=True,
138-
target="..\\..\\target\\path",
138+
target="..\\target\\path",
139139
)
140140

141141
def test_symlink_precedence(self):
@@ -159,7 +159,7 @@ def test_symlink_precedence(self):
159159
zf,
160160
"runfiles/my_ws/path/to/file",
161161
is_symlink=True,
162-
target="../../../../symlink/target",
162+
target="../../../symlink/target",
163163
)
164164

165165
def test_timestamps_are_deterministic(self):

tools/private/zipapp/zipper.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,27 @@ def read_manifest(
101101
return entries
102102

103103

104-
def path_norm(path, pathsep):
105-
if pathsep == "/":
106-
return path.replace("\\", pathsep)
104+
def convert_symlink_target(path, platform_pathsep):
105+
"""Converts the path a symlink points to the target-platform format.
106+
107+
On Windows, relative symlinks must use backslashes.
108+
"""
109+
if platform_pathsep == "/":
110+
# Convert Windows to Unix
111+
return path.replace("\\", platform_pathsep)
107112
else:
113+
# Convert Unix to Windows
108114
return path.replace("/", "\\")
109115

116+
# Zip files use forward slash for the entries, even on Windows
117+
def normalize_zip_path(path):
118+
return path.replace("\\", "/")
110119

111-
def _write_entry(zf, entry, compress_type, seen, pathsep):
120+
def _write_entry(zf, entry, compress_type, seen, platform_pathsep):
112121
type_, is_symlink_str, zip_path, content_path = entry
113122
# Normalize slashes, otherwise the `seen` logic doesn't
114123
# work correctly.
115-
zip_path = path_norm(zip_path, pathsep)
124+
zip_path = normalize_zip_path(zip_path)
116125
if zip_path in seen:
117126
# This can occur because symlink entries have precedence
118127
# over non-symlink entries.
@@ -133,8 +142,7 @@ def _write_entry(zf, entry, compress_type, seen, pathsep):
133142
zi.date_time = (1980, 1, 1, 0, 0, 0)
134143
zi.create_system = 3 # Unix
135144
zi.compress_type = compress_type
136-
# Windows doesn't like symlinks with forward slashes
137-
target = path_norm(content_path, pathsep)
145+
target = convert_symlink_target(content_path, platform_pathsep)
138146
# Set permissions to 777 for symlink (standard)
139147
zi.external_attr = (S_IFLNK | 0o777) << 16
140148
zf.writestr(zi, target)
@@ -153,14 +161,14 @@ def _write_entry(zf, entry, compress_type, seen, pathsep):
153161
zi.date_time = (1980, 1, 1, 0, 0, 0)
154162
zi.create_system = 3 # Unix
155163
zi.compress_type = compress_type
156-
# Windows doesn't like symlinks with forward slashes
157-
target = path_norm(os.readlink(content_path), pathsep)
164+
target = convert_symlink_target(os.readlink(content_path), platform_pathsep)
158165
# Set permissions to 777 for symlink (standard)
159166
zi.external_attr = (S_IFLNK | 0o777) << 16
160167
zf.writestr(zi, target)
161168
else:
162169
st = os.stat(content_path)
163170
zi = zipfile.ZipInfo(zip_path)
171+
print("store:", zip_path)
164172
zi.date_time = (1980, 1, 1, 0, 0, 0)
165173
zi.create_system = 3 # Unix
166174
zi.compress_type = compress_type
@@ -255,8 +263,8 @@ def main():
255263
"--runfiles-dir", default="runfiles", help="Name of the runfiles directory"
256264
)
257265
parser.add_argument(
258-
"--pathsep",
259-
default="/",
266+
"--target-platform-pathsep",
267+
help = "The path separator for the target platform"
260268
)
261269
args = parser.parse_args()
262270

@@ -268,7 +276,7 @@ def main():
268276
workspace_name=args.workspace_name,
269277
legacy_external_runfiles=args.legacy_external_runfiles == "1",
270278
runfiles_dir=args.runfiles_dir,
271-
pathsep=args.pathsep,
279+
platform_pathsep=args.target_platform_pathsep,
272280
)
273281
except Exception as e:
274282
e.add_note(f"Error creating zip {args.output}")

0 commit comments

Comments
 (0)