Skip to content

Commit 30343d4

Browse files
committed
Light refactor; add test
1 parent 6864dec commit 30343d4

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

tests/tools/BUILD.bazel

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2025 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
load("//python:py_test.bzl", "py_test")
15+
16+
licenses(["notice"])
17+
18+
py_test(
19+
name = "wheelmaker_test",
20+
size = "small",
21+
srcs = ["wheelmaker_test.py"],
22+
deps = ["//tools:wheelmaker"],
23+
)

tests/tools/wheelmaker_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
3+
import tools.wheelmaker as wheelmaker
4+
5+
6+
class ArcNameFromTest(unittest.TestCase):
7+
8+
def test_arcname_from(self) -> None:
9+
10+
# (name, distribution_prefix, strip_path_prefixes, want) tuples
11+
checks = [
12+
("foo/bar/baz/file.py", "", [], "foo/bar/baz/file.py"),
13+
("foo/bar/baz/file.py", "", ["foo"], "/bar/baz/file.py"),
14+
("foo/bar/baz/file.py", "", ["foo/"], "bar/baz/file.py"),
15+
("foo/bar/baz/file.py", "", ["foo/bar"], "/baz/file.py"),
16+
("foo/bar/baz/file.py", "", ["foo/bar", "baz"], "/baz/file.py"),
17+
("foo/bar/baz/file.py", "", ["foo", "bar"], "/bar/baz/file.py"),
18+
("foo/bar/baz/file.py", "", ["baz", "foo/bar"], "/baz/file.py"),
19+
]
20+
for name, prefix, strip, want in checks:
21+
with self.subTest(name=name, distribution_prefix=prefix, strip_path_prefixes=strip, want=want):
22+
got = wheelmaker.arcname_from(name=name, distribution_prefix=prefix, strip_path_prefixes=strip)
23+
self.assertEqual(got, want)
24+
25+
26+
if __name__ == "__main__":
27+
unittest.main()

tools/wheelmaker.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ def normalize_pep440(version):
9898
return str(packaging.version.Version(f"0+{sanitized}"))
9999

100100

101+
def arcname_from(name: str, distribution_prefix: str, strip_path_prefixes:list[str] | None = None):
102+
# Always use unix path separators.
103+
normalized_arcname = name.replace(os.path.sep, "/")
104+
# Don't manipulate names filenames in the .distinfo or .data directories.
105+
if distribution_prefix and normalized_arcname.startswith(distribution_prefix):
106+
return normalized_arcname
107+
for prefix in strip_path_prefixes:
108+
if normalized_arcname.startswith(prefix):
109+
return normalized_arcname[len(prefix) :]
110+
111+
return normalized_arcname
112+
113+
101114
class _WhlFile(zipfile.ZipFile):
102115
def __init__(
103116
self,
@@ -126,18 +139,6 @@ def data_path(self, basename):
126139
def add_file(self, package_filename, real_filename):
127140
"""Add given file to the distribution."""
128141

129-
def arcname_from(name):
130-
# Always use unix path separators.
131-
normalized_arcname = name.replace(os.path.sep, "/")
132-
# Don't manipulate names filenames in the .distinfo or .data directories.
133-
if normalized_arcname.startswith(self._distribution_prefix):
134-
return normalized_arcname
135-
for prefix in self._strip_path_prefixes:
136-
if normalized_arcname.startswith(prefix):
137-
return normalized_arcname[len(prefix) :]
138-
139-
return normalized_arcname
140-
141142
if os.path.isdir(real_filename):
142143
directory_contents = os.listdir(real_filename)
143144
for file_ in directory_contents:

0 commit comments

Comments
 (0)