Skip to content

Commit 1e23941

Browse files
committed
trying to get tests to work
1 parent bf2173c commit 1e23941

File tree

3 files changed

+79
-69
lines changed

3 files changed

+79
-69
lines changed

python/private/pypi/dependency_specifier_flag.bzl

+66-65
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,50 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
12
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
23
load(":pep508_evaluate.bzl", "evaluate")
34

5+
def depspec_flag(**kwargs):
6+
pypa_dependency_specification(
7+
# todo: copied from pep508_env.bzl
8+
os_name = select({
9+
# The "java" value is documented, but with Jython defunct,
10+
# shouldn't occur in practice.
11+
# The osname value is technically a property of the runtime, not the
12+
# targetted OS at runtime, but the distinction shouldn't matter in
13+
# practice.
14+
"@platforms//os:windows": "nt",
15+
"//conditions:default": "posix",
16+
}),
17+
# todo: copied from pep508_env.bzl
18+
sys_platform = select({
19+
"@platforms//os:windows": "win32",
20+
"@platforms//os:linux": "linux",
21+
"@platforms//os:osx": "darwin",
22+
# todo: what does spec say unknown value is?
23+
"//conditions:default": "",
24+
}),
25+
# todo: copied from pep508_env.bzl
26+
# todo: pep508_env and evaluate have an "aliases" thing that needs
27+
# to be incorporated
28+
# todo: there are many more cpus. Unfortunately, it doesn't look like
29+
# the value is directly accessible to starlark. It might be possible to
30+
# get it via CcToolchain.cpu though.
31+
platform_machine = select({
32+
"@platforms//cpu:x86_64": "x86_64",
33+
"@platforms//cpu:aarch64": "aarch64",
34+
# todo: what does spec say unknown value is?
35+
"//conditions:default": "",
36+
}),
37+
# todo: copied from pep508_env.bzl
38+
platform_system = select({
39+
"@platforms//os:windows": "Windows",
40+
"@platforms//os:linux": "Linux",
41+
"@platforms//os:osx": "Darwin",
42+
# todo: what does spec say unknown value is?
43+
"//conditions:default": "",
44+
}),
45+
**kwargs
46+
)
47+
448
# todo: maybe put all the env into a single target and have a
549
# PyPiEnvMarkersInfo provider? Have --pypi_env=//some:target?
650
def _impl(ctx):
@@ -15,8 +59,8 @@ def _impl(ctx):
1559
env["python_full_version"] = full_version
1660
env["implementation_version"] = full_version
1761
else:
18-
env["python_version"] = get_flag(ctx.attr._python_version)
19-
full_version = get_flag(ctx.attr._python_full_version)
62+
env["python_version"] = _get_flag(ctx.attr._python_version)
63+
full_version = _get_flag(ctx.attr._python_full_version)
2064
env["python_full_version"] = full_version
2165
env["implementation_version"] = full_version
2266

@@ -40,76 +84,16 @@ def _impl(ctx):
4084
env["platform_system"] = ctx.attr.platform_system
4185
env["platform_version"] = ctx.attr._platform_version_config_flag[BuildSettingInfo].value
4286

43-
if evalute(ctx.attr.expression, env):
87+
if evaluate(ctx.attr.expression, env):
4488
value = "yes"
4589
else:
4690
value = "no"
4791
return [config_common.FeatureFlagInfo(value = value)]
4892

49-
# Adapted from spec code at:
50-
# https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers
51-
def format_full_info(info):
52-
kind = info.releaselevel
53-
if kind == "final":
54-
kind = ""
55-
serial = ""
56-
else:
57-
kind = kind[0] if kind else ""
58-
serial = str(info.serial) if info.serial else ""
59-
60-
return "{v.major}.{v.minor}.{v.micro}{kind}{serial}".format(
61-
v = version_info,
62-
kind = kind,
63-
serial = serial,
64-
)
65-
return version
66-
67-
def pypa_dep_spec(**kwargs):
68-
pypa_dependency_specification(
69-
# todo: copied from pep508_env.bzl
70-
os_name = select({
71-
# The "java" value is documented, but with Jython defunct,
72-
# shouldn't occur in practice.
73-
# The osname value is technically a property of the runtime, not the
74-
# targetted OS at runtime, but the distinction shouldn't matter in
75-
# practice.
76-
"@//platforms/os:windows": "nt",
77-
"//conditions:default": "posix",
78-
}),
79-
# todo: copied from pep508_env.bzl
80-
sys_platform = select({
81-
"@//platforms/os:windows": "win32",
82-
"@//platforms/os:linux": "linux",
83-
"@//platforms/os:osx": "darwin",
84-
# todo: what does spec say unknown value is?
85-
"//conditions:default": "",
86-
}),
87-
# todo: copied from pep508_env.bzl
88-
# todo: pep508_env and evaluate have an "aliases" thing that needs
89-
# to be incorporated
90-
# todo: there are many more cpus. Unfortunately, it doesn't look like
91-
# the value is directly accessible to starlark. It might be possible to
92-
# get it via CcToolchain.cpu though.
93-
platform_machine = select({
94-
"@platforms//cpu:x86_64": "x86_64",
95-
"@platforms//cpu:aarch64": "aarch64",
96-
# todo: what does spec say unknown value is?
97-
"//conditions:default": "",
98-
}),
99-
# todo: copied from pep508_env.bzl
100-
platform_system = select({
101-
"@//platforms/os:windows": "Windows",
102-
"@//platforms/os:linux": "Linux",
103-
"@//platforms/os:osx": "Darwin",
104-
# todo: what does spec say unknown value is?
105-
"//conditions:default": "",
106-
}),
107-
)
108-
10993
pypa_dependency_specification = rule(
11094
implementation = _impl,
11195
attrs = {
112-
"expression": attt.string(),
96+
"expression": attr.string(),
11397
"os_name": attr.string(),
11498
"sys_platform": attr.string(),
11599
"platform_machine": attr.string(),
@@ -121,7 +105,7 @@ pypa_dependency_specification = rule(
121105
default = "//python/config_settings:pip_platform_version_config",
122106
),
123107
"_python_version_flag": attr.label(
124-
default = "//python/config_settings:_python_version_major_minor",
108+
default = "//python/config_settings:python_version_major_minor",
125109
),
126110
"_python_full_version_flag": attr.label(
127111
default = "//python/config_settings:python_version",
@@ -133,6 +117,23 @@ pypa_dependency_specification = rule(
133117
],
134118
)
135119

120+
# Adapted from spec code at:
121+
# https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers
122+
def format_full_version(info):
123+
kind = info.releaselevel
124+
if kind == "final":
125+
kind = ""
126+
serial = ""
127+
else:
128+
kind = kind[0] if kind else ""
129+
serial = str(info.serial) if info.serial else ""
130+
131+
return "{v.major}.{v.minor}.{v.micro}{kind}{serial}".format(
132+
v = info,
133+
kind = kind,
134+
serial = serial,
135+
)
136+
136137
def _get_flag(t):
137138
if config_common.FeatureFlagInfo in t:
138139
return t[config_common.FeatureFlagInfo].value

python/private/pypi/flags.bzl

+4-2
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,18 @@ def define_pypi_internal_flags(name):
8484
_platform_release_config(
8585
name = "_pip_platform_release_default_config",
8686
value = select({
87-
"//conditions:osx": "USE_OSX_VERSION_FLAG",
87+
"@platforms//os:osx": "USE_OSX_VERSION_FLAG",
8888
"//conditions:default": "",
8989
}),
90+
visibility = ["//visibility:public"],
9091
)
9192
_platform_version_config(
9293
name = "_pip_platform_version_default_config",
9394
value = select({
94-
"//conditions:osx": "USE_OSX_VERSION_FLAG",
95+
"@platforms//os:osx": "USE_OSX_VERSION_FLAG",
9596
"//conditions:default": "",
9697
}),
98+
visibility = ["//visibility:public"],
9799
)
98100

99101
def _allow_wheels_flag_impl(ctx):

tests/pypi/pep508/depspec_flag_tests.bzl

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
12
load("@rules_testing//lib:test_suite.bzl", "test_suite")
3+
load("//python/private/pypi:dependency_specifier_flag.bzl", "depspec_flag")
24
load("//python/private/pypi:pep508_env.bzl", pep508_env = "env") # buildifier: disable=bzl-visibility
35
load("//python/private/pypi:pep508_evaluate.bzl", "evaluate", "tokenize") # buildifier: disable=bzl-visibility
6+
load("//tests/support:support.bzl", "PYTHON_VERSION")
47

58
_tests = []
69

7-
def test_whatever(name):
10+
def _test_whatever(name):
811
def impl(env, target):
912
# todo: create FeatureFlagInfo subject
1013
actual = target[config_common.FeatureFlagInfo].value
1114
env.expect.that_string(actual).equals("yes")
1215

1316
depspec_flag(
1417
name = name + "_subject",
18+
expression = "python_version >= '3.12.0'",
1519
)
1620
analysis_test(
1721
name = name,
1822
impl = impl,
1923
target = name + "_subject",
2024
config_settings = {
25+
PYTHON_VERSION: "3.12.0",
2126
},
2227
)
2328

24-
def depspec_flag_tests(name):
29+
_tests.append(_test_whatever)
30+
31+
def depspec_flag_test_suite(name):
2532
test_suite(
2633
name = name,
2734
tests = _tests,

0 commit comments

Comments
 (0)