blob: 1097da22684152b6d3c59b61c0497af3b8a13cdd [file] [log] [blame]
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -07001// Copyright 2019 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
15package rust
16
17import (
18 "strings"
19 "testing"
Ivan Lozano2b081132020-09-08 12:46:52 -040020
21 "android/soong/android"
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070022)
23
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070024func TestRustTest(t *testing.T) {
25 ctx := testRust(t, `
26 rust_test_host {
27 name: "my_test",
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070028 srcs: ["foo.rs"],
Ivan Lozano9da4aa82021-01-29 12:48:05 -050029 data: ["data.txt"],
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070030 }`)
31
Ivan Lozanofc80fe72020-06-11 13:25:36 -040032 testingModule := ctx.ModuleForTests("my_test", "linux_glibc_x86_64")
33 expectedOut := "my_test/linux_glibc_x86_64/my_test"
34 outPath := testingModule.Output("my_test").Output.String()
35 if !strings.Contains(outPath, expectedOut) {
36 t.Errorf("wrong output path: %v; expected: %v", outPath, expectedOut)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070037 }
Ivan Lozano9da4aa82021-01-29 12:48:05 -050038
39 dataPaths := testingModule.Module().(*Module).compiler.(*testDecorator).dataPaths()
40 if len(dataPaths) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -080041 t.Errorf("expected exactly one test data file. test data files: [%v]", dataPaths)
Ivan Lozano9da4aa82021-01-29 12:48:05 -050042 return
43 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070044}
Ivan Lozano2b081132020-09-08 12:46:52 -040045
46func TestRustTestLinkage(t *testing.T) {
47 ctx := testRust(t, `
48 rust_test {
49 name: "my_test",
50 srcs: ["foo.rs"],
51 rustlibs: ["libfoo"],
52 rlibs: ["libbar"],
53 }
54 rust_library {
55 name: "libfoo",
56 srcs: ["foo.rs"],
57 crate_name: "foo",
58 }
59 rust_library {
60 name: "libbar",
61 srcs: ["foo.rs"],
62 crate_name: "bar",
63 }`)
64
65 testingModule := ctx.ModuleForTests("my_test", "android_arm64_armv8-a").Module().(*Module)
66
67 if !android.InList("libfoo.rlib-std", testingModule.Properties.AndroidMkRlibs) {
68 t.Errorf("rlib-std variant for libfoo not detected as a rustlib-defined rlib dependency for device rust_test module")
69 }
70 if !android.InList("libbar.rlib-std", testingModule.Properties.AndroidMkRlibs) {
71 t.Errorf("rlib-std variant for libbar not detected as an rlib dependency for device rust_test module")
72 }
73 if !android.InList("libstd", testingModule.Properties.AndroidMkRlibs) {
74 t.Errorf("Device rust_test module 'my_test' does not link libstd as an rlib")
75 }
76}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -040077
78func TestDataLibs(t *testing.T) {
79 bp := `
80 cc_library {
81 name: "test_lib",
82 srcs: ["test_lib.cpp"],
83 }
84
85 rust_binary {
86 name: "rusty",
87 srcs: ["foo.rs"],
88 compile_multilib: "both",
89 }
90
91 rust_ffi {
92 name: "librust_test_lib",
93 crate_name: "rust_test_lib",
94 srcs: ["test_lib.rs"],
95 relative_install_path: "foo/bar/baz",
96 compile_multilib: "64",
97 }
98
99 rust_test {
100 name: "main_test",
101 srcs: ["foo.rs"],
102 data_libs: ["test_lib"],
103 data_bins: ["rusty"],
104 }
105 `
106
107 ctx := testRust(t, bp)
108
mrziwang0cbd3b02024-06-20 16:39:25 -0700109 testingModule := ctx.ModuleForTests("main_test", "android_arm64_armv8-a")
110 testBinary := testingModule.Module().(*Module).compiler.(*testDecorator)
Yu Liu51c22312024-08-20 23:56:15 +0000111 outputFiles := testingModule.OutputFiles(ctx, t, "")
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400112 if len(outputFiles) != 1 {
113 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
114 }
115 if len(testBinary.dataPaths()) != 2 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800116 t.Fatalf("expected exactly two test data files. test data files: [%v]", testBinary.dataPaths())
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400117 }
118
119 outputPath := outputFiles[0].String()
120 dataLibraryPath := testBinary.dataPaths()[0].SrcPath.String()
121 dataBinaryPath := testBinary.dataPaths()[1].SrcPath.String()
122
123 if !strings.HasSuffix(outputPath, "/main_test") {
124 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
125 }
126 if !strings.HasSuffix(dataLibraryPath, "/test_lib.so") {
127 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", dataLibraryPath)
128 }
129 if !strings.HasSuffix(dataBinaryPath, "/rusty") {
130 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", dataBinaryPath)
131 }
132}
133
134func TestDataLibsRelativeInstallPath(t *testing.T) {
135 bp := `
136 cc_library {
137 name: "test_lib",
138 srcs: ["test_lib.cpp"],
139 relative_install_path: "foo/bar/baz",
140 compile_multilib: "64",
141 }
142
143 rust_ffi {
144 name: "librust_test_lib",
145 crate_name: "rust_test_lib",
146 srcs: ["test_lib.rs"],
147 relative_install_path: "foo/bar/baz",
148 compile_multilib: "64",
149 }
150
151 rust_binary {
152 name: "rusty",
153 srcs: ["foo.rs"],
154 relative_install_path: "foo/bar/baz",
155 compile_multilib: "64",
156 }
157
158 rust_test {
159 name: "main_test",
160 srcs: ["foo.rs"],
161 data_libs: ["test_lib", "librust_test_lib"],
162 data_bins: ["rusty"],
163 compile_multilib: "64",
164 }
165 `
166
167 ctx := testRust(t, bp)
mrziwang0cbd3b02024-06-20 16:39:25 -0700168 testingModule := ctx.ModuleForTests("main_test", "android_arm64_armv8-a")
169 module := testingModule.Module()
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400170 testBinary := module.(*Module).compiler.(*testDecorator)
Yu Liu51c22312024-08-20 23:56:15 +0000171 outputFiles := testingModule.OutputFiles(ctx, t, "")
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400172 if len(outputFiles) != 1 {
173 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
174 }
175 if len(testBinary.dataPaths()) != 3 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800176 t.Fatalf("expected exactly two test data files. test data files: [%v]", testBinary.dataPaths())
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400177 }
178
179 outputPath := outputFiles[0].String()
180
181 if !strings.HasSuffix(outputPath, "/main_test") {
182 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
183 }
184 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Jooyung Han10bea7d2022-04-29 02:04:30 +0900185 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:lib64/foo/bar/baz") {
186 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:lib64/foo/bar/baz`,"+
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400187 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
188 }
Jooyung Han10bea7d2022-04-29 02:04:30 +0900189 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":librust_test_lib.so:lib64/foo/bar/baz") {
190 t.Errorf("expected LOCAL_TEST_DATA to end with `:librust_test_lib.so:lib64/foo/bar/baz`,"+
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400191 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
192 }
193 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][2], ":rusty:foo/bar/baz") {
194 t.Errorf("expected LOCAL_TEST_DATA to end with `:rusty:foo/bar/baz`,"+
195 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][2])
196 }
197}