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 ( |
| 18 | "fmt" |
| 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "testing" |
| 23 | |
| 24 | "android/soong/android" |
Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 25 | "android/soong/genrule" |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | type dataFile struct { |
| 29 | path string |
| 30 | file string |
| 31 | } |
| 32 | |
| 33 | var testDataTests = []struct { |
| 34 | name string |
| 35 | modules string |
| 36 | data []dataFile |
| 37 | }{ |
| 38 | { |
| 39 | name: "data files", |
| 40 | modules: ` |
| 41 | test { |
| 42 | name: "foo", |
| 43 | data: [ |
| 44 | "baz", |
| 45 | "bar/baz", |
| 46 | ], |
| 47 | }`, |
| 48 | data: []dataFile{ |
| 49 | {"dir", "baz"}, |
| 50 | {"dir", "bar/baz"}, |
| 51 | }, |
| 52 | }, |
| 53 | { |
| 54 | name: "filegroup", |
| 55 | modules: ` |
| 56 | filegroup { |
| 57 | name: "fg", |
| 58 | srcs: [ |
| 59 | "baz", |
| 60 | "bar/baz", |
| 61 | ], |
| 62 | } |
| 63 | |
| 64 | test { |
| 65 | name: "foo", |
| 66 | data: [":fg"], |
| 67 | }`, |
| 68 | data: []dataFile{ |
| 69 | {"dir", "baz"}, |
| 70 | {"dir", "bar/baz"}, |
| 71 | }, |
| 72 | }, |
| 73 | { |
| 74 | name: "relative filegroup", |
| 75 | modules: ` |
| 76 | filegroup { |
| 77 | name: "fg", |
| 78 | srcs: [ |
| 79 | "bar/baz", |
| 80 | ], |
| 81 | path: "bar", |
| 82 | } |
| 83 | |
| 84 | test { |
| 85 | name: "foo", |
| 86 | data: [":fg"], |
| 87 | }`, |
| 88 | data: []dataFile{ |
| 89 | {"dir/bar", "baz"}, |
| 90 | }, |
| 91 | }, |
| 92 | { |
| 93 | name: "relative filegroup trailing slash", |
| 94 | modules: ` |
| 95 | filegroup { |
| 96 | name: "fg", |
| 97 | srcs: [ |
| 98 | "bar/baz", |
| 99 | ], |
| 100 | path: "bar/", |
| 101 | } |
| 102 | |
| 103 | test { |
| 104 | name: "foo", |
| 105 | data: [":fg"], |
| 106 | }`, |
| 107 | data: []dataFile{ |
| 108 | {"dir/bar", "baz"}, |
| 109 | }, |
| 110 | }, |
| 111 | } |
| 112 | |
| 113 | func TestDataTests(t *testing.T) { |
| 114 | buildDir, err := ioutil.TempDir("", "soong_test_test") |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | defer os.RemoveAll(buildDir) |
| 119 | |
Colin Cross | 6ccbc91 | 2017-10-10 23:07:38 -0700 | [diff] [blame^] | 120 | config := android.TestConfig(buildDir, nil) |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 121 | |
| 122 | for _, test := range testDataTests { |
| 123 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 124 | ctx := android.NewTestContext() |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 125 | ctx.MockFileSystem(map[string][]byte{ |
| 126 | "Blueprints": []byte(`subdirs = ["dir"]`), |
| 127 | "dir/Blueprints": []byte(test.modules), |
| 128 | "dir/baz": nil, |
| 129 | "dir/bar/baz": nil, |
| 130 | }) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 131 | ctx.RegisterModuleType("filegroup", |
| 132 | android.ModuleFactoryAdaptor(genrule.FileGroupFactory)) |
| 133 | ctx.RegisterModuleType("test", |
| 134 | android.ModuleFactoryAdaptor(newTest)) |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 135 | ctx.Register() |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 136 | |
| 137 | _, errs := ctx.ParseBlueprintsFiles("Blueprints") |
| 138 | fail(t, errs) |
| 139 | _, errs = ctx.PrepareBuildActions(config) |
| 140 | fail(t, errs) |
| 141 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 142 | foo := ctx.ModuleForTests("foo", "") |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 143 | |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 144 | got := foo.Module().(*testDataTest).data |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 145 | if len(got) != len(test.data) { |
| 146 | t.Errorf("expected %d data files, got %d", |
| 147 | len(test.data), len(got)) |
| 148 | } |
| 149 | |
| 150 | for i := range got { |
| 151 | if i >= len(test.data) { |
| 152 | break |
| 153 | } |
| 154 | |
| 155 | path := filepath.Join(test.data[i].path, test.data[i].file) |
| 156 | if test.data[i].file != got[i].Rel() || |
| 157 | path != got[i].String() { |
| 158 | fmt.Errorf("expected %s:%s got %s:%s", |
| 159 | path, test.data[i].file, |
| 160 | got[i].String(), got[i].Rel()) |
| 161 | } |
| 162 | } |
| 163 | }) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | type testDataTest struct { |
| 168 | android.ModuleBase |
| 169 | data android.Paths |
| 170 | Properties struct { |
| 171 | Data []string |
| 172 | } |
| 173 | } |
| 174 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 175 | func newTest() android.Module { |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 176 | m := &testDataTest{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 177 | m.AddProperties(&m.Properties) |
| 178 | android.InitAndroidModule(m) |
| 179 | return m |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | func (test *testDataTest) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 183 | android.ExtractSourcesDeps(ctx, test.Properties.Data) |
| 184 | } |
| 185 | |
| 186 | func (test *testDataTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 187 | test.data = ctx.ExpandSources(test.Properties.Data, nil) |
| 188 | } |
| 189 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 190 | func fail(t *testing.T, errs []error) { |
| 191 | if len(errs) > 0 { |
| 192 | for _, err := range errs { |
| 193 | t.Error(err) |
| 194 | } |
| 195 | t.FailNow() |
| 196 | } |
| 197 | } |