Skip to content

Commit 7628c22

Browse files
committed
add tests
1 parent de1987b commit 7628c22

5 files changed

Lines changed: 100 additions & 1 deletion

File tree

python/private/pypi/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
16+
load("//python:py_library.bzl", "py_library")
1617

1718
package(default_visibility = ["//:__subpackages__"])
1819

@@ -377,6 +378,12 @@ bzl_library(
377378
],
378379
)
379380

381+
py_library(
382+
name = "repack_whl",
383+
srcs = ["repack_whl.py"],
384+
deps = ["//tools:wheelmaker"],
385+
)
386+
380387
bzl_library(
381388
name = "requirements_files_by_platform_bzl",
382389
srcs = ["requirements_files_by_platform.bzl"],

python/private/pypi/repack_whl.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444
_DISTINFO = "dist-info"
4545

4646

47+
def _has_all_quoted_filenames(record_contents: str) -> bool:
48+
"""Check if all filenames in the RECORD are quoted.
49+
50+
Some wheels (like torch) have all filenames quoted in their RECORD file.
51+
We detect this to preserve the quoting style when repacking.
52+
"""
53+
lines = record_contents.splitlines()
54+
return all(line.startswith('"') for line in lines)
55+
56+
4757
def _unidiff_output(expected, actual, record):
4858
"""
4959
Helper function. Returns a string containing the unified diff of two
@@ -151,7 +161,7 @@ def main(sys_argv):
151161
logging.debug(f"Found dist-info dir: {distinfo_dir}")
152162
record_path = distinfo_dir / "RECORD"
153163
record_contents = record_path.read_text() if record_path.exists() else ""
154-
quote_files = all(line.startswith('"') for line in record_contents.splitlines())
164+
quote_files = _has_all_quoted_filenames(record_contents)
155165
distribution_prefix = distinfo_dir.with_suffix("").name
156166

157167
with _WhlFile(

tests/pypi/repack_whl/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//python:py_test.bzl", "py_test")
2+
3+
py_test(
4+
name = "repack_whl_test",
5+
size = "small",
6+
srcs = ["repack_whl_test.py"],
7+
deps = ["//python/private/pypi:repack_whl"],
8+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
3+
from python.private.pypi import repack_whl
4+
5+
6+
class HasAllQuotedFilenamesTest(unittest.TestCase):
7+
"""Tests for _has_all_quoted_filenames detection logic."""
8+
9+
def test_all_quoted(self) -> None:
10+
"""Returns True when all lines start with quotes (torch-style)."""
11+
record = """\
12+
"torch/__init__.py",sha256=abc,123
13+
"torch/utils.py",sha256=def,456
14+
"torch-2.0.0.dist-info/WHEEL",sha256=ghi,789
15+
"""
16+
self.assertTrue(repack_whl._has_all_quoted_filenames(record))
17+
18+
def test_none_quoted(self) -> None:
19+
"""Returns False when no lines are quoted (standard style)."""
20+
record = """\
21+
torch/__init__.py,sha256=abc,123
22+
torch/utils.py,sha256=def,456
23+
torch-2.0.0.dist-info/WHEEL,sha256=ghi,789
24+
"""
25+
self.assertFalse(repack_whl._has_all_quoted_filenames(record))
26+
27+
def test_mixed_quoting(self) -> None:
28+
"""Returns False when only some lines are quoted."""
29+
record = """\
30+
"file,with,commas.py",sha256=abc,123
31+
normal_file.py,sha256=def,456
32+
"""
33+
self.assertFalse(repack_whl._has_all_quoted_filenames(record))
34+
35+
36+
if __name__ == "__main__":
37+
unittest.main()

tests/tools/wheelmaker_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,45 @@
1+
import io
12
import unittest
23

34
import tools.wheelmaker as wheelmaker
45

56

7+
class QuoteAllFilenamesTest(unittest.TestCase):
8+
"""Tests for quote_all_filenames behavior in _WhlFile.
9+
10+
Some wheels (like torch) have all filenames quoted in their RECORD file.
11+
When repacking, we preserve this style to minimize diffs.
12+
"""
13+
14+
def _make_whl_file(self, quote_all: bool) -> wheelmaker._WhlFile:
15+
"""Create a _WhlFile instance for testing."""
16+
buf = io.BytesIO()
17+
return wheelmaker._WhlFile(
18+
buf,
19+
mode="w",
20+
distribution_prefix="test-1.0.0",
21+
quote_all_filenames=quote_all,
22+
)
23+
24+
def test_quote_all_quotes_simple_filenames(self) -> None:
25+
"""When quote_all_filenames=True, all filenames are quoted."""
26+
whl = self._make_whl_file(quote_all=True)
27+
self.assertEqual(whl._quote_filename("foo/bar.py"), '"foo/bar.py"')
28+
29+
def test_quote_all_false_leaves_simple_filenames_unquoted(self) -> None:
30+
"""When quote_all_filenames=False, simple filenames stay unquoted."""
31+
whl = self._make_whl_file(quote_all=False)
32+
self.assertEqual(whl._quote_filename("foo/bar.py"), "foo/bar.py")
33+
34+
def test_quote_all_quotes_filenames_with_commas(self) -> None:
35+
"""Filenames with commas are always quoted, regardless of quote_all_filenames."""
36+
whl = self._make_whl_file(quote_all=True)
37+
self.assertEqual(whl._quote_filename("foo,bar/baz.py"), '"foo,bar/baz.py"')
38+
39+
whl = self._make_whl_file(quote_all=False)
40+
self.assertEqual(whl._quote_filename("foo,bar/baz.py"), '"foo,bar/baz.py"')
41+
42+
643
class ArcNameFromTest(unittest.TestCase):
744
def test_arcname_from(self) -> None:
845
# (name, distribution_prefix, strip_path_prefixes, want) tuples

0 commit comments

Comments
 (0)