blob: fbf2baa97fd00a64feb481ef39694cc31fe5ce6d [file] [log] [blame]
Jooyung Han12df5fb2019-07-11 16:18:47 +09001// Copyright 2019 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 java
16
17import (
Jooyung Han12df5fb2019-07-11 16:18:47 +090018 "bytes"
19 "io"
20 "io/ioutil"
21 "strings"
22 "testing"
Jaewoong Jungf9a04432019-07-17 11:15:09 -070023
24 "android/soong/android"
Jooyung Han12df5fb2019-07-11 16:18:47 +090025)
26
27type testAndroidMk struct {
28 *testing.T
29 body []byte
30}
Jaewoong Jungf9a04432019-07-17 11:15:09 -070031
Jooyung Han12df5fb2019-07-11 16:18:47 +090032type testAndroidMkModule struct {
33 *testing.T
34 props map[string]string
35}
36
37func newTestAndroidMk(t *testing.T, r io.Reader) *testAndroidMk {
38 t.Helper()
39 buf, err := ioutil.ReadAll(r)
40 if err != nil {
41 t.Fatal("failed to open read Android.mk.", err)
42 }
43 return &testAndroidMk{
44 T: t,
45 body: buf,
46 }
47}
48
49func parseAndroidMkProps(lines []string) map[string]string {
50 props := make(map[string]string)
51 for _, line := range lines {
52 line = strings.TrimLeft(line, " ")
53 if line == "" || strings.HasPrefix(line, "#") {
54 continue
55 }
56 tokens := strings.Split(line, " ")
57 if tokens[1] == "+=" {
58 props[tokens[0]] += " " + strings.Join(tokens[2:], " ")
59 } else {
60 props[tokens[0]] = strings.Join(tokens[2:], " ")
61 }
62 }
63 return props
64}
65
66func (t *testAndroidMk) moduleFor(moduleName string) *testAndroidMkModule {
67 t.Helper()
68 lines := strings.Split(string(t.body), "\n")
69 index := android.IndexList("LOCAL_MODULE := "+moduleName, lines)
70 if index == -1 {
71 t.Fatalf("%q is not found.", moduleName)
72 }
73 lines = lines[index:]
74 includeIndex := android.IndexListPred(func(line string) bool {
75 return strings.HasPrefix(line, "include")
76 }, lines)
77 if includeIndex == -1 {
78 t.Fatalf("%q is not properly defined. (\"include\" not found).", moduleName)
79 }
80 props := parseAndroidMkProps(lines[:includeIndex])
81 return &testAndroidMkModule{
82 T: t.T,
83 props: props,
84 }
85}
86
87func (t *testAndroidMkModule) hasRequired(dep string) {
88 t.Helper()
89 required, ok := t.props["LOCAL_REQUIRED_MODULES"]
90 if !ok {
91 t.Error("LOCAL_REQUIRED_MODULES is not found.")
92 return
93 }
94 if !android.InList(dep, strings.Split(required, " ")) {
95 t.Errorf("%q is expected in LOCAL_REQUIRED_MODULES, but not found in %q.", dep, required)
96 }
97}
98
99func (t *testAndroidMkModule) hasNoRequired(dep string) {
100 t.Helper()
101 required, ok := t.props["LOCAL_REQUIRED_MODULES"]
102 if !ok {
103 return
104 }
105 if android.InList(dep, strings.Split(required, " ")) {
106 t.Errorf("%q is not expected in LOCAL_REQUIRED_MODULES, but found.", dep)
107 }
108}
109
110func getAndroidMk(t *testing.T, ctx *android.TestContext, config android.Config, name string) *testAndroidMk {
111 t.Helper()
112 lib, _ := ctx.ModuleForTests(name, "android_common").Module().(*Library)
113 data := android.AndroidMkDataForTest(t, config, "", lib)
114 w := &bytes.Buffer{}
115 data.Custom(w, name, "", "", data)
116 return newTestAndroidMk(t, w)
117}
118
119func TestRequired(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700120 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900121 java_library {
122 name: "foo",
123 srcs: ["a.java"],
124 required: ["libfoo"],
125 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700126 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900127
128 mk := getAndroidMk(t, ctx, config, "foo")
129 mk.moduleFor("foo").hasRequired("libfoo")
130}
131
132func TestHostdex(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700133 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900134 java_library {
135 name: "foo",
136 srcs: ["a.java"],
137 hostdex: true,
138 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700139 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900140
141 mk := getAndroidMk(t, ctx, config, "foo")
142 mk.moduleFor("foo")
143 mk.moduleFor("foo-hostdex")
144}
145
146func TestHostdexRequired(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700147 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900148 java_library {
149 name: "foo",
150 srcs: ["a.java"],
151 hostdex: true,
152 required: ["libfoo"],
153 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700154 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900155
156 mk := getAndroidMk(t, ctx, config, "foo")
157 mk.moduleFor("foo").hasRequired("libfoo")
158 mk.moduleFor("foo-hostdex").hasRequired("libfoo")
159}
160
161func TestHostdexSpecificRequired(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700162 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900163 java_library {
164 name: "foo",
165 srcs: ["a.java"],
166 hostdex: true,
167 target: {
168 hostdex: {
169 required: ["libfoo"],
170 },
171 },
172 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700173 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900174
175 mk := getAndroidMk(t, ctx, config, "foo")
176 mk.moduleFor("foo").hasNoRequired("libfoo")
177 mk.moduleFor("foo-hostdex").hasRequired("libfoo")
178}