github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/cc/test_data_test.go (about)

     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"
    25  	"android/soong/genrule"
    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  
   120  	config := android.TestConfig(buildDir, nil)
   121  
   122  	for _, test := range testDataTests {
   123  		t.Run(test.name, func(t *testing.T) {
   124  			ctx := android.NewTestContext()
   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  			})
   131  			ctx.RegisterModuleType("filegroup",
   132  				android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
   133  			ctx.RegisterModuleType("test",
   134  				android.ModuleFactoryAdaptor(newTest))
   135  			ctx.Register()
   136  
   137  			_, errs := ctx.ParseBlueprintsFiles("Blueprints")
   138  			android.FailIfErrored(t, errs)
   139  			_, errs = ctx.PrepareBuildActions(config)
   140  			android.FailIfErrored(t, errs)
   141  
   142  			foo := ctx.ModuleForTests("foo", "")
   143  
   144  			got := foo.Module().(*testDataTest).data
   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  
   175  func newTest() android.Module {
   176  	m := &testDataTest{}
   177  	m.AddProperties(&m.Properties)
   178  	android.InitAndroidModule(m)
   179  	return m
   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  }