diff --git a/fake_coverage_test.go b/fake_coverage_test.go index b201b2d..03c2f63 100644 --- a/fake_coverage_test.go +++ b/fake_coverage_test.go @@ -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) } diff --git a/fake_tb.go b/fake_tb.go index d1903fb..d058413 100644 --- a/fake_tb.go +++ b/fake_tb.go @@ -1,6 +1,7 @@ package faket import ( + "context" "fmt" "os" "path/filepath" @@ -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 @@ -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{}), } @@ -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) @@ -450,3 +459,7 @@ func (tb *fakeTB) Chdir(dir string) { } }) } + +func (tb *fakeTB) Context() context.Context { + return tb.ctx +} diff --git a/integration_1_24_test.go b/integration_1_24_test.go index d5c9b06..d0050b2 100644 --- a/integration_1_24_test.go +++ b/integration_1_24_test.go @@ -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 }, } @@ -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()) + }) + }) +} diff --git a/testdata/cmp_test_results.json b/testdata/cmp_test_results.json index a9f0295..9802ab4 100644 --- a/testdata/cmp_test_results.json +++ b/testdata/cmp_test_results.json @@ -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"}