Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions fake_coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ func TestTBCoverage(t *testing.T) {
}

mn := m.Name
// TODO(prashant): Implement new methods added in 1.24 before release.
if mn == "Context" {
continue
}
if _, ok := ftSet[mn]; !ok {
t.Errorf("faket missing testing.TB.%s", mn)
}
Expand Down
13 changes: 13 additions & 0 deletions fake_tb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package faket

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -19,6 +20,9 @@ type fakeTB struct {
// Since this is an interface, unimplemented methods will panic.
testing.TB

ctx context.Context
cancelCtx context.CancelFunc

mu sync.Mutex // protects all of the below fields.

cleanups []cleanup
Expand Down Expand Up @@ -68,7 +72,10 @@ func RunTest(testFn func(t testing.TB)) TestResult {
}

func newFakeTB() *fakeTB {
ctx, cancel := context.WithCancel(context.Background())
return &fakeTB{
ctx: ctx,
cancelCtx: cancel,
completed: make(chan struct{}),
helpers: make(map[uintptr]struct{}),
}
Expand Down Expand Up @@ -105,6 +112,8 @@ func (tb *fakeTB) checkPanic() {
}

func (tb *fakeTB) runCleanups() {
tb.cancelCtx()

// Set cleanupRoot so log callers can use cleanup's callers.
if self := getCaller(withSelf); self != 0 {
f := pcToFunction(self)
Expand Down Expand Up @@ -450,3 +459,7 @@ func (tb *fakeTB) Chdir(dir string) {
}
})
}

func (tb *fakeTB) Context() context.Context {
return tb.ctx
}
16 changes: 15 additions & 1 deletion integration_1_24_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCmp_Chdir(t *testing.T) {
opts := cmptest.Opts{
LogReplace: func(s string) string {
s = regexp.MustCompile("testing.go:[0-9]+").ReplaceAllString(s, "testing.go:Chdir")
s = strings.ReplaceAll(s, "fake_tb.go:428", "testing.go:Chdir")
s = regexp.MustCompile("fake_tb.go:[0-9]+").ReplaceAllString(s, "testing.go:Chdir")
return s
},
}
Expand Down Expand Up @@ -58,3 +58,17 @@ func TestCmp_Chdir(t *testing.T) {
want.Equal(t, "Getwd", postWD, initialWD)
want.Equal(t, "PWD", postPWD, initialPWD)
}

func TestCmp_Context(t *testing.T) {
cmptest.Compare(t, func(t testing.TB) {
ctx := t.Context()
t.Log("no ctx.Err initially", ctx.Err())

deadline, ok := ctx.Deadline()
t.Log("ctx has no deadline", ok, deadline)

t.Cleanup(func() {
t.Log("but err in cleanup", ctx.Err())
})
})
}
7 changes: 7 additions & 0 deletions testdata/cmp_test_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
{"Time":"2022-06-11T00:00:00.0Z","Action":"output","Package":"github.com/prashantv/faket","Test":"TestCmp_Chdir/success","Output":" --- PASS: TestCmp_Chdir/success (0.01s)\n"}
{"Time":"2022-06-11T00:00:00.0Z","Action":"pass","Package":"github.com/prashantv/faket","Test":"TestCmp_Chdir/success","Elapsed":0}
{"Time":"2022-06-11T00:00:00.0Z","Action":"fail","Package":"github.com/prashantv/faket","Test":"TestCmp_Chdir","Elapsed":0}
{"Time":"2022-06-11T00:00:00.0Z","Action":"run","Package":"github.com/prashantv/faket","Test":"TestCmp_Context"}
{"Time":"2022-06-11T00:00:00.0Z","Action":"output","Package":"github.com/prashantv/faket","Test":"TestCmp_Context","Output":"=== RUN TestCmp_Context\n"}
{"Time":"2022-06-11T00:00:00.0Z","Action":"output","Package":"github.com/prashantv/faket","Test":"TestCmp_Context","Output":" integration_1_24_test.go:65: no ctx.Err initially \u003cnil\u003e\n"}
{"Time":"2022-06-11T00:00:00.0Z","Action":"output","Package":"github.com/prashantv/faket","Test":"TestCmp_Context","Output":" integration_1_24_test.go:68: ctx has no deadline false 0001-01-01 00:00:00 +0000 UTC\n"}
{"Time":"2022-06-11T00:00:00.0Z","Action":"output","Package":"github.com/prashantv/faket","Test":"TestCmp_Context","Output":" integration_1_24_test.go:71: but err in cleanup context canceled\n"}
{"Time":"2022-06-11T00:00:00.0Z","Action":"output","Package":"github.com/prashantv/faket","Test":"TestCmp_Context","Output":"--- PASS: TestCmp_Context (0.01s)\n"}
{"Time":"2022-06-11T00:00:00.0Z","Action":"pass","Package":"github.com/prashantv/faket","Test":"TestCmp_Context","Elapsed":0}
{"Time":"2022-06-11T00:00:00.0Z","Action":"run","Package":"github.com/prashantv/faket","Test":"TestCmp_Success"}
{"Time":"2022-06-11T00:00:00.0Z","Action":"output","Package":"github.com/prashantv/faket","Test":"TestCmp_Success","Output":"=== RUN TestCmp_Success\n"}
{"Time":"2022-06-11T00:00:00.0Z","Action":"output","Package":"github.com/prashantv/faket","Test":"TestCmp_Success","Output":" integration_test.go:20: log1\n"}
Expand Down