Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame^] | 18 | "bufio" |
| 19 | "bytes" |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 20 | "io/ioutil" |
| 21 | "os" |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame^] | 22 | "strings" |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 23 | "testing" |
| 24 | ) |
| 25 | |
| 26 | func 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 | |
| 50 | func 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 | |
| 60 | func tearDown(buildDir string) { |
| 61 | os.RemoveAll(buildDir) |
| 62 | } |
| 63 | |
| 64 | func 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 Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 97 | |
| 98 | func 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 Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 107 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 108 | if p.outputFilePath.Base() != "foo.installed.conf" { |
| 109 | t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base()) |
| 110 | } |
| 111 | } |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 112 | |
| 113 | func 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 Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 126 | p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 127 | if p.outputFilePath.Base() != "my_foo" { |
| 128 | t.Errorf("expected my_foo, got %q", p.outputFilePath.Base()) |
| 129 | } |
| 130 | |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 131 | p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 132 | if p.outputFilePath.Base() != "bar.conf" { |
| 133 | t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base()) |
| 134 | } |
| 135 | } |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame^] | 136 | |
| 137 | func 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 | } |