protobuf_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2020 The Android Open Source Project
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rust
  15. import (
  16. "strings"
  17. "testing"
  18. "android/soong/android"
  19. )
  20. func TestRustProtobuf(t *testing.T) {
  21. ctx := testRust(t, `
  22. rust_protobuf {
  23. name: "librust_proto",
  24. protos: ["buf.proto", "proto.proto"],
  25. crate_name: "rust_proto",
  26. source_stem: "buf",
  27. shared_libs: ["libfoo_shared"],
  28. static_libs: ["libfoo_static"],
  29. }
  30. cc_library_shared {
  31. name: "libfoo_shared",
  32. export_include_dirs: ["shared_include"],
  33. }
  34. cc_library_static {
  35. name: "libfoo_static",
  36. export_include_dirs: ["static_include"],
  37. }
  38. `)
  39. // Check that libprotobuf is added as a dependency.
  40. librust_proto := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_dylib").Module().(*Module)
  41. if !android.InList("libprotobuf_deprecated", librust_proto.Properties.AndroidMkDylibs) {
  42. t.Errorf("libprotobuf_deprecated dependency missing for rust_protobuf (dependency missing from AndroidMkDylibs)")
  43. }
  44. // Make sure the correct plugin is being used.
  45. librust_proto_out := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Output("buf.rs")
  46. cmd := librust_proto_out.RuleParams.Command
  47. if w := "protoc-gen-rust-deprecated"; !strings.Contains(cmd, w) {
  48. t.Errorf("expected %q in %q", w, cmd)
  49. }
  50. // Check exported include directories
  51. if w := "-Ishared_include"; !strings.Contains(cmd, w) {
  52. t.Errorf("expected %q in %q", w, cmd)
  53. }
  54. if w := "-Istatic_include"; !strings.Contains(cmd, w) {
  55. t.Errorf("expected %q in %q", w, cmd)
  56. }
  57. // Check proto.rs, the second protobuf, is listed as an output
  58. librust_proto_outputs := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").AllOutputs()
  59. if android.InList("proto.rs", librust_proto_outputs) {
  60. t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto.rs' in list, got: %#v ",
  61. librust_proto_outputs)
  62. }
  63. }
  64. func TestRustProtobuf3(t *testing.T) {
  65. ctx := testRust(t, `
  66. rust_protobuf {
  67. name: "librust_proto",
  68. protos: ["buf.proto", "proto.proto"],
  69. crate_name: "rust_proto",
  70. source_stem: "buf",
  71. use_protobuf3: true,
  72. shared_libs: ["libfoo_shared"],
  73. static_libs: ["libfoo_static"],
  74. }
  75. cc_library_shared {
  76. name: "libfoo_shared",
  77. export_include_dirs: ["shared_include"],
  78. }
  79. cc_library_static {
  80. name: "libfoo_static",
  81. export_include_dirs: ["static_include"],
  82. }
  83. `)
  84. // Check that libprotobuf is added as a dependency.
  85. librust_proto := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_dylib").Module().(*Module)
  86. if !android.InList("libprotobuf", librust_proto.Properties.AndroidMkDylibs) {
  87. t.Errorf("libprotobuf dependency missing for rust_protobuf (dependency missing from AndroidMkDylibs)")
  88. }
  89. // Make sure the correct plugin is being used.
  90. librust_proto_out := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Output("buf.rs")
  91. cmd := librust_proto_out.RuleParams.Command
  92. if w := "protoc-gen-rust"; !strings.Contains(cmd, w) {
  93. t.Errorf("expected %q in %q", w, cmd)
  94. }
  95. // Check exported include directories
  96. if w := "-Ishared_include"; !strings.Contains(cmd, w) {
  97. t.Errorf("expected %q in %q", w, cmd)
  98. }
  99. if w := "-Istatic_include"; !strings.Contains(cmd, w) {
  100. t.Errorf("expected %q in %q", w, cmd)
  101. }
  102. // Check proto.rs, the second protobuf, is listed as an output
  103. librust_proto_outputs := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").AllOutputs()
  104. if android.InList("proto.rs", librust_proto_outputs) {
  105. t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto.rs' in list, got: %#v ",
  106. librust_proto_outputs)
  107. }
  108. }
  109. func TestRustGrpc(t *testing.T) {
  110. ctx := testRust(t, `
  111. rust_protobuf {
  112. name: "librust_grpcio",
  113. protos: ["buf.proto"],
  114. grpc_protos: ["foo.proto", "proto.proto"],
  115. crate_name: "rust_grpcio",
  116. source_stem: "buf",
  117. }
  118. `)
  119. // Check that libprotobuf is added as a dependency.
  120. librust_grpcio_module := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_dylib").Module().(*Module)
  121. // Check that libgrpcio is added as a dependency.
  122. if !android.InList("libgrpcio", librust_grpcio_module.Properties.AndroidMkDylibs) {
  123. t.Errorf("libgrpcio dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)")
  124. }
  125. // Check that libfutures is added as a dependency.
  126. if !android.InList("libfutures", librust_grpcio_module.Properties.AndroidMkDylibs) {
  127. t.Errorf("libfutures dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)")
  128. }
  129. // Make sure the correct plugin is being used.
  130. librust_grpcio_out := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").Output("foo_grpc.rs")
  131. cmd := librust_grpcio_out.RuleParams.Command
  132. if w := "protoc-gen-grpc"; !strings.Contains(cmd, w) {
  133. t.Errorf("expected %q in %q", w, cmd)
  134. }
  135. // Check that we're including the exported directory from libprotobuf-cpp-full
  136. if w := "-I" + rustDefaultsDir + "libprotobuf-cpp-full-includes"; !strings.Contains(cmd, w) {
  137. t.Errorf("expected %q in %q", w, cmd)
  138. }
  139. // Check proto.rs, the second protobuf, is listed as an output
  140. librust_grpcio_outputs := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").AllOutputs()
  141. if android.InList("proto_grpc.rs", librust_grpcio_outputs) {
  142. t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto_grpc.rs' in list, got: %#v ",
  143. librust_grpcio_outputs)
  144. }
  145. }
  146. func TestRustProtoErrors(t *testing.T) {
  147. testRustError(t, "A proto can only be added once to either grpc_protos or protos.*", `
  148. rust_protobuf {
  149. name: "librust_grpcio",
  150. protos: ["buf.proto"],
  151. grpc_protos: ["buf.proto"],
  152. crate_name: "rust_grpcio",
  153. source_stem: "buf",
  154. }
  155. `)
  156. testRustError(t, "proto filenames must be unique across 'protos' and 'grpc_protos'.*", `
  157. rust_protobuf {
  158. name: "librust_grpcio",
  159. protos: ["buf.proto"],
  160. grpc_protos: ["proto/buf.proto"],
  161. crate_name: "rust_grpcio",
  162. source_stem: "buf",
  163. }
  164. `)
  165. }