blob: a5c44809d252bbf4560537a29c28e985ed7f03d7 [file] [log] [blame]
Tao Bao0ba5c942018-08-14 22:20:22 -07001// Copyright 2018 Google Inc. All rights reserved.
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
15package android
16
17import (
18 "io/ioutil"
19 "os"
Patrice Arruda300cef92019-02-22 15:47:57 -080020 "path/filepath"
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070021 "reflect"
Tao Bao0ba5c942018-08-14 22:20:22 -070022 "testing"
23)
24
Patrice Arruda300cef92019-02-22 15:47:57 -080025func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) {
Tao Bao0ba5c942018-08-14 22:20:22 -070026 config, buildDir := setUp(t)
27 defer tearDown(buildDir)
28 ctx := NewTestArchContext()
29 ctx.RegisterModuleType("prebuilt_etc", ModuleFactoryAdaptor(PrebuiltEtcFactory))
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -080030 ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(PrebuiltEtcHostFactory))
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080031 ctx.RegisterModuleType("prebuilt_usr_share", ModuleFactoryAdaptor(PrebuiltUserShareFactory))
Patrice Arruda300cef92019-02-22 15:47:57 -080032 ctx.RegisterModuleType("prebuilt_usr_share_host", ModuleFactoryAdaptor(PrebuiltUserShareHostFactory))
Patrice Arruda61583eb2019-05-14 08:20:45 -070033 ctx.RegisterModuleType("prebuilt_font", ModuleFactoryAdaptor(PrebuiltFontFactory))
Tao Bao0ba5c942018-08-14 22:20:22 -070034 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
35 ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel()
36 })
37 ctx.Register()
38 mockFiles := map[string][]byte{
39 "Android.bp": []byte(bp),
40 "foo.conf": nil,
41 "bar.conf": nil,
42 "baz.conf": nil,
43 }
44 ctx.MockFileSystem(mockFiles)
45 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
46 FailIfErrored(t, errs)
47 _, errs = ctx.PrepareBuildActions(config)
48 FailIfErrored(t, errs)
49
Patrice Arruda300cef92019-02-22 15:47:57 -080050 return ctx, config
Tao Bao0ba5c942018-08-14 22:20:22 -070051}
52
53func setUp(t *testing.T) (config Config, buildDir string) {
54 buildDir, err := ioutil.TempDir("", "soong_prebuilt_etc_test")
55 if err != nil {
56 t.Fatal(err)
57 }
58
59 config = TestArchConfig(buildDir, nil)
60 return
61}
62
63func tearDown(buildDir string) {
64 os.RemoveAll(buildDir)
65}
66
67func TestPrebuiltEtcVariants(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080068 ctx, _ := testPrebuiltEtc(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070069 prebuilt_etc {
70 name: "foo.conf",
71 src: "foo.conf",
72 }
73 prebuilt_etc {
74 name: "bar.conf",
75 src: "bar.conf",
76 recovery_available: true,
77 }
78 prebuilt_etc {
79 name: "baz.conf",
80 src: "baz.conf",
81 recovery: true,
82 }
83 `)
84
85 foo_variants := ctx.ModuleVariantsForTests("foo.conf")
86 if len(foo_variants) != 1 {
87 t.Errorf("expected 1, got %#v", foo_variants)
88 }
89
90 bar_variants := ctx.ModuleVariantsForTests("bar.conf")
91 if len(bar_variants) != 2 {
92 t.Errorf("expected 2, got %#v", bar_variants)
93 }
94
95 baz_variants := ctx.ModuleVariantsForTests("baz.conf")
96 if len(baz_variants) != 1 {
97 t.Errorf("expected 1, got %#v", bar_variants)
98 }
99}
Jiyong Park139a2e62018-10-26 21:49:39 +0900100
101func TestPrebuiltEtcOutputPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800102 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +0900103 prebuilt_etc {
104 name: "foo.conf",
105 src: "foo.conf",
106 filename: "foo.installed.conf",
107 }
108 `)
109
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800110 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +0900111 if p.outputFilePath.Base() != "foo.installed.conf" {
112 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
113 }
114}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900115
116func TestPrebuiltEtcGlob(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800117 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +0900118 prebuilt_etc {
119 name: "my_foo",
120 src: "foo.*",
121 }
122 prebuilt_etc {
123 name: "my_bar",
124 src: "bar.*",
125 filename_from_src: true,
126 }
127 `)
128
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800129 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900130 if p.outputFilePath.Base() != "my_foo" {
131 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
132 }
133
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800134 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900135 if p.outputFilePath.Base() != "bar.conf" {
136 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
137 }
138}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000139
140func TestPrebuiltEtcAndroidMk(t *testing.T) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700141 ctx, config := testPrebuiltEtc(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000142 prebuilt_etc {
143 name: "foo",
144 src: "foo.conf",
145 owner: "abc",
146 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700147 required: ["modA", "moduleB"],
148 host_required: ["hostModA", "hostModB"],
149 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000150 }
151 `)
152
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700153 expected := map[string][]string{
154 "LOCAL_MODULE": {"foo"},
155 "LOCAL_MODULE_CLASS": {"ETC"},
156 "LOCAL_MODULE_OWNER": {"abc"},
157 "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
158 "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
159 "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
160 "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
Anton Hanssonce0e2582019-02-04 14:19:27 +0000161 }
162
163 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700164 entries := AndroidMkEntriesForTest(t, config, "", mod)
165 for k, expectedValue := range expected {
166 if value, ok := entries.EntryMap[k]; ok {
167 if !reflect.DeepEqual(value, expectedValue) {
168 t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000169 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700170 } else {
171 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000172 }
173 }
174}
Jaewoong Jung24788182019-02-04 14:34:10 -0800175
176func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800177 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800178 prebuilt_etc_host {
179 name: "foo.conf",
180 src: "foo.conf",
181 }
182 `)
183
184 buildOS := BuildOs.String()
185 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
186 if !p.Host() {
187 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
188 }
189}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800190
191func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800192 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800193 prebuilt_usr_share {
194 name: "foo.conf",
195 src: "foo.conf",
196 sub_dir: "bar",
197 }
198 `)
199
200 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
201 expected := "target/product/test_device/system/usr/share/bar"
202 if p.installDirPath.RelPathString() != expected {
203 t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString())
204 }
205}
Patrice Arruda300cef92019-02-22 15:47:57 -0800206
207func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
208 ctx, config := testPrebuiltEtc(t, `
209 prebuilt_usr_share_host {
210 name: "foo.conf",
211 src: "foo.conf",
212 sub_dir: "bar",
213 }
214 `)
215
216 buildOS := BuildOs.String()
217 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
218 expected := filepath.Join("host", config.PrebuiltOS(), "usr", "share", "bar")
219 if p.installDirPath.RelPathString() != expected {
220 t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString())
221 }
222}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700223
224func TestPrebuiltFontInstallDirPath(t *testing.T) {
225 ctx, _ := testPrebuiltEtc(t, `
226 prebuilt_font {
227 name: "foo.conf",
228 src: "foo.conf",
229 }
230 `)
231
232 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
233 expected := "target/product/test_device/system/fonts"
234 if p.installDirPath.RelPathString() != expected {
235 t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString())
236 }
237}