2017-02-01 23:12:44 +01:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dataFile struct {
|
|
|
|
path string
|
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
|
|
|
var testDataTests = []struct {
|
|
|
|
name string
|
|
|
|
modules string
|
|
|
|
data []dataFile
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "data files",
|
|
|
|
modules: `
|
|
|
|
test {
|
|
|
|
name: "foo",
|
|
|
|
data: [
|
|
|
|
"baz",
|
|
|
|
"bar/baz",
|
|
|
|
],
|
|
|
|
}`,
|
|
|
|
data: []dataFile{
|
|
|
|
{"dir", "baz"},
|
|
|
|
{"dir", "bar/baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filegroup",
|
|
|
|
modules: `
|
|
|
|
filegroup {
|
|
|
|
name: "fg",
|
|
|
|
srcs: [
|
|
|
|
"baz",
|
|
|
|
"bar/baz",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
test {
|
|
|
|
name: "foo",
|
|
|
|
data: [":fg"],
|
|
|
|
}`,
|
|
|
|
data: []dataFile{
|
|
|
|
{"dir", "baz"},
|
|
|
|
{"dir", "bar/baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "relative filegroup",
|
|
|
|
modules: `
|
|
|
|
filegroup {
|
|
|
|
name: "fg",
|
|
|
|
srcs: [
|
|
|
|
"bar/baz",
|
|
|
|
],
|
|
|
|
path: "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
test {
|
|
|
|
name: "foo",
|
|
|
|
data: [":fg"],
|
|
|
|
}`,
|
|
|
|
data: []dataFile{
|
|
|
|
{"dir/bar", "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "relative filegroup trailing slash",
|
|
|
|
modules: `
|
|
|
|
filegroup {
|
|
|
|
name: "fg",
|
|
|
|
srcs: [
|
|
|
|
"bar/baz",
|
|
|
|
],
|
|
|
|
path: "bar/",
|
|
|
|
}
|
|
|
|
|
|
|
|
test {
|
|
|
|
name: "foo",
|
|
|
|
data: [":fg"],
|
|
|
|
}`,
|
|
|
|
data: []dataFile{
|
|
|
|
{"dir/bar", "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDataTests(t *testing.T) {
|
|
|
|
buildDir, err := ioutil.TempDir("", "soong_test_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(buildDir)
|
|
|
|
|
|
|
|
for _, test := range testDataTests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2019-12-14 05:41:13 +01:00
|
|
|
config := android.TestConfig(buildDir, nil, "", map[string][]byte{
|
|
|
|
"dir/Android.bp": []byte(test.modules),
|
2017-02-01 23:12:44 +01:00
|
|
|
"dir/baz": nil,
|
|
|
|
"dir/bar/baz": nil,
|
|
|
|
})
|
2020-10-30 01:09:13 +01:00
|
|
|
ctx := android.NewTestContext(config)
|
2019-11-23 00:25:03 +01:00
|
|
|
ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
|
|
|
|
ctx.RegisterModuleType("test", newTest)
|
2020-10-30 01:09:13 +01:00
|
|
|
ctx.Register()
|
2017-02-01 23:12:44 +01:00
|
|
|
|
2021-09-02 11:46:24 +02:00
|
|
|
_, errs := ctx.ParseBlueprintsFiles("Android.bp")
|
2018-03-12 09:29:17 +01:00
|
|
|
android.FailIfErrored(t, errs)
|
2017-02-01 23:12:44 +01:00
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
2018-03-12 09:29:17 +01:00
|
|
|
android.FailIfErrored(t, errs)
|
2017-02-01 23:12:44 +01:00
|
|
|
|
2017-07-13 23:43:27 +02:00
|
|
|
foo := ctx.ModuleForTests("foo", "")
|
2017-02-01 23:12:44 +01:00
|
|
|
|
2017-07-13 23:43:27 +02:00
|
|
|
got := foo.Module().(*testDataTest).data
|
2017-02-01 23:12:44 +01:00
|
|
|
if len(got) != len(test.data) {
|
|
|
|
t.Errorf("expected %d data files, got %d",
|
|
|
|
len(test.data), len(got))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range got {
|
|
|
|
if i >= len(test.data) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(test.data[i].path, test.data[i].file)
|
|
|
|
if test.data[i].file != got[i].Rel() ||
|
|
|
|
path != got[i].String() {
|
2018-07-23 06:18:45 +02:00
|
|
|
t.Errorf("expected %s:%s got %s:%s",
|
2017-02-01 23:12:44 +01:00
|
|
|
path, test.data[i].file,
|
|
|
|
got[i].String(), got[i].Rel())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testDataTest struct {
|
|
|
|
android.ModuleBase
|
|
|
|
data android.Paths
|
|
|
|
Properties struct {
|
2019-03-05 07:35:41 +01:00
|
|
|
Data []string `android:"path"`
|
2017-02-01 23:12:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 00:06:31 +02:00
|
|
|
func newTest() android.Module {
|
2017-02-01 23:12:44 +01:00
|
|
|
m := &testDataTest{}
|
2017-06-24 00:06:31 +02:00
|
|
|
m.AddProperties(&m.Properties)
|
|
|
|
android.InitAndroidModule(m)
|
|
|
|
return m
|
2017-02-01 23:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (test *testDataTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2019-03-06 07:25:09 +01:00
|
|
|
test.data = android.PathsForModuleSrc(ctx, test.Properties.Data)
|
2017-02-01 23:12:44 +01:00
|
|
|
}
|