Skip to content

Commit

Permalink
Add protobuf loads to unit tests
Browse files Browse the repository at this point in the history
This is needed to remove proto rules from Bazel. The automatic loading is disabled in unit tests.

PiperOrigin-RevId: 686069253
Change-Id: I8a2e5751ab98f24be1dc5e7854a57f68a06a6323
  • Loading branch information
comius authored and copybara-github committed Oct 15, 2024
1 parent f1e796e commit e63c615
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static void registerProtoToolchain(MockToolsConfig config) throws IOExce
config.append("MODULE.bazel", "register_toolchains('//tools/proto/toolchains:all')");
config.create(
"tools/proto/toolchains/BUILD",
TestConstants.LOAD_PROTO_TOOLCHAIN,
"load('@protobuf//bazel/toolchains:proto_toolchain.bzl', 'proto_toolchain')",
TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
"proto_toolchain(name = 'protoc_sources',"
+ "proto_compiler = '"
Expand Down Expand Up @@ -289,7 +289,7 @@ private static void createNetProto2(MockToolsConfig config) throws IOException {
""");
config.create(
"net/proto2/proto/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"package(default_visibility=['//visibility:public'])",
"genrule(name = 'go_internal_bootstrap_hack',",
" srcs = [ 'descriptor.pb.go-prebuilt' ],",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void setUp() throws Exception {
scratch.appendFile(
"third_party/protobuf/BUILD.bazel",
TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"package(default_visibility=['//visibility:public'])",
"proto_library(",
" name = 'any_proto',",
Expand Down Expand Up @@ -83,7 +83,8 @@ public void protoToolchainResolution_enabled() throws Exception {
CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES));
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])",
"proto_library(name = 'foo_proto', srcs = ['foo.proto'])");
assertThat(prettyArtifactNames(getFilesToBuild(getConfiguredTarget("//x:foo_cc_proto"))))
Expand All @@ -107,7 +108,8 @@ public void basic() throws Exception {
CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES));
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])",
"proto_library(name = 'foo_proto', srcs = ['foo.proto'])");
assertThat(prettyArtifactNames(getFilesToBuild(getConfiguredTarget("//x:foo_cc_proto"))))
Expand All @@ -119,7 +121,8 @@ public void basic() throws Exception {
public void canBeUsedFromCcRules() throws Exception {
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_library(name = 'foo', srcs = ['foo.cc'], deps = ['foo_cc_proto'])",
"cc_binary(name = 'bin', srcs = ['bin.cc'], deps = ['foo_cc_proto'])",
"cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])",
Expand All @@ -139,7 +142,8 @@ public void disallowMultipleDeps() throws Exception {
"x",
"foo_cc_proto",
"'deps' attribute must contain exactly one label",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto', 'bar_proto'])",
"proto_library(name = 'foo_proto', srcs = ['foo.proto'])",
"proto_library(name = 'bar_proto', srcs = ['bar.proto'])");
Expand All @@ -148,14 +152,16 @@ public void disallowMultipleDeps() throws Exception {
"y",
"foo_cc_proto",
"'deps' attribute must contain exactly one label",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = [])");
}

@Test
public void aliasProtos() throws Exception {
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['alias_proto'])",
"proto_library(name = 'alias_proto', deps = [':foo_proto'])",
"proto_library(name = 'foo_proto', srcs = ['foo.proto'])");
Expand All @@ -170,7 +176,7 @@ public void aliasProtos() throws Exception {
public void blacklistedProtos() throws Exception {
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'any_cc_proto', deps = ['@protobuf//:any_proto'])");

CcCompilationContext ccCompilationContext =
Expand All @@ -182,7 +188,8 @@ public void blacklistedProtos() throws Exception {
public void blacklistedProtosInTransitiveDeps() throws Exception {
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])",
"proto_library(",
" name = 'foo_proto',",
Expand All @@ -200,7 +207,8 @@ public void blacklistedProtosInTransitiveDeps() throws Exception {
public void ccCompilationContext() throws Exception {
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])",
"proto_library(name = 'foo_proto', srcs = ['foo.proto'], deps = [':bar_proto'])",
"proto_library(name = 'bar_proto', srcs = ['bar.proto'])");
Expand All @@ -215,7 +223,8 @@ public void ccCompilationContext() throws Exception {
public void outputDirectoryForProtoCompileAction() throws Exception {
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = [':bar_proto'])",
"proto_library(name = 'bar_proto', srcs = ['bar.proto'])");

Expand All @@ -235,13 +244,15 @@ public void outputDirectoryForProtoCompileAction_externalRepos() throws Exceptio
setBuildLanguageOptions(
"--experimental_builtins_injection_override=+cc_proto_library", "--enable_workspace");
scratch.file(
"x/BUILD", "cc_proto_library(name = 'foo_cc_proto', deps = ['@bla//foo:bar_proto'])");
"x/BUILD",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['@bla//foo:bar_proto'])");

scratch.file("/bla/WORKSPACE");
// Create the rule '@bla//foo:bar_proto'.
scratch.file(
"/bla/foo/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"package(default_visibility=['//visibility:public'])",
"proto_library(name = 'bar_proto', srcs = ['bar.proto'])");
String existingWorkspace =
Expand Down Expand Up @@ -281,7 +292,8 @@ public void commandLineControlsOutputFileSuffixes() throws Exception {
"--cc_proto_library_source_suffixes=.pb.cc,.pb.cc.meta");
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])",
"proto_library(name = 'foo_proto', srcs = ['foo.proto'])");

Expand All @@ -297,7 +309,8 @@ public void generatedSourcesNotCoverageInstrumented() throws Exception {
useConfiguration("--collect_code_coverage", "--instrumentation_filter=.");
scratch.file(
"x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])",
"proto_library(name = 'foo_proto', srcs = ['foo.proto'])");
ConfiguredTarget target = getConfiguredTarget("//x:foo_cc_proto");
Expand All @@ -321,7 +334,8 @@ public void importPrefixWorksWithRepositories() throws Exception {
scratch.file("/yolo_repo/yolo_pkg/yolo.proto");
scratch.file(
"/yolo_repo/yolo_pkg/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"proto_library(",
" name = 'yolo_proto',",
" srcs = ['yolo.proto'],",
Expand All @@ -347,7 +361,8 @@ public void stripImportPrefixWorksWithRepositories() throws Exception {
scratch.file("/yolo_repo/yolo_pkg/yolo.proto");
scratch.file(
"/yolo_repo/yolo_pkg/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"proto_library(",
" name = 'yolo_proto',",
" srcs = ['yolo.proto'],",
Expand All @@ -372,7 +387,8 @@ public void importPrefixAndStripImportPrefixWorksWithRepositories() throws Excep
scratch.file("/yolo_repo/yolo_pkg/yolo.proto");
scratch.file(
"/yolo_repo/yolo_pkg/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"proto_library(",
" name = 'yolo_proto',",
" srcs = ['yolo.proto'],",
Expand Down Expand Up @@ -414,7 +430,8 @@ private void setupTestCcProtoLibraryLoadedThroughMacro(boolean loadMacro) throws
scratch.file(
"a/BUILD",
getAnalysisMock().ccSupport().getMacroLoadStatement(loadMacro, "cc_proto_library"),
TestConstants.LOAD_PROTO_LIBRARY,
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:cc_proto_library.bzl', 'cc_proto_library')",
"cc_proto_library(",
" name='a',",
" deps=[':a_p'],",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,16 @@ private void mockToolchains() throws IOException {
scratch.appendFile(
"tools/proto/toolchains/BUILD",
"""
package(default_visibility = ["//visibility:public"])
proto_lang_toolchain(
name = "javalite",
command_line = "--java_out=lite,immutable:$(OUT)",
progress_message = "Generating JavaLite proto_library %{label}",
runtime = "//protobuf:javalite_runtime",
)
""");
load('@protobuf//bazel/toolchains:proto_lang_toolchain.bzl', 'proto_lang_toolchain')
package(default_visibility = ["//visibility:public"])
proto_lang_toolchain(
name = "javalite",
command_line = "--java_out=lite,immutable:$(OUT)",
progress_message = "Generating JavaLite proto_library %{label}",
runtime = "//protobuf:javalite_runtime",
)
""");
}

private void mockRuntimes() throws IOException {
Expand All @@ -116,6 +117,8 @@ public void testBinaryDeps() throws Exception {
scratch.file(
"x/BUILD",
"""
load('@protobuf//bazel:proto_library.bzl', 'proto_library')
load('@protobuf//bazel:java_lite_proto_library.bzl', 'java_lite_proto_library')
java_lite_proto_library(
name = "lite_pb2",
deps = [":foo"],
Expand Down Expand Up @@ -165,6 +168,8 @@ public void testJavaProto2CompilerArgs() throws Exception {
scratch.file(
"x/BUILD",
"""
load('@protobuf//bazel:proto_library.bzl', 'proto_library')
load('@protobuf//bazel:java_lite_proto_library.bzl', 'java_lite_proto_library')
java_lite_proto_library(
name = "lite_pb2",
deps = [":protolib"],
Expand Down Expand Up @@ -199,6 +204,9 @@ public void testProtoLibraryBuildsCompiledJar() throws Exception {
scratchConfiguredTarget(
"java",
"lite_pb2",
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:java_lite_proto_library.bzl',"
+ " 'java_lite_proto_library')",
"java_lite_proto_library(name = 'lite_pb2', deps = [':compiled'])",
"proto_library(name = 'compiled',",
" srcs = [ 'ok.proto' ])");
Expand All @@ -217,6 +225,8 @@ public void testCommandLineContainsTargetLabel() throws Exception {
scratch.file(
"java/lib/BUILD",
"""
load('@protobuf//bazel:proto_library.bzl', 'proto_library')
load('@protobuf//bazel:java_lite_proto_library.bzl', 'java_lite_proto_library')
java_lite_proto_library(
name = "lite_pb2",
deps = [":proto"],
Expand Down Expand Up @@ -247,6 +257,9 @@ public void testEmptySrcsForJavaApi() throws Exception {
scratchConfiguredTarget(
"notbad",
"lite_pb2",
"load('@protobuf//bazel:proto_library.bzl', 'proto_library')",
"load('@protobuf//bazel:java_lite_proto_library.bzl',"
+ " 'java_lite_proto_library')",
"java_lite_proto_library(name = 'lite_pb2', deps = [':null_lib'])",
"proto_library(name = 'null_lib')");
JavaCompilationArgsProvider compilationArgsProvider =
Expand All @@ -267,6 +280,8 @@ public void testSameVersionCompilerArguments() throws Exception {
scratch.file(
"cross/BUILD",
"""
load('@protobuf//bazel:proto_library.bzl', 'proto_library')
load('@protobuf//bazel:java_lite_proto_library.bzl', 'java_lite_proto_library')
java_lite_proto_library(
name = "lite_pb2",
deps = ["bravo"],
Expand Down Expand Up @@ -344,6 +359,8 @@ public void testExperimentalProtoExtraActions() throws Exception {
scratch.file(
"x/BUILD",
"""
load('@protobuf//bazel:proto_library.bzl', 'proto_library')
load('@protobuf//bazel:java_lite_proto_library.bzl', 'java_lite_proto_library')
java_lite_proto_library(
name = "lite_pb2",
deps = [":foo"],
Expand Down Expand Up @@ -411,6 +428,8 @@ def _impl(ctx):
scratch.file(
"protolib/BUILD",
"""
load('@protobuf//bazel:proto_library.bzl', 'proto_library')
load('@protobuf//bazel:java_lite_proto_library.bzl', 'java_lite_proto_library')
load("//proto:extensions.bzl", "custom_rule")
proto_library(
Expand Down Expand Up @@ -446,6 +465,8 @@ public void testProtoLibraryInterop() throws Exception {
scratch.file(
"protolib/BUILD",
"""
load('@protobuf//bazel:proto_library.bzl', 'proto_library')
load('@protobuf//bazel:java_lite_proto_library.bzl', 'java_lite_proto_library')
proto_library(
name = "proto",
srcs = ["file.proto"],
Expand Down Expand Up @@ -481,6 +502,8 @@ public void jplCorrectlyDefinesDirectJars_strictDepsEnabled_aliasProto() throws
scratch.file(
"x/BUILD",
"""
load('@protobuf//bazel:proto_library.bzl', 'proto_library')
load('@protobuf//bazel:java_lite_proto_library.bzl', 'java_lite_proto_library')
java_lite_proto_library(
name = "foo_java_proto_lite",
deps = [":foo_proto"],
Expand Down
Loading

0 comments on commit e63c615

Please sign in to comment.