Skip to content

Commit e9cef54

Browse files
committed
code reuse and turn off warning printing in tests
1 parent 2588281 commit e9cef54

File tree

6 files changed

+129
-195
lines changed

6 files changed

+129
-195
lines changed

python/private/pypi/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ bzl_library(
111111
deps = [
112112
":evaluate_markers_bzl",
113113
":hub_builder_bzl",
114+
":platform_bzl",
114115
":hub_repository_bzl",
115116
":parse_whl_name_bzl",
116117
":pep508_env_bzl",

python/private/pypi/extension.bzl

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ load(":parse_whl_name.bzl", "parse_whl_name")
2828
load(":pep508_env.bzl", "env")
2929
load(":pip_repository_attrs.bzl", "ATTRS")
3030
load(":simpleapi_download.bzl", "simpleapi_download")
31+
load(":platform.bzl", _plat = "platform")
3132
load(":whl_library.bzl", "whl_library")
3233

3334
def _whl_mods_impl(whl_mods_dict):
@@ -55,31 +56,6 @@ def _whl_mods_impl(whl_mods_dict):
5556
whl_mods = whl_mods,
5657
)
5758

58-
def _plat(*, name, arch_name, os_name, config_settings = [], env = {}, marker = "", whl_abi_tags = [], whl_platform_tags = []):
59-
# NOTE @aignas 2025-07-08: the least preferred is the first item in the list
60-
if "any" not in whl_platform_tags:
61-
# the lowest priority one needs to be the first one
62-
whl_platform_tags = ["any"] + whl_platform_tags
63-
64-
whl_abi_tags = whl_abi_tags or ["abi3", "cp{major}{minor}"]
65-
if "none" not in whl_abi_tags:
66-
# the lowest priority one needs to be the first one
67-
whl_abi_tags = ["none"] + whl_abi_tags
68-
69-
return struct(
70-
name = name,
71-
arch_name = arch_name,
72-
os_name = os_name,
73-
config_settings = config_settings,
74-
env = {
75-
# defaults for env
76-
"implementation_name": "cpython",
77-
} | env,
78-
marker = marker,
79-
whl_abi_tags = whl_abi_tags,
80-
whl_platform_tags = whl_platform_tags,
81-
)
82-
8359
def _configure(config, *, override = False, **kwargs):
8460
"""Set the value in the config if the value is provided"""
8561
env = kwargs.get("env")

python/private/pypi/platform.bzl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""A common platform structure for using internally."""
2+
3+
def platform(*, name, arch_name, os_name, config_settings = [], env = {}, marker = "", whl_abi_tags = [], whl_platform_tags = []):
4+
"""A platform structure for using internally.
5+
6+
Args:
7+
name: {type}`str` the human friendly name of the platform.
8+
arch_name: {type}`str` the @platforms//cpu:<arch_name> value.
9+
os_name: {type}`str` the @platforms//os:<os_name> value.
10+
config_settings: {type}`list[Label|str]` The list of labels for selecting the
11+
platform.
12+
env: {type}`dict[str, str]` the PEP508 environment for marker evaluation.
13+
marker: {type}`str` the env marker expression that is evaluated to determine if we
14+
should use the platform. This is useful to turn on certain platforms for
15+
particular python versions.
16+
whl_abi_tags: {type}`list[str]` A list of values for matching abi tags.
17+
whl_platform_tags: {type}`list[str]` A list of values for matching platform tags.
18+
19+
Returns:
20+
struct with the necessary values for pipstar implementation.
21+
"""
22+
# NOTE @aignas 2025-07-08: the least preferred is the first item in the list
23+
if "any" not in whl_platform_tags:
24+
# the lowest priority one needs to be the first one
25+
whl_platform_tags = ["any"] + whl_platform_tags
26+
27+
whl_abi_tags = whl_abi_tags or ["abi3", "cp{major}{minor}"]
28+
if "none" not in whl_abi_tags:
29+
# the lowest priority one needs to be the first one
30+
whl_abi_tags = ["none"] + whl_abi_tags
31+
32+
return struct(
33+
name = name,
34+
arch_name = arch_name,
35+
os_name = os_name,
36+
config_settings = config_settings,
37+
env = {
38+
# defaults for env
39+
"implementation_name": "cpython",
40+
} | env,
41+
marker = marker,
42+
whl_abi_tags = whl_abi_tags,
43+
whl_platform_tags = whl_platform_tags,
44+
)

tests/pypi/extension/extension_tests.bzl

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ load("@rules_testing//lib:test_suite.bzl", "test_suite")
1818
load("@rules_testing//lib:truth.bzl", "subjects")
1919
load("//python/private/pypi:extension.bzl", "build_config", "parse_modules") # buildifier: disable=bzl-visibility
2020
load("//python/private/pypi:whl_config_setting.bzl", "whl_config_setting") # buildifier: disable=bzl-visibility
21+
load(":pip_parse.bzl", _parse = "pip_parse")
2122

2223
_tests = []
2324

@@ -133,74 +134,6 @@ def _default(
133134
whl_platform_tags = whl_platform_tags or [],
134135
)
135136

136-
def _parse(
137-
*,
138-
hub_name,
139-
python_version,
140-
add_libdir_to_library_search_path = False,
141-
auth_patterns = {},
142-
download_only = False,
143-
enable_implicit_namespace_pkgs = False,
144-
environment = {},
145-
envsubst = {},
146-
experimental_index_url = "",
147-
experimental_requirement_cycles = {},
148-
experimental_target_platforms = [],
149-
extra_hub_aliases = {},
150-
extra_pip_args = [],
151-
isolated = True,
152-
netrc = None,
153-
parse_all_requirements_files = True,
154-
pip_data_exclude = None,
155-
python_interpreter = None,
156-
python_interpreter_target = None,
157-
quiet = True,
158-
requirements_by_platform = {},
159-
requirements_darwin = None,
160-
requirements_linux = None,
161-
requirements_lock = None,
162-
requirements_windows = None,
163-
simpleapi_skip = [],
164-
timeout = 600,
165-
whl_modifications = {},
166-
**kwargs):
167-
return struct(
168-
auth_patterns = auth_patterns,
169-
add_libdir_to_library_search_path = add_libdir_to_library_search_path,
170-
download_only = download_only,
171-
enable_implicit_namespace_pkgs = enable_implicit_namespace_pkgs,
172-
environment = environment,
173-
envsubst = envsubst,
174-
experimental_index_url = experimental_index_url,
175-
experimental_requirement_cycles = experimental_requirement_cycles,
176-
experimental_target_platforms = experimental_target_platforms,
177-
extra_hub_aliases = extra_hub_aliases,
178-
extra_pip_args = extra_pip_args,
179-
hub_name = hub_name,
180-
isolated = isolated,
181-
netrc = netrc,
182-
parse_all_requirements_files = parse_all_requirements_files,
183-
pip_data_exclude = pip_data_exclude,
184-
python_interpreter = python_interpreter,
185-
python_interpreter_target = python_interpreter_target,
186-
python_version = python_version,
187-
quiet = quiet,
188-
requirements_by_platform = requirements_by_platform,
189-
requirements_darwin = requirements_darwin,
190-
requirements_linux = requirements_linux,
191-
requirements_lock = requirements_lock,
192-
requirements_windows = requirements_windows,
193-
timeout = timeout,
194-
whl_modifications = whl_modifications,
195-
# The following are covered by other unit tests
196-
experimental_extra_index_urls = [],
197-
parallel_download = False,
198-
experimental_index_url_overrides = {},
199-
simpleapi_skip = simpleapi_skip,
200-
_evaluate_markers_srcs = [],
201-
**kwargs
202-
)
203-
204137
def _test_simple(env):
205138
pypi = _parse_modules(
206139
env,

tests/pypi/extension/pip_parse.bzl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""A simple test helper"""
2+
3+
def pip_parse(
4+
*,
5+
hub_name,
6+
python_version,
7+
add_libdir_to_library_search_path = False,
8+
auth_patterns = {},
9+
download_only = False,
10+
enable_implicit_namespace_pkgs = False,
11+
environment = {},
12+
envsubst = {},
13+
experimental_index_url = "",
14+
experimental_requirement_cycles = {},
15+
experimental_target_platforms = [],
16+
extra_hub_aliases = {},
17+
extra_pip_args = [],
18+
isolated = True,
19+
netrc = None,
20+
parse_all_requirements_files = True,
21+
pip_data_exclude = None,
22+
python_interpreter = None,
23+
python_interpreter_target = None,
24+
quiet = True,
25+
requirements_by_platform = {},
26+
requirements_darwin = None,
27+
requirements_linux = None,
28+
requirements_lock = None,
29+
requirements_windows = None,
30+
simpleapi_skip = [],
31+
timeout = 600,
32+
whl_modifications = {},
33+
**kwargs):
34+
"""A simple helper for testing to simulate the PyPI extension parse tag class"""
35+
return struct(
36+
auth_patterns = auth_patterns,
37+
add_libdir_to_library_search_path = add_libdir_to_library_search_path,
38+
download_only = download_only,
39+
enable_implicit_namespace_pkgs = enable_implicit_namespace_pkgs,
40+
environment = environment,
41+
envsubst = envsubst,
42+
experimental_index_url = experimental_index_url,
43+
experimental_requirement_cycles = experimental_requirement_cycles,
44+
experimental_target_platforms = experimental_target_platforms,
45+
extra_hub_aliases = extra_hub_aliases,
46+
extra_pip_args = extra_pip_args,
47+
hub_name = hub_name,
48+
isolated = isolated,
49+
netrc = netrc,
50+
parse_all_requirements_files = parse_all_requirements_files,
51+
pip_data_exclude = pip_data_exclude,
52+
python_interpreter = python_interpreter,
53+
python_interpreter_target = python_interpreter_target,
54+
python_version = python_version,
55+
quiet = quiet,
56+
requirements_by_platform = requirements_by_platform,
57+
requirements_darwin = requirements_darwin,
58+
requirements_linux = requirements_linux,
59+
requirements_lock = requirements_lock,
60+
requirements_windows = requirements_windows,
61+
timeout = timeout,
62+
whl_modifications = whl_modifications,
63+
# The following are covered by other unit tests
64+
experimental_extra_index_urls = [],
65+
parallel_download = False,
66+
experimental_index_url_overrides = {},
67+
simpleapi_skip = simpleapi_skip,
68+
_evaluate_markers_srcs = [],
69+
**kwargs
70+
)

tests/pypi/hub_builder/hub_builder_tests.bzl

Lines changed: 12 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "REPO_VERBOSITY_EN
2020
load("//python/private/pypi:hub_builder.bzl", _hub_builder = "hub_builder") # buildifier: disable=bzl-visibility
2121
load("//python/private/pypi:parse_simpleapi_html.bzl", "parse_simpleapi_html") # buildifier: disable=bzl-visibility
2222
load("//python/private/pypi:whl_config_setting.bzl", "whl_config_setting") # buildifier: disable=bzl-visibility
23+
load("//tests/pypi/extension:pip_parse.bzl", _parse = "pip_parse")
24+
load("//python/private/pypi:platform.bzl", _plat = "platform")
2325

2426
_tests = []
2527

@@ -36,33 +38,6 @@ simple==0.0.1 \
3638
--hash=sha256:deadbaaf"""),
3739
)
3840

39-
# TODO @aignas 2025-09-07: reuse the same function from
40-
# //python/private/pypi:extension.bzl
41-
def _plat(*, name, arch_name, os_name, config_settings = [], env = {}, marker = "", whl_abi_tags = [], whl_platform_tags = []):
42-
# NOTE @aignas 2025-07-08: the least preferred is the first item in the list
43-
if "any" not in whl_platform_tags:
44-
# the lowest priority one needs to be the first one
45-
whl_platform_tags = ["any"] + whl_platform_tags
46-
47-
whl_abi_tags = whl_abi_tags or ["abi3", "cp{major}{minor}"]
48-
if "none" not in whl_abi_tags:
49-
# the lowest priority one needs to be the first one
50-
whl_abi_tags = ["none"] + whl_abi_tags
51-
52-
return struct(
53-
name = name,
54-
arch_name = arch_name,
55-
os_name = os_name,
56-
config_settings = config_settings,
57-
env = {
58-
# defaults for env
59-
"implementation_name": "cpython",
60-
} | env,
61-
marker = marker,
62-
whl_abi_tags = whl_abi_tags,
63-
whl_platform_tags = whl_platform_tags,
64-
)
65-
6641
def hub_builder(
6742
env,
6843
enable_pipstar = 0,
@@ -108,14 +83,17 @@ def hub_builder(
10883
},
10984
simpleapi_download_fn = simpleapi_download_fn or (lambda *a, **k: {}),
11085
evaluate_markers_fn = evaluate_markers_fn,
111-
logger = repo_utils.logger(struct(
112-
os = struct(
113-
environ = {
114-
REPO_DEBUG_ENV_VAR: "1",
115-
REPO_VERBOSITY_ENV_VAR: "TRACE" if debug else "INFO",
116-
},
86+
logger = repo_utils.logger(
87+
struct(
88+
os = struct(
89+
environ = {
90+
REPO_DEBUG_ENV_VAR: "1",
91+
REPO_VERBOSITY_ENV_VAR: "TRACE" if debug else "FAIL",
92+
},
93+
),
11794
),
118-
), "unit-test"),
95+
"unit-test",
96+
),
11997
)
12098
self = struct(
12199
build = lambda *a, **k: env.expect.that_struct(
@@ -132,74 +110,6 @@ def hub_builder(
132110
)
133111
return self
134112

135-
def _parse(
136-
*,
137-
hub_name,
138-
python_version,
139-
add_libdir_to_library_search_path = False,
140-
auth_patterns = {},
141-
download_only = False,
142-
enable_implicit_namespace_pkgs = False,
143-
environment = {},
144-
envsubst = {},
145-
experimental_index_url = "",
146-
experimental_requirement_cycles = {},
147-
experimental_target_platforms = [],
148-
extra_hub_aliases = {},
149-
extra_pip_args = [],
150-
isolated = True,
151-
netrc = None,
152-
parse_all_requirements_files = True,
153-
pip_data_exclude = None,
154-
python_interpreter = None,
155-
python_interpreter_target = None,
156-
quiet = True,
157-
requirements_by_platform = {},
158-
requirements_darwin = None,
159-
requirements_linux = None,
160-
requirements_lock = None,
161-
requirements_windows = None,
162-
simpleapi_skip = [],
163-
timeout = 600,
164-
whl_modifications = {},
165-
**kwargs):
166-
return struct(
167-
auth_patterns = auth_patterns,
168-
add_libdir_to_library_search_path = add_libdir_to_library_search_path,
169-
download_only = download_only,
170-
enable_implicit_namespace_pkgs = enable_implicit_namespace_pkgs,
171-
environment = environment,
172-
envsubst = envsubst,
173-
experimental_index_url = experimental_index_url,
174-
experimental_requirement_cycles = experimental_requirement_cycles,
175-
experimental_target_platforms = experimental_target_platforms,
176-
extra_hub_aliases = extra_hub_aliases,
177-
extra_pip_args = extra_pip_args,
178-
hub_name = hub_name,
179-
isolated = isolated,
180-
netrc = netrc,
181-
parse_all_requirements_files = parse_all_requirements_files,
182-
pip_data_exclude = pip_data_exclude,
183-
python_interpreter = python_interpreter,
184-
python_interpreter_target = python_interpreter_target,
185-
python_version = python_version,
186-
quiet = quiet,
187-
requirements_by_platform = requirements_by_platform,
188-
requirements_darwin = requirements_darwin,
189-
requirements_linux = requirements_linux,
190-
requirements_lock = requirements_lock,
191-
requirements_windows = requirements_windows,
192-
timeout = timeout,
193-
whl_modifications = whl_modifications,
194-
# The following are covered by other unit tests
195-
experimental_extra_index_urls = [],
196-
parallel_download = False,
197-
experimental_index_url_overrides = {},
198-
simpleapi_skip = simpleapi_skip,
199-
_evaluate_markers_srcs = [],
200-
**kwargs
201-
)
202-
203113
def _test_simple(env):
204114
builder = hub_builder(env)
205115
builder.pip_parse(

0 commit comments

Comments
 (0)