Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame^] | 1 | package codegen |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 2 | |
| 3 | import ( |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 4 | "fmt" |
| 5 | "testing" |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 6 | |
| 7 | "android/soong/android" |
| 8 | "android/soong/rust" |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | func 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 Shen | ba6ed2d | 2023-11-08 21:02:53 +0000 | [diff] [blame] | 26 | rust_library { |
| 27 | name: "liblazy_static", // test mock |
| 28 | crate_name: "lazy_static", |
| 29 | srcs: ["src/lib.rs"], |
| 30 | } |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 31 | 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 Cross | df0ed70 | 2023-09-22 21:59:59 +0000 | [diff] [blame] | 59 | android.AssertStringEquals( |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 60 | 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 Cross | df0ed70 | 2023-09-22 21:59:59 +0000 | [diff] [blame] | 63 | variant.Rule("rustc").Inputs[0].RelativeToTop().String(), |
Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 64 | ) |
| 65 | } |
| 66 | } |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 67 | |
| 68 | var rustCodegenModeTestData = []struct { |
| 69 | setting, expected string |
| 70 | }{ |
| 71 | {"", "production"}, |
| 72 | {"mode: `production`,", "production"}, |
| 73 | {"mode: `test`,", "test"}, |
| 74 | {"mode: `exported`,", "exported"}, |
| 75 | } |
| 76 | |
| 77 | func TestRustCodegenMode(t *testing.T) { |
| 78 | for _, testData := range rustCodegenModeTestData { |
| 79 | testRustCodegenModeHelper(t, testData.setting, testData.expected) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func testRustCodegenModeHelper(t *testing.T, bpMode string, ruleMode string) { |
| 84 | t.Helper() |
| 85 | result := android.GroupFixturePreparers( |
| 86 | PrepareForTestWithAconfigBuildComponents, |
| 87 | rust.PrepareForTestWithRustIncludeVndk). |
| 88 | ExtendWithErrorHandler(android.FixtureExpectsNoErrors). |
| 89 | RunTestWithBp(t, fmt.Sprintf(` |
| 90 | rust_library { |
| 91 | name: "libflags_rust", // test mock |
| 92 | crate_name: "flags_rust", |
| 93 | srcs: ["lib.rs"], |
| 94 | } |
| 95 | rust_library { |
| 96 | name: "liblazy_static", // test mock |
| 97 | crate_name: "lazy_static", |
| 98 | srcs: ["src/lib.rs"], |
| 99 | } |
| 100 | aconfig_declarations { |
| 101 | name: "my_aconfig_declarations", |
| 102 | package: "com.example.package", |
| 103 | srcs: ["foo.aconfig"], |
| 104 | } |
| 105 | rust_aconfig_library { |
| 106 | name: "libmy_rust_aconfig_library", |
| 107 | crate_name: "my_rust_aconfig_library", |
| 108 | aconfig_declarations: "my_aconfig_declarations", |
| 109 | %s |
| 110 | } |
| 111 | `, bpMode)) |
| 112 | |
| 113 | module := result.ModuleForTests("libmy_rust_aconfig_library", "android_arm64_armv8-a_source") |
| 114 | rule := module.Rule("rust_aconfig_library") |
| 115 | android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode) |
| 116 | } |
| 117 | |
| 118 | var incorrectRustCodegenModeTestData = []struct { |
| 119 | setting, expectedErr string |
| 120 | }{ |
| 121 | {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"}, |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | func TestIncorrectRustCodegenMode(t *testing.T) { |
| 125 | for _, testData := range incorrectRustCodegenModeTestData { |
| 126 | testIncorrectRustCodegenModeHelper(t, testData.setting, testData.expectedErr) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func testIncorrectRustCodegenModeHelper(t *testing.T, bpMode string, err string) { |
| 131 | t.Helper() |
| 132 | android.GroupFixturePreparers( |
| 133 | PrepareForTestWithAconfigBuildComponents, |
| 134 | rust.PrepareForTestWithRustIncludeVndk). |
| 135 | ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)). |
| 136 | RunTestWithBp(t, fmt.Sprintf(` |
| 137 | rust_library { |
| 138 | name: "libflags_rust", // test mock |
| 139 | crate_name: "flags_rust", |
| 140 | srcs: ["lib.rs"], |
| 141 | } |
| 142 | rust_library { |
| 143 | name: "liblazy_static", // test mock |
| 144 | crate_name: "lazy_static", |
| 145 | srcs: ["src/lib.rs"], |
| 146 | } |
| 147 | aconfig_declarations { |
| 148 | name: "my_aconfig_declarations", |
| 149 | package: "com.example.package", |
| 150 | srcs: ["foo.aconfig"], |
| 151 | } |
| 152 | rust_aconfig_library { |
| 153 | name: "libmy_rust_aconfig_library", |
| 154 | crate_name: "my_rust_aconfig_library", |
| 155 | aconfig_declarations: "my_aconfig_declarations", |
| 156 | %s |
| 157 | } |
| 158 | `, bpMode)) |
| 159 | } |