blob: a66b1e19f0704d5d8228fe5f60646cf71a868054 [file] [log] [blame]
Colin Crosscec81712017-07-13 14:43:27 -07001// 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
15package android
16
17import (
18 "fmt"
Paul Duffin9b478b02019-12-10 13:41:51 +000019 "path/filepath"
Logan Chienee97c3e2018-03-12 16:34:26 +080020 "regexp"
Martin Stjernholm4c021242020-05-13 01:13:50 +010021 "sort"
Colin Crosscec81712017-07-13 14:43:27 -070022 "strings"
Logan Chien42039712018-03-12 16:29:17 +080023 "testing"
Colin Crosscec81712017-07-13 14:43:27 -070024
25 "github.com/google/blueprint"
26)
27
Colin Crossae8600b2020-10-29 17:09:13 -070028func NewTestContext(config Config) *TestContext {
Jeff Gaston088e29e2017-11-29 16:47:17 -080029 namespaceExportFilter := func(namespace *Namespace) bool {
30 return true
31 }
Jeff Gastonb274ed32017-12-01 17:10:33 -080032
33 nameResolver := NewNameResolver(namespaceExportFilter)
34 ctx := &TestContext{
Colin Crossae8600b2020-10-29 17:09:13 -070035 Context: &Context{blueprint.NewContext(), config},
Jeff Gastonb274ed32017-12-01 17:10:33 -080036 NameResolver: nameResolver,
37 }
38
39 ctx.SetNameInterface(nameResolver)
Jeff Gaston088e29e2017-11-29 16:47:17 -080040
Colin Cross1b488422019-03-04 22:33:56 -080041 ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator)
42
Colin Crossae8600b2020-10-29 17:09:13 -070043 ctx.SetFs(ctx.config.fs)
44 if ctx.config.mockBpList != "" {
45 ctx.SetModuleListFile(ctx.config.mockBpList)
46 }
47
Jeff Gaston088e29e2017-11-29 16:47:17 -080048 return ctx
Colin Crosscec81712017-07-13 14:43:27 -070049}
50
Colin Crossae8600b2020-10-29 17:09:13 -070051func NewTestArchContext(config Config) *TestContext {
52 ctx := NewTestContext(config)
Colin Crossae4c6182017-09-15 17:33:55 -070053 ctx.preDeps = append(ctx.preDeps, registerArchMutator)
54 return ctx
55}
56
Colin Crosscec81712017-07-13 14:43:27 -070057type TestContext struct {
Colin Cross4c83e5c2019-02-25 14:54:28 -080058 *Context
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000059 preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc
60 NameResolver *NameResolver
Colin Crosscec81712017-07-13 14:43:27 -070061}
62
63func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
64 ctx.preArch = append(ctx.preArch, f)
65}
66
Paul Duffina80ef842020-01-14 12:09:36 +000067func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
68 // Register mutator function as normal for testing.
69 ctx.PreArchMutators(f)
70}
71
Colin Crosscec81712017-07-13 14:43:27 -070072func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
73 ctx.preDeps = append(ctx.preDeps, f)
74}
75
76func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
77 ctx.postDeps = append(ctx.postDeps, f)
78}
79
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000080func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) {
81 ctx.finalDeps = append(ctx.finalDeps, f)
82}
83
Colin Crossae8600b2020-10-29 17:09:13 -070084func (ctx *TestContext) Register() {
Martin Stjernholm710ec3a2020-01-16 15:12:04 +000085 registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps)
Colin Crosscec81712017-07-13 14:43:27 -070086
Colin Cross4b49b762019-11-22 15:25:03 -080087 ctx.RegisterSingletonType("env", EnvSingleton)
Colin Cross31a738b2019-12-30 18:45:15 -080088}
89
90func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
91 // This function adapts the old style ParseFileList calls that are spread throughout the tests
92 // to the new style that takes a config.
93 return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config)
94}
95
96func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) {
97 // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the
98 // tests to the new style that takes a config.
99 return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config)
Colin Cross4b49b762019-11-22 15:25:03 -0800100}
101
102func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
103 ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
104}
105
Colin Cross9aed5bc2020-12-28 15:15:34 -0800106func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
107 s, m := SingletonModuleFactoryAdaptor(name, factory)
108 ctx.RegisterSingletonType(name, s)
109 ctx.RegisterModuleType(name, m)
110}
111
Colin Cross4b49b762019-11-22 15:25:03 -0800112func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
Colin Cross06fa5882020-10-29 18:21:38 -0700113 ctx.Context.RegisterSingletonType(name, SingletonFactoryAdaptor(ctx.Context, factory))
Colin Crosscec81712017-07-13 14:43:27 -0700114}
115
116func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
117 var module Module
118 ctx.VisitAllModules(func(m blueprint.Module) {
119 if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
120 module = m.(Module)
121 }
122 })
123
124 if module == nil {
Jeff Gaston294356f2017-09-27 17:05:30 -0700125 // find all the modules that do exist
Colin Crossbeae6ec2020-08-11 12:02:11 -0700126 var allModuleNames []string
127 var allVariants []string
Jeff Gaston294356f2017-09-27 17:05:30 -0700128 ctx.VisitAllModules(func(m blueprint.Module) {
Colin Crossbeae6ec2020-08-11 12:02:11 -0700129 allModuleNames = append(allModuleNames, ctx.ModuleName(m))
130 if ctx.ModuleName(m) == name {
131 allVariants = append(allVariants, ctx.ModuleSubDir(m))
132 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700133 })
Martin Stjernholm4c021242020-05-13 01:13:50 +0100134 sort.Strings(allModuleNames)
Colin Crossbeae6ec2020-08-11 12:02:11 -0700135 sort.Strings(allVariants)
Jeff Gaston294356f2017-09-27 17:05:30 -0700136
Colin Crossbeae6ec2020-08-11 12:02:11 -0700137 if len(allVariants) == 0 {
138 panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
139 name, strings.Join(allModuleNames, "\n ")))
140 } else {
141 panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s",
142 name, variant, strings.Join(allVariants, "\n ")))
143 }
Colin Crosscec81712017-07-13 14:43:27 -0700144 }
145
146 return TestingModule{module}
147}
148
Jiyong Park37b25202018-07-11 10:49:27 +0900149func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
150 var variants []string
151 ctx.VisitAllModules(func(m blueprint.Module) {
152 if ctx.ModuleName(m) == name {
153 variants = append(variants, ctx.ModuleSubDir(m))
154 }
155 })
156 return variants
157}
158
Colin Cross4c83e5c2019-02-25 14:54:28 -0800159// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
160func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
161 allSingletonNames := []string{}
162 for _, s := range ctx.Singletons() {
163 n := ctx.SingletonName(s)
164 if n == name {
165 return TestingSingleton{
166 singleton: s.(*singletonAdaptor).Singleton,
167 provider: s.(testBuildProvider),
168 }
169 }
170 allSingletonNames = append(allSingletonNames, n)
171 }
172
173 panic(fmt.Errorf("failed to find singleton %q."+
174 "\nall singletons: %v", name, allSingletonNames))
175}
176
Colin Cross4c83e5c2019-02-25 14:54:28 -0800177type testBuildProvider interface {
178 BuildParamsForTests() []BuildParams
179 RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
180}
181
182type TestingBuildParams struct {
183 BuildParams
184 RuleParams blueprint.RuleParams
185}
186
187func newTestingBuildParams(provider testBuildProvider, bparams BuildParams) TestingBuildParams {
188 return TestingBuildParams{
189 BuildParams: bparams,
190 RuleParams: provider.RuleParamsForTests()[bparams.Rule],
191 }
192}
193
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200194func maybeBuildParamsFromRule(provider testBuildProvider, rule string) (TestingBuildParams, []string) {
195 var searchedRules []string
Colin Cross4c83e5c2019-02-25 14:54:28 -0800196 for _, p := range provider.BuildParamsForTests() {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200197 searchedRules = append(searchedRules, p.Rule.String())
Colin Cross4c83e5c2019-02-25 14:54:28 -0800198 if strings.Contains(p.Rule.String(), rule) {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200199 return newTestingBuildParams(provider, p), searchedRules
Colin Cross4c83e5c2019-02-25 14:54:28 -0800200 }
201 }
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200202 return TestingBuildParams{}, searchedRules
Colin Cross4c83e5c2019-02-25 14:54:28 -0800203}
204
205func buildParamsFromRule(provider testBuildProvider, rule string) TestingBuildParams {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200206 p, searchRules := maybeBuildParamsFromRule(provider, rule)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800207 if p.Rule == nil {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200208 panic(fmt.Errorf("couldn't find rule %q.\nall rules: %v", rule, searchRules))
Colin Cross4c83e5c2019-02-25 14:54:28 -0800209 }
210 return p
211}
212
213func maybeBuildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
214 for _, p := range provider.BuildParamsForTests() {
Colin Crossb88b3c52019-06-10 15:15:17 -0700215 if strings.Contains(p.Description, desc) {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800216 return newTestingBuildParams(provider, p)
217 }
218 }
219 return TestingBuildParams{}
220}
221
222func buildParamsFromDescription(provider testBuildProvider, desc string) TestingBuildParams {
223 p := maybeBuildParamsFromDescription(provider, desc)
224 if p.Rule == nil {
225 panic(fmt.Errorf("couldn't find description %q", desc))
226 }
227 return p
228}
229
230func maybeBuildParamsFromOutput(provider testBuildProvider, file string) (TestingBuildParams, []string) {
231 var searchedOutputs []string
232 for _, p := range provider.BuildParamsForTests() {
233 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700234 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800235 if p.Output != nil {
236 outputs = append(outputs, p.Output)
237 }
238 for _, f := range outputs {
239 if f.String() == file || f.Rel() == file {
240 return newTestingBuildParams(provider, p), nil
241 }
242 searchedOutputs = append(searchedOutputs, f.Rel())
243 }
244 }
245 return TestingBuildParams{}, searchedOutputs
246}
247
248func buildParamsFromOutput(provider testBuildProvider, file string) TestingBuildParams {
249 p, searchedOutputs := maybeBuildParamsFromOutput(provider, file)
250 if p.Rule == nil {
251 panic(fmt.Errorf("couldn't find output %q.\nall outputs: %v",
252 file, searchedOutputs))
253 }
254 return p
255}
256
257func allOutputs(provider testBuildProvider) []string {
258 var outputFullPaths []string
259 for _, p := range provider.BuildParamsForTests() {
260 outputs := append(WritablePaths(nil), p.Outputs...)
Colin Cross1d2cf042019-03-29 15:33:06 -0700261 outputs = append(outputs, p.ImplicitOutputs...)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800262 if p.Output != nil {
263 outputs = append(outputs, p.Output)
264 }
265 outputFullPaths = append(outputFullPaths, outputs.Strings()...)
266 }
267 return outputFullPaths
268}
269
Colin Crossb77ffc42019-01-05 22:09:19 -0800270// TestingModule is wrapper around an android.Module that provides methods to find information about individual
271// ctx.Build parameters for verification in tests.
Colin Crosscec81712017-07-13 14:43:27 -0700272type TestingModule struct {
273 module Module
274}
275
Colin Crossb77ffc42019-01-05 22:09:19 -0800276// Module returns the Module wrapped by the TestingModule.
Colin Crosscec81712017-07-13 14:43:27 -0700277func (m TestingModule) Module() Module {
278 return m.module
279}
280
Colin Crossb77ffc42019-01-05 22:09:19 -0800281// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
282// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800283func (m TestingModule) MaybeRule(rule string) TestingBuildParams {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200284 r, _ := maybeBuildParamsFromRule(m.module, rule)
285 return r
Colin Crosscec81712017-07-13 14:43:27 -0700286}
287
Colin Crossb77ffc42019-01-05 22:09:19 -0800288// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800289func (m TestingModule) Rule(rule string) TestingBuildParams {
290 return buildParamsFromRule(m.module, rule)
Colin Crossb77ffc42019-01-05 22:09:19 -0800291}
292
293// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
294// BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800295func (m TestingModule) MaybeDescription(desc string) TestingBuildParams {
296 return maybeBuildParamsFromDescription(m.module, desc)
Nan Zhanged19fc32017-10-19 13:06:22 -0700297}
298
Colin Crossb77ffc42019-01-05 22:09:19 -0800299// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
300// found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800301func (m TestingModule) Description(desc string) TestingBuildParams {
302 return buildParamsFromDescription(m.module, desc)
Colin Crossb77ffc42019-01-05 22:09:19 -0800303}
304
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800305// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
Colin Crossb77ffc42019-01-05 22:09:19 -0800306// value matches the provided string. Returns an empty BuildParams if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800307func (m TestingModule) MaybeOutput(file string) TestingBuildParams {
308 p, _ := maybeBuildParamsFromOutput(m.module, file)
Colin Crossb77ffc42019-01-05 22:09:19 -0800309 return p
310}
311
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800312// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
Colin Crossb77ffc42019-01-05 22:09:19 -0800313// value matches the provided string. Panics if no rule is found.
Colin Cross4c83e5c2019-02-25 14:54:28 -0800314func (m TestingModule) Output(file string) TestingBuildParams {
315 return buildParamsFromOutput(m.module, file)
Colin Crosscec81712017-07-13 14:43:27 -0700316}
Logan Chien42039712018-03-12 16:29:17 +0800317
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800318// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
319func (m TestingModule) AllOutputs() []string {
Colin Cross4c83e5c2019-02-25 14:54:28 -0800320 return allOutputs(m.module)
321}
322
323// TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
324// ctx.Build parameters for verification in tests.
325type TestingSingleton struct {
326 singleton Singleton
327 provider testBuildProvider
328}
329
330// Singleton returns the Singleton wrapped by the TestingSingleton.
331func (s TestingSingleton) Singleton() Singleton {
332 return s.singleton
333}
334
335// MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
336// BuildParams if no rule is found.
337func (s TestingSingleton) MaybeRule(rule string) TestingBuildParams {
ThiƩbaud Weksteen3600b802020-08-27 15:50:24 +0200338 r, _ := maybeBuildParamsFromRule(s.provider, rule)
339 return r
Colin Cross4c83e5c2019-02-25 14:54:28 -0800340}
341
342// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
343func (s TestingSingleton) Rule(rule string) TestingBuildParams {
344 return buildParamsFromRule(s.provider, rule)
345}
346
347// MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
348// BuildParams if no rule is found.
349func (s TestingSingleton) MaybeDescription(desc string) TestingBuildParams {
350 return maybeBuildParamsFromDescription(s.provider, desc)
351}
352
353// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
354// found.
355func (s TestingSingleton) Description(desc string) TestingBuildParams {
356 return buildParamsFromDescription(s.provider, desc)
357}
358
359// MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
360// value matches the provided string. Returns an empty BuildParams if no rule is found.
361func (s TestingSingleton) MaybeOutput(file string) TestingBuildParams {
362 p, _ := maybeBuildParamsFromOutput(s.provider, file)
363 return p
364}
365
366// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
367// value matches the provided string. Panics if no rule is found.
368func (s TestingSingleton) Output(file string) TestingBuildParams {
369 return buildParamsFromOutput(s.provider, file)
370}
371
372// AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
373func (s TestingSingleton) AllOutputs() []string {
374 return allOutputs(s.provider)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800375}
376
Logan Chien42039712018-03-12 16:29:17 +0800377func FailIfErrored(t *testing.T, errs []error) {
378 t.Helper()
379 if len(errs) > 0 {
380 for _, err := range errs {
381 t.Error(err)
382 }
383 t.FailNow()
384 }
385}
Logan Chienee97c3e2018-03-12 16:34:26 +0800386
387func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) {
388 t.Helper()
389
390 matcher, err := regexp.Compile(pattern)
391 if err != nil {
392 t.Errorf("failed to compile regular expression %q because %s", pattern, err)
393 }
394
395 found := false
396 for _, err := range errs {
397 if matcher.FindStringIndex(err.Error()) != nil {
398 found = true
399 break
400 }
401 }
402 if !found {
403 t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
404 for i, err := range errs {
Colin Crossaede88c2020-08-11 12:17:01 -0700405 t.Errorf("errs[%d] = %q", i, err)
Logan Chienee97c3e2018-03-12 16:34:26 +0800406 }
407 }
408}
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700409
Paul Duffin91e38192019-08-05 15:07:57 +0100410func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
411 t.Helper()
412
413 if expectedErrorPatterns == nil {
414 FailIfErrored(t, errs)
415 } else {
416 for _, expectedError := range expectedErrorPatterns {
417 FailIfNoMatchingErrors(t, expectedError, errs)
418 }
419 if len(errs) > len(expectedErrorPatterns) {
420 t.Errorf("additional errors found, expected %d, found %d",
421 len(expectedErrorPatterns), len(errs))
422 for i, expectedError := range expectedErrorPatterns {
423 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
424 }
425 for i, err := range errs {
426 t.Errorf("errs[%d] = %s", i, err)
427 }
428 }
429 }
430
431}
432
Jingwen Chencda22c92020-11-23 00:22:30 -0500433func SetKatiEnabledForTests(config Config) {
434 config.katiEnabled = true
Paul Duffin8c3fec42020-03-04 20:15:08 +0000435}
436
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900437func AndroidMkEntriesForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) []AndroidMkEntries {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700438 var p AndroidMkEntriesProvider
439 var ok bool
440 if p, ok = mod.(AndroidMkEntriesProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100441 t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700442 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900443
444 entriesList := p.AndroidMkEntries()
445 for i, _ := range entriesList {
446 entriesList[i].fillInEntries(config, bpPath, mod)
447 }
448 return entriesList
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700449}
Jooyung Han12df5fb2019-07-11 16:18:47 +0900450
451func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod blueprint.Module) AndroidMkData {
452 var p AndroidMkDataProvider
453 var ok bool
454 if p, ok = mod.(AndroidMkDataProvider); !ok {
Roland Levillaindfe75b32019-07-23 16:53:32 +0100455 t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
Jooyung Han12df5fb2019-07-11 16:18:47 +0900456 }
457 data := p.AndroidMk()
458 data.fillInData(config, bpPath, mod)
459 return data
460}
Paul Duffin9b478b02019-12-10 13:41:51 +0000461
462// Normalize the path for testing.
463//
464// If the path is relative to the build directory then return the relative path
465// to avoid tests having to deal with the dynamically generated build directory.
466//
467// Otherwise, return the supplied path as it is almost certainly a source path
468// that is relative to the root of the source tree.
469//
470// The build and source paths should be distinguishable based on their contents.
471func NormalizePathForTesting(path Path) string {
472 p := path.String()
473 if w, ok := path.(WritablePath); ok {
474 rel, err := filepath.Rel(w.buildDir(), p)
475 if err != nil {
476 panic(err)
477 }
478 return rel
479 }
480 return p
481}
482
483func NormalizePathsForTesting(paths Paths) []string {
484 var result []string
485 for _, path := range paths {
486 relative := NormalizePathForTesting(path)
487 result = append(result, relative)
488 }
489 return result
490}