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