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