Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 18 | "bytes" |
| 19 | "io" |
| 20 | "io/ioutil" |
| 21 | "strings" |
| 22 | "testing" |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | type testAndroidMk struct { |
| 28 | *testing.T |
| 29 | body []byte |
| 30 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 31 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 32 | type testAndroidMkModule struct { |
| 33 | *testing.T |
| 34 | props map[string]string |
| 35 | } |
| 36 | |
| 37 | func 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 | |
| 49 | func 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 | |
| 66 | func (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 | |
| 87 | func (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 | |
| 99 | func (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 | |
| 110 | func 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 | |
| 119 | func TestRequired(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 120 | ctx, config := testJava(t, ` |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 121 | java_library { |
| 122 | name: "foo", |
| 123 | srcs: ["a.java"], |
| 124 | required: ["libfoo"], |
| 125 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 126 | `) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 127 | |
| 128 | mk := getAndroidMk(t, ctx, config, "foo") |
| 129 | mk.moduleFor("foo").hasRequired("libfoo") |
| 130 | } |
| 131 | |
| 132 | func TestHostdex(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 133 | ctx, config := testJava(t, ` |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 134 | java_library { |
| 135 | name: "foo", |
| 136 | srcs: ["a.java"], |
| 137 | hostdex: true, |
| 138 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 139 | `) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 140 | |
| 141 | mk := getAndroidMk(t, ctx, config, "foo") |
| 142 | mk.moduleFor("foo") |
| 143 | mk.moduleFor("foo-hostdex") |
| 144 | } |
| 145 | |
| 146 | func TestHostdexRequired(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 147 | ctx, config := testJava(t, ` |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 148 | java_library { |
| 149 | name: "foo", |
| 150 | srcs: ["a.java"], |
| 151 | hostdex: true, |
| 152 | required: ["libfoo"], |
| 153 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 154 | `) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 155 | |
| 156 | mk := getAndroidMk(t, ctx, config, "foo") |
| 157 | mk.moduleFor("foo").hasRequired("libfoo") |
| 158 | mk.moduleFor("foo-hostdex").hasRequired("libfoo") |
| 159 | } |
| 160 | |
| 161 | func TestHostdexSpecificRequired(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 162 | ctx, config := testJava(t, ` |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 163 | java_library { |
| 164 | name: "foo", |
| 165 | srcs: ["a.java"], |
| 166 | hostdex: true, |
| 167 | target: { |
| 168 | hostdex: { |
| 169 | required: ["libfoo"], |
| 170 | }, |
| 171 | }, |
| 172 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 173 | `) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 174 | |
| 175 | mk := getAndroidMk(t, ctx, config, "foo") |
| 176 | mk.moduleFor("foo").hasNoRequired("libfoo") |
| 177 | mk.moduleFor("foo-hostdex").hasRequired("libfoo") |
| 178 | } |