|
| 1 | +package testbox |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "runtime" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | +) |
| 9 | + |
| 10 | +// SandboxResult describes the aggregate test state produced by calling SandboxTest. |
| 11 | +type SandboxResult struct { |
| 12 | + // True if any failures were reported during SandboxTest. |
| 13 | + Failed bool |
| 14 | + |
| 15 | + // True if the test run with SandboxTest called Skip or SkipNow. This is only true if |
| 16 | + // the top-level TestingT was skipped, not any subtests. |
| 17 | + Skipped bool |
| 18 | + |
| 19 | + // All failures logged during SandboxTest, including subtests. |
| 20 | + Failures []LogItem |
| 21 | + |
| 22 | + // All tests that were skipped during SandboxTest, including subtests. |
| 23 | + Skips []LogItem |
| 24 | +} |
| 25 | + |
| 26 | +type testState struct { |
| 27 | + failed bool |
| 28 | + skipped bool |
| 29 | + failures []LogItem |
| 30 | + skips []LogItem |
| 31 | +} |
| 32 | + |
| 33 | +// TestPath identifies the level of test that failed or skipped. SandboxResult.Failures and |
| 34 | +// SandboxResult.Skips use this type to distinguish between the top-level test that was run with |
| 35 | +// SandboxTest and subtests that were run within that test with TestingT.Run(). A nil value means the |
| 36 | +// top-level test; a single string element is the name of a subtest run from the top level with |
| 37 | +// TestingT.Run(); nested subtests add an element for each level. |
| 38 | +type TestPath []string |
| 39 | + |
| 40 | +// LogItem describes either a failed assertion or a skip that happened during SandboxTest. |
| 41 | +type LogItem struct { |
| 42 | + // Path identifies the level of test that failed or was skipped. |
| 43 | + Path TestPath |
| 44 | + |
| 45 | + // Message is the failure message or skip message, if any. It is the result of calling fmt.Sprintf |
| 46 | + // or Sprintln on the arguments that were passed to TestingT.Errorf or TestingT.Skip. If a test |
| 47 | + // failed without specifying a message, this is "". |
| 48 | + Message string |
| 49 | +} |
| 50 | + |
| 51 | +type mockTestingT struct { |
| 52 | + testState |
| 53 | + path TestPath |
| 54 | + lock sync.Mutex |
| 55 | +} |
| 56 | + |
| 57 | +// SandboxTest runs a test function against a TestingT instance that applies only to the scope of |
| 58 | +// that test. If the function makes a failed assertion, marks the test as skipped, or forces an early |
| 59 | +// exit with FailNow or SkipNow, this is reflected in the SandboxResult but does not affect the state |
| 60 | +// of the regular test framework (assuming that this code is even executing within a Go test; it does |
| 61 | +// not have to be). |
| 62 | +// |
| 63 | +// The reason this uses a callback function parameter, rather than simply having the SandboxResult |
| 64 | +// implement TestingT itself, is that the function must be run on a separate goroutine so that |
| 65 | +// the sandbox can intercept any early exits from FailNow or SkipNow. |
| 66 | +// |
| 67 | +// SandboxTest does not recover from panics. |
| 68 | +// |
| 69 | +// See TestingT for more details. |
| 70 | +func SandboxTest(action func(TestingT)) SandboxResult { |
| 71 | + sub := new(mockTestingT) |
| 72 | + sub.runSafely(action) |
| 73 | + state := sub.getState() |
| 74 | + return SandboxResult{ |
| 75 | + Failed: state.failed, |
| 76 | + Skipped: state.skipped, |
| 77 | + Failures: state.failures, |
| 78 | + Skips: state.skips, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func (m *mockTestingT) Errorf(format string, args ...interface{}) { |
| 83 | + m.lock.Lock() |
| 84 | + defer m.lock.Unlock() |
| 85 | + m.failed = true |
| 86 | + m.failures = append(m.failures, LogItem{Path: m.path, Message: fmt.Sprintf(format, args...)}) |
| 87 | +} |
| 88 | + |
| 89 | +func (m *mockTestingT) Run(name string, action func(TestingT)) { |
| 90 | + sub := &mockTestingT{path: append(m.path, name)} |
| 91 | + sub.runSafely(action) |
| 92 | + subState := sub.getState() |
| 93 | + |
| 94 | + m.lock.Lock() |
| 95 | + defer m.lock.Unlock() |
| 96 | + m.failed = m.failed || subState.failed |
| 97 | + m.failures = append(m.failures, subState.failures...) |
| 98 | + m.skips = append(m.skips, subState.skips...) |
| 99 | +} |
| 100 | + |
| 101 | +func (m *mockTestingT) FailNow() { |
| 102 | + m.lock.Lock() |
| 103 | + defer m.lock.Unlock() |
| 104 | + m.testState.failed = true |
| 105 | + runtime.Goexit() |
| 106 | +} |
| 107 | + |
| 108 | +func (m *mockTestingT) Failed() bool { |
| 109 | + m.lock.Lock() |
| 110 | + defer m.lock.Unlock() |
| 111 | + return m.failed |
| 112 | +} |
| 113 | + |
| 114 | +func (m *mockTestingT) Skip(args ...interface{}) { |
| 115 | + m.lock.Lock() |
| 116 | + defer m.lock.Unlock() |
| 117 | + m.skipped = true |
| 118 | + m.skips = append(m.skips, LogItem{Path: m.path, Message: strings.TrimSuffix(fmt.Sprintln(args...), "\n")}) |
| 119 | + runtime.Goexit() |
| 120 | +} |
| 121 | + |
| 122 | +func (m *mockTestingT) SkipNow() { |
| 123 | + m.Skip() |
| 124 | +} |
| 125 | + |
| 126 | +func (m *mockTestingT) getState() testState { |
| 127 | + m.lock.Lock() |
| 128 | + defer m.lock.Unlock() |
| 129 | + ret := testState{failed: m.failed, skipped: m.skipped} |
| 130 | + if len(m.failures) > 0 { |
| 131 | + ret.failures = make([]LogItem, len(m.failures)) |
| 132 | + copy(ret.failures, m.failures) |
| 133 | + } |
| 134 | + if len(m.skips) > 0 { |
| 135 | + ret.skips = make([]LogItem, len(m.skips)) |
| 136 | + copy(ret.skips, m.skips) |
| 137 | + } |
| 138 | + return ret |
| 139 | +} |
| 140 | + |
| 141 | +func (m *mockTestingT) runSafely(action func(TestingT)) { |
| 142 | + exited := make(chan struct{}, 1) |
| 143 | + go func() { |
| 144 | + defer func() { |
| 145 | + close(exited) |
| 146 | + }() |
| 147 | + action(m) |
| 148 | + }() |
| 149 | + <-exited |
| 150 | +} |
0 commit comments