Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 1 | // Copyright 2021 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 android |
| 16 | |
| 17 | import ( |
Paul Duffin | bbccfcf | 2021-03-03 00:44:00 +0000 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame^] | 19 | "strings" |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | // Provides support for creating test fixtures on which tests can be run. Reduces duplication |
| 24 | // of test setup by allow tests to easily reuse setup code. |
| 25 | // |
| 26 | // Fixture |
| 27 | // ======= |
| 28 | // These determine the environment within which a test can be run. Fixtures are mutable and are |
| 29 | // created by FixtureFactory instances and mutated by FixturePreparer instances. They are created by |
| 30 | // first creating a base Fixture (which is essentially empty) and then applying FixturePreparer |
| 31 | // instances to it to modify the environment. |
| 32 | // |
| 33 | // FixtureFactory |
| 34 | // ============== |
| 35 | // These are responsible for creating fixtures. Factories are immutable and are intended to be |
| 36 | // initialized once and reused to create multiple fixtures. Each factory has a list of fixture |
| 37 | // preparers that prepare a fixture for running a test. Factories can also be used to create other |
| 38 | // factories by extending them with additional fixture preparers. |
| 39 | // |
| 40 | // FixturePreparer |
| 41 | // =============== |
| 42 | // These are responsible for modifying a Fixture in preparation for it to run a test. Preparers are |
| 43 | // intended to be immutable and able to prepare multiple Fixture objects simultaneously without |
| 44 | // them sharing any data. |
| 45 | // |
| 46 | // FixturePreparers are only ever invoked once per test fixture. Prior to invocation the list of |
| 47 | // FixturePreparers are flattened and deduped while preserving the order they first appear in the |
| 48 | // list. This makes it easy to reuse, group and combine FixturePreparers together. |
| 49 | // |
| 50 | // Each small self contained piece of test setup should be their own FixturePreparer. e.g. |
| 51 | // * A group of related modules. |
| 52 | // * A group of related mutators. |
| 53 | // * A combination of both. |
| 54 | // * Configuration. |
| 55 | // |
| 56 | // They should not overlap, e.g. the same module type should not be registered by different |
| 57 | // FixturePreparers as using them both would cause a build error. In that case the preparer should |
| 58 | // be split into separate parts and combined together using FixturePreparers(...). |
| 59 | // |
| 60 | // e.g. attempting to use AllPreparers in preparing a Fixture would break as it would attempt to |
| 61 | // register module bar twice: |
| 62 | // var Preparer1 = FixtureRegisterWithContext(RegisterModuleFooAndBar) |
| 63 | // var Preparer2 = FixtureRegisterWithContext(RegisterModuleBarAndBaz) |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame] | 64 | // var AllPreparers = GroupFixturePreparers(Preparer1, Preparer2) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 65 | // |
| 66 | // However, when restructured like this it would work fine: |
| 67 | // var PreparerFoo = FixtureRegisterWithContext(RegisterModuleFoo) |
| 68 | // var PreparerBar = FixtureRegisterWithContext(RegisterModuleBar) |
| 69 | // var PreparerBaz = FixtureRegisterWithContext(RegisterModuleBaz) |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame] | 70 | // var Preparer1 = GroupFixturePreparers(RegisterModuleFoo, RegisterModuleBar) |
| 71 | // var Preparer2 = GroupFixturePreparers(RegisterModuleBar, RegisterModuleBaz) |
| 72 | // var AllPreparers = GroupFixturePreparers(Preparer1, Preparer2) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 73 | // |
| 74 | // As after deduping and flattening AllPreparers would result in the following preparers being |
| 75 | // applied: |
| 76 | // 1. PreparerFoo |
| 77 | // 2. PreparerBar |
| 78 | // 3. PreparerBaz |
| 79 | // |
| 80 | // Preparers can be used for both integration and unit tests. |
| 81 | // |
| 82 | // Integration tests typically use all the module types, mutators and singletons that are available |
| 83 | // for that package to try and replicate the behavior of the runtime build as closely as possible. |
| 84 | // However, that realism comes at a cost of increased fragility (as they can be broken by changes in |
| 85 | // many different parts of the build) and also increased runtime, especially if they use lots of |
| 86 | // singletons and mutators. |
| 87 | // |
| 88 | // Unit tests on the other hand try and minimize the amount of code being tested which makes them |
| 89 | // less susceptible to changes elsewhere in the build and quick to run but at a cost of potentially |
| 90 | // not testing realistic scenarios. |
| 91 | // |
| 92 | // Supporting unit tests effectively require that preparers are available at the lowest granularity |
| 93 | // possible. Supporting integration tests effectively require that the preparers are organized into |
| 94 | // groups that provide all the functionality available. |
| 95 | // |
| 96 | // At least in terms of tests that check the behavior of build components via processing |
| 97 | // `Android.bp` there is no clear separation between a unit test and an integration test. Instead |
| 98 | // they vary from one end that tests a single module (e.g. filegroup) to the other end that tests a |
| 99 | // whole system of modules, mutators and singletons (e.g. apex + hiddenapi). |
| 100 | // |
| 101 | // TestResult |
| 102 | // ========== |
| 103 | // These are created by running tests in a Fixture and provide access to the Config and TestContext |
| 104 | // in which the tests were run. |
| 105 | // |
| 106 | // Example |
| 107 | // ======= |
| 108 | // |
| 109 | // An exported preparer for use by other packages that need to use java modules. |
| 110 | // |
| 111 | // package java |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame] | 112 | // var PrepareForIntegrationTestWithJava = GroupFixturePreparers( |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 113 | // android.PrepareForIntegrationTestWithAndroid, |
| 114 | // FixtureRegisterWithContext(RegisterAGroupOfRelatedModulesMutatorsAndSingletons), |
| 115 | // FixtureRegisterWithContext(RegisterAnotherGroupOfRelatedModulesMutatorsAndSingletons), |
| 116 | // ... |
| 117 | // ) |
| 118 | // |
| 119 | // Some files to use in tests in the java package. |
| 120 | // |
| 121 | // var javaMockFS = android.MockFS{ |
| 122 | // "api/current.txt": nil, |
| 123 | // "api/removed.txt": nil, |
| 124 | // ... |
| 125 | // } |
| 126 | // |
| 127 | // A package private factory for use for testing java within the java package. |
| 128 | // |
| 129 | // var javaFixtureFactory = NewFixtureFactory( |
| 130 | // PrepareForIntegrationTestWithJava, |
| 131 | // FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
| 132 | // ctx.RegisterModuleType("test_module", testModule) |
| 133 | // }), |
| 134 | // javaMockFS.AddToFixture(), |
| 135 | // ... |
| 136 | // } |
| 137 | // |
| 138 | // func TestJavaStuff(t *testing.T) { |
| 139 | // result := javaFixtureFactory.RunTest(t, |
| 140 | // android.FixtureWithRootAndroidBp(`java_library {....}`), |
| 141 | // android.MockFS{...}.AddToFixture(), |
| 142 | // ) |
| 143 | // ... test result ... |
| 144 | // } |
| 145 | // |
| 146 | // package cc |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame] | 147 | // var PrepareForTestWithCC = GroupFixturePreparers( |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 148 | // android.PrepareForArchMutator, |
| 149 | // android.prepareForPrebuilts, |
| 150 | // FixtureRegisterWithContext(RegisterRequiredBuildComponentsForTest), |
| 151 | // ... |
| 152 | // ) |
| 153 | // |
| 154 | // package apex |
| 155 | // |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame] | 156 | // var PrepareForApex = GroupFixturePreparers( |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 157 | // ... |
| 158 | // ) |
| 159 | // |
| 160 | // Use modules and mutators from java, cc and apex. Any duplicate preparers (like |
| 161 | // android.PrepareForArchMutator) will be automatically deduped. |
| 162 | // |
| 163 | // var apexFixtureFactory = android.NewFixtureFactory( |
| 164 | // PrepareForJava, |
| 165 | // PrepareForCC, |
| 166 | // PrepareForApex, |
| 167 | // ) |
| 168 | |
| 169 | // Factory for Fixture objects. |
| 170 | // |
| 171 | // This is configured with a set of FixturePreparer objects that are used to |
| 172 | // initialize each Fixture instance this creates. |
| 173 | type FixtureFactory interface { |
| 174 | |
| 175 | // Creates a copy of this instance and adds some additional preparers. |
| 176 | // |
| 177 | // Before the preparers are used they are combined with the preparers provided when the factory |
| 178 | // was created, any groups of preparers are flattened, and the list is deduped so that each |
| 179 | // preparer is only used once. See the file documentation in android/fixture.go for more details. |
| 180 | Extend(preparers ...FixturePreparer) FixtureFactory |
| 181 | |
| 182 | // Create a Fixture. |
| 183 | Fixture(t *testing.T, preparers ...FixturePreparer) Fixture |
| 184 | |
Paul Duffin | 46e3774 | 2021-03-09 11:55:20 +0000 | [diff] [blame] | 185 | // ExtendWithErrorHandler creates a new FixtureFactory that will use the supplied error handler |
| 186 | // to check the errors (may be 0) reported by the test. |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 187 | // |
| 188 | // The default handlers is FixtureExpectsNoErrors which will fail the go test immediately if any |
| 189 | // errors are reported. |
Paul Duffin | 46e3774 | 2021-03-09 11:55:20 +0000 | [diff] [blame] | 190 | ExtendWithErrorHandler(errorHandler FixtureErrorHandler) FixtureFactory |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 191 | |
| 192 | // Run the test, checking any errors reported and returning a TestResult instance. |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 193 | // |
| 194 | // Shorthand for Fixture(t, preparers...).RunTest() |
| 195 | RunTest(t *testing.T, preparers ...FixturePreparer) *TestResult |
| 196 | |
| 197 | // Run the test with the supplied Android.bp file. |
| 198 | // |
| 199 | // Shorthand for RunTest(t, android.FixtureWithRootAndroidBp(bp)) |
| 200 | RunTestWithBp(t *testing.T, bp string) *TestResult |
Paul Duffin | 72018ad | 2021-03-04 19:36:49 +0000 | [diff] [blame] | 201 | |
| 202 | // RunTestWithConfig is a temporary method added to help ease the migration of existing tests to |
| 203 | // the test fixture. |
| 204 | // |
| 205 | // In order to allow the Config object to be customized separately to the TestContext a lot of |
| 206 | // existing test code has `test...WithConfig` funcs that allow the Config object to be supplied |
| 207 | // from the test and then have the TestContext created and configured automatically. e.g. |
| 208 | // testCcWithConfig, testCcErrorWithConfig, testJavaWithConfig, etc. |
| 209 | // |
| 210 | // This method allows those methods to be migrated to use the test fixture pattern without |
| 211 | // requiring that every test that uses those methods be migrated at the same time. That allows |
| 212 | // those tests to benefit from correctness in the order of registration quickly. |
| 213 | // |
| 214 | // This method discards the config (along with its mock file system, product variables, |
| 215 | // environment, etc.) that may have been set up by FixturePreparers. |
| 216 | // |
| 217 | // deprecated |
| 218 | RunTestWithConfig(t *testing.T, config Config) *TestResult |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // Create a new FixtureFactory that will apply the supplied preparers. |
| 222 | // |
| 223 | // The buildDirSupplier is a pointer to the package level buildDir variable that is initialized by |
| 224 | // the package level setUp method. It has to be a pointer to the variable as the variable will not |
Paul Duffin | dff5ff0 | 2021-03-15 15:42:40 +0000 | [diff] [blame] | 225 | // have been initialized at the time the factory is created. If it is nil then a test specific |
| 226 | // temporary directory will be created instead. |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 227 | func NewFixtureFactory(buildDirSupplier *string, preparers ...FixturePreparer) FixtureFactory { |
| 228 | return &fixtureFactory{ |
| 229 | buildDirSupplier: buildDirSupplier, |
| 230 | preparers: dedupAndFlattenPreparers(nil, preparers), |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 231 | |
| 232 | // Set the default error handler. |
| 233 | errorHandler: FixtureExpectsNoErrors, |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | // A set of mock files to add to the mock file system. |
| 238 | type MockFS map[string][]byte |
| 239 | |
Paul Duffin | 6e9a400 | 2021-03-11 19:01:26 +0000 | [diff] [blame] | 240 | // Merge adds the extra entries from the supplied map to this one. |
| 241 | // |
| 242 | // Fails if the supplied map files with the same paths are present in both of them. |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 243 | func (fs MockFS) Merge(extra map[string][]byte) { |
| 244 | for p, c := range extra { |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame^] | 245 | validateFixtureMockFSPath(p) |
Paul Duffin | 6e9a400 | 2021-03-11 19:01:26 +0000 | [diff] [blame] | 246 | if _, ok := fs[p]; ok { |
| 247 | panic(fmt.Errorf("attempted to add file %s to the mock filesystem but it already exists", p)) |
| 248 | } |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 249 | fs[p] = c |
| 250 | } |
| 251 | } |
| 252 | |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame^] | 253 | // Ensure that tests cannot add paths into the mock file system which would not be allowed in the |
| 254 | // runtime, e.g. absolute paths, paths relative to the 'out/' directory. |
| 255 | func validateFixtureMockFSPath(path string) { |
| 256 | // This uses validateSafePath rather than validatePath because the latter prevents adding files |
| 257 | // that include a $ but there are tests that allow files with a $ to be used, albeit only by |
| 258 | // globbing. |
| 259 | validatedPath, err := validateSafePath(path) |
| 260 | if err != nil { |
| 261 | panic(err) |
| 262 | } |
| 263 | |
| 264 | // Make sure that the path is canonical. |
| 265 | if validatedPath != path { |
| 266 | panic(fmt.Errorf("path %q is not a canonical path, use %q instead", path, validatedPath)) |
| 267 | } |
| 268 | |
| 269 | if path == "out" || strings.HasPrefix(path, "out/") { |
| 270 | panic(fmt.Errorf("cannot add output path %q to the mock file system", path)) |
| 271 | } |
| 272 | } |
| 273 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 274 | func (fs MockFS) AddToFixture() FixturePreparer { |
| 275 | return FixtureMergeMockFs(fs) |
| 276 | } |
| 277 | |
| 278 | // Modify the config |
| 279 | func FixtureModifyConfig(mutator func(config Config)) FixturePreparer { |
| 280 | return newSimpleFixturePreparer(func(f *fixture) { |
| 281 | mutator(f.config) |
| 282 | }) |
| 283 | } |
| 284 | |
| 285 | // Modify the config and context |
| 286 | func FixtureModifyConfigAndContext(mutator func(config Config, ctx *TestContext)) FixturePreparer { |
| 287 | return newSimpleFixturePreparer(func(f *fixture) { |
| 288 | mutator(f.config, f.ctx) |
| 289 | }) |
| 290 | } |
| 291 | |
| 292 | // Modify the context |
| 293 | func FixtureModifyContext(mutator func(ctx *TestContext)) FixturePreparer { |
| 294 | return newSimpleFixturePreparer(func(f *fixture) { |
| 295 | mutator(f.ctx) |
| 296 | }) |
| 297 | } |
| 298 | |
| 299 | func FixtureRegisterWithContext(registeringFunc func(ctx RegistrationContext)) FixturePreparer { |
| 300 | return FixtureModifyContext(func(ctx *TestContext) { registeringFunc(ctx) }) |
| 301 | } |
| 302 | |
| 303 | // Modify the mock filesystem |
| 304 | func FixtureModifyMockFS(mutator func(fs MockFS)) FixturePreparer { |
| 305 | return newSimpleFixturePreparer(func(f *fixture) { |
| 306 | mutator(f.mockFS) |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame^] | 307 | |
| 308 | // Make sure that invalid paths were not added to the mock filesystem. |
| 309 | for p, _ := range f.mockFS { |
| 310 | validateFixtureMockFSPath(p) |
| 311 | } |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 312 | }) |
| 313 | } |
| 314 | |
| 315 | // Merge the supplied file system into the mock filesystem. |
| 316 | // |
| 317 | // Paths that already exist in the mock file system are overridden. |
| 318 | func FixtureMergeMockFs(mockFS MockFS) FixturePreparer { |
| 319 | return FixtureModifyMockFS(func(fs MockFS) { |
| 320 | fs.Merge(mockFS) |
| 321 | }) |
| 322 | } |
| 323 | |
| 324 | // Add a file to the mock filesystem |
Paul Duffin | 6e9a400 | 2021-03-11 19:01:26 +0000 | [diff] [blame] | 325 | // |
| 326 | // Fail if the filesystem already contains a file with that path, use FixtureOverrideFile instead. |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 327 | func FixtureAddFile(path string, contents []byte) FixturePreparer { |
| 328 | return FixtureModifyMockFS(func(fs MockFS) { |
Paul Duffin | 80f4cea | 2021-03-16 14:08:00 +0000 | [diff] [blame^] | 329 | validateFixtureMockFSPath(path) |
Paul Duffin | 6e9a400 | 2021-03-11 19:01:26 +0000 | [diff] [blame] | 330 | if _, ok := fs[path]; ok { |
| 331 | panic(fmt.Errorf("attempted to add file %s to the mock filesystem but it already exists, use FixtureOverride*File instead", path)) |
| 332 | } |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 333 | fs[path] = contents |
| 334 | }) |
| 335 | } |
| 336 | |
| 337 | // Add a text file to the mock filesystem |
Paul Duffin | 6e9a400 | 2021-03-11 19:01:26 +0000 | [diff] [blame] | 338 | // |
| 339 | // Fail if the filesystem already contains a file with that path. |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 340 | func FixtureAddTextFile(path string, contents string) FixturePreparer { |
| 341 | return FixtureAddFile(path, []byte(contents)) |
| 342 | } |
| 343 | |
Paul Duffin | 6e9a400 | 2021-03-11 19:01:26 +0000 | [diff] [blame] | 344 | // Override a file in the mock filesystem |
| 345 | // |
| 346 | // If the file does not exist this behaves as FixtureAddFile. |
| 347 | func FixtureOverrideFile(path string, contents []byte) FixturePreparer { |
| 348 | return FixtureModifyMockFS(func(fs MockFS) { |
| 349 | fs[path] = contents |
| 350 | }) |
| 351 | } |
| 352 | |
| 353 | // Override a text file in the mock filesystem |
| 354 | // |
| 355 | // If the file does not exist this behaves as FixtureAddTextFile. |
| 356 | func FixtureOverrideTextFile(path string, contents string) FixturePreparer { |
| 357 | return FixtureOverrideFile(path, []byte(contents)) |
| 358 | } |
| 359 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 360 | // Add the root Android.bp file with the supplied contents. |
| 361 | func FixtureWithRootAndroidBp(contents string) FixturePreparer { |
| 362 | return FixtureAddTextFile("Android.bp", contents) |
| 363 | } |
| 364 | |
Paul Duffin | bbccfcf | 2021-03-03 00:44:00 +0000 | [diff] [blame] | 365 | // Merge some environment variables into the fixture. |
| 366 | func FixtureMergeEnv(env map[string]string) FixturePreparer { |
| 367 | return FixtureModifyConfig(func(config Config) { |
| 368 | for k, v := range env { |
| 369 | if k == "PATH" { |
| 370 | panic("Cannot set PATH environment variable") |
| 371 | } |
| 372 | config.env[k] = v |
| 373 | } |
| 374 | }) |
| 375 | } |
| 376 | |
| 377 | // Modify the env. |
| 378 | // |
| 379 | // Will panic if the mutator changes the PATH environment variable. |
| 380 | func FixtureModifyEnv(mutator func(env map[string]string)) FixturePreparer { |
| 381 | return FixtureModifyConfig(func(config Config) { |
| 382 | oldPath := config.env["PATH"] |
| 383 | mutator(config.env) |
| 384 | newPath := config.env["PATH"] |
| 385 | if newPath != oldPath { |
| 386 | panic(fmt.Errorf("Cannot change PATH environment variable from %q to %q", oldPath, newPath)) |
| 387 | } |
| 388 | }) |
| 389 | } |
| 390 | |
Paul Duffin | 2e0323d | 2021-03-04 15:11:01 +0000 | [diff] [blame] | 391 | // Allow access to the product variables when preparing the fixture. |
| 392 | type FixtureProductVariables struct { |
| 393 | *productVariables |
| 394 | } |
| 395 | |
| 396 | // Modify product variables. |
| 397 | func FixtureModifyProductVariables(mutator func(variables FixtureProductVariables)) FixturePreparer { |
| 398 | return FixtureModifyConfig(func(config Config) { |
| 399 | productVariables := FixtureProductVariables{&config.productVariables} |
| 400 | mutator(productVariables) |
| 401 | }) |
| 402 | } |
| 403 | |
Paul Duffin | a560d5a | 2021-02-28 01:38:51 +0000 | [diff] [blame] | 404 | // GroupFixturePreparers creates a composite FixturePreparer that is equivalent to applying each of |
| 405 | // the supplied FixturePreparer instances in order. |
| 406 | // |
| 407 | // Before preparing the fixture the list of preparers is flattened by replacing each |
| 408 | // instance of GroupFixturePreparers with its contents. |
| 409 | func GroupFixturePreparers(preparers ...FixturePreparer) FixturePreparer { |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 410 | return &compositeFixturePreparer{dedupAndFlattenPreparers(nil, preparers)} |
| 411 | } |
| 412 | |
Paul Duffin | 50deaae | 2021-03-16 17:46:12 +0000 | [diff] [blame] | 413 | // NullFixturePreparer is a preparer that does nothing. |
| 414 | var NullFixturePreparer = GroupFixturePreparers() |
| 415 | |
| 416 | // OptionalFixturePreparer will return the supplied preparer if it is non-nil, otherwise it will |
| 417 | // return the NullFixturePreparer |
| 418 | func OptionalFixturePreparer(preparer FixturePreparer) FixturePreparer { |
| 419 | if preparer == nil { |
| 420 | return NullFixturePreparer |
| 421 | } else { |
| 422 | return preparer |
| 423 | } |
| 424 | } |
| 425 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 426 | type simpleFixturePreparerVisitor func(preparer *simpleFixturePreparer) |
| 427 | |
| 428 | // FixturePreparer is an opaque interface that can change a fixture. |
| 429 | type FixturePreparer interface { |
| 430 | // visit calls the supplied visitor with each *simpleFixturePreparer instances in this preparer, |
| 431 | visit(simpleFixturePreparerVisitor) |
| 432 | } |
| 433 | |
| 434 | type fixturePreparers []FixturePreparer |
| 435 | |
| 436 | func (f fixturePreparers) visit(visitor simpleFixturePreparerVisitor) { |
| 437 | for _, p := range f { |
| 438 | p.visit(visitor) |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // dedupAndFlattenPreparers removes any duplicates and flattens any composite FixturePreparer |
| 443 | // instances. |
| 444 | // |
| 445 | // base - a list of already flattened and deduped preparers that will be applied first before |
| 446 | // the list of additional preparers. Any duplicates of these in the additional preparers |
| 447 | // will be ignored. |
| 448 | // |
| 449 | // preparers - a list of additional unflattened, undeduped preparers that will be applied after the |
| 450 | // base preparers. |
| 451 | // |
| 452 | // Returns a deduped and flattened list of the preparers minus any that exist in the base preparers. |
| 453 | func dedupAndFlattenPreparers(base []*simpleFixturePreparer, preparers fixturePreparers) []*simpleFixturePreparer { |
| 454 | var list []*simpleFixturePreparer |
| 455 | visited := make(map[*simpleFixturePreparer]struct{}) |
| 456 | |
| 457 | // Mark the already flattened and deduped preparers, if any, as having been seen so that |
| 458 | // duplicates of these in the additional preparers will be discarded. |
| 459 | for _, s := range base { |
| 460 | visited[s] = struct{}{} |
| 461 | } |
| 462 | |
| 463 | preparers.visit(func(preparer *simpleFixturePreparer) { |
| 464 | if _, seen := visited[preparer]; !seen { |
| 465 | visited[preparer] = struct{}{} |
| 466 | list = append(list, preparer) |
| 467 | } |
| 468 | }) |
| 469 | return list |
| 470 | } |
| 471 | |
| 472 | // compositeFixturePreparer is a FixturePreparer created from a list of fixture preparers. |
| 473 | type compositeFixturePreparer struct { |
| 474 | preparers []*simpleFixturePreparer |
| 475 | } |
| 476 | |
| 477 | func (c *compositeFixturePreparer) visit(visitor simpleFixturePreparerVisitor) { |
| 478 | for _, p := range c.preparers { |
| 479 | p.visit(visitor) |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // simpleFixturePreparer is a FixturePreparer that applies a function to a fixture. |
| 484 | type simpleFixturePreparer struct { |
| 485 | function func(fixture *fixture) |
| 486 | } |
| 487 | |
| 488 | func (s *simpleFixturePreparer) visit(visitor simpleFixturePreparerVisitor) { |
| 489 | visitor(s) |
| 490 | } |
| 491 | |
| 492 | func newSimpleFixturePreparer(preparer func(fixture *fixture)) FixturePreparer { |
| 493 | return &simpleFixturePreparer{function: preparer} |
| 494 | } |
| 495 | |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 496 | // FixtureErrorHandler determines how to respond to errors reported by the code under test. |
| 497 | // |
| 498 | // Some possible responses: |
| 499 | // * Fail the test if any errors are reported, see FixtureExpectsNoErrors. |
| 500 | // * Fail the test if at least one error that matches a pattern is not reported see |
| 501 | // FixtureExpectsAtLeastOneErrorMatchingPattern |
| 502 | // * Fail the test if any unexpected errors are reported. |
| 503 | // |
| 504 | // Although at the moment all the error handlers are implemented as simply a wrapper around a |
| 505 | // function this is defined as an interface to allow future enhancements, e.g. provide different |
| 506 | // ways other than patterns to match an error and to combine handlers together. |
| 507 | type FixtureErrorHandler interface { |
| 508 | // CheckErrors checks the errors reported. |
| 509 | // |
| 510 | // The supplied result can be used to access the state of the code under test just as the main |
| 511 | // body of the test would but if any errors other than ones expected are reported the state may |
| 512 | // be indeterminate. |
Paul Duffin | c81854a | 2021-03-12 12:22:27 +0000 | [diff] [blame] | 513 | CheckErrors(t *testing.T, result *TestResult) |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | type simpleErrorHandler struct { |
Paul Duffin | c81854a | 2021-03-12 12:22:27 +0000 | [diff] [blame] | 517 | function func(t *testing.T, result *TestResult) |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Paul Duffin | c81854a | 2021-03-12 12:22:27 +0000 | [diff] [blame] | 520 | func (h simpleErrorHandler) CheckErrors(t *testing.T, result *TestResult) { |
| 521 | t.Helper() |
| 522 | h.function(t, result) |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | // The default fixture error handler. |
| 526 | // |
| 527 | // Will fail the test immediately if any errors are reported. |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 528 | // |
| 529 | // If the test fails this handler will call `result.FailNow()` which will exit the goroutine within |
| 530 | // which the test is being run which means that the RunTest() method will not return. |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 531 | var FixtureExpectsNoErrors = FixtureCustomErrorHandler( |
Paul Duffin | c81854a | 2021-03-12 12:22:27 +0000 | [diff] [blame] | 532 | func(t *testing.T, result *TestResult) { |
| 533 | t.Helper() |
| 534 | FailIfErrored(t, result.Errs) |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 535 | }, |
| 536 | ) |
| 537 | |
Paul Duffin | 85034e9 | 2021-03-17 00:20:34 +0000 | [diff] [blame] | 538 | // FixtureIgnoreErrors ignores any errors. |
| 539 | // |
| 540 | // If this is used then it is the responsibility of the test to check the TestResult.Errs does not |
| 541 | // contain any unexpected errors. |
| 542 | var FixtureIgnoreErrors = FixtureCustomErrorHandler(func(t *testing.T, result *TestResult) { |
| 543 | // Ignore the errors |
| 544 | }) |
| 545 | |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 546 | // FixtureExpectsAtLeastOneMatchingError returns an error handler that will cause the test to fail |
| 547 | // if at least one error that matches the regular expression is not found. |
| 548 | // |
| 549 | // The test will be failed if: |
| 550 | // * No errors are reported. |
| 551 | // * One or more errors are reported but none match the pattern. |
| 552 | // |
| 553 | // The test will not fail if: |
| 554 | // * Multiple errors are reported that do not match the pattern as long as one does match. |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 555 | // |
| 556 | // If the test fails this handler will call `result.FailNow()` which will exit the goroutine within |
| 557 | // which the test is being run which means that the RunTest() method will not return. |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 558 | func FixtureExpectsAtLeastOneErrorMatchingPattern(pattern string) FixtureErrorHandler { |
Paul Duffin | c81854a | 2021-03-12 12:22:27 +0000 | [diff] [blame] | 559 | return FixtureCustomErrorHandler(func(t *testing.T, result *TestResult) { |
| 560 | t.Helper() |
| 561 | if !FailIfNoMatchingErrors(t, pattern, result.Errs) { |
| 562 | t.FailNow() |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 563 | } |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 564 | }) |
| 565 | } |
| 566 | |
| 567 | // FixtureExpectsOneErrorToMatchPerPattern returns an error handler that will cause the test to fail |
| 568 | // if there are any unexpected errors. |
| 569 | // |
| 570 | // The test will be failed if: |
| 571 | // * The number of errors reported does not exactly match the patterns. |
| 572 | // * One or more of the reported errors do not match a pattern. |
| 573 | // * No patterns are provided and one or more errors are reported. |
| 574 | // |
| 575 | // The test will not fail if: |
| 576 | // * One or more of the patterns does not match an error. |
Paul Duffin | ea8a386 | 2021-03-04 17:58:33 +0000 | [diff] [blame] | 577 | // |
| 578 | // If the test fails this handler will call `result.FailNow()` which will exit the goroutine within |
| 579 | // which the test is being run which means that the RunTest() method will not return. |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 580 | func FixtureExpectsAllErrorsToMatchAPattern(patterns []string) FixtureErrorHandler { |
Paul Duffin | c81854a | 2021-03-12 12:22:27 +0000 | [diff] [blame] | 581 | return FixtureCustomErrorHandler(func(t *testing.T, result *TestResult) { |
| 582 | t.Helper() |
| 583 | CheckErrorsAgainstExpectations(t, result.Errs, patterns) |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 584 | }) |
| 585 | } |
| 586 | |
| 587 | // FixtureCustomErrorHandler creates a custom error handler |
Paul Duffin | c81854a | 2021-03-12 12:22:27 +0000 | [diff] [blame] | 588 | func FixtureCustomErrorHandler(function func(t *testing.T, result *TestResult)) FixtureErrorHandler { |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 589 | return simpleErrorHandler{ |
| 590 | function: function, |
| 591 | } |
| 592 | } |
| 593 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 594 | // Fixture defines the test environment. |
| 595 | type Fixture interface { |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 596 | // Run the test, checking any errors reported and returning a TestResult instance. |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 597 | RunTest() *TestResult |
| 598 | } |
| 599 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 600 | // Struct to allow TestResult to embed a *TestContext and allow call forwarding to its methods. |
| 601 | type testContext struct { |
| 602 | *TestContext |
| 603 | } |
| 604 | |
| 605 | // The result of running a test. |
| 606 | type TestResult struct { |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 607 | testContext |
| 608 | |
| 609 | fixture *fixture |
| 610 | Config Config |
Paul Duffin | 942481b | 2021-03-04 18:58:11 +0000 | [diff] [blame] | 611 | |
| 612 | // The errors that were reported during the test. |
| 613 | Errs []error |
Paul Duffin | 78c3621 | 2021-03-16 23:57:12 +0000 | [diff] [blame] | 614 | |
| 615 | // The ninja deps is a list of the ninja files dependencies that were added by the modules and |
| 616 | // singletons via the *.AddNinjaFileDeps() methods. |
| 617 | NinjaDeps []string |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | var _ FixtureFactory = (*fixtureFactory)(nil) |
| 621 | |
| 622 | type fixtureFactory struct { |
| 623 | buildDirSupplier *string |
| 624 | preparers []*simpleFixturePreparer |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 625 | errorHandler FixtureErrorHandler |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | func (f *fixtureFactory) Extend(preparers ...FixturePreparer) FixtureFactory { |
Paul Duffin | fa29885 | 2021-03-08 15:05:24 +0000 | [diff] [blame] | 629 | // Create a new slice to avoid accidentally sharing the preparers slice from this factory with |
| 630 | // the extending factories. |
| 631 | var all []*simpleFixturePreparer |
| 632 | all = append(all, f.preparers...) |
| 633 | all = append(all, dedupAndFlattenPreparers(f.preparers, preparers)...) |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 634 | // Copy the existing factory. |
| 635 | extendedFactory := &fixtureFactory{} |
| 636 | *extendedFactory = *f |
| 637 | // Use the extended list of preparers. |
| 638 | extendedFactory.preparers = all |
| 639 | return extendedFactory |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | func (f *fixtureFactory) Fixture(t *testing.T, preparers ...FixturePreparer) Fixture { |
Paul Duffin | dff5ff0 | 2021-03-15 15:42:40 +0000 | [diff] [blame] | 643 | var buildDir string |
| 644 | if f.buildDirSupplier == nil { |
| 645 | // Create a new temporary directory for this run. It will be automatically cleaned up when the |
| 646 | // test finishes. |
| 647 | buildDir = t.TempDir() |
| 648 | } else { |
| 649 | // Retrieve the buildDir from the supplier. |
| 650 | buildDir = *f.buildDirSupplier |
| 651 | } |
| 652 | config := TestConfig(buildDir, nil, "", nil) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 653 | ctx := NewTestContext(config) |
| 654 | fixture := &fixture{ |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 655 | factory: f, |
| 656 | t: t, |
| 657 | config: config, |
| 658 | ctx: ctx, |
| 659 | mockFS: make(MockFS), |
| 660 | errorHandler: f.errorHandler, |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | for _, preparer := range f.preparers { |
| 664 | preparer.function(fixture) |
| 665 | } |
| 666 | |
| 667 | for _, preparer := range dedupAndFlattenPreparers(f.preparers, preparers) { |
| 668 | preparer.function(fixture) |
| 669 | } |
| 670 | |
| 671 | return fixture |
| 672 | } |
| 673 | |
Paul Duffin | 46e3774 | 2021-03-09 11:55:20 +0000 | [diff] [blame] | 674 | func (f *fixtureFactory) ExtendWithErrorHandler(errorHandler FixtureErrorHandler) FixtureFactory { |
Paul Duffin | 52323b5 | 2021-03-04 19:15:47 +0000 | [diff] [blame] | 675 | newFactory := &fixtureFactory{} |
| 676 | *newFactory = *f |
| 677 | newFactory.errorHandler = errorHandler |
| 678 | return newFactory |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 681 | func (f *fixtureFactory) RunTest(t *testing.T, preparers ...FixturePreparer) *TestResult { |
| 682 | t.Helper() |
| 683 | fixture := f.Fixture(t, preparers...) |
| 684 | return fixture.RunTest() |
| 685 | } |
| 686 | |
| 687 | func (f *fixtureFactory) RunTestWithBp(t *testing.T, bp string) *TestResult { |
| 688 | t.Helper() |
| 689 | return f.RunTest(t, FixtureWithRootAndroidBp(bp)) |
| 690 | } |
| 691 | |
Paul Duffin | 72018ad | 2021-03-04 19:36:49 +0000 | [diff] [blame] | 692 | func (f *fixtureFactory) RunTestWithConfig(t *testing.T, config Config) *TestResult { |
| 693 | t.Helper() |
| 694 | // Create the fixture as normal. |
| 695 | fixture := f.Fixture(t).(*fixture) |
| 696 | |
| 697 | // Discard the mock filesystem as otherwise that will override the one in the config. |
| 698 | fixture.mockFS = nil |
| 699 | |
| 700 | // Replace the config with the supplied one in the fixture. |
| 701 | fixture.config = config |
| 702 | |
| 703 | // Ditto with config derived information in the TestContext. |
| 704 | ctx := fixture.ctx |
| 705 | ctx.config = config |
| 706 | ctx.SetFs(ctx.config.fs) |
| 707 | if ctx.config.mockBpList != "" { |
| 708 | ctx.SetModuleListFile(ctx.config.mockBpList) |
| 709 | } |
| 710 | |
| 711 | return fixture.RunTest() |
| 712 | } |
| 713 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 714 | type fixture struct { |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 715 | // The factory used to create this fixture. |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 716 | factory *fixtureFactory |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 717 | |
| 718 | // The gotest state of the go test within which this was created. |
| 719 | t *testing.T |
| 720 | |
| 721 | // The configuration prepared for this fixture. |
| 722 | config Config |
| 723 | |
| 724 | // The test context prepared for this fixture. |
| 725 | ctx *TestContext |
| 726 | |
| 727 | // The mock filesystem prepared for this fixture. |
| 728 | mockFS MockFS |
| 729 | |
| 730 | // The error handler used to check the errors, if any, that are reported. |
| 731 | errorHandler FixtureErrorHandler |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | func (f *fixture) RunTest() *TestResult { |
| 735 | f.t.Helper() |
| 736 | |
| 737 | ctx := f.ctx |
| 738 | |
Paul Duffin | 72018ad | 2021-03-04 19:36:49 +0000 | [diff] [blame] | 739 | // Do not use the fixture's mockFS to initialize the config's mock file system if it has been |
| 740 | // cleared by RunTestWithConfig. |
| 741 | if f.mockFS != nil { |
| 742 | // The TestConfig() method assumes that the mock filesystem is available when creating so |
| 743 | // creates the mock file system immediately. Similarly, the NewTestContext(Config) method |
| 744 | // assumes that the supplied Config's FileSystem has been properly initialized before it is |
| 745 | // called and so it takes its own reference to the filesystem. However, fixtures create the |
| 746 | // Config and TestContext early so they can be modified by preparers at which time the mockFS |
| 747 | // has not been populated (because it too is modified by preparers). So, this reinitializes the |
| 748 | // Config and TestContext's FileSystem using the now populated mockFS. |
| 749 | f.config.mockFileSystem("", f.mockFS) |
| 750 | |
| 751 | ctx.SetFs(ctx.config.fs) |
| 752 | if ctx.config.mockBpList != "" { |
| 753 | ctx.SetModuleListFile(ctx.config.mockBpList) |
| 754 | } |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | ctx.Register() |
Paul Duffin | 78c3621 | 2021-03-16 23:57:12 +0000 | [diff] [blame] | 758 | var ninjaDeps []string |
| 759 | extraNinjaDeps, errs := ctx.ParseBlueprintsFiles("ignored") |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 760 | if len(errs) == 0 { |
Paul Duffin | 78c3621 | 2021-03-16 23:57:12 +0000 | [diff] [blame] | 761 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
| 762 | extraNinjaDeps, errs = ctx.PrepareBuildActions(f.config) |
| 763 | if len(errs) == 0 { |
| 764 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
| 765 | } |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 766 | } |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 767 | |
| 768 | result := &TestResult{ |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 769 | testContext: testContext{ctx}, |
| 770 | fixture: f, |
| 771 | Config: f.config, |
Paul Duffin | 942481b | 2021-03-04 18:58:11 +0000 | [diff] [blame] | 772 | Errs: errs, |
Paul Duffin | 78c3621 | 2021-03-16 23:57:12 +0000 | [diff] [blame] | 773 | NinjaDeps: ninjaDeps, |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 774 | } |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 775 | |
Paul Duffin | c81854a | 2021-03-12 12:22:27 +0000 | [diff] [blame] | 776 | f.errorHandler.CheckErrors(f.t, result) |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 777 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 778 | return result |
| 779 | } |
| 780 | |
| 781 | // NormalizePathForTesting removes the test invocation specific build directory from the supplied |
| 782 | // path. |
| 783 | // |
| 784 | // If the path is within the build directory (e.g. an OutputPath) then this returns the relative |
| 785 | // path to avoid tests having to deal with the dynamically generated build directory. |
| 786 | // |
| 787 | // Otherwise, this returns the supplied path as it is almost certainly a source path that is |
| 788 | // relative to the root of the source tree. |
| 789 | // |
| 790 | // Even though some information is removed from some paths and not others it should be possible to |
| 791 | // differentiate between them by the paths themselves, e.g. output paths will likely include |
| 792 | // ".intermediates" but source paths won't. |
| 793 | func (r *TestResult) NormalizePathForTesting(path Path) string { |
| 794 | pathContext := PathContextForTesting(r.Config) |
| 795 | pathAsString := path.String() |
| 796 | if rel, isRel := MaybeRel(pathContext, r.Config.BuildDir(), pathAsString); isRel { |
| 797 | return rel |
| 798 | } |
| 799 | return pathAsString |
| 800 | } |
| 801 | |
| 802 | // NormalizePathsForTesting normalizes each path in the supplied list and returns their normalized |
| 803 | // forms. |
| 804 | func (r *TestResult) NormalizePathsForTesting(paths Paths) []string { |
| 805 | var result []string |
| 806 | for _, path := range paths { |
| 807 | result = append(result, r.NormalizePathForTesting(path)) |
| 808 | } |
| 809 | return result |
| 810 | } |
| 811 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 812 | // Module returns the module with the specific name and of the specified variant. |
| 813 | func (r *TestResult) Module(name string, variant string) Module { |
| 814 | return r.ModuleForTests(name, variant).Module() |
| 815 | } |