| // Copyright 2023 The ChromiumOS Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package testing |
| |
| import ( |
| "fmt" |
| "regexp" |
| |
| "go.chromium.org/tast/core/internal/caller" |
| "go.chromium.org/tast/core/internal/packages" |
| "go.chromium.org/tast/core/internal/testing" |
| ) |
| |
| // verifier is a global singleton to check if AddTest() is used as designed. |
| var tastTestsPattern = `^go.chromium.org/tast-tests/cros/(local|remote)/bundles/[^/]+/[^/]+\.init.\d+$` |
| var tastTestsPrivatePattern = `^go.chromium.org/tast-tests-private/crosint/(local|remote)/bundles/[^/]+/[^/]+\.init.\d+$` |
| |
| // TODO: b/187792551 -- remove old tast test pattern after Tast tests directory structure changes. |
| var oldTastTestsPattern = `^chromiumos/tast/(local|remote)/bundles/[^/]+/[^/]+\.init.\d+$` |
| var verifier = newCallerVerifier(regexp.MustCompile(fmt.Sprintf("(%s)|(%s)|(%s)", |
| oldTastTestsPattern, tastTestsPattern, tastTestsPrivatePattern))) |
| |
| // RegistrationErrors returns errors generated by calls to AddTest. |
| func RegistrationErrors() []error { |
| return testing.GlobalRegistry().Errors() |
| } |
| |
| // AddTest adds test t to the global registry. |
| // This should be called only once in a test main file's init(), |
| // and it should be the top level statement of the init()'s body. |
| // The argument of AddTest() in the case should be a pointer to a |
| // composite literal of testing.Test. |
| func AddTest(t *Test) { |
| if err := verifier.verifyAndRegister(caller.Func(2)); err != nil { |
| testing.GlobalRegistry().RecordError(err) |
| return |
| } |
| testing.GlobalRegistry().AddTest(t) |
| } |
| |
| // AddService adds service s to the global registry. |
| // This should be called only once in a service main file's init(). |
| func AddService(s *Service) { |
| testing.GlobalRegistry().AddService(s) |
| } |
| |
| // AddFixture adds fixture f to the global registry. |
| func AddFixture(f *Fixture) { |
| pkg, _ := packages.SplitFuncName(caller.Get(2)) |
| testing.GlobalRegistry().AddFixture(f, pkg) |
| } |