mirror of
https://github.com/tailscale/tailscale.git
synced 2026-07-27 07:46:26 -04:00
Change tstest's exported functions (AssertNotParallel, Replace, Parallel, RequireRoot, SkipOnKernelVersions, MinAllocsPerRun, FixLogs, UnfixLogs, CheckIsZero, ResourceCheck) to take testenv.TB instead of testing.TB or *testing.T, so importing tstest from non-test code no longer links the testing package and its flag registration side effects into the binary. Add testenv.Verbose to replace the one use of testing.Verbose, and a deptest check to keep testing out of tstest's dependency graph. Callers are unaffected: *testing.T and testing.TB both satisfy testenv.TB. Updates tailscale/corp#45223 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Change-Id: Ib373ff66ceff638d071582baf8367245987e9155
85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package testenv provides utility functions for tests. It does not depend on
|
|
// the `testing` package to allow usage in non-test code.
|
|
package testenv
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"io"
|
|
|
|
"tailscale.com/types/lazy"
|
|
)
|
|
|
|
var lazyInTest lazy.SyncValue[bool]
|
|
|
|
// InTest reports whether the current binary is a test binary.
|
|
func InTest() bool {
|
|
return lazyInTest.Get(func() bool {
|
|
return flag.Lookup("test.v") != nil
|
|
})
|
|
}
|
|
|
|
// Verbose reports whether the test binary is running with verbose
|
|
// output (the -test.v flag), like testing.Verbose but without
|
|
// importing the testing package.
|
|
func Verbose() bool {
|
|
f := flag.Lookup("test.v")
|
|
if f == nil {
|
|
return false
|
|
}
|
|
// The flag's String value is "false" when off, and "true" or
|
|
// "test2json" when on.
|
|
return f.Value.String() != "false"
|
|
}
|
|
|
|
// TB is testing.TB, to avoid importing "testing" in non-test code.
|
|
type TB interface {
|
|
ArtifactDir() string
|
|
Attr(key, value string)
|
|
Cleanup(func())
|
|
Error(args ...any)
|
|
Errorf(format string, args ...any)
|
|
Fail()
|
|
FailNow()
|
|
Failed() bool
|
|
Fatal(args ...any)
|
|
Fatalf(format string, args ...any)
|
|
Helper()
|
|
Log(args ...any)
|
|
Logf(format string, args ...any)
|
|
Name() string
|
|
Output() io.Writer
|
|
Setenv(key, value string)
|
|
Chdir(dir string)
|
|
Skip(args ...any)
|
|
SkipNow()
|
|
Skipf(format string, args ...any)
|
|
Skipped() bool
|
|
TempDir() string
|
|
Context() context.Context
|
|
}
|
|
|
|
// InParallelTest reports whether t is running as a parallel test.
|
|
//
|
|
// Use of this function taints t such that its Parallel method (assuming t is an
|
|
// actual *testing.T) will panic if called after this function.
|
|
func InParallelTest(t TB) (isParallel bool) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
isParallel = true
|
|
}
|
|
}()
|
|
t.Chdir(".") // panics in a t.Parallel test
|
|
return false
|
|
}
|
|
|
|
// AssertInTest panics if called outside of a test binary.
|
|
func AssertInTest() {
|
|
if !InTest() {
|
|
panic("func called outside of test binary")
|
|
}
|
|
}
|