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"}, |
Zhi Dou | 70e2124 | 2023-12-20 23:14:34 +0000 | [diff] [blame^] | 75 | {"mode: `force-read-only`,", "force-read-only"}, |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func TestRustCodegenMode(t *testing.T) { |
| 79 | for _, testData := range rustCodegenModeTestData { |
| 80 | testRustCodegenModeHelper(t, testData.setting, testData.expected) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func 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 | |
| 119 | var incorrectRustCodegenModeTestData = []struct { |
| 120 | setting, expectedErr string |
| 121 | }{ |
| 122 | {"mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode"}, |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | func TestIncorrectRustCodegenMode(t *testing.T) { |
| 126 | for _, testData := range incorrectRustCodegenModeTestData { |
| 127 | testIncorrectRustCodegenModeHelper(t, testData.setting, testData.expectedErr) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func 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 | } |