diff --git a/pkg/private/install.py.tpl b/pkg/private/install.py.tpl index 4c66caa4..886e7caa 100644 --- a/pkg/private/install.py.tpl +++ b/pkg/private/install.py.tpl @@ -62,6 +62,7 @@ class NativeInstaller(object): self.destdir = destdir self.wipe_destdir = wipe_destdir self.entries = [] + self.tree_files = manifest.TreeFileTracker() # Logger helper method, may not be necessary or desired def _subst_destdir(path, self): @@ -137,11 +138,19 @@ class NativeInstaller(object): self._chown_chmod(entry.dest, entry.mode, entry.user, entry.group) def _install_treeartifact_file(self, entry, src, dst): + if not self.tree_files.add_file(dst, src, entry.src): + return self._do_file_copy(src, dst) self._chown_chmod(dst, entry.mode, entry.user, entry.group) def _install_treeartifact(self, entry): logging.debug("COPYTREE %s <- %s/**", entry.dest, entry.src) + for root, dirs, _ in os.walk(entry.src): + rel_root = os.path.relpath(root, entry.src) + for d in dirs: + self.tree_files.add_dir( + os.path.normpath(os.path.join(entry.dest, rel_root, d)), + entry.src) shutil.copytree( src=entry.src, dst=entry.dest, diff --git a/pkg/private/manifest.py b/pkg/private/manifest.py index 8d2a87c0..56fa0a7d 100644 --- a/pkg/private/manifest.py +++ b/pkg/private/manifest.py @@ -15,6 +15,7 @@ """Common package builder manifest helpers """ +import filecmp import json @@ -79,3 +80,43 @@ def entry_type_to_string(et): return "empty_file" else: raise ValueError("Invalid entry id {}".format(et)) + + +class TreeFileTracker(object): + """Detects conflicting paths provided by several tree artifacts.""" + + def __init__(self): + self._seen = {} + + def add_file(self, dest, content_path, tree): + """Records the file at dest, and returns whether it must be written. + + Raises: + ValueError: dest was already provided by another tree as a directory + or as a file with different content. + """ + previous = self._seen.get(dest) + if previous is None: + self._seen[dest] = (content_path, tree) + return True + previous_path, previous_tree = previous + if previous_path is not None and filecmp.cmp(previous_path, content_path, shallow=False): + return False + raise ValueError( + "Conflicting path %s, provided by tree artifacts %s and %s" % + (dest, previous_tree, tree)) + + def add_dir(self, dest, tree): + """Records the directory at dest. + + Raises: + ValueError: dest was already provided by another tree as a file. + """ + dest = dest.rstrip("/") + previous = self._seen.get(dest) + if previous is None: + self._seen[dest] = (None, tree) + elif previous[0] is not None: + raise ValueError( + "Conflicting path %s, provided by tree artifacts %s and %s" % + (dest, previous[1], tree)) diff --git a/pkg/private/pkg_files.bzl b/pkg/private/pkg_files.bzl index 31e7ae3c..271214d8 100644 --- a/pkg/private/pkg_files.bzl +++ b/pkg/private/pkg_files.bzl @@ -20,12 +20,12 @@ Concepts and terms: When attributes are empty in DestFile, we let the package tool decide their values. - content map: The map of destination paths to DestFile instances. Note that + content map: The map of destination paths to lists of DestFile instances. Note that several distinct destinations make share the same source path. Attempting to insert a duplicate entry in the content map is an error, because it means you are collapsing files together. We may want to relax this in the future if their DestFiles - are equal. + are equal. Only tree artifacts may share a destination. manifest: The file which represents the content map. This is generated by rule implementations and passed to the build_*.py helpers. @@ -137,10 +137,21 @@ def create_mapping_context_from_ctx( default_gid = None, ) +def _is_tree(entry): + return entry.src != None and entry.src.is_directory + +def _add_entry(content_map, dest, entry): + old_entries = content_map.get(dest) + if old_entries and _is_tree(entry) and _is_tree(old_entries[0]): + content_map[dest] = [e for e in old_entries if e.src != entry.src] + [entry] + else: + content_map[dest] = [entry] + def _check_dest(content_map, dest, src, origin, allow_duplicates_with_different_content = False): - old_entry = content_map.get(dest) - if not old_entry: + old_entries = content_map.get(dest) + if not old_entries: return + old_entry = old_entries[0] if old_entry.src == src or old_entry.origin == origin: return @@ -158,7 +169,7 @@ def _check_dest(content_map, dest, src, origin, allow_duplicates_with_different_ msg = "Duplicate output path: <%s>, declared in %s and %s\n SRC: %s" % ( dest, origin, - content_map[dest].origin, + old_entry.origin, src, ) if allow_duplicates_with_different_content: @@ -204,7 +215,7 @@ def _process_pkg_dirs(mapping_context, pkg_dirs_info, origin): for dir in pkg_dirs_info.dirs: dest = dir.strip("/") _check_dest(mapping_context.content_map, dest, None, origin, mapping_context.allow_duplicates_with_different_content) - mapping_context.content_map[dest] = _DestFile( + _add_entry(mapping_context.content_map, dest, _DestFile( src = None, entry_type = ENTRY_IS_DIR, mode = attrs[0], @@ -213,14 +224,14 @@ def _process_pkg_dirs(mapping_context, pkg_dirs_info, origin): uid = attrs[3], gid = attrs[4], origin = origin, - ) + )) def _process_pkg_files(mapping_context, pkg_files_info, origin): attrs = _merge_context_attributes(pkg_files_info, mapping_context) for filename, src in pkg_files_info.dest_src_map.items(): dest = filename.strip("/") _check_dest(mapping_context.content_map, dest, src, origin, mapping_context.allow_duplicates_with_different_content) - mapping_context.content_map[dest] = _DestFile( + _add_entry(mapping_context.content_map, dest, _DestFile( src = src, entry_type = ENTRY_IS_TREE if src.is_directory else ENTRY_IS_FILE, mode = attrs[0], @@ -229,13 +240,13 @@ def _process_pkg_files(mapping_context, pkg_files_info, origin): uid = attrs[3], gid = attrs[4], origin = origin, - ) + )) def _process_pkg_symlink(mapping_context, pkg_symlink_info, origin): dest = pkg_symlink_info.destination.strip("/") attrs = _merge_context_attributes(pkg_symlink_info, mapping_context) _check_dest(mapping_context.content_map, dest, None, origin, mapping_context.allow_duplicates_with_different_content) - mapping_context.content_map[dest] = _DestFile( + _add_entry(mapping_context.content_map, dest, _DestFile( src = None, entry_type = ENTRY_IS_LINK, mode = attrs[0], @@ -245,7 +256,7 @@ def _process_pkg_symlink(mapping_context, pkg_symlink_info, origin): gid = attrs[4], origin = origin, link_to = pkg_symlink_info.target, - ) + )) def _process_pkg_filegroup(mapping_context, pkg_filegroup_info): if hasattr(pkg_filegroup_info, "pkg_dirs"): @@ -317,7 +328,7 @@ def add_directory(mapping_context, dir_path, origin, mode = None, user = None, g uid: numeric uid gid: numeric gid """ - mapping_context.content_map[dir_path.strip("/")] = _DestFile( + _add_entry(mapping_context.content_map, dir_path.strip("/"), _DestFile( src = None, entry_type = ENTRY_IS_DIR, origin = origin, @@ -326,7 +337,7 @@ def add_directory(mapping_context, dir_path, origin, mode = None, user = None, g group = group or mapping_context.default_group, uid = uid or mapping_context.default_uid, gid = gid or mapping_context.default_gid, - ) + )) def add_empty_file(mapping_context, dest_path, origin, mode = None, user = None, group = None, uid = None, gid = None): """Add a single file to the content map. @@ -343,7 +354,7 @@ def add_empty_file(mapping_context, dest_path, origin, mode = None, user = None, """ dest = dest_path.strip("/") _check_dest(mapping_context.content_map, dest, None, origin) - mapping_context.content_map[dest] = _DestFile( + _add_entry(mapping_context.content_map, dest, _DestFile( src = None, entry_type = ENTRY_IS_EMPTY_FILE, origin = origin, @@ -352,7 +363,7 @@ def add_empty_file(mapping_context, dest_path, origin, mode = None, user = None, group = group or mapping_context.default_group, uid = uid or mapping_context.default_uid, gid = gid or mapping_context.default_gid, - ) + )) def add_label_list(mapping_context, srcs): """Helper method to add a list of labels (typically 'srcs') to a content_map. @@ -463,7 +474,7 @@ def add_from_default_info( else: entry_type = ENTRY_IS_FILE - mapping_context.content_map[d_path] = _DestFile( + _add_entry(mapping_context.content_map, d_path, _DestFile( src = rf, entry_type = entry_type, origin = src.label, @@ -472,7 +483,7 @@ def add_from_default_info( group = mapping_context.default_group, uid = mapping_context.default_uid, gid = mapping_context.default_gid, - ) + )) if runfiles.files: mapping_context.file_deps_transitive.append(runfiles.files) @@ -572,7 +583,7 @@ def add_single_file(mapping_context, dest_path, src, origin, mode = None, user = else: entry_type = ENTRY_IS_FILE - mapping_context.content_map[dest] = _DestFile( + _add_entry(mapping_context.content_map, dest, _DestFile( src = src, entry_type = entry_type, origin = origin, @@ -581,7 +592,7 @@ def add_single_file(mapping_context, dest_path, src, origin, mode = None, user = group = group or mapping_context.default_group, uid = uid or mapping_context.default_uid, gid = gid or mapping_context.default_gid, - ) + )) def add_symlink(mapping_context, dest_path, src, origin): """Add a symlink to the content map. @@ -598,7 +609,7 @@ def add_symlink(mapping_context, dest_path, src, origin): """ dest = dest_path.strip("/") _check_dest(mapping_context.content_map, dest, None, origin) - mapping_context.content_map[dest] = _DestFile( + _add_entry(mapping_context.content_map, dest, _DestFile( src = None, link_to = src, entry_type = ENTRY_IS_LINK, @@ -608,7 +619,7 @@ def add_symlink(mapping_context, dest_path, src, origin): group = mapping_context.default_group, uid = mapping_context.default_uid, gid = mapping_context.default_gid, - ) + )) def add_tree_artifact(content_map, dest_path, src, origin, mode = None, user = None, group = None, uid = None, gid = None): """Add an tree artifact (directory output) to the content map. @@ -624,7 +635,7 @@ def add_tree_artifact(content_map, dest_path, src, origin, mode = None, user = N uid: User id for the entry (probably unused) gid: Group id for the entry (probably unused) """ - content_map[dest_path] = _DestFile( + _add_entry(content_map, dest_path, _DestFile( src = src, origin = origin, entry_type = ENTRY_IS_TREE, @@ -633,7 +644,7 @@ def add_tree_artifact(content_map, dest_path, src, origin, mode = None, user = N group = group, uid = uid, gid = gid, - ) + )) def write_manifest(ctx, manifest_file, content_map, use_short_path = False, pretty_print = False): """Write a content map to a manifest file. @@ -655,8 +666,9 @@ def write_manifest(ctx, manifest_file, content_map, use_short_path = False, pret manifest_file, "[\n" + ",\n".join( [ - _encode_manifest_entry(ctx, dst, content_map[dst], use_short_path, pretty_print) + _encode_manifest_entry(ctx, dst, entry, use_short_path, pretty_print) for dst in sorted(content_map.keys()) + for entry in content_map[dst] ], ) + "\n]\n", ) diff --git a/pkg/private/tar/build_tar.py b/pkg/private/tar/build_tar.py index 5ecfccf4..4f043f5e 100644 --- a/pkg/private/tar/build_tar.py +++ b/pkg/private/tar/build_tar.py @@ -60,6 +60,7 @@ def __init__(self, output, directory, compression, compressor, create_parents, self.preserve_mtime = preserve_mtime def __enter__(self): + self.tree_files = manifest.TreeFileTracker() self.tarfile = tar_writer.TarFileWriter( self.output, self.compression, @@ -284,6 +285,7 @@ def add_tree(self, tree_top, destpath, mode=None, ids=None, names=None): else: dest_dir = dest for dir in dirs: + self.tree_files.add_dir(dest_dir + dir, tree_top) to_write[dest_dir + dir] = None for file in sorted(files): content_path = os.path.abspath(os.path.join(root, file)) @@ -308,7 +310,7 @@ def add_tree(self, tree_top, destpath, mode=None, ids=None, names=None): ids=ids, names=names, kind=tarfile.DIRTYPE) - else: + elif self.tree_files.add_file(path, content_path, tree_top): # If mode is unspecified, derive the mode from the file's mode. if mode is None: f_mode = 0o755 if os.access(content_path, os.X_OK) else 0o644 diff --git a/pkg/private/zip/build_zip.py b/pkg/private/zip/build_zip.py index 53af08cc..28c86bd7 100644 --- a/pkg/private/zip/build_zip.py +++ b/pkg/private/zip/build_zip.py @@ -93,6 +93,7 @@ def __init__(self, output_path: str, time_stamp: int, default_mode: int, compres self.output_path = output_path self.time_stamp = time_stamp self.default_mode = default_mode + self.tree_files = manifest.TreeFileTracker() compressions = { "deflated": zipfile.ZIP_DEFLATED, "lzma": zipfile.ZIP_LZMA, @@ -224,6 +225,7 @@ def add_tree(self, tree_top: str, destpath: str, mode: int): dest_dir = dest + rel_path_from_top + '/' else: dest_dir = dest + self.tree_files.add_dir(dest_dir, tree_top) to_write[dest_dir] = None for file in files: content_path = os.path.abspath(os.path.join(root, file)) @@ -239,6 +241,8 @@ def add_tree(self, tree_top: str, destpath: str, mode: int): for path in sorted(to_write.keys()): content_path = to_write[path] if content_path: + if not self.tree_files.add_file(path, content_path, tree_top): + continue # If mode is unspecified, derive the mode from the file's mode. if mode is None: f_mode = "0o755" if os.access(content_path, os.X_OK) else self.default_mode @@ -253,6 +257,8 @@ def add_tree(self, tree_top: str, destpath: str, mode: int): dir_path = path if not dir_path.endswith('/'): dir_path += '/' + if dir_path in self.zip_file.NameToInfo: + continue entry_info = self.make_zipinfo(path=dir_path, mode="0o755") entry_info.compress_type = zipfile.ZIP_STORED # Set directory bits @@ -261,10 +267,16 @@ def add_tree(self, tree_top: str, destpath: str, mode: int): def _load_manifest(prefix, manifest_path): manifest_map = {} + shared_dest_trees = [] for entry in manifest.read_entries_from(manifest_path): entry.dest = _combine_paths(prefix, entry.dest) - manifest_map[entry.dest] = entry + existing = manifest_map.get(entry.dest) + if (entry.type == manifest.ENTRY_IS_TREE and existing is not None and + existing.type == manifest.ENTRY_IS_TREE): + shared_dest_trees.append(entry) + else: + manifest_map[entry.dest] = entry # We modify the dictionary as we're iterating over it, so we need to listify # the keys here. @@ -288,7 +300,7 @@ def _load_manifest(prefix, manifest_path): origin = "parent directory of {}".format(manifest_map[dest].origin), ) - return sorted(manifest_map.values(), key = lambda x: x.dest) + return sorted(list(manifest_map.values()) + shared_dest_trees, key = lambda x: x.dest) def main(args): unix_ts = max(ZIP_EPOCH, args.timestamp) diff --git a/tests/BUILD b/tests/BUILD index 93b4626c..adb900aa 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -131,6 +131,27 @@ cc_binary( ], ) +py_test( + name = "shared_tree_dest_test", + srcs = ["shared_tree_dest_test.py"], + data = [ + "//pkg/private/tar:build_tar", + "//pkg/private/zip:build_zip", + ], + env = { + "BUILD_TAR": "$(rlocationpath //pkg/private/tar:build_tar)", + "BUILD_ZIP": "$(rlocationpath //pkg/private/zip:build_zip)", + }, + imports = [".."], + python_version = "PY3", + srcs_version = "PY3", + tags = ["no-windows"], + deps = [ + "//pkg/private:manifest", + "@rules_python//python/runfiles", + ], +) + py_test( name = "helpers_test", srcs = ["helpers_test.py"], diff --git a/tests/install/BUILD b/tests/install/BUILD index 81019397..4ef572be 100644 --- a/tests/install/BUILD +++ b/tests/install/BUILD @@ -27,6 +27,8 @@ py_test( data = [ ":test_installer", ":test_installer_flag", + ":test_installer_shared_tree_conflict", + ":test_installer_shared_tree_type_conflict", "@mappings_test_external_repo//pkg:install_cross_repo", ], imports = ["../.."], @@ -131,3 +133,54 @@ pkg_install( ], destdir_flag = ":install_dir", ) + +directory( + name = "shared_dest_tree_a", + contents = "a", + filenames = ["x.h"], +) + +directory( + name = "shared_dest_tree_b", + contents = "b", + filenames = ["x.h"], +) + +pkg_files( + name = "shared_dest_tree_a_files", + srcs = [":shared_dest_tree_a"], + renames = {":shared_dest_tree_a": "include"}, +) + +pkg_files( + name = "shared_dest_tree_b_files", + srcs = [":shared_dest_tree_b"], + renames = {":shared_dest_tree_b": "include"}, +) + +pkg_install( + name = "test_installer_shared_tree_conflict", + srcs = [ + ":shared_dest_tree_a_files", + ":shared_dest_tree_b_files", + ], +) + +directory( + name = "shared_dest_tree_dir", + filenames = ["x.h/y"], +) + +pkg_files( + name = "shared_dest_tree_dir_files", + srcs = [":shared_dest_tree_dir"], + renames = {":shared_dest_tree_dir": "include"}, +) + +pkg_install( + name = "test_installer_shared_tree_type_conflict", + srcs = [ + ":shared_dest_tree_a_files", + ":shared_dest_tree_dir_files", + ], +) diff --git a/tests/install/test.py b/tests/install/test.py index e6e35a6a..007f73b3 100644 --- a/tests/install/test.py +++ b/tests/install/test.py @@ -257,6 +257,32 @@ def test_overwrite_existing_symlink(self): self.assertEqual(os.readlink(link), "fake.so.1.2.3") +class SharedTreeConflictTest(PkgInstallTestBase): + """Tests that trees providing the same path fail.""" + + def test_conflict(self): + result = subprocess.run([ + self.runfiles.Rlocation(f"rules_pkg/tests/install/test_installer_shared_tree_conflict{self._extension}"), + "--destdir", self.installdir / "conflict", + ], + env=self.runfiles.EnvVars(), + capture_output=True, + text=True) + self.assertNotEqual(result.returncode, 0) + self.assertIn("Conflicting path", result.stderr) + + def test_type_conflict(self): + result = subprocess.run([ + self.runfiles.Rlocation(f"rules_pkg/tests/install/test_installer_shared_tree_type_conflict{self._extension}"), + "--destdir", self.installdir / "type_conflict", + ], + env=self.runfiles.EnvVars(), + capture_output=True, + text=True) + self.assertNotEqual(result.returncode, 0) + self.assertIn("Conflicting path", result.stderr) + + class CrossRepoInstallTest(unittest.TestCase): """Test external repo's pkg_install can reference main repo files.""" diff --git a/tests/mappings/BUILD b/tests/mappings/BUILD index 8d6b7b60..51b0a4a0 100644 --- a/tests/mappings/BUILD +++ b/tests/mappings/BUILD @@ -100,6 +100,42 @@ manifest_golden_test( target = "all", ) +directory( + name = "tree_a", + filenames = ["a.h"], +) + +directory( + name = "tree_b", + filenames = ["b/b.h"], +) + +pkg_files( + name = "tree_a_files", + srcs = [":tree_a"], + renames = {":tree_a": "include"}, +) + +pkg_files( + name = "tree_b_files", + srcs = [":tree_b"], + renames = {":tree_b": "include"}, +) + +write_content_manifest( + name = "shared_tree_dest", + srcs = [ + ":tree_a_files", + ":tree_b_files", + ], +) + +manifest_golden_test( + name = "shared_tree_dest_test", + expected = "shared_tree_dest.manifest.golden", + target = "shared_tree_dest", +) + # # Unicode handling tests # diff --git a/tests/mappings/shared_tree_dest.manifest.golden b/tests/mappings/shared_tree_dest.manifest.golden new file mode 100644 index 00000000..04304931 --- /dev/null +++ b/tests/mappings/shared_tree_dest.manifest.golden @@ -0,0 +1,4 @@ +[ + {"type": "tree", "dest": "include", "src": "tests/mappings/tree_a", "mode": "0644", "user": null, "group": null, "uid": null, "gid": null, "origin": "@//tests/mappings:tree_a_files", "repository": "_main"}, + {"type": "tree", "dest": "include", "src": "tests/mappings/tree_b", "mode": "0644", "user": null, "group": null, "uid": null, "gid": null, "origin": "@//tests/mappings:tree_b_files", "repository": "_main"} +] diff --git a/tests/shared_tree_dest_test.py b/tests/shared_tree_dest_test.py new file mode 100644 index 00000000..c9e35c69 --- /dev/null +++ b/tests/shared_tree_dest_test.py @@ -0,0 +1,135 @@ +# Copyright 2026 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import os +import subprocess +import tempfile +import unittest + +from pkg.private import manifest +from python.runfiles import runfiles + + +def _make_tree(root, name, files): + tree = os.path.join(root, name) + for path, content in files.items(): + full = os.path.join(tree, path) + os.makedirs(os.path.dirname(full), exist_ok=True) + with open(full, "w") as f: + f.write(content) + return tree + + +def _write_manifest(root, trees): + path = os.path.join(root, "manifest.json") + with open(path, "w") as f: + json.dump([{ + "type": manifest.ENTRY_IS_TREE, + "dest": "include", + "src": tree, + "mode": "", + "user": None, + "group": None, + "uid": None, + "gid": None, + "origin": "@//:" + os.path.basename(tree), + "repository": None, + } for tree in trees], f) + return path + + +class TreeFileTrackerTest(unittest.TestCase): + + def setUp(self): + self.root = tempfile.mkdtemp(dir=os.getenv("TEST_TMPDIR")) + self.tracker = manifest.TreeFileTracker() + + def _file(self, tree, path, content): + return os.path.join(_make_tree(self.root, tree, {path: content}), path) + + def test_distinct_files(self): + self.assertTrue(self.tracker.add_file("include/a.h", self._file("a", "a.h", "a"), "a")) + self.assertTrue(self.tracker.add_file("include/b.h", self._file("b", "b.h", "b"), "b")) + + def test_identical_file(self): + self.assertTrue(self.tracker.add_file("include/x.h", self._file("a", "x.h", "same"), "a")) + self.assertFalse(self.tracker.add_file("include/x.h", self._file("b", "x.h", "same"), "b")) + + def test_conflicting_file(self): + self.tracker.add_file("include/x.h", self._file("a", "x.h", "a"), "a") + with self.assertRaisesRegex(ValueError, "Conflicting path include/x.h"): + self.tracker.add_file("include/x.h", self._file("b", "x.h", "b"), "b") + + def test_shared_dir(self): + self.tracker.add_dir("include/x", "a") + self.tracker.add_dir("include/x/", "b") + + def test_file_then_dir(self): + self.tracker.add_file("include/x", self._file("a", "x", "a"), "a") + with self.assertRaisesRegex(ValueError, "Conflicting path include/x"): + self.tracker.add_dir("include/x/", "b") + + def test_dir_then_file(self): + self.tracker.add_dir("include/x", "a") + with self.assertRaisesRegex(ValueError, "Conflicting path include/x"): + self.tracker.add_file("include/x", self._file("b", "x", "b"), "b") + + +class WriterConflictTest(unittest.TestCase): + + def setUp(self): + self.runfiles = runfiles.Create() + self.root = tempfile.mkdtemp(dir=os.getenv("TEST_TMPDIR")) + + def _run(self, binary, args): + env = dict(os.environ) + env.update(self.runfiles.EnvVars()) + path = self.runfiles.Rlocation(os.environ[binary]) + self.assertIsNotNone(path, binary) + return subprocess.run( + [path] + args, + env=env, capture_output=True, text=True) + + def _check(self, binary, make_args): + out = os.path.join(self.root, "out") + ok = _write_manifest(self.root, [ + _make_tree(self.root, "ok_a", {"a.h": "a", "x.h": "same"}), + _make_tree(self.root, "ok_b", {"b/b.h": "b", "x.h": "same"}), + ]) + result = self._run(binary, make_args(out, ok)) + self.assertEqual(result.returncode, 0, result.stderr) + + for name, trees in (("file", ({"x.h": "a"}, {"x.h": "b"})), + ("file_dir", ({"x": "a"}, {"x/y": "b"})), + ("dir_file", ({"x/y": "b"}, {"x": "a"}))): + conflict = _write_manifest(self.root, [ + _make_tree(self.root, name + "_a", trees[0]), + _make_tree(self.root, name + "_b", trees[1]), + ]) + result = self._run(binary, make_args(out, conflict)) + self.assertNotEqual(result.returncode, 0, name) + self.assertIn("Conflicting path include/x", result.stderr, name) + + def test_tar(self): + self._check("BUILD_TAR", + lambda out, m: ["--output", out + ".tar", "--directory", "/", "--manifest", m]) + + def test_zip(self): + self._check("BUILD_ZIP", + lambda out, m: ["--output", out + ".zip", "-m", "0644", "-c", "deflated", "-l", "6", "--manifest", m]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/tar/BUILD b/tests/tar/BUILD index ec020bbb..4ea5aa4b 100644 --- a/tests/tar/BUILD +++ b/tests/tar/BUILD @@ -386,6 +386,45 @@ verify_archive_test( target = ":test-tar-with-only-runfiles", ) +directory( + name = "shared_dest_tree_a", + filenames = ["a.h"], +) + +directory( + name = "shared_dest_tree_b", + filenames = ["b/b.h"], +) + +pkg_files( + name = "shared_dest_tree_a_files", + srcs = [":shared_dest_tree_a"], + renames = {":shared_dest_tree_a": "include"}, +) + +pkg_files( + name = "shared_dest_tree_b_files", + srcs = [":shared_dest_tree_b"], + renames = {":shared_dest_tree_b": "include"}, +) + +pkg_tar( + name = "test-tar-shared-tree-dest", + srcs = [ + ":shared_dest_tree_a_files", + ":shared_dest_tree_b_files", + ], +) + +verify_archive_test( + name = "shared_tree_dest_test", + must_contain = [ + "include/a.h", + "include/b/b.h", + ], + target = ":test-tar-shared-tree-dest", +) + pkg_tar( name = "test-tar-remap-runfiles-base-path", srcs = [ diff --git a/tests/zip/BUILD b/tests/zip/BUILD index c2c42761..866e2de1 100644 --- a/tests/zip/BUILD +++ b/tests/zip/BUILD @@ -15,7 +15,7 @@ load("@bazel_skylib//rules:copy_file.bzl", "copy_file") load("@rules_python//python:defs.bzl", "py_library", "py_test") -load("//pkg:mappings.bzl", "pkg_attributes", "pkg_mkdirs", "pkg_mklink") +load("//pkg:mappings.bzl", "pkg_attributes", "pkg_files", "pkg_mkdirs", "pkg_mklink") load("//pkg:zip.bzl", "pkg_zip") load("//tests:my_package_name.bzl", "my_package_naming") load("//tests/util:defs.bzl", "directory") @@ -135,6 +135,36 @@ pkg_zip( srcs = [":generate_tree"], ) +directory( + name = "shared_dest_tree_a", + filenames = ["a.h"], +) + +directory( + name = "shared_dest_tree_b", + filenames = ["b/b.h"], +) + +pkg_files( + name = "shared_dest_tree_a_files", + srcs = [":shared_dest_tree_a"], + renames = {":shared_dest_tree_a": "include"}, +) + +pkg_files( + name = "shared_dest_tree_b_files", + srcs = [":shared_dest_tree_b"], + renames = {":shared_dest_tree_b": "include"}, +) + +pkg_zip( + name = "test_zip_shared_tree_dest", + srcs = [ + ":shared_dest_tree_a_files", + ":shared_dest_tree_b_files", + ], +) + pkg_zip( name = "test_zip_permissions", srcs = [ @@ -305,6 +335,7 @@ py_test( ":test_zip_permissions.zip", ":test_zip_stored", ":test_zip_timestamp.zip", + ":test_zip_shared_tree_dest.zip", ":test_zip_tree.zip", ], python_version = "PY3", diff --git a/tests/zip/zip_test.py b/tests/zip/zip_test.py index c7efebb3..5c3cd602 100644 --- a/tests/zip/zip_test.py +++ b/tests/zip/zip_test.py @@ -121,6 +121,14 @@ def test_zip_tree(self): {"filename": "generate_tree/b/e"}, ]) + def test_zip_shared_tree_dest(self): + self.assertZipFileContent("test_zip_shared_tree_dest.zip", [ + {"filename": "include/", "isdir": True, "attr": 0o755}, + {"filename": "include/a.h", "attr": 0o644}, + {"filename": "include/b/", "isdir": True, "attr": 0o755}, + {"filename": "include/b/b.h", "attr": 0o644}, + ]) + def test_compression_deflated(self): if sys.version_info >= (3, 7): self.assertZipFileContent("test_zip_deflated_level_3.zip", [