Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 18 | "encoding/json" |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "path/filepath" |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 21 | "strings" |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 22 | "testing" |
| 23 | |
| 24 | "android/soong/android" |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 25 | ) |
| 26 | |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 27 | // testProjectJson run the generation of rust-project.json. It returns the raw |
| 28 | // content of the generated file. |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 29 | func 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 Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 34 | |
| 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 Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 38 | content, err := ioutil.ReadFile(filepath.Join(buildDir, rustProjectJsonFileName)) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 39 | if err != nil { |
| 40 | t.Errorf("rust-project.json has not been generated") |
| 41 | } |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 42 | 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. |
| 50 | func 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 | |
| 70 | func TestProjectJsonDep(t *testing.T) { |
| 71 | bp := ` |
| 72 | rust_library { |
| 73 | name: "liba", |
| 74 | srcs: ["a/src/lib.rs"], |
| 75 | crate_name: "a" |
| 76 | } |
| 77 | rust_library { |
| 78 | name: "libb", |
| 79 | srcs: ["b/src/lib.rs"], |
| 80 | crate_name: "b", |
| 81 | rlibs: ["liba"], |
| 82 | } |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 83 | ` |
| 84 | jsonContent := testProjectJson(t, bp) |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 85 | validateJsonCrates(t, jsonContent) |
| 86 | } |
| 87 | |
| 88 | func TestProjectJsonBindGen(t *testing.T) { |
| 89 | bp := ` |
| 90 | rust_library { |
| 91 | name: "liba", |
| 92 | srcs: ["src/lib.rs"], |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 93 | rlibs: ["libbindings1"], |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 94 | crate_name: "a" |
| 95 | } |
| 96 | rust_bindgen { |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 97 | name: "libbindings1", |
| 98 | crate_name: "bindings1", |
| 99 | source_stem: "bindings1", |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 100 | host_supported: true, |
| 101 | wrapper_src: "src/any.h", |
| 102 | } |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 103 | rust_library_host { |
| 104 | name: "libb", |
| 105 | srcs: ["src/lib.rs"], |
| 106 | rustlibs: ["libbindings2"], |
| 107 | crate_name: "b" |
| 108 | } |
| 109 | rust_bindgen_host { |
| 110 | name: "libbindings2", |
| 111 | crate_name: "bindings2", |
| 112 | source_stem: "bindings2", |
| 113 | wrapper_src: "src/any.h", |
| 114 | } |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 115 | ` |
| 116 | jsonContent := testProjectJson(t, bp) |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 117 | crates := validateJsonCrates(t, jsonContent) |
| 118 | for _, c := range crates { |
| 119 | crate, ok := c.(map[string]interface{}) |
| 120 | if !ok { |
| 121 | t.Fatalf("Unexpected type for crate: %v", c) |
| 122 | } |
| 123 | rootModule, ok := crate["root_module"].(string) |
| 124 | if !ok { |
| 125 | t.Fatalf("Unexpected type for root_module: %v", crate["root_module"]) |
| 126 | } |
| 127 | if strings.Contains(rootModule, "libbindings1") && !strings.Contains(rootModule, "android_arm64") { |
Thiébaud Weksteen | 4f6c766 | 2020-09-30 13:33:41 +0200 | [diff] [blame] | 128 | t.Errorf("The source path for libbindings1 does not contain android_arm64, got %v", rootModule) |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 129 | } |
Thiébaud Weksteen | 4f6c766 | 2020-09-30 13:33:41 +0200 | [diff] [blame] | 130 | if strings.Contains(rootModule, "libbindings2") && !strings.Contains(rootModule, android.BuildOs.String()) { |
| 131 | t.Errorf("The source path for libbindings2 does not contain the BuildOs, got %v; want %v", |
| 132 | rootModule, android.BuildOs.String()) |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 133 | } |
| 134 | } |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 135 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 136 | |
| 137 | func TestProjectJsonMultiVersion(t *testing.T) { |
| 138 | bp := ` |
| 139 | rust_library { |
| 140 | name: "liba1", |
| 141 | srcs: ["a1/src/lib.rs"], |
| 142 | crate_name: "a" |
| 143 | } |
| 144 | rust_library { |
| 145 | name: "liba2", |
| 146 | srcs: ["a2/src/lib.rs"], |
| 147 | crate_name: "a", |
| 148 | } |
| 149 | rust_library { |
| 150 | name: "libb", |
| 151 | srcs: ["b/src/lib.rs"], |
| 152 | crate_name: "b", |
| 153 | rustlibs: ["liba1", "liba2"], |
| 154 | } |
Thiébaud Weksteen | 0a75e52 | 2020-10-07 14:30:03 +0200 | [diff] [blame] | 155 | ` |
| 156 | jsonContent := testProjectJson(t, bp) |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 157 | crates := validateJsonCrates(t, jsonContent) |
| 158 | for _, crate := range crates { |
| 159 | c := crate.(map[string]interface{}) |
| 160 | if c["root_module"] == "b/src/lib.rs" { |
| 161 | deps, ok := c["deps"].([]interface{}) |
| 162 | if !ok { |
| 163 | t.Errorf("Unexpected format for deps: %v", c["deps"]) |
| 164 | } |
| 165 | aCount := 0 |
| 166 | for _, dep := range deps { |
| 167 | d, ok := dep.(map[string]interface{}) |
| 168 | if !ok { |
| 169 | t.Errorf("Unexpected format for dep: %v", dep) |
| 170 | } |
| 171 | if d["name"] == "a" { |
| 172 | aCount++ |
| 173 | } |
| 174 | } |
| 175 | if aCount != 2 { |
| 176 | t.Errorf("Unexpected number of liba dependencies want %v, got %v: %v", 2, aCount, deps) |
| 177 | } |
| 178 | return |
| 179 | } |
| 180 | } |
| 181 | t.Errorf("libb crate has not been found: %v", crates) |
| 182 | } |