Skip to content

Commit 93aceef

Browse files
committed
raw symlinks in zip: add test
1 parent 4d5ba71 commit 93aceef

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

tests/zip/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ load("//pkg:mappings.bzl", "pkg_attributes", "pkg_mkdirs", "pkg_mklink")
1919
load("//pkg:zip.bzl", "pkg_zip")
2020
load("//tests:my_package_name.bzl", "my_package_naming")
2121
load("//tests/util:defs.bzl", "directory")
22+
load("//tests/zip:symlink_util.bzl", "create_fake_symlink")
2223

2324
package(default_applicable_licenses = ["//:license"])
2425

@@ -268,6 +269,20 @@ pkg_zip(
268269
compression_type = "stored",
269270
)
270271

272+
create_fake_symlink(
273+
name = "fake_symlink",
274+
link = "fake_symlink",
275+
target = "../does_not_exist",
276+
)
277+
278+
pkg_zip(
279+
name = "test_zip_symlink",
280+
srcs = [
281+
":fake_symlink",
282+
"//tests:file_and_link",
283+
],
284+
)
285+
271286
py_test(
272287
name = "zip_test",
273288
srcs = [
@@ -287,6 +302,7 @@ py_test(
287302
":test_zip_package_dir_substitution.zip",
288303
":test_zip_permissions.zip",
289304
":test_zip_stored",
305+
":test_zip_symlink",
290306
":test_zip_timestamp.zip",
291307
":test_zip_tree.zip",
292308
],

tests/zip/symlink_util.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def _create_fake_symlink_impl(ctx):
2+
symlink = ctx.actions.declare_symlink(ctx.attr.link)
3+
ctx.actions.symlink(output = symlink, target_path = ctx.attr.target)
4+
return [DefaultInfo(files = depset([symlink]))]
5+
6+
create_fake_symlink = rule(
7+
implementation = _create_fake_symlink_impl,
8+
attrs = {
9+
"link": attr.string(mandatory = True),
10+
"target": attr.string(mandatory = True),
11+
},
12+
)

tests/zip/zip_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ def test_compression_stored(self):
150150
{"filename": "loremipsum.txt", "crc": LOREM_CRC, "size": 543},
151151
])
152152

153+
def test_symlink(self):
154+
self.assertZipFileContent("test_zip_symlink.zip", [
155+
{"filename": "BUILD", "islink": False},
156+
{"filename": "fake_symlink", "islink": True}, # raw symlink -> keep symlink
157+
{"filename": "outer_BUILD", "islink": False},# nonraw symlink -> copy
158+
])
159+
153160

154161

155162
if __name__ == "__main__":

tests/zip/zip_test_lib.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
MSDOS_DIR_BIT = 0x10
2626
UNIX_RWX_BITS = 0o777
2727
UNIX_RX_BITS = 0o555
28+
UNIX_SYMLINK_BIT = 0o120000
2829

2930
# The ZIP epoch date: (1980, 1, 1, 0, 0, 0)
3031
_ZIP_EPOCH_DT = datetime.datetime(1980, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc)
@@ -77,7 +78,17 @@ def assertZipFileContent(self, zip_file, content):
7778
oct(expected.get("attr", 0o755)))
7879
elif "isexe" in expected:
7980
got_mode = (info.external_attr >> 16) & UNIX_RX_BITS
80-
self.assertEqual(oct(got_mode), oct(UNIX_RX_BITS))
81+
if expected['isexe']:
82+
self.assertEqual(oct(got_mode), oct(UNIX_RX_BITS))
83+
else:
84+
self.assertNotEqual(oct(got_mode), oct(UNIX_RX_BITS))
85+
elif "islink" in expected:
86+
got_mode = (info.external_attr >> 16) & UNIX_SYMLINK_BIT
87+
if expected['islink']:
88+
self.assertEqual(oct(got_mode), oct(UNIX_SYMLINK_BIT))
89+
else:
90+
self.assertNotEqual(oct(got_mode), oct(UNIX_SYMLINK_BIT))
91+
8192
elif "size" in expected:
8293
self.assertEqual(info.compress_size, expected["size"])
8394

0 commit comments

Comments
 (0)