Skip to content

Commit

Permalink
pkg_tar should not prefix tree artifacts with ./ (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
clint-stripe authored Mar 16, 2023
1 parent f117c63 commit edd4d3d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/private/tar/build_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ def add_tree(self, tree_top, destpath, mode=None, ids=None, names=None):

# Again, we expect /-style paths.
dest = normpath(dest)
# normpath may be ".", and dest paths should not start with "./"
dest = '' if dest == '.' else dest + '/'

if ids is None:
ids = (0, 0)
if names is None:
Expand All @@ -246,9 +249,9 @@ def add_tree(self, tree_top, destpath, mode=None, ids=None, names=None):
dirs = sorted(dirs)
rel_path_from_top = root[len(tree_top):].lstrip('/')
if rel_path_from_top:
dest_dir = dest + '/' + rel_path_from_top + '/'
dest_dir = dest + rel_path_from_top + '/'
else:
dest_dir = dest + '/'
dest_dir = dest
for dir in dirs:
to_write[dest_dir + dir] = None
for file in sorted(files):
Expand Down
19 changes: 19 additions & 0 deletions tests/tar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ py_test(
":test-tar-tree-artifact",
":test-tar-tree-artifact-noroot",
":test-tar-with-runfiles",
":test-tree-input-with-strip-prefix",
"//tests:testdata/tar_test.tar",
"//tests:testdata/tar_test.tar.bz2",
"//tests:testdata/tar_test.tar.gz",
Expand Down Expand Up @@ -471,3 +472,21 @@ pkg_tar(
":relative_links_tar",
],
)

directory(
name = "generate_tree_with_prefix",
contents = "hello there",
filenames = [
"a/a",
"a/b",
],
outdir = "tree_prefix_to_strip",
)

pkg_tar(
name = "test-tree-input-with-strip-prefix",
srcs = [
":generate_tree_with_prefix",
],
strip_prefix = "tree_prefix_to_strip",
)
8 changes: 8 additions & 0 deletions tests/tar/pkg_tar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ def test_tar_leading_dotslash(self):
]
self.assertTarFileContent('test_tar_leading_dotslash.tar', content)

def test_tar_with_tree_artifact_and_strip_prefix(self):
content = [
{'name': 'a', 'isdir': True},
{'name': 'a/a'},
{'name': 'a/b'},
]
self.assertTarFileContent('test-tree-input-with-strip-prefix.tar', content)


if __name__ == '__main__':
unittest.main()

0 comments on commit edd4d3d

Please sign in to comment.