Skip to content

Commit f8f0a90

Browse files
committed
tests: support whl_from_dir_repo on Windows
The `whl_from_dir_repo` repository rule previously relied on the Unix `zip` utility to create wheels. Because this command isn't natively available on Windows, any tests that depended on repositories generated by this rule had to be explicitly skipped on Windows hosts. To fix this and expand our test coverage, this adds a native Windows fallback. When running on Windows, the rule now invokes a helper PowerShell script that uses .NET compression APIs to create the archive. This script ensures the resulting wheel remains uncompressed and uses zeroed-out timestamps to match the deterministic behavior of the original `zip -0X` command. With this constraint removed, the Unix-only compatibility flags (`SUPPORTS_BZLMOD_UNIXY`) have been dropped, enabling several namespace package and wheel-related integration tests to finally run on Windows.
1 parent 1567357 commit f8f0a90

7 files changed

Lines changed: 94 additions & 27 deletions

File tree

tests/implicit_namespace_packages/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
load("//python:py_test.bzl", "py_test")
2-
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD_UNIXY")
2+
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD")
33

44
py_test(
55
name = "namespace_packages_test",
66
srcs = ["namespace_packages_test.py"],
7-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
7+
target_compatible_with = SUPPORTS_BZLMOD,
88
deps = [
99
"@implicit_namespace_ns_sub1//:pkg",
1010
"@implicit_namespace_ns_sub2//:pkg",

tests/pypi/whl_library/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
load("//python:py_test.bzl", "py_test")
2-
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD_UNIXY")
2+
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD")
33

44
py_test(
55
name = "whl_library_extras_test",
66
srcs = ["whl_library_extras_test.py"],
7-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
7+
target_compatible_with = SUPPORTS_BZLMOD,
88
deps = [
99
"@whl_library_extras_direct_dep//:pkg",
1010
],

tests/support/support.bzl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ SUPPORTS_BOOTSTRAP_SCRIPT = select({
3535
"//conditions:default": [],
3636
})
3737

38-
SUPPORTS_BZLMOD_UNIXY = select({
39-
"@platforms//os:windows": ["@platforms//:incompatible"],
40-
"//conditions:default": [],
41-
}) if BZLMOD_ENABLED else ["@platforms//:incompatible"]
38+
SUPPORTS_BZLMOD = [] if BZLMOD_ENABLED else ["@platforms//:incompatible"]
4239

4340
NOT_WINDOWS = select({
4441
"@platforms//os:windows": ["@platforms//:incompatible"],

tests/support/whl_from_dir/whl_from_dir_repo.bzl

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,41 @@ def _whl_from_dir_repo(rctx):
1111
rctx.watch_tree(root)
1212

1313
output = rctx.path(rctx.attr.output)
14-
repo_utils.execute_checked(
15-
rctx,
16-
# cd to root so zip recursively takes everything there.
17-
working_directory = str(root),
18-
op = "WhlFromDir",
19-
arguments = [
20-
"zip",
21-
"-0", # Skip compressing
22-
"-X", # Don't store file time or metadata
23-
str(output),
24-
"-r",
25-
".",
26-
],
27-
)
14+
if repo_utils.get_platforms_os_name(rctx) == "windows":
15+
powershell_exe = rctx.which("powershell.exe") or rctx.which("powershell")
16+
if not powershell_exe:
17+
fail("powershell not found on PATH")
18+
19+
zip_script = rctx.path(rctx.attr._zip_script)
20+
21+
repo_utils.execute_checked(
22+
rctx,
23+
op = "WhlFromDir",
24+
arguments = [
25+
powershell_exe,
26+
"-NoProfile",
27+
"-File",
28+
str(zip_script),
29+
str(output),
30+
str(root),
31+
],
32+
# zip.ps1 handles changing the directory.
33+
)
34+
else:
35+
repo_utils.execute_checked(
36+
rctx,
37+
# cd to root so zip recursively takes everything there.
38+
working_directory = str(root),
39+
op = "WhlFromDir",
40+
arguments = [
41+
"zip",
42+
"-0", # Skip compressing
43+
"-X", # Don't store file time or metadata
44+
str(output),
45+
"-r",
46+
".",
47+
],
48+
)
2849
rctx.file("BUILD.bazel", 'exports_files(glob(["*"]))')
2950

3051
whl_from_dir_repo = repository_rule(
@@ -46,5 +67,9 @@ A file whose directory will be put into the output wheel. All files
4667
are included verbatim.
4768
""",
4869
),
70+
"_zip_script": attr.label(
71+
default = "//tests/support/whl_from_dir:zip.ps1",
72+
allow_single_file = True,
73+
),
4974
},
5075
)

tests/support/whl_from_dir/zip.ps1

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
param (
2+
[Parameter(Position=0, Mandatory=$true)]
3+
[string]$Output,
4+
5+
[Parameter(Position=1, Mandatory=$true)]
6+
[string]$Root
7+
)
8+
9+
Add-Type -AssemblyName System.IO.Compression
10+
11+
$fixedTime = [datetime]"1980-01-01T00:00:00"
12+
$RootFull = (Resolve-Path $Root).Path
13+
14+
$stream = [System.IO.File]::Open($Output, [System.IO.FileMode]::Create)
15+
try {
16+
$archive = [System.IO.Compression.ZipArchive]::new($stream, [System.IO.Compression.ZipArchiveMode]::Create)
17+
try {
18+
$files = Get-ChildItem -Path $RootFull -Recurse -File
19+
foreach ($file in $files) {
20+
# Relativize path and normalize separators
21+
$relPath = $file.FullName.Substring($RootFull.Length).TrimStart('\', '/')
22+
$relPath = $relPath -replace '\\', '/'
23+
24+
$entry = $archive.CreateEntry($relPath, [System.IO.Compression.CompressionLevel]::NoCompression)
25+
$entry.LastWriteTime = $fixedTime
26+
27+
$entryStream = $entry.Open()
28+
try {
29+
$fileStream = [System.IO.File]::OpenRead($file.FullName)
30+
try {
31+
$fileStream.CopyTo($entryStream)
32+
} finally {
33+
$fileStream.Dispose()
34+
}
35+
} finally {
36+
$entryStream.Dispose()
37+
}
38+
}
39+
} finally {
40+
$archive.Dispose()
41+
}
42+
} finally {
43+
$stream.Dispose()
44+
}
45+

tests/venv_site_packages_libs/app_files_building/app_files_building_tests.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ load("//python:py_library.bzl", "py_library")
77
load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility
88
load("//python/private:py_info.bzl", "VenvSymlinkEntry", "VenvSymlinkKind") # buildifier: disable=bzl-visibility
99
load("//python/private:venv_runfiles.bzl", "build_link_map", "get_venv_symlinks") # buildifier: disable=bzl-visibility
10-
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD_UNIXY")
10+
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD")
1111

1212
def _empty_files_impl(ctx):
1313
files = []
@@ -425,7 +425,7 @@ def _test_optimized_grouping_pkgutil_whls(name):
425425
"@pkgutil_nspkg1//:pkg",
426426
"@pkgutil_nspkg2//:pkg",
427427
],
428-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
428+
target_compatible_with = SUPPORTS_BZLMOD,
429429
)
430430
analysis_test(
431431
name = name,
@@ -435,7 +435,7 @@ def _test_optimized_grouping_pkgutil_whls(name):
435435
labels.VENVS_SITE_PACKAGES: "yes",
436436
},
437437
attr_values = dict(
438-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
438+
target_compatible_with = SUPPORTS_BZLMOD,
439439
),
440440
)
441441

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
load("//python:py_test.bzl", "py_test")
2-
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD_UNIXY")
2+
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD")
33

44
py_test(
55
name = "verify_files_test",
66
srcs = ["verify_files_test.py"],
7-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
7+
target_compatible_with = SUPPORTS_BZLMOD,
88
deps = ["@somepkg_with_build_files//:pkg"],
99
)

0 commit comments

Comments
 (0)