blob: 53f278fe808bac9134e748518d4b16a909e59452 [file] [edit]
// 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"
"runtime"
"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+$`
var tastTestsIntelPattern = `^go.chromium.org/partner-intel-private/crosint_intel/(local|remote)/bundles/[^/]+/[^/]+\.init.\d+$`
var verifier = newCallerVerifier(regexp.MustCompile(fmt.Sprintf("(%s)|(%s)|(%s)",
tastTestsPattern, tastTestsPrivatePattern, tastTestsIntelPattern)))
// 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) {
_, fn, _, _ := runtime.Caller(1)
pkg, _ := packages.SplitFuncName(caller.Get(2))
srcPath := packages.SrcPathInTastRepo(fn)
testing.GlobalRegistry().AddFixture(f, pkg, srcPath)
}