Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 cc |
| 16 | |
| 17 | import ( |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "testing" |
| 22 | |
| 23 | "android/soong/android" |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | type dataFile struct { |
| 27 | path string |
| 28 | file string |
| 29 | } |
| 30 | |
| 31 | var testDataTests = []struct { |
| 32 | name string |
| 33 | modules string |
| 34 | data []dataFile |
| 35 | }{ |
| 36 | { |
| 37 | name: "data files", |
| 38 | modules: ` |
| 39 | test { |
| 40 | name: "foo", |
| 41 | data: [ |
| 42 | "baz", |
| 43 | "bar/baz", |
| 44 | ], |
| 45 | }`, |
| 46 | data: []dataFile{ |
| 47 | {"dir", "baz"}, |
| 48 | {"dir", "bar/baz"}, |
| 49 | }, |
| 50 | }, |
| 51 | { |
| 52 | name: "filegroup", |
| 53 | modules: ` |
| 54 | filegroup { |
| 55 | name: "fg", |
| 56 | srcs: [ |
| 57 | "baz", |
| 58 | "bar/baz", |
| 59 | ], |
| 60 | } |
| 61 | |
| 62 | test { |
| 63 | name: "foo", |
| 64 | data: [":fg"], |
| 65 | }`, |
| 66 | data: []dataFile{ |
| 67 | {"dir", "baz"}, |
| 68 | {"dir", "bar/baz"}, |
| 69 | }, |
| 70 | }, |
| 71 | { |
| 72 | name: "relative filegroup", |
| 73 | modules: ` |
| 74 | filegroup { |
| 75 | name: "fg", |
| 76 | srcs: [ |
| 77 | "bar/baz", |
| 78 | ], |
| 79 | path: "bar", |
| 80 | } |
| 81 | |
| 82 | test { |
| 83 | name: "foo", |
| 84 | data: [":fg"], |
| 85 | }`, |
| 86 | data: []dataFile{ |
| 87 | {"dir/bar", "baz"}, |
| 88 | }, |
| 89 | }, |
| 90 | { |
| 91 | name: "relative filegroup trailing slash", |
| 92 | modules: ` |
| 93 | filegroup { |
| 94 | name: "fg", |
| 95 | srcs: [ |
| 96 | "bar/baz", |
| 97 | ], |
| 98 | path: "bar/", |
| 99 | } |
| 100 | |
| 101 | test { |
| 102 | name: "foo", |
| 103 | data: [":fg"], |
| 104 | }`, |
| 105 | data: []dataFile{ |
| 106 | {"dir/bar", "baz"}, |
| 107 | }, |
| 108 | }, |
| 109 | } |
| 110 | |
| 111 | func TestDataTests(t *testing.T) { |
| 112 | buildDir, err := ioutil.TempDir("", "soong_test_test") |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | defer os.RemoveAll(buildDir) |
| 117 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 118 | for _, test := range testDataTests { |
| 119 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 120 | config := android.TestConfig(buildDir, nil, "", map[string][]byte{ |
| 121 | "dir/Android.bp": []byte(test.modules), |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 122 | "dir/baz": nil, |
| 123 | "dir/bar/baz": nil, |
| 124 | }) |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 125 | ctx := android.NewTestContext(config) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 126 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
| 127 | ctx.RegisterModuleType("test", newTest) |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 128 | ctx.Register() |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 129 | |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 130 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 131 | android.FailIfErrored(t, errs) |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 132 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 133 | android.FailIfErrored(t, errs) |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 134 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 135 | foo := ctx.ModuleForTests("foo", "") |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 136 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 137 | got := foo.Module().(*testDataTest).data |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 138 | if len(got) != len(test.data) { |
| 139 | t.Errorf("expected %d data files, got %d", |
| 140 | len(test.data), len(got)) |
| 141 | } |
| 142 | |
| 143 | for i := range got { |
| 144 | if i >= len(test.data) { |
| 145 | break |
| 146 | } |
| 147 | |
| 148 | path := filepath.Join(test.data[i].path, test.data[i].file) |
| 149 | if test.data[i].file != got[i].Rel() || |
| 150 | path != got[i].String() { |
Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 151 | t.Errorf("expected %s:%s got %s:%s", |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 152 | path, test.data[i].file, |
| 153 | got[i].String(), got[i].Rel()) |
| 154 | } |
| 155 | } |
| 156 | }) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | type testDataTest struct { |
| 161 | android.ModuleBase |
| 162 | data android.Paths |
| 163 | Properties struct { |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 164 | Data []string `android:"path"` |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 168 | func newTest() android.Module { |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 169 | m := &testDataTest{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 170 | m.AddProperties(&m.Properties) |
| 171 | android.InitAndroidModule(m) |
| 172 | return m |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 175 | func (test *testDataTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 176 | test.data = android.PathsForModuleSrc(ctx, test.Properties.Data) |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 177 | } |