blob: ba66215c5df868de0d54ca29cfb078758b617563 [file] [log] [blame]
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +02001// Copyright 2020 Google Inc. All rights reserved.
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
15package rust
16
17import (
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +020018 "encoding/json"
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020019 "io/ioutil"
20 "path/filepath"
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +020021 "strings"
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020022 "testing"
23
24 "android/soong/android"
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020025)
26
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +020027// testProjectJson run the generation of rust-project.json. It returns the raw
28// content of the generated file.
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +020029func testProjectJson(t *testing.T, bp string) []byte {
30 tctx := newTestRustCtx(t, bp)
31 tctx.env = map[string]string{"SOONG_GEN_RUST_PROJECT": "1"}
32 tctx.generateConfig()
33 tctx.parse(t)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020034
35 // The JSON file is generated via WriteFileToOutputDir. Therefore, it
36 // won't appear in the Output of the TestingSingleton. Manually verify
37 // it exists.
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +020038 content, err := ioutil.ReadFile(filepath.Join(buildDir, rustProjectJsonFileName))
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020039 if err != nil {
40 t.Errorf("rust-project.json has not been generated")
41 }
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +020042 return content
43}
44
45// validateJsonCrates validates that content follows the basic structure of
46// rust-project.json. It returns the crates attribute if the validation
47// succeeded.
48// It uses an empty interface instead of relying on a defined structure to
49// avoid a strong dependency on our implementation.
50func validateJsonCrates(t *testing.T, rawContent []byte) []interface{} {
51 var content interface{}
52 err := json.Unmarshal(rawContent, &content)
53 if err != nil {
54 t.Errorf("Unable to parse the rust-project.json as JSON: %v", err)
55 }
56 root, ok := content.(map[string]interface{})
57 if !ok {
58 t.Errorf("Unexpected JSON format: %v", content)
59 }
60 if _, ok = root["crates"]; !ok {
61 t.Errorf("No crates attribute in rust-project.json: %v", root)
62 }
63 crates, ok := root["crates"].([]interface{})
64 if !ok {
65 t.Errorf("Unexpected crates format: %v", root["crates"])
66 }
67 return crates
68}
69
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +010070// validateCrate ensures that a crate can be parsed as a map.
71func validateCrate(t *testing.T, crate interface{}) map[string]interface{} {
72 c, ok := crate.(map[string]interface{})
73 if !ok {
74 t.Fatalf("Unexpected type for crate: %v", c)
75 }
76 return c
77}
78
79// validateDependencies parses the dependencies for a crate. It returns a list
80// of the dependencies name.
81func validateDependencies(t *testing.T, crate map[string]interface{}) []string {
82 var dependencies []string
83 deps, ok := crate["deps"].([]interface{})
84 if !ok {
85 t.Errorf("Unexpected format for deps: %v", crate["deps"])
86 }
87 for _, dep := range deps {
88 d, ok := dep.(map[string]interface{})
89 if !ok {
90 t.Errorf("Unexpected format for dependency: %v", dep)
91 }
92 name, ok := d["name"].(string)
93 if !ok {
94 t.Errorf("Dependency is missing the name key: %v", d)
95 }
96 dependencies = append(dependencies, name)
97 }
98 return dependencies
99}
100
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200101func TestProjectJsonDep(t *testing.T) {
102 bp := `
103 rust_library {
104 name: "liba",
105 srcs: ["a/src/lib.rs"],
106 crate_name: "a"
107 }
108 rust_library {
109 name: "libb",
110 srcs: ["b/src/lib.rs"],
111 crate_name: "b",
112 rlibs: ["liba"],
113 }
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200114 `
115 jsonContent := testProjectJson(t, bp)
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200116 validateJsonCrates(t, jsonContent)
117}
118
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100119func TestProjectJsonBinary(t *testing.T) {
120 bp := `
121 rust_binary {
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100122 name: "libz",
123 srcs: ["z/src/lib.rs"],
124 crate_name: "z"
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100125 }
126 `
127 jsonContent := testProjectJson(t, bp)
128 crates := validateJsonCrates(t, jsonContent)
129 for _, c := range crates {
130 crate := validateCrate(t, c)
131 rootModule, ok := crate["root_module"].(string)
132 if !ok {
133 t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
134 }
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100135 if rootModule == "z/src/lib.rs" {
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100136 return
137 }
138 }
139 t.Errorf("Entry for binary %q not found: %s", "a", jsonContent)
140}
141
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200142func TestProjectJsonBindGen(t *testing.T) {
143 bp := `
144 rust_library {
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100145 name: "libd",
146 srcs: ["d/src/lib.rs"],
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200147 rlibs: ["libbindings1"],
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100148 crate_name: "d"
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200149 }
150 rust_bindgen {
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200151 name: "libbindings1",
152 crate_name: "bindings1",
153 source_stem: "bindings1",
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200154 host_supported: true,
155 wrapper_src: "src/any.h",
156 }
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200157 rust_library_host {
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100158 name: "libe",
159 srcs: ["e/src/lib.rs"],
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200160 rustlibs: ["libbindings2"],
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100161 crate_name: "e"
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200162 }
163 rust_bindgen_host {
164 name: "libbindings2",
165 crate_name: "bindings2",
166 source_stem: "bindings2",
167 wrapper_src: "src/any.h",
168 }
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200169 `
170 jsonContent := testProjectJson(t, bp)
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200171 crates := validateJsonCrates(t, jsonContent)
172 for _, c := range crates {
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100173 crate := validateCrate(t, c)
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200174 rootModule, ok := crate["root_module"].(string)
175 if !ok {
176 t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
177 }
178 if strings.Contains(rootModule, "libbindings1") && !strings.Contains(rootModule, "android_arm64") {
Thiébaud Weksteen4f6c7662020-09-30 13:33:41 +0200179 t.Errorf("The source path for libbindings1 does not contain android_arm64, got %v", rootModule)
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200180 }
Thiébaud Weksteen4f6c7662020-09-30 13:33:41 +0200181 if strings.Contains(rootModule, "libbindings2") && !strings.Contains(rootModule, android.BuildOs.String()) {
182 t.Errorf("The source path for libbindings2 does not contain the BuildOs, got %v; want %v",
183 rootModule, android.BuildOs.String())
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200184 }
Thiébaud Weksteen3c5905b2020-11-25 16:09:32 +0100185 // Check that libbindings1 does not depend on itself.
186 if strings.Contains(rootModule, "libbindings1") {
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100187 for _, depName := range validateDependencies(t, crate) {
188 if depName == "bindings1" {
Thiébaud Weksteen3c5905b2020-11-25 16:09:32 +0100189 t.Errorf("libbindings1 depends on itself")
190 }
191 }
192 }
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100193 // Check that liba depends on libbindings1
194 if strings.Contains(rootModule, "d/src/lib.rs") {
195 found := false
196 for _, depName := range validateDependencies(t, crate) {
197 if depName == "bindings1" {
198 found = true
199 break
200 }
201 }
202 if !found {
203 t.Errorf("liba does not depend on libbindings1: %v", crate)
204 }
205 }
Thiébaud Weksteena6351ca2020-09-29 09:53:21 +0200206 }
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200207}
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +0200208
209func TestProjectJsonMultiVersion(t *testing.T) {
210 bp := `
211 rust_library {
212 name: "liba1",
213 srcs: ["a1/src/lib.rs"],
214 crate_name: "a"
215 }
216 rust_library {
217 name: "liba2",
218 srcs: ["a2/src/lib.rs"],
219 crate_name: "a",
220 }
221 rust_library {
222 name: "libb",
223 srcs: ["b/src/lib.rs"],
224 crate_name: "b",
225 rustlibs: ["liba1", "liba2"],
226 }
Thiébaud Weksteen0a75e522020-10-07 14:30:03 +0200227 `
228 jsonContent := testProjectJson(t, bp)
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +0200229 crates := validateJsonCrates(t, jsonContent)
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100230 for _, c := range crates {
231 crate := validateCrate(t, c)
232 rootModule, ok := crate["root_module"].(string)
233 if !ok {
234 t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
235 }
236 // Make sure that b has 2 different dependencies.
237 if rootModule == "b/src/lib.rs" {
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +0200238 aCount := 0
Thiébaud Weksteen064f6e92020-12-03 20:58:32 +0100239 deps := validateDependencies(t, crate)
240 for _, depName := range deps {
241 if depName == "a" {
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +0200242 aCount++
243 }
244 }
245 if aCount != 2 {
246 t.Errorf("Unexpected number of liba dependencies want %v, got %v: %v", 2, aCount, deps)
247 }
248 return
249 }
250 }
251 t.Errorf("libb crate has not been found: %v", crates)
252}