blob: 330cecd737efeef9cc6a3fecef9a3590eb977cc9 [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,
14 rust.PrepareForTestWithRustIncludeVndk,
15 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 }
Vinh Tran457ddef2023-08-02 13:50:26 -040036 aconfig_declarations {
37 name: "my_aconfig_declarations",
38 package: "com.example.package",
39 srcs: ["foo.aconfig"],
40 }
41
42 rust_aconfig_library {
43 name: "libmy_rust_aconfig_library",
44 crate_name: "my_rust_aconfig_library",
45 aconfig_declarations: "my_aconfig_declarations",
46 }
47 `))
48
49 sourceVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_source")
50 rule := sourceVariant.Rule("rust_aconfig_library")
51 android.AssertStringEquals(t, "rule must contain production mode", rule.Args["mode"], "production")
52
53 dylibVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_dylib")
54 rlibRlibStdVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_rlib_rlib-std")
55 rlibDylibStdVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_rlib_dylib-std")
56
57 variants := []android.TestingModule{
58 dylibVariant,
59 rlibDylibStdVariant,
60 rlibRlibStdVariant,
61 }
62
63 for _, variant := range variants {
Colin Crossdf0ed702023-09-22 21:59:59 +000064 android.AssertStringEquals(
Vinh Tran457ddef2023-08-02 13:50:26 -040065 t,
66 "dylib variant builds from generated rust code",
67 "out/soong/.intermediates/libmy_rust_aconfig_library/android_arm64_armv8-a_source/gen/src/lib.rs",
Colin Crossdf0ed702023-09-22 21:59:59 +000068 variant.Rule("rustc").Inputs[0].RelativeToTop().String(),
Vinh Tran457ddef2023-08-02 13:50:26 -040069 )
70 }
71}
Zi Wang275f6542023-11-09 14:59:31 -080072
73var rustCodegenModeTestData = []struct {
74 setting, expected string
75}{
76 {"", "production"},
77 {"mode: `production`,", "production"},
78 {"mode: `test`,", "test"},
79 {"mode: `exported`,", "exported"},
Zhi Dou70e21242023-12-20 23:14:34 +000080 {"mode: `force-read-only`,", "force-read-only"},
Zi Wang275f6542023-11-09 14:59:31 -080081}
82
83func TestRustCodegenMode(t *testing.T) {
84 for _, testData := range rustCodegenModeTestData {
85 testRustCodegenModeHelper(t, testData.setting, testData.expected)
86 }
87}
88
89func testRustCodegenModeHelper(t *testing.T, bpMode string, ruleMode string) {
90 t.Helper()
91 result := android.GroupFixturePreparers(
92 PrepareForTestWithAconfigBuildComponents,
93 rust.PrepareForTestWithRustIncludeVndk).
94 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
95 RunTestWithBp(t, fmt.Sprintf(`
96 rust_library {
97 name: "libflags_rust", // test mock
98 crate_name: "flags_rust",
99 srcs: ["lib.rs"],
100 }
101 rust_library {
102 name: "liblazy_static", // test mock
103 crate_name: "lazy_static",
104 srcs: ["src/lib.rs"],
105 }
Ted Bauer02d475c2024-03-27 20:56:26 +0000106 rust_library {
107 name: "libaconfig_storage_read_api", // test mock
108 crate_name: "aconfig_storage_read_api",
109 srcs: ["lib.rs"],
110 }
Zi Wang275f6542023-11-09 14:59:31 -0800111 aconfig_declarations {
112 name: "my_aconfig_declarations",
113 package: "com.example.package",
114 srcs: ["foo.aconfig"],
115 }
116 rust_aconfig_library {
117 name: "libmy_rust_aconfig_library",
118 crate_name: "my_rust_aconfig_library",
119 aconfig_declarations: "my_aconfig_declarations",
120 %s
121 }
122 `, bpMode))
123
124 module := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_source")
125 rule := module.Rule("rust_aconfig_library")
126 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
127}
128
129var incorrectRustCodegenModeTestData = []struct {
130 setting, expectedErr string
131}{
132 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"},
Zi Wang275f6542023-11-09 14:59:31 -0800133}
134
135func TestIncorrectRustCodegenMode(t *testing.T) {
136 for _, testData := range incorrectRustCodegenModeTestData {
137 testIncorrectRustCodegenModeHelper(t, testData.setting, testData.expectedErr)
138 }
139}
140
141func testIncorrectRustCodegenModeHelper(t *testing.T, bpMode string, err string) {
142 t.Helper()
143 android.GroupFixturePreparers(
144 PrepareForTestWithAconfigBuildComponents,
145 rust.PrepareForTestWithRustIncludeVndk).
146 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
147 RunTestWithBp(t, fmt.Sprintf(`
148 rust_library {
149 name: "libflags_rust", // test mock
150 crate_name: "flags_rust",
151 srcs: ["lib.rs"],
152 }
153 rust_library {
154 name: "liblazy_static", // test mock
155 crate_name: "lazy_static",
156 srcs: ["src/lib.rs"],
157 }
Ted Bauer02d475c2024-03-27 20:56:26 +0000158 rust_library {
159 name: "libaconfig_storage_read_api", // test mock
160 crate_name: "aconfig_storage_read_api",
161 srcs: ["lib.rs"],
162 }
Zi Wang275f6542023-11-09 14:59:31 -0800163 aconfig_declarations {
164 name: "my_aconfig_declarations",
165 package: "com.example.package",
166 srcs: ["foo.aconfig"],
167 }
168 rust_aconfig_library {
169 name: "libmy_rust_aconfig_library",
170 crate_name: "my_rust_aconfig_library",
171 aconfig_declarations: "my_aconfig_declarations",
172 %s
173 }
174 `, bpMode))
175}