blob: fe28f944147418a836114b3ae988750e375af30b [file] [log] [blame]
Yu Liueae7b362023-11-16 17:05:47 -08001package codegen
Vinh Tran457ddef2023-08-02 13:50:26 -04002
3import (
Vinh Tran457ddef2023-08-02 13:50:26 -04004 "fmt"
5 "testing"
Zi Wang275f6542023-11-09 14:59:31 -08006
7 "android/soong/android"
8 "android/soong/rust"
Vinh Tran457ddef2023-08-02 13:50:26 -04009)
10
11func TestRustAconfigLibrary(t *testing.T) {
12 result := android.GroupFixturePreparers(
13 PrepareForTestWithAconfigBuildComponents,
Kiyoung Kim1db4a742024-04-01 15:50:01 +090014 rust.PrepareForIntegrationTestWithRust,
Vinh Tran457ddef2023-08-02 13:50:26 -040015 android.PrepareForTestWithArchMutator,
16 android.PrepareForTestWithDefaults,
17 android.PrepareForTestWithPrebuilts,
18 ).
19 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
20 RunTestWithBp(t, fmt.Sprintf(`
21 rust_library {
22 name: "libflags_rust", // test mock
23 crate_name: "flags_rust",
24 srcs: ["lib.rs"],
25 }
Dennis Shenba6ed2d2023-11-08 21:02:53 +000026 rust_library {
27 name: "liblazy_static", // test mock
28 crate_name: "lazy_static",
29 srcs: ["src/lib.rs"],
30 }
Ted Bauer02d475c2024-03-27 20:56:26 +000031 rust_library {
32 name: "libaconfig_storage_read_api", // test mock
33 crate_name: "aconfig_storage_read_api",
34 srcs: ["lib.rs"],
35 }
Ted Bauer6ef40db2024-03-29 14:04:10 +000036 rust_library {
37 name: "liblogger", // test mock
38 crate_name: "logger",
39 srcs: ["lib.rs"],
40 }
41 rust_library {
42 name: "liblog_rust", // test mock
43 crate_name: "log_rust",
44 srcs: ["lib.rs"],
45 }
Vinh Tran457ddef2023-08-02 13:50:26 -040046 aconfig_declarations {
47 name: "my_aconfig_declarations",
48 package: "com.example.package",
49 srcs: ["foo.aconfig"],
50 }
51
52 rust_aconfig_library {
53 name: "libmy_rust_aconfig_library",
54 crate_name: "my_rust_aconfig_library",
55 aconfig_declarations: "my_aconfig_declarations",
56 }
57 `))
58
59 sourceVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_source")
60 rule := sourceVariant.Rule("rust_aconfig_library")
61 android.AssertStringEquals(t, "rule must contain production mode", rule.Args["mode"], "production")
62
63 dylibVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_dylib")
64 rlibRlibStdVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_rlib_rlib-std")
65 rlibDylibStdVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_rlib_dylib-std")
66
67 variants := []android.TestingModule{
68 dylibVariant,
69 rlibDylibStdVariant,
70 rlibRlibStdVariant,
71 }
72
73 for _, variant := range variants {
Colin Crossdf0ed702023-09-22 21:59:59 +000074 android.AssertStringEquals(
Vinh Tran457ddef2023-08-02 13:50:26 -040075 t,
76 "dylib variant builds from generated rust code",
77 "out/soong/.intermediates/libmy_rust_aconfig_library/android_arm64_armv8-a_source/gen/src/lib.rs",
Colin Crossdf0ed702023-09-22 21:59:59 +000078 variant.Rule("rustc").Inputs[0].RelativeToTop().String(),
Vinh Tran457ddef2023-08-02 13:50:26 -040079 )
80 }
81}
Zi Wang275f6542023-11-09 14:59:31 -080082
83var rustCodegenModeTestData = []struct {
84 setting, expected string
85}{
86 {"", "production"},
87 {"mode: `production`,", "production"},
88 {"mode: `test`,", "test"},
89 {"mode: `exported`,", "exported"},
Zhi Dou70e21242023-12-20 23:14:34 +000090 {"mode: `force-read-only`,", "force-read-only"},
Zi Wang275f6542023-11-09 14:59:31 -080091}
92
93func TestRustCodegenMode(t *testing.T) {
94 for _, testData := range rustCodegenModeTestData {
95 testRustCodegenModeHelper(t, testData.setting, testData.expected)
96 }
97}
98
99func testRustCodegenModeHelper(t *testing.T, bpMode string, ruleMode string) {
100 t.Helper()
101 result := android.GroupFixturePreparers(
102 PrepareForTestWithAconfigBuildComponents,
Kiyoung Kim1db4a742024-04-01 15:50:01 +0900103 rust.PrepareForIntegrationTestWithRust).
Zi Wang275f6542023-11-09 14:59:31 -0800104 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
105 RunTestWithBp(t, fmt.Sprintf(`
106 rust_library {
107 name: "libflags_rust", // test mock
108 crate_name: "flags_rust",
109 srcs: ["lib.rs"],
110 }
111 rust_library {
112 name: "liblazy_static", // test mock
113 crate_name: "lazy_static",
114 srcs: ["src/lib.rs"],
115 }
Ted Bauer02d475c2024-03-27 20:56:26 +0000116 rust_library {
117 name: "libaconfig_storage_read_api", // test mock
118 crate_name: "aconfig_storage_read_api",
119 srcs: ["lib.rs"],
120 }
Ted Bauer6ef40db2024-03-29 14:04:10 +0000121 rust_library {
122 name: "liblogger", // test mock
123 crate_name: "logger",
124 srcs: ["lib.rs"],
125 }
126 rust_library {
127 name: "liblog_rust", // test mock
128 crate_name: "log_rust",
129 srcs: ["lib.rs"],
130 }
Zi Wang275f6542023-11-09 14:59:31 -0800131 aconfig_declarations {
132 name: "my_aconfig_declarations",
133 package: "com.example.package",
134 srcs: ["foo.aconfig"],
135 }
136 rust_aconfig_library {
137 name: "libmy_rust_aconfig_library",
138 crate_name: "my_rust_aconfig_library",
139 aconfig_declarations: "my_aconfig_declarations",
140 %s
141 }
142 `, bpMode))
143
144 module := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_source")
145 rule := module.Rule("rust_aconfig_library")
146 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
147}
148
149var incorrectRustCodegenModeTestData = []struct {
150 setting, expectedErr string
151}{
152 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
Zi Wang275f6542023-11-09 14:59:31 -0800153}
154
155func TestIncorrectRustCodegenMode(t *testing.T) {
156 for _, testData := range incorrectRustCodegenModeTestData {
157 testIncorrectRustCodegenModeHelper(t, testData.setting, testData.expectedErr)
158 }
159}
160
161func testIncorrectRustCodegenModeHelper(t *testing.T, bpMode string, err string) {
162 t.Helper()
163 android.GroupFixturePreparers(
164 PrepareForTestWithAconfigBuildComponents,
Kiyoung Kim1db4a742024-04-01 15:50:01 +0900165 rust.PrepareForIntegrationTestWithRust).
Zi Wang275f6542023-11-09 14:59:31 -0800166 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
167 RunTestWithBp(t, fmt.Sprintf(`
168 rust_library {
169 name: "libflags_rust", // test mock
170 crate_name: "flags_rust",
171 srcs: ["lib.rs"],
172 }
173 rust_library {
174 name: "liblazy_static", // test mock
175 crate_name: "lazy_static",
176 srcs: ["src/lib.rs"],
177 }
Ted Bauer02d475c2024-03-27 20:56:26 +0000178 rust_library {
179 name: "libaconfig_storage_read_api", // test mock
180 crate_name: "aconfig_storage_read_api",
181 srcs: ["lib.rs"],
182 }
Ted Bauer6ef40db2024-03-29 14:04:10 +0000183 rust_library {
184 name: "liblogger", // test mock
185 crate_name: "logger",
186 srcs: ["lib.rs"],
187 }
188 rust_library {
189 name: "liblog_rust", // test mock
190 crate_name: "log_rust",
191 srcs: ["lib.rs"],
192 }
Zi Wang275f6542023-11-09 14:59:31 -0800193 aconfig_declarations {
194 name: "my_aconfig_declarations",
195 package: "com.example.package",
196 srcs: ["foo.aconfig"],
197 }
198 rust_aconfig_library {
199 name: "libmy_rust_aconfig_library",
200 crate_name: "my_rust_aconfig_library",
201 aconfig_declarations: "my_aconfig_declarations",
202 %s
203 }
204 `, bpMode))
205}