Skip to content

Commit 65cee3a

Browse files
Internal change
PiperOrigin-RevId: 728759324
1 parent 73a5afe commit 65cee3a

File tree

2 files changed

+274
-0
lines changed

2 files changed

+274
-0
lines changed

bazel/tests/BUILD

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load(":proto_common_check_collocated_tests.bzl", "proto_common_check_collocated_
33
load(":proto_common_compile_tests.bzl", "proto_common_compile_test_suite")
44
load(":proto_common_declare_generated_files_tests.bzl", "proto_common_declare_generated_files_test_suite")
55
load(":proto_common_should_generate_tests.bzl", "proto_common_should_generate_test_suite")
6+
load(":proto_lang_toolchain_tests.bzl", "proto_lang_toolchain_test_suite")
67

78
package(default_applicable_licenses = ["//:license"])
89

@@ -15,3 +16,5 @@ proto_common_declare_generated_files_test_suite(name = "proto_common_declare_gen
1516
proto_common_check_collocated_test_suite(name = "proto_common_check_collocated_test_suite")
1617

1718
java_proto_library_test_suite(name = "java_proto_library_test_suite")
19+
20+
proto_lang_toolchain_test_suite(name = "proto_lang_toolchain_test_suite")
+271
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
# Protocol Buffers - Google's data interchange format
2+
# Copyright 2024 Google Inc. All rights reserved.
3+
#
4+
# Use of this source code is governed by a BSD-style
5+
# license that can be found in the LICENSE file or at
6+
# https://developers.google.com/open-source/licenses/bsd
7+
#
8+
"""Tests for `proto_lang_toolchain` function."""
9+
10+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
11+
load("@rules_testing//lib:truth.bzl", "matching")
12+
load("@rules_testing//lib:util.bzl", "util")
13+
load("//bazel:proto_library.bzl", "proto_library")
14+
load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo")
15+
load("//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain")
16+
17+
def proto_lang_toolchain_test_suite(name):
18+
# Are these needed?
19+
# licenses(["unencumbered"])
20+
21+
cc_binary(
22+
name = "plugin",
23+
srcs = ["plugin.cc"],
24+
)
25+
cc_library(
26+
name = "runtime",
27+
srcs = ["runtime.cc"],
28+
)
29+
# filegroup(
30+
# name = "descriptors",
31+
# srcs = [
32+
# "descriptor.proto",
33+
# "metadata.proto",
34+
# ],
35+
# )
36+
# filegroup(
37+
# name = "any",
38+
# srcs = ["any.proto"],
39+
# )
40+
41+
util.helper_target(
42+
proto_library,
43+
name = "denied",
44+
srcs = [
45+
":any",
46+
":descriptors",
47+
],
48+
)
49+
50+
util.helper_target(
51+
proto_lang_toolchain,
52+
name = "toolchain",
53+
out_replacement_format_flag = "cmd-line:%s",
54+
plugin_format_flag = "--plugin=%s",
55+
plugin = ":plugin",
56+
runtime = ":runtime",
57+
protoc_opts = ["--myflag"],
58+
progress_message = "Progress Message %{label}",
59+
mnemonic = "MyMnemonic",
60+
)
61+
test_suite(
62+
name = name,
63+
tests = [
64+
_test_proto_toolchain,
65+
_test_proto_toolchain_resolution_enabled,
66+
_test_proto_toolchain_denylist_proto_libraries,
67+
_test_proto_toolchain_denylist_transitive_protos,
68+
_test_optional_fields_are_empty,
69+
],
70+
)
71+
72+
def _test_proto_toolchain(name):
73+
analysis_test(
74+
name = name,
75+
target = ":toolchain",
76+
impl = _test_proto_lang_toolchain_impl,
77+
)
78+
79+
def _test_proto_lang_toolchain_impl(env, target):
80+
proto_lang_toolchain_info = env.expect.that_target(target).actual[ProtoLangToolchainInfo]
81+
82+
# Validate proto_lang_toolchain
83+
env.expect.that_str(proto_lang_toolchain_info.out_replacement_format_flag).equals("cmd-line:%s")
84+
env.expect.that_str(proto_lang_toolchain_info.plugin_format_flag).equals("--plugin=%s")
85+
env.expect.that_str(proto_lang_toolchain_info.plugin_executable.executable.short_path).equals("plugin")
86+
env.expect.that_str(str(proto_lang_toolchain_info.runtime.label)).equals("//:runtime")
87+
env.expect.that_list(proto_lang_toolchain_info.protoc_opts).equals(["--myflag"])
88+
env.expect.that_str(proto_lang_toolchain_info.progress_message).equals("Progress Message %{label}")
89+
env.expect.that_str(proto_lang_toolchain_info.mnemonic).equals("MyMnemonic")
90+
91+
# Validate proto_compiler
92+
actual_protoc_label = "@//net/proto2/compiler/public:protocol_compiler"
93+
env.expect.that_str(str(target.proto_compiler.executable.short_path)).equals(actual_protoc_label)
94+
95+
def _test_proto_toolchain_resolution_enabled(name):
96+
# licenses(["unencumbered"])
97+
98+
cc_binary(
99+
name = "plugin",
100+
srcs = ["plugin.cc"],
101+
)
102+
103+
cc_library(
104+
name = "runtime",
105+
srcs = ["runtime.cc"],
106+
)
107+
108+
# filegroup(
109+
# name = "descriptors",
110+
# srcs = [
111+
# "descriptor.proto",
112+
# "metadata.proto",
113+
# ],
114+
# )
115+
116+
# filegroup(
117+
# name = "any",
118+
# srcs = ["any.proto"],
119+
# )
120+
121+
util.helper_target(
122+
proto_library,
123+
name = "denied",
124+
srcs = [
125+
":any",
126+
":descriptors",
127+
],
128+
)
129+
130+
util.helper_target(
131+
proto_lang_toolchain,
132+
name = "toolchain",
133+
out_replacement_format_flag = "cmd-line:%s",
134+
plugin_format_flag = "--plugin=%s",
135+
plugin = ":plugin",
136+
runtime = ":runtime",
137+
protoc_opts = ["--myflag"],
138+
progress_message = "Progress Message %{label}",
139+
mnemonic = "MyMnemonic",
140+
)
141+
142+
analysis_test(
143+
name = name,
144+
target = ":toolchain",
145+
impl = _test_proto_lang_toolchain_impl,
146+
)
147+
148+
def _test_proto_toolchain_denylist_proto_libraries(name):
149+
# licenses(['unencumbered'])
150+
151+
cc_binary(
152+
name = "plugin",
153+
srcs = ["plugin.cc"],
154+
)
155+
156+
cc_library(
157+
name = "runtime",
158+
srcs = ["runtime.cc"],
159+
)
160+
161+
util.helper_target(
162+
proto_library,
163+
name = "descriptors",
164+
srcs = [
165+
"metadata.proto",
166+
"descriptor.proto",
167+
],
168+
)
169+
170+
util.helper_target(
171+
proto_library,
172+
name = "any",
173+
srcs = [
174+
"any.proto",
175+
],
176+
strip_import_prefix = "/third_party",
177+
)
178+
179+
util.helper_target(
180+
proto_lang_toolchain,
181+
name = "toolchain",
182+
out_replacement_format_flag = "cmd-line:%s",
183+
plugin_format_flag = "--plugin=%s",
184+
plugin = ":plugin",
185+
runtime = ":runtime",
186+
protoc_opts = ["--myflag"],
187+
progress_message = "Progress Message %{label}",
188+
mnemonic = "MyMnemonic",
189+
)
190+
191+
analysis_test(
192+
name = name,
193+
target = ":toolchain",
194+
impl = _test_proto_lang_toolchain_impl,
195+
)
196+
197+
def _test_proto_toolchain_denylist_transitive_protos(name):
198+
# licenses(["unencumbered"])
199+
200+
cc_binary(
201+
name = "plugin",
202+
srcs = ["plugin.cc"],
203+
)
204+
205+
cc_library(
206+
name = "runtime",
207+
srcs = ["runtime.cc"],
208+
)
209+
210+
util.helper_target(
211+
proto_library,
212+
name = "descriptors",
213+
srcs = [
214+
"metadata.proto",
215+
"descriptor.proto",
216+
],
217+
)
218+
219+
util.helper_target(
220+
proto_library,
221+
name = "any",
222+
srcs = [
223+
"any.proto",
224+
],
225+
deps = [
226+
":descriptors",
227+
],
228+
)
229+
230+
util.helper_target(
231+
proto_lang_toolchain,
232+
name = "toolchain",
233+
out_replacement_format_flag = "cmd-line:%s",
234+
plugin_format_flag = "--plugin=%s",
235+
plugin = ":plugin",
236+
runtime = ":runtime",
237+
protoc_opts = ["--myflag"],
238+
progress_message = "Progress Message %{label}",
239+
mnemonic = "MyMnemonic",
240+
)
241+
242+
analysis_test(
243+
name = name,
244+
target = ":toolchain",
245+
impl = _test_proto_lang_toolchain_impl,
246+
)
247+
248+
def _test_optional_fields_are_empty(name):
249+
# TestConstants.LOAD_PROTO_LANG_TOOLCHAIN
250+
# public static final String LOAD_PROTO_LANG_TOOLCHAIN =
251+
# "load('@//bazel/toolchains:proto_lang_toolchain.bzl',"
252+
# + " 'proto_lang_toolchain')";
253+
254+
util.helper_target(
255+
proto_lang_toolchain,
256+
name = "toolchain",
257+
out_replacement_format_flag = "cmd-line:%s",
258+
)
259+
analysis_test(
260+
name = name,
261+
target = ":toolchain",
262+
impl = _test_optional_fields_are_empty_impl,
263+
)
264+
265+
def _test_optional_fields_are_empty_impl(env, target):
266+
proto_lang_toolchain_info = env.expect.that_target(target).actual[ProtoLangToolchainInfo]
267+
268+
# Validate proto_lang_toolchain
269+
env.expect.that_str(proto_lang_toolchain_info.plugin_executable.executable.short_path).is_none()
270+
env.expect.that_str(proto_lang_toolchain_info.runtime.label).is_none()
271+
env.expect.that_str(proto_lang_toolchain_info.mnemonic).equals("GenProto")

0 commit comments

Comments
 (0)