1package codegen 2 3import ( 4 "fmt" 5 "testing" 6 7 "android/soong/android" 8 "android/soong/rust" 9) 10 11func TestRustAconfigLibrary(t *testing.T) { 12 result := android.GroupFixturePreparers( 13 PrepareForTestWithAconfigBuildComponents, 14 rust.PrepareForIntegrationTestWithRust, 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 } 26 rust_library { 27 name: "liblazy_static", // test mock 28 crate_name: "lazy_static", 29 srcs: ["src/lib.rs"], 30 } 31 rust_library { 32 name: "libaconfig_storage_read_api", // test mock 33 crate_name: "aconfig_storage_read_api", 34 srcs: ["lib.rs"], 35 } 36 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 } 46 aconfig_declarations { 47 name: "my_aconfig_declarations", 48 package: "com.example.package", 49 container: "com.android.foo", 50 srcs: ["foo.aconfig"], 51 } 52 53 rust_aconfig_library { 54 name: "libmy_rust_aconfig_library", 55 crate_name: "my_rust_aconfig_library", 56 aconfig_declarations: "my_aconfig_declarations", 57 } 58 `)) 59 60 sourceVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_source") 61 rule := sourceVariant.Rule("rust_aconfig_library") 62 android.AssertStringEquals(t, "rule must contain production mode", rule.Args["mode"], "production") 63 64 dylibVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_dylib") 65 rlibRlibStdVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_rlib_rlib-std") 66 rlibDylibStdVariant := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_rlib_dylib-std") 67 68 variants := []android.TestingModule{ 69 dylibVariant, 70 rlibDylibStdVariant, 71 rlibRlibStdVariant, 72 } 73 74 for _, variant := range variants { 75 android.AssertStringEquals( 76 t, 77 "dylib variant builds from generated rust code", 78 "out/soong/.intermediates/libmy_rust_aconfig_library/android_arm64_armv8-a_source/gen/src/lib.rs", 79 variant.Rule("rustc").Inputs[0].RelativeToTop().String(), 80 ) 81 } 82} 83 84var rustCodegenModeTestData = []struct { 85 setting, expected string 86}{ 87 {"", "production"}, 88 {"mode: `production`,", "production"}, 89 {"mode: `test`,", "test"}, 90 {"mode: `exported`,", "exported"}, 91 {"mode: `force-read-only`,", "force-read-only"}, 92} 93 94func TestRustCodegenMode(t *testing.T) { 95 for _, testData := range rustCodegenModeTestData { 96 testRustCodegenModeHelper(t, testData.setting, testData.expected) 97 } 98} 99 100func testRustCodegenModeHelper(t *testing.T, bpMode string, ruleMode string) { 101 t.Helper() 102 result := android.GroupFixturePreparers( 103 PrepareForTestWithAconfigBuildComponents, 104 rust.PrepareForIntegrationTestWithRust). 105 ExtendWithErrorHandler(android.FixtureExpectsNoErrors). 106 RunTestWithBp(t, fmt.Sprintf(` 107 rust_library { 108 name: "libflags_rust", // test mock 109 crate_name: "flags_rust", 110 srcs: ["lib.rs"], 111 } 112 rust_library { 113 name: "liblazy_static", // test mock 114 crate_name: "lazy_static", 115 srcs: ["src/lib.rs"], 116 } 117 rust_library { 118 name: "libaconfig_storage_read_api", // test mock 119 crate_name: "aconfig_storage_read_api", 120 srcs: ["lib.rs"], 121 } 122 rust_library { 123 name: "liblogger", // test mock 124 crate_name: "logger", 125 srcs: ["lib.rs"], 126 } 127 rust_library { 128 name: "liblog_rust", // test mock 129 crate_name: "log_rust", 130 srcs: ["lib.rs"], 131 } 132 aconfig_declarations { 133 name: "my_aconfig_declarations", 134 package: "com.example.package", 135 container: "com.android.foo", 136 srcs: ["foo.aconfig"], 137 } 138 rust_aconfig_library { 139 name: "libmy_rust_aconfig_library", 140 crate_name: "my_rust_aconfig_library", 141 aconfig_declarations: "my_aconfig_declarations", 142 %s 143 } 144 `, bpMode)) 145 146 module := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_source") 147 rule := module.Rule("rust_aconfig_library") 148 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode) 149} 150 151var incorrectRustCodegenModeTestData = []struct { 152 setting, expectedErr string 153}{ 154 {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"}, 155} 156 157func TestIncorrectRustCodegenMode(t *testing.T) { 158 for _, testData := range incorrectRustCodegenModeTestData { 159 testIncorrectRustCodegenModeHelper(t, testData.setting, testData.expectedErr) 160 } 161} 162 163func testIncorrectRustCodegenModeHelper(t *testing.T, bpMode string, err string) { 164 t.Helper() 165 android.GroupFixturePreparers( 166 PrepareForTestWithAconfigBuildComponents, 167 rust.PrepareForIntegrationTestWithRust). 168 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)). 169 RunTestWithBp(t, fmt.Sprintf(` 170 rust_library { 171 name: "libflags_rust", // test mock 172 crate_name: "flags_rust", 173 srcs: ["lib.rs"], 174 } 175 rust_library { 176 name: "liblazy_static", // test mock 177 crate_name: "lazy_static", 178 srcs: ["src/lib.rs"], 179 } 180 rust_library { 181 name: "libaconfig_storage_read_api", // test mock 182 crate_name: "aconfig_storage_read_api", 183 srcs: ["lib.rs"], 184 } 185 rust_library { 186 name: "liblogger", // test mock 187 crate_name: "logger", 188 srcs: ["lib.rs"], 189 } 190 rust_library { 191 name: "liblog_rust", // test mock 192 crate_name: "log_rust", 193 srcs: ["lib.rs"], 194 } 195 aconfig_declarations { 196 name: "my_aconfig_declarations", 197 package: "com.example.package", 198 container: "com.android.foo", 199 srcs: ["foo.aconfig"], 200 } 201 rust_aconfig_library { 202 name: "libmy_rust_aconfig_library", 203 crate_name: "my_rust_aconfig_library", 204 aconfig_declarations: "my_aconfig_declarations", 205 %s 206 } 207 `, bpMode)) 208} 209