| Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 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 |  | 
|  | 15 | package rust | 
|  | 16 |  | 
|  | 17 | import ( | 
| Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 18 | "strings" | 
| Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 19 | "testing" | 
| Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 20 |  | 
|  | 21 | "android/soong/android" | 
| Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 22 | ) | 
|  | 23 |  | 
| Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame] | 24 | func TestRustProtobuf3(t *testing.T) { | 
|  | 25 | ctx := testRust(t, ` | 
|  | 26 | rust_protobuf { | 
|  | 27 | name: "librust_proto", | 
|  | 28 | protos: ["buf.proto", "proto.proto"], | 
|  | 29 | crate_name: "rust_proto", | 
|  | 30 | source_stem: "buf", | 
|  | 31 | use_protobuf3: true, | 
|  | 32 | shared_libs: ["libfoo_shared"], | 
|  | 33 | static_libs: ["libfoo_static"], | 
|  | 34 | } | 
|  | 35 | cc_library_shared { | 
|  | 36 | name: "libfoo_shared", | 
|  | 37 | export_include_dirs: ["shared_include"], | 
|  | 38 | } | 
|  | 39 | cc_library_static { | 
|  | 40 | name: "libfoo_static", | 
|  | 41 | export_include_dirs: ["static_include"], | 
|  | 42 | } | 
|  | 43 | `) | 
|  | 44 | // Check that libprotobuf is added as a dependency. | 
|  | 45 | librust_proto := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_dylib").Module().(*Module) | 
|  | 46 | if !android.InList("libprotobuf", librust_proto.Properties.AndroidMkDylibs) { | 
|  | 47 | t.Errorf("libprotobuf dependency missing for rust_protobuf (dependency missing from AndroidMkDylibs)") | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | // Make sure the correct plugin is being used. | 
|  | 51 | librust_proto_out := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Output("buf.rs") | 
|  | 52 | cmd := librust_proto_out.RuleParams.Command | 
|  | 53 | if w := "protoc-gen-rust"; !strings.Contains(cmd, w) { | 
|  | 54 | t.Errorf("expected %q in %q", w, cmd) | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | // Check exported include directories | 
|  | 58 | if w := "-Ishared_include"; !strings.Contains(cmd, w) { | 
|  | 59 | t.Errorf("expected %q in %q", w, cmd) | 
|  | 60 | } | 
|  | 61 | if w := "-Istatic_include"; !strings.Contains(cmd, w) { | 
|  | 62 | t.Errorf("expected %q in %q", w, cmd) | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | // Check proto.rs, the second protobuf, is listed as an output | 
|  | 66 | librust_proto_outputs := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").AllOutputs() | 
|  | 67 | if android.InList("proto.rs", librust_proto_outputs) { | 
|  | 68 | t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto.rs' in list, got: %#v ", | 
|  | 69 | librust_proto_outputs) | 
|  | 70 | } | 
|  | 71 | } | 
|  | 72 |  | 
| Ivan Lozano | d106efe | 2023-09-21 23:30:26 -0400 | [diff] [blame] | 73 | func TestRustProtobufInclude(t *testing.T) { | 
|  | 74 | ctx := testRust(t, ` | 
|  | 75 | rust_protobuf { | 
|  | 76 | name: "librust_proto", | 
|  | 77 | protos: ["proto.proto"], | 
|  | 78 | crate_name: "rust_proto", | 
|  | 79 | source_stem: "proto", | 
|  | 80 | use_protobuf3: true, | 
|  | 81 | rustlibs: ["librust_exported_proto", "libfoo"], | 
|  | 82 | } | 
|  | 83 | rust_protobuf { | 
|  | 84 | name: "librust_exported_proto", | 
|  | 85 | protos: ["proto.proto"], | 
|  | 86 | crate_name: "rust_exported_proto", | 
|  | 87 | source_stem: "exported_proto", | 
|  | 88 | use_protobuf3: true, | 
|  | 89 | exported_include_dirs: ["proto"] | 
|  | 90 | } | 
|  | 91 | rust_library { | 
|  | 92 | name: "libfoo", | 
|  | 93 | crate_name: "foo", | 
|  | 94 | srcs: ["foo.rs"], | 
|  | 95 | } | 
|  | 96 | `) | 
|  | 97 | // Check that librust_exported_proto is added as additional crate to generate source. | 
|  | 98 | librust_proto := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Module().(*Module).sourceProvider.(*protobufDecorator) | 
|  | 99 | if !android.InList("rust_exported_proto", librust_proto.additionalCrates) { | 
|  | 100 | t.Errorf("librust_proto should have librust_exported_proto included as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates) | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | // Make sure the default crates aren't being included. | 
|  | 104 | if android.InList("std", librust_proto.additionalCrates) { | 
|  | 105 | t.Errorf("librust_proto should not have included libstd as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates) | 
|  | 106 | } | 
|  | 107 | if android.InList("protobuf", librust_proto.additionalCrates) { | 
|  | 108 | t.Errorf("librust_proto should not have included libprotobuf as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates) | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | // And make sure that non-protobuf crates aren't getting included either. | 
|  | 112 | if android.InList("foo", librust_proto.additionalCrates) { | 
|  | 113 | t.Errorf("librust_proto should not have included libfoo as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates) | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | // Check librust_proto args includes -Iproto | 
|  | 117 | librust_proto_rule := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Output("proto.rs") | 
|  | 118 | cmd := librust_proto_rule.RuleParams.Command | 
|  | 119 | if w := "-Iproto"; !strings.Contains(cmd, w) { | 
|  | 120 | t.Errorf("expected %q in %q", w, cmd) | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | } | 
|  | 124 |  | 
| Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 125 | func TestRustGrpc(t *testing.T) { | 
| Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 126 | ctx := testRust(t, ` | 
| Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 127 | rust_protobuf { | 
| Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 128 | name: "librust_grpcio", | 
| Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 129 | protos: ["buf.proto"], | 
|  | 130 | grpc_protos: ["foo.proto", "proto.proto"], | 
| Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 131 | crate_name: "rust_grpcio", | 
|  | 132 | source_stem: "buf", | 
|  | 133 | } | 
|  | 134 | `) | 
|  | 135 |  | 
|  | 136 | // Check that libprotobuf is added as a dependency. | 
|  | 137 | librust_grpcio_module := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_dylib").Module().(*Module) | 
| Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 138 |  | 
| Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 139 | // Check that libgrpcio is added as a dependency. | 
|  | 140 | if !android.InList("libgrpcio", librust_grpcio_module.Properties.AndroidMkDylibs) { | 
|  | 141 | t.Errorf("libgrpcio dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)") | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | // Check that libfutures is added as a dependency. | 
|  | 145 | if !android.InList("libfutures", librust_grpcio_module.Properties.AndroidMkDylibs) { | 
|  | 146 | t.Errorf("libfutures dependency missing for rust_grpcio (dependency missing from AndroidMkDylibs)") | 
|  | 147 | } | 
|  | 148 |  | 
| Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 149 | // Make sure the correct plugin is being used. | 
| Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 150 | librust_grpcio_out := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").Output("foo_grpc.rs") | 
| Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 151 | cmd := librust_grpcio_out.RuleParams.Command | 
|  | 152 | if w := "protoc-gen-grpc"; !strings.Contains(cmd, w) { | 
|  | 153 | t.Errorf("expected %q in %q", w, cmd) | 
|  | 154 | } | 
| Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 155 |  | 
| Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 156 | // Check that we're including the exported directory from libprotobuf-cpp-full | 
| Paul Duffin | 2c4ca8d | 2021-03-07 19:18:38 +0000 | [diff] [blame] | 157 | if w := "-I" + rustDefaultsDir + "libprotobuf-cpp-full-includes"; !strings.Contains(cmd, w) { | 
| Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 158 | t.Errorf("expected %q in %q", w, cmd) | 
|  | 159 | } | 
| Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 160 |  | 
|  | 161 | // Check proto.rs, the second protobuf, is listed as an output | 
|  | 162 | librust_grpcio_outputs := ctx.ModuleForTests("librust_grpcio", "android_arm64_armv8-a_source").AllOutputs() | 
|  | 163 | if android.InList("proto_grpc.rs", librust_grpcio_outputs) { | 
|  | 164 | t.Errorf("rust_protobuf is not producing multiple outputs; expected 'proto_grpc.rs' in list, got: %#v ", | 
|  | 165 | librust_grpcio_outputs) | 
|  | 166 | } | 
| Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 167 | } | 
| Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 168 |  | 
|  | 169 | func TestRustProtoErrors(t *testing.T) { | 
|  | 170 | testRustError(t, "A proto can only be added once to either grpc_protos or protos.*", ` | 
|  | 171 | rust_protobuf { | 
|  | 172 | name: "librust_grpcio", | 
|  | 173 | protos: ["buf.proto"], | 
|  | 174 | grpc_protos: ["buf.proto"], | 
|  | 175 | crate_name: "rust_grpcio", | 
|  | 176 | source_stem: "buf", | 
|  | 177 | } | 
|  | 178 | `) | 
|  | 179 |  | 
|  | 180 | testRustError(t, "proto filenames must be unique across  'protos' and 'grpc_protos'.*", ` | 
|  | 181 | rust_protobuf { | 
|  | 182 | name: "librust_grpcio", | 
|  | 183 | protos: ["buf.proto"], | 
|  | 184 | grpc_protos: ["proto/buf.proto"], | 
|  | 185 | crate_name: "rust_grpcio", | 
|  | 186 | source_stem: "buf", | 
|  | 187 | } | 
|  | 188 | `) | 
|  | 189 | } |