blob: 206f53b2a2842c828821db8251bf890ca7262284 [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 (
Anton Hanssonce0e2582019-02-04 14:19:27 +000018 "bufio"
19 "bytes"
Tao Bao0ba5c942018-08-14 22:20:22 -070020 "io/ioutil"
21 "os"
Anton Hanssonce0e2582019-02-04 14:19:27 +000022 "strings"
Tao Bao0ba5c942018-08-14 22:20:22 -070023 "testing"
24)
25
26func testPrebuiltEtc(t *testing.T, bp string) *TestContext {
27 config, buildDir := setUp(t)
28 defer tearDown(buildDir)
29 ctx := NewTestArchContext()
30 ctx.RegisterModuleType("prebuilt_etc", ModuleFactoryAdaptor(PrebuiltEtcFactory))
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -080031 ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(PrebuiltEtcHostFactory))
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080032 ctx.RegisterModuleType("prebuilt_usr_share", ModuleFactoryAdaptor(PrebuiltUserShareFactory))
Tao Bao0ba5c942018-08-14 22:20:22 -070033 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
34 ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel()
35 })
36 ctx.Register()
37 mockFiles := map[string][]byte{
38 "Android.bp": []byte(bp),
39 "foo.conf": nil,
40 "bar.conf": nil,
41 "baz.conf": nil,
42 }
43 ctx.MockFileSystem(mockFiles)
44 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
45 FailIfErrored(t, errs)
46 _, errs = ctx.PrepareBuildActions(config)
47 FailIfErrored(t, errs)
48
49 return ctx
50}
51
52func setUp(t *testing.T) (config Config, buildDir string) {
53 buildDir, err := ioutil.TempDir("", "soong_prebuilt_etc_test")
54 if err != nil {
55 t.Fatal(err)
56 }
57
58 config = TestArchConfig(buildDir, nil)
59 return
60}
61
62func tearDown(buildDir string) {
63 os.RemoveAll(buildDir)
64}
65
66func TestPrebuiltEtcVariants(t *testing.T) {
67 ctx := testPrebuiltEtc(t, `
68 prebuilt_etc {
69 name: "foo.conf",
70 src: "foo.conf",
71 }
72 prebuilt_etc {
73 name: "bar.conf",
74 src: "bar.conf",
75 recovery_available: true,
76 }
77 prebuilt_etc {
78 name: "baz.conf",
79 src: "baz.conf",
80 recovery: true,
81 }
82 `)
83
84 foo_variants := ctx.ModuleVariantsForTests("foo.conf")
85 if len(foo_variants) != 1 {
86 t.Errorf("expected 1, got %#v", foo_variants)
87 }
88
89 bar_variants := ctx.ModuleVariantsForTests("bar.conf")
90 if len(bar_variants) != 2 {
91 t.Errorf("expected 2, got %#v", bar_variants)
92 }
93
94 baz_variants := ctx.ModuleVariantsForTests("baz.conf")
95 if len(baz_variants) != 1 {
96 t.Errorf("expected 1, got %#v", bar_variants)
97 }
98}
Jiyong Park139a2e62018-10-26 21:49:39 +090099
100func TestPrebuiltEtcOutputPath(t *testing.T) {
101 ctx := testPrebuiltEtc(t, `
102 prebuilt_etc {
103 name: "foo.conf",
104 src: "foo.conf",
105 filename: "foo.installed.conf",
106 }
107 `)
108
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800109 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +0900110 if p.outputFilePath.Base() != "foo.installed.conf" {
111 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
112 }
113}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900114
115func TestPrebuiltEtcGlob(t *testing.T) {
116 ctx := testPrebuiltEtc(t, `
117 prebuilt_etc {
118 name: "my_foo",
119 src: "foo.*",
120 }
121 prebuilt_etc {
122 name: "my_bar",
123 src: "bar.*",
124 filename_from_src: true,
125 }
126 `)
127
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800128 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900129 if p.outputFilePath.Base() != "my_foo" {
130 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
131 }
132
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800133 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900134 if p.outputFilePath.Base() != "bar.conf" {
135 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
136 }
137}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000138
139func TestPrebuiltEtcAndroidMk(t *testing.T) {
140 ctx := testPrebuiltEtc(t, `
141 prebuilt_etc {
142 name: "foo",
143 src: "foo.conf",
144 owner: "abc",
145 filename_from_src: true,
146 }
147 `)
148
149 data := AndroidMkData{}
150 data.Required = append(data.Required, "modA", "moduleB")
151
152 expected := map[string]string{
153 "LOCAL_MODULE": "foo",
154 "LOCAL_MODULE_CLASS": "ETC",
155 "LOCAL_MODULE_OWNER": "abc",
156 "LOCAL_INSTALLED_MODULE_STEM": "foo.conf",
157 "LOCAL_REQUIRED_MODULES": "modA moduleB",
158 }
159
160 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
161 buf := &bytes.Buffer{}
162 mod.AndroidMk().Custom(buf, "foo", "", "", data)
163 for k, expected := range expected {
164 found := false
165 scanner := bufio.NewScanner(bytes.NewReader(buf.Bytes()))
166 for scanner.Scan() {
167 line := scanner.Text()
168 tok := strings.Split(line, " := ")
169 if tok[0] == k {
170 found = true
171 if tok[1] != expected {
172 t.Errorf("Incorrect %s '%s', expected '%s'", k, tok[1], expected)
173 }
174 }
175 }
176
177 if !found {
178 t.Errorf("No %s defined, saw %s", k, buf.String())
179 }
180 }
181}
Jaewoong Jung24788182019-02-04 14:34:10 -0800182
183func TestPrebuiltEtcHost(t *testing.T) {
184 ctx := testPrebuiltEtc(t, `
185 prebuilt_etc_host {
186 name: "foo.conf",
187 src: "foo.conf",
188 }
189 `)
190
191 buildOS := BuildOs.String()
192 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
193 if !p.Host() {
194 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
195 }
196}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800197
198func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
199 ctx := testPrebuiltEtc(t, `
200 prebuilt_usr_share {
201 name: "foo.conf",
202 src: "foo.conf",
203 sub_dir: "bar",
204 }
205 `)
206
207 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
208 expected := "target/product/test_device/system/usr/share/bar"
209 if p.installDirPath.RelPathString() != expected {
210 t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString())
211 }
212}