blob: 259a42fdb7fbd70b4513e45fbda2e2e2ef03d776 [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 (
18 "io/ioutil"
19 "path/filepath"
20 "testing"
21
22 "android/soong/android"
23 "android/soong/cc"
24)
25
Thiébaud Weksteenc8105102020-08-06 07:50:31 +000026func TestProjectJson(t *testing.T) {
27 bp := `rust_library {
28 name: "liba",
29 srcs: ["src/lib.rs"],
30 crate_name: "a"
31 }` + GatherRequiredDepsForTest()
32 env := map[string]string{"SOONG_GEN_RUST_PROJECT": "1"}
33 fs := map[string][]byte{
34 "foo.rs": nil,
35 "src/lib.rs": nil,
36 }
37
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020038 cc.GatherRequiredFilesForTest(fs)
39
40 config := android.TestArchConfig(buildDir, env, bp, fs)
41 ctx := CreateTestContext()
42 ctx.Register(config)
43 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
44 android.FailIfErrored(t, errs)
45 _, errs = ctx.PrepareBuildActions(config)
46 android.FailIfErrored(t, errs)
47
48 // The JSON file is generated via WriteFileToOutputDir. Therefore, it
49 // won't appear in the Output of the TestingSingleton. Manually verify
50 // it exists.
Thiébaud Weksteenc8105102020-08-06 07:50:31 +000051 _, err := ioutil.ReadFile(filepath.Join(buildDir, "rust-project.json"))
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020052 if err != nil {
53 t.Errorf("rust-project.json has not been generated")
54 }
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020055}
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +020056
57func TestProjectJsonMultiVersion(t *testing.T) {
58 bp := `
59 rust_library {
60 name: "liba1",
61 srcs: ["a1/src/lib.rs"],
62 crate_name: "a"
63 }
64 rust_library {
65 name: "liba2",
66 srcs: ["a2/src/lib.rs"],
67 crate_name: "a",
68 }
69 rust_library {
70 name: "libb",
71 srcs: ["b/src/lib.rs"],
72 crate_name: "b",
73 rustlibs: ["liba1", "liba2"],
74 }
75 ` + GatherRequiredDepsForTest()
76 fs := map[string][]byte{
77 "a1/src/lib.rs": nil,
78 "a2/src/lib.rs": nil,
79 "b/src/lib.rs": nil,
80 }
81 jsonContent := testProjectJson(t, bp, fs)
82 crates := validateJsonCrates(t, jsonContent)
83 for _, crate := range crates {
84 c := crate.(map[string]interface{})
85 if c["root_module"] == "b/src/lib.rs" {
86 deps, ok := c["deps"].([]interface{})
87 if !ok {
88 t.Errorf("Unexpected format for deps: %v", c["deps"])
89 }
90 aCount := 0
91 for _, dep := range deps {
92 d, ok := dep.(map[string]interface{})
93 if !ok {
94 t.Errorf("Unexpected format for dep: %v", dep)
95 }
96 if d["name"] == "a" {
97 aCount++
98 }
99 }
100 if aCount != 2 {
101 t.Errorf("Unexpected number of liba dependencies want %v, got %v: %v", 2, aCount, deps)
102 }
103 return
104 }
105 }
106 t.Errorf("libb crate has not been found: %v", crates)
107}