Kiyoung Kim | ab9a31c | 2021-07-23 15:47:56 +0900 | [diff] [blame] | 1 | // Copyright 2021 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package etc |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
| 24 | var registerSourceModule = func(ctx android.RegistrationContext) { |
| 25 | ctx.RegisterModuleType("source", newSourceModule) |
| 26 | } |
| 27 | |
| 28 | type sourceModuleProperties struct { |
| 29 | Deps []string `android:"path,arch_variant"` |
| 30 | } |
| 31 | |
| 32 | type sourceModule struct { |
| 33 | android.ModuleBase |
| 34 | android.OverridableModuleBase |
| 35 | |
| 36 | properties sourceModuleProperties |
| 37 | dependsOnSourceModule, dependsOnPrebuiltModule bool |
| 38 | deps android.Paths |
| 39 | src android.Path |
| 40 | } |
| 41 | |
| 42 | func newSourceModule() android.Module { |
| 43 | m := &sourceModule{} |
| 44 | m.AddProperties(&m.properties) |
| 45 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibFirst) |
| 46 | android.InitOverridableModule(m, nil) |
| 47 | return m |
| 48 | } |
| 49 | |
| 50 | func (s *sourceModule) OverridablePropertiesDepsMutator(ctx android.BottomUpMutatorContext) { |
| 51 | // s.properties.Deps are annotated with android:path, so they are |
| 52 | // automatically added to the dependency by pathDeps mutator |
| 53 | } |
| 54 | |
| 55 | func (s *sourceModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 56 | s.deps = android.PathsForModuleSrc(ctx, s.properties.Deps) |
| 57 | s.src = android.PathForModuleSrc(ctx, "source_file") |
| 58 | } |
| 59 | |
| 60 | func (s *sourceModule) Srcs() android.Paths { |
| 61 | return android.Paths{s.src} |
| 62 | } |
| 63 | |
| 64 | var prepareForSnapshotEtcTest = android.GroupFixturePreparers( |
| 65 | android.PrepareForTestWithArchMutator, |
| 66 | android.PrepareForTestWithPrebuilts, |
| 67 | PrepareForTestWithPrebuiltEtc, |
| 68 | android.FixtureRegisterWithContext(RegisterSnapshotEtcModule), |
| 69 | android.FixtureRegisterWithContext(registerSourceModule), |
| 70 | android.FixtureMergeMockFs(android.MockFS{ |
| 71 | "foo.conf": nil, |
| 72 | "bar.conf": nil, |
| 73 | }), |
| 74 | ) |
| 75 | |
| 76 | func TestSnapshotWithFilename(t *testing.T) { |
| 77 | var androidBp = ` |
| 78 | snapshot_etc { |
| 79 | name: "etc_module", |
| 80 | src: "foo.conf", |
| 81 | filename: "bar.conf", |
| 82 | } |
| 83 | ` |
| 84 | |
| 85 | result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp) |
| 86 | for _, variant := range result.ModuleVariantsForTests("etc_module") { |
| 87 | module := result.ModuleForTests("etc_module", variant) |
| 88 | s, ok := module.Module().(*SnapshotEtc) |
| 89 | if !ok { |
| 90 | t.Errorf("Expected snapshot_etc module type") |
| 91 | } |
| 92 | if s.outputFilePath.Base() != "bar.conf" { |
| 93 | t.Errorf("Output file path does not match with specified filename") |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestSnapshotEtcWithOrigin(t *testing.T) { |
| 99 | var androidBp = ` |
| 100 | prebuilt_etc { |
| 101 | name: "etc_module", |
| 102 | src: "foo.conf", |
| 103 | } |
| 104 | |
| 105 | snapshot_etc { |
| 106 | name: "etc_module", |
| 107 | src: "bar.conf", |
| 108 | } |
| 109 | |
| 110 | source { |
| 111 | name: "source", |
| 112 | deps: [":etc_module"], |
| 113 | } |
| 114 | ` |
| 115 | |
| 116 | result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp) |
| 117 | |
| 118 | for _, variant := range result.ModuleVariantsForTests("source") { |
| 119 | source := result.ModuleForTests("source", variant) |
| 120 | |
| 121 | result.VisitDirectDeps(source.Module(), func(m blueprint.Module) { |
| 122 | if _, ok := m.(*PrebuiltEtc); !ok { |
| 123 | t.Errorf("Original prebuilt_etc module expected.") |
| 124 | } |
| 125 | }) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestSnapshotEtcWithOriginAndPrefer(t *testing.T) { |
| 130 | var androidBp = ` |
| 131 | prebuilt_etc { |
| 132 | name: "etc_module", |
| 133 | src: "foo.conf", |
| 134 | } |
| 135 | |
| 136 | snapshot_etc { |
| 137 | name: "etc_module", |
| 138 | src: "bar.conf", |
| 139 | prefer: true, |
| 140 | } |
| 141 | |
| 142 | source { |
| 143 | name: "source", |
| 144 | deps: [":etc_module"], |
| 145 | } |
| 146 | ` |
| 147 | |
| 148 | result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp) |
| 149 | |
| 150 | for _, variant := range result.ModuleVariantsForTests("source") { |
| 151 | source := result.ModuleForTests("source", variant) |
| 152 | |
| 153 | result.VisitDirectDeps(source.Module(), func(m blueprint.Module) { |
| 154 | if _, ok := m.(*SnapshotEtc); !ok { |
| 155 | t.Errorf("Preferred snapshot_etc module expected.") |
| 156 | } |
| 157 | }) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestSnapshotEtcWithoutOrigin(t *testing.T) { |
| 162 | var androidBp = ` |
| 163 | snapshot_etc { |
| 164 | name: "etc_module", |
| 165 | src: "bar.conf", |
| 166 | } |
| 167 | |
| 168 | source { |
| 169 | name: "source", |
| 170 | deps: [":etc_module"], |
| 171 | } |
| 172 | ` |
| 173 | |
| 174 | result := prepareForSnapshotEtcTest.RunTestWithBp(t, androidBp) |
| 175 | |
| 176 | for _, variant := range result.ModuleVariantsForTests("source") { |
| 177 | source := result.ModuleForTests("source", variant) |
| 178 | |
| 179 | result.VisitDirectDeps(source.Module(), func(m blueprint.Module) { |
| 180 | if _, ok := m.(*SnapshotEtc); !ok { |
| 181 | t.Errorf("Only source snapshot_etc module expected.") |
| 182 | } |
| 183 | }) |
| 184 | } |
| 185 | } |