Skip to content

Commit f9b581c

Browse files
Rachel Goldfingercopybara-github
authored andcommitted
This change moves several tests from CcProtoTest.java to Starlark-based analysis tests in cc_proto_library.bzl
PiperOrigin-RevId: 863340736
1 parent 588fa9d commit f9b581c

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

bazel/tests/BUILD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22
load("//bazel:proto_descriptor_set.bzl", "proto_descriptor_set")
33
load("//bazel:proto_library.bzl", "proto_library")
44
load(":bazel_proto_library_tests.bzl", "bazel_proto_library_test_suite")
5+
load(":cc_proto_library_tests.bzl", "cc_proto_library_test_suite")
56
load(":cc_toolchain_tests.bzl", "cc_toolchain_test_suite")
67
load(":java_proto_library_tests.bzl", "java_proto_library_test_suite")
78
load(":proto_common_check_collocated_tests.bzl", "proto_common_check_collocated_test_suite")
@@ -28,6 +29,23 @@ java_proto_library_test_suite(name = "java_proto_library_test_suite")
2829

2930
py_proto_library_test_suite(name = "py_proto_library_test_suite")
3031

32+
cc_proto_library_test_suite(name = "cc_proto_library_tests")
33+
34+
bzl_library(
35+
name = "cc_proto_library_tests_bzl",
36+
srcs = ["cc_proto_library_tests.bzl"],
37+
parse_tests = False,
38+
visibility = ["//visibility:private"],
39+
deps = [
40+
"//bazel:cc_proto_library_bzl",
41+
"//bazel:proto_library_bzl",
42+
"@rules_cc//cc:core_rules",
43+
"@rules_testing//lib:analysis_test_bzl",
44+
"@rules_testing//lib:truth_bzl",
45+
"@rules_testing//lib:util_bzl",
46+
],
47+
)
48+
3149
proto_library(
3250
name = "empty_proto_library",
3351
)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Protocol Buffers - Google's data interchange format
2+
# Copyright 2025 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+
"""Tests for `cc_proto_library`."""
8+
9+
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
10+
load("@rules_cc//cc:defs.bzl", "cc_library")
11+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
12+
load("@rules_testing//lib:truth.bzl", "matching")
13+
load("@rules_testing//lib:util.bzl", "util")
14+
load("//bazel:cc_proto_library.bzl", "cc_proto_library")
15+
load("//bazel:proto_library.bzl", "proto_library")
16+
17+
def cc_proto_library_test_suite(name):
18+
test_suite(
19+
name = name,
20+
tests = [
21+
_test_link_order_with_mixed_deps,
22+
_test_link_order_with_mixed_deps_and_intermediate_library,
23+
_test_proto_library_in_srcs,
24+
],
25+
)
26+
27+
def _test_link_order_with_mixed_deps(name):
28+
util.helper_target(cc_library, name = name + "_a", srcs = ["a.cc"])
29+
util.helper_target(proto_library, name = name + "_b", srcs = ["b.proto"])
30+
util.helper_target(cc_proto_library, name = name + "_bc", deps = [":" + name + "_b"])
31+
util.helper_target(cc_library, name = name + "_c", srcs = ["c.cc"])
32+
util.helper_target(
33+
cc_binary,
34+
name = name + "_foo",
35+
srcs = ["foo.cc"],
36+
deps = [
37+
":" + name + "_a",
38+
":" + name + "_bc",
39+
":" + name + "_c",
40+
],
41+
)
42+
43+
analysis_test(
44+
name = name,
45+
target = ":" + name + "_foo",
46+
impl = _test_link_order_with_mixed_deps_impl,
47+
)
48+
49+
def _test_link_order_with_mixed_deps_impl(env, target):
50+
action = env.expect.that_target(target).action_named("CppLink")
51+
action.inputs().contains_at_least_predicates([
52+
matching.file_basename_contains("a.pic.o"),
53+
matching.file_basename_contains("b.pb.pic.o"),
54+
matching.file_basename_contains("c.pic.o"),
55+
matching.file_basename_contains("foo.pic.o"),
56+
])
57+
58+
def _test_link_order_with_mixed_deps_and_intermediate_library(name):
59+
util.helper_target(cc_library, name = name + "_a", srcs = ["a.cc"])
60+
util.helper_target(proto_library, name = name + "_b", srcs = ["b.proto"])
61+
util.helper_target(cc_proto_library, name = name + "_bc", deps = [":" + name + "_b"])
62+
util.helper_target(cc_library, name = name + "_c", srcs = ["c.cc"])
63+
util.helper_target(
64+
cc_library,
65+
name = name + "_lib",
66+
srcs = ["lib.cc"],
67+
deps = [
68+
":" + name + "_a",
69+
":" + name + "_bc",
70+
":" + name + "_c",
71+
],
72+
)
73+
util.helper_target(
74+
cc_binary,
75+
name = name + "_foo",
76+
srcs = ["foo.cc"],
77+
deps = [":" + name + "_lib"],
78+
)
79+
80+
analysis_test(
81+
name = name,
82+
target = ":" + name + "_foo",
83+
impl = _test_link_order_with_mixed_deps_and_intermediate_library_impl,
84+
)
85+
86+
def _test_link_order_with_mixed_deps_and_intermediate_library_impl(env, target):
87+
action = env.expect.that_target(target).action_named("CppLink")
88+
action.inputs().contains_at_least_predicates([
89+
matching.file_basename_contains("lib.pic.o"),
90+
matching.file_basename_contains("a.pic.o"),
91+
matching.file_basename_contains("b.pb.pic.o"),
92+
matching.file_basename_contains("c.pic.o"),
93+
matching.file_basename_contains("foo.pic.o"),
94+
])
95+
96+
def _test_proto_library_in_srcs(name):
97+
util.helper_target(proto_library, name = name + "_one", srcs = ["one.proto"])
98+
util.helper_target(cc_proto_library, name = name + "_one_cc_proto", deps = [":" + name + "_one"])
99+
util.helper_target(cc_library, name = name + "_two", srcs = [":" + name + "_one_cc_proto"])
100+
101+
analysis_test(
102+
name = name,
103+
target = ":" + name + "_two",
104+
impl = _test_proto_library_in_srcs_impl,
105+
)
106+
107+
def _test_proto_library_in_srcs_impl():
108+
# Just verifying that it doesn't fail to configure
109+
pass

0 commit comments

Comments
 (0)