Skip to content

Commit 292ed6f

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 292ed6f

7 files changed

Lines changed: 73 additions & 31 deletions

File tree

tests/implicit_namespace_packages/BUILD.bazel

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

43
py_test(
54
name = "namespace_packages_test",
65
srcs = ["namespace_packages_test.py"],
7-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
86
deps = [
97
"@implicit_namespace_ns_sub1//:pkg",
108
"@implicit_namespace_ns_sub2//:pkg",

tests/pypi/whl_library/BUILD.bazel

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

43
py_test(
54
name = "whl_library_extras_test",
65
srcs = ["whl_library_extras_test.py"],
7-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
86
deps = [
97
"@whl_library_extras_direct_dep//:pkg",
108
],

tests/support/support.bzl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
# places.
2121

2222
load("@rules_python_internal//:rules_python_config.bzl", "config")
23-
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility
2423

2524
PY_TOOLCHAINS = str(Label("//tests/support/py_toolchains:all"))
2625
CC_TOOLCHAIN = str(Label("//tests/support/cc_toolchains:all"))
@@ -35,11 +34,6 @@ SUPPORTS_BOOTSTRAP_SCRIPT = select({
3534
"//conditions:default": [],
3635
})
3736

38-
SUPPORTS_BZLMOD_UNIXY = select({
39-
"@platforms//os:windows": ["@platforms//:incompatible"],
40-
"//conditions:default": [],
41-
}) if BZLMOD_ENABLED else ["@platforms//:incompatible"]
42-
4337
NOT_WINDOWS = select({
4438
"@platforms//os:windows": ["@platforms//:incompatible"],
4539
"//conditions:default": [],

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
$archive = [System.IO.Compression.ZipArchive]::new($stream, [System.IO.Compression.ZipArchiveMode]::Create)
16+
17+
$files = Get-ChildItem -Path $RootFull -Recurse -File
18+
foreach ($file in $files) {
19+
# Relativize path and normalize separators
20+
$relPath = $file.FullName.Substring($RootFull.Length).TrimStart('\', '/')
21+
$relPath = $relPath -replace '\\', '/'
22+
23+
$entry = $archive.CreateEntry($relPath, [System.IO.Compression.CompressionLevel]::NoCompression)
24+
$entry.LastWriteTime = $fixedTime
25+
26+
$entryStream = $entry.Open()
27+
$fileStream = [System.IO.File]::OpenRead($file.FullName)
28+
$fileStream.CopyTo($entryStream)
29+
$fileStream.Close()
30+
$entryStream.Close()
31+
}
32+
33+
$archive.Dispose()
34+
$stream.Close()

tests/venv_site_packages_libs/app_files_building/app_files_building_tests.bzl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ 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")
1110

1211
def _empty_files_impl(ctx):
1312
files = []
@@ -425,7 +424,6 @@ def _test_optimized_grouping_pkgutil_whls(name):
425424
"@pkgutil_nspkg1//:pkg",
426425
"@pkgutil_nspkg2//:pkg",
427426
],
428-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
429427
)
430428
analysis_test(
431429
name = name,
@@ -434,9 +432,6 @@ def _test_optimized_grouping_pkgutil_whls(name):
434432
config_settings = {
435433
labels.VENVS_SITE_PACKAGES: "yes",
436434
},
437-
attr_values = dict(
438-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
439-
),
440435
)
441436

442437
_tests.append(_test_optimized_grouping_pkgutil_whls)
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
load("//python:py_test.bzl", "py_test")
2-
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD_UNIXY")
32

43
py_test(
54
name = "verify_files_test",
65
srcs = ["verify_files_test.py"],
7-
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
86
deps = ["@somepkg_with_build_files//:pkg"],
97
)

0 commit comments

Comments
 (0)