complement/ct/test.go
kegsay 5ecb086412
refactor: make logging test agnostic and colourful (#703)
- The entire Complement API now no longer uses `t *testing.T`, instead
  opting for `ct.TestLike` which is an interface. In practice, this changes
  nothing. However, this enables Complement to be used in many more places
  where you do not have a `t *testing.T` object to use e.g benchmarks, scripts.
  This is of particular use for Complement-Crypto which has to run parts of the
  test as a standalone binary, and therefore has no `t *testing.T` to use.
- The entire Complement API now uses `ct.Fatalf` and `ct.Errorf` for highlighting
  test failures in red. This should make it significantly easier to skim for the
  test failure message.
- Add `Deployment.ContainerID(TestLike, hsName) string` to allow tests to interact
  with the container beyond the abilities of the Complement API e.g log extraction,
  memory use, CPU use.
2024-01-17 12:41:04 +00:00

37 lines
1.4 KiB
Go

// package ct contains wrappers and interfaces around testing.T
//
// The intention is that _all_ complement functions deal with these wrapper interfaces
// rather than the literal testing.T. This enables Complement to be run in environments
// that aren't strictly via `go test`.
package ct
// TestLike is an interface that testing.T satisfies. All client functions accept a TestLike interface,
// with the intention of a `testing.T` being passed into them. However, the client may be used in non-test
// scenarios e.g benchmarks, which can then use the same client by just implementing this interface.
type TestLike interface {
Helper()
Logf(msg string, args ...interface{})
Skipf(msg string, args ...interface{})
Error(args ...interface{})
Errorf(msg string, args ...interface{})
Fatalf(msg string, args ...interface{})
Failed() bool
Name() string
}
const ansiRedForeground = "\x1b[31m"
const ansiResetForeground = "\x1b[39m"
// Errorf is a wrapper around t.Errorf which prints the failing error message in red.
func Errorf(t TestLike, format string, args ...any) {
t.Helper()
format = ansiRedForeground + format + ansiResetForeground
t.Errorf(format, args...)
}
// Fatalf is a wrapper around t.Fatalf which prints the failing error message in red.
func Fatalf(t TestLike, format string, args ...any) {
t.Helper()
format = ansiRedForeground + format + ansiResetForeground
t.Fatalf(format, args...)
}