Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Add Cleanup, clarify versioning in README
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Mar 24, 2020
1 parent 6d0b801 commit 424dfd8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 129 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ You can also call the test helper at runtime if needed:
TestHelper(&testing.RuntimeT{})
}

## Versioning

The tagged version matches the version of Go that the interface is
compatible with. For example, the version "1.14.0" is for Go 1.14 and
introduced the `Cleanup` function. The patch version (the ".0" in the
prior example) is used to fix any bugs found in this library and has no
correlation to the supported Go version.

## Why?!

**Why would I call a test helper that takes a *testing.T at runtime?**
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/mitchellh/go-testing-interface

go 1.14
65 changes: 44 additions & 21 deletions testing.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !go1.9

package testing

import (
Expand All @@ -12,13 +10,15 @@ import (
// In unit tests you can just pass a *testing.T struct. At runtime, outside
// of tests, you can pass in a RuntimeT struct from this package.
type T interface {
Cleanup(func())
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fail()
FailNow()
Failed() bool
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Helper()
Log(args ...interface{})
Logf(format string, args ...interface{})
Name() string
Expand All @@ -31,10 +31,13 @@ type T interface {
// RuntimeT implements T and can be instantiated and run at runtime to
// mimic *testing.T behavior. Unlike *testing.T, this will simply panic
// for calls to Fatal. For calls to Error, you'll have to check the errors
// list to determine whether to exit yourself. Name and Skip methods are
// unimplemented noops.
// list to determine whether to exit yourself.
//
// Cleanup does NOT work, so if you're using a helper that uses Cleanup,
// there may be dangling resources.
type RuntimeT struct {
failed bool
skipped bool
failed bool
}

func (t *RuntimeT) Error(args ...interface{}) {
Expand All @@ -43,20 +46,10 @@ func (t *RuntimeT) Error(args ...interface{}) {
}

func (t *RuntimeT) Errorf(format string, args ...interface{}) {
log.Println(fmt.Sprintf(format, args...))
log.Printf(format, args...)
t.Fail()
}

func (t *RuntimeT) Fatal(args ...interface{}) {
log.Println(fmt.Sprintln(args...))
t.FailNow()
}

func (t *RuntimeT) Fatalf(format string, args ...interface{}) {
log.Println(fmt.Sprintf(format, args...))
t.FailNow()
}

func (t *RuntimeT) Fail() {
t.failed = true
}
Expand All @@ -69,6 +62,16 @@ func (t *RuntimeT) Failed() bool {
return t.failed
}

func (t *RuntimeT) Fatal(args ...interface{}) {
log.Print(args...)
t.FailNow()
}

func (t *RuntimeT) Fatalf(format string, args ...interface{}) {
log.Printf(format, args...)
t.FailNow()
}

func (t *RuntimeT) Log(args ...interface{}) {
log.Println(fmt.Sprintln(args...))
}
Expand All @@ -77,8 +80,28 @@ func (t *RuntimeT) Logf(format string, args ...interface{}) {
log.Println(fmt.Sprintf(format, args...))
}

func (t *RuntimeT) Name() string { return "" }
func (t *RuntimeT) Skip(args ...interface{}) {}
func (t *RuntimeT) SkipNow() {}
func (t *RuntimeT) Skipf(format string, args ...interface{}) {}
func (t *RuntimeT) Skipped() bool { return false }
func (t *RuntimeT) Name() string {
return ""
}

func (t *RuntimeT) Skip(args ...interface{}) {
log.Print(args...)
t.SkipNow()
}

func (t *RuntimeT) SkipNow() {
t.skipped = true
}

func (t *RuntimeT) Skipf(format string, args ...interface{}) {
log.Printf(format, args...)
t.SkipNow()
}

func (t *RuntimeT) Skipped() bool {
return t.skipped
}

func (t *RuntimeT) Helper() {}

func (t *RuntimeT) Cleanup(func()) {}
108 changes: 0 additions & 108 deletions testing_go19.go

This file was deleted.

0 comments on commit 424dfd8

Please sign in to comment.