blob: f31fc9ea98f60079432571ba54ead5ef164d4e57 [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 Jung24788182019-02-04 14:34:10 -080031 ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(prebuiltEtcHostFactory))
Tao Bao0ba5c942018-08-14 22:20:22 -070032 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
33 ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel()
34 })
35 ctx.Register()
36 mockFiles := map[string][]byte{
37 "Android.bp": []byte(bp),
38 "foo.conf": nil,
39 "bar.conf": nil,
40 "baz.conf": nil,
41 }
42 ctx.MockFileSystem(mockFiles)
43 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
44 FailIfErrored(t, errs)
45 _, errs = ctx.PrepareBuildActions(config)
46 FailIfErrored(t, errs)
47
48 return ctx
49}
50
51func setUp(t *testing.T) (config Config, buildDir string) {
52 buildDir, err := ioutil.TempDir("", "soong_prebuilt_etc_test")
53 if err != nil {
54 t.Fatal(err)
55 }
56
57 config = TestArchConfig(buildDir, nil)
58 return
59}
60
61func tearDown(buildDir string) {
62 os.RemoveAll(buildDir)
63}
64
65func TestPrebuiltEtcVariants(t *testing.T) {
66 ctx := testPrebuiltEtc(t, `
67 prebuilt_etc {
68 name: "foo.conf",
69 src: "foo.conf",
70 }
71 prebuilt_etc {
72 name: "bar.conf",
73 src: "bar.conf",
74 recovery_available: true,
75 }
76 prebuilt_etc {
77 name: "baz.conf",
78 src: "baz.conf",
79 recovery: true,
80 }
81 `)
82
83 foo_variants := ctx.ModuleVariantsForTests("foo.conf")
84 if len(foo_variants) != 1 {
85 t.Errorf("expected 1, got %#v", foo_variants)
86 }
87
88 bar_variants := ctx.ModuleVariantsForTests("bar.conf")
89 if len(bar_variants) != 2 {
90 t.Errorf("expected 2, got %#v", bar_variants)
91 }
92
93 baz_variants := ctx.ModuleVariantsForTests("baz.conf")
94 if len(baz_variants) != 1 {
95 t.Errorf("expected 1, got %#v", bar_variants)
96 }
97}
Jiyong Park139a2e62018-10-26 21:49:39 +090098
99func TestPrebuiltEtcOutputPath(t *testing.T) {
100 ctx := testPrebuiltEtc(t, `
101 prebuilt_etc {
102 name: "foo.conf",
103 src: "foo.conf",
104 filename: "foo.installed.conf",
105 }
106 `)
107
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800108 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +0900109 if p.outputFilePath.Base() != "foo.installed.conf" {
110 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
111 }
112}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900113
114func TestPrebuiltEtcGlob(t *testing.T) {
115 ctx := testPrebuiltEtc(t, `
116 prebuilt_etc {
117 name: "my_foo",
118 src: "foo.*",
119 }
120 prebuilt_etc {
121 name: "my_bar",
122 src: "bar.*",
123 filename_from_src: true,
124 }
125 `)
126
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800127 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900128 if p.outputFilePath.Base() != "my_foo" {
129 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
130 }
131
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800132 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900133 if p.outputFilePath.Base() != "bar.conf" {
134 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
135 }
136}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000137
138func TestPrebuiltEtcAndroidMk(t *testing.T) {
139 ctx := testPrebuiltEtc(t, `
140 prebuilt_etc {
141 name: "foo",
142 src: "foo.conf",
143 owner: "abc",
144 filename_from_src: true,
145 }
146 `)
147
148 data := AndroidMkData{}
149 data.Required = append(data.Required, "modA", "moduleB")
150
151 expected := map[string]string{
152 "LOCAL_MODULE": "foo",
153 "LOCAL_MODULE_CLASS": "ETC",
154 "LOCAL_MODULE_OWNER": "abc",
155 "LOCAL_INSTALLED_MODULE_STEM": "foo.conf",
156 "LOCAL_REQUIRED_MODULES": "modA moduleB",
157 }
158
159 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
160 buf := &bytes.Buffer{}
161 mod.AndroidMk().Custom(buf, "foo", "", "", data)
162 for k, expected := range expected {
163 found := false
164 scanner := bufio.NewScanner(bytes.NewReader(buf.Bytes()))
165 for scanner.Scan() {
166 line := scanner.Text()
167 tok := strings.Split(line, " := ")
168 if tok[0] == k {
169 found = true
170 if tok[1] != expected {
171 t.Errorf("Incorrect %s '%s', expected '%s'", k, tok[1], expected)
172 }
173 }
174 }
175
176 if !found {
177 t.Errorf("No %s defined, saw %s", k, buf.String())
178 }
179 }
180}
Jaewoong Jung24788182019-02-04 14:34:10 -0800181
182func TestPrebuiltEtcHost(t *testing.T) {
183 ctx := testPrebuiltEtc(t, `
184 prebuilt_etc_host {
185 name: "foo.conf",
186 src: "foo.conf",
187 }
188 `)
189
190 buildOS := BuildOs.String()
191 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
192 if !p.Host() {
193 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
194 }
195}