Skip to content

Commit 37a511b

Browse files
committed
bump to go 1.24.0 - support new testing.TB methods and add a test to cover testing.TB regressions
1 parent 7556a86 commit 37a511b

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

ginkgo_t_dsl.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ginkgo
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/onsi/ginkgo/v2/internal/testingtproxy"
@@ -48,6 +49,8 @@ The portion of the interface returned by GinkgoT() that maps onto methods in the
4849
*/
4950
type GinkgoTInterface interface {
5051
Cleanup(func())
52+
Chdir(dir string)
53+
Context() context.Context
5154
Setenv(kev, value string)
5255
Error(args ...any)
5356
Errorf(format string, args ...any)

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/onsi/ginkgo/v2
22

3-
go 1.22.0
4-
5-
toolchain go1.23.0
3+
go 1.23.0
64

75
require (
86
github.com/go-logr/logr v1.4.2

internal/testingtproxy/testing_t_proxy.go

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package testingtproxy
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"os"
@@ -80,6 +81,26 @@ func (t *ginkgoTestingTProxy) Setenv(key, value string) {
8081
}
8182
}
8283

84+
func (t *ginkgoTestingTProxy) Chdir(dir string) {
85+
currentDir, err := os.Getwd()
86+
if err != nil {
87+
t.fail(fmt.Sprintf("Failed to get current directory: %v", err), 1)
88+
}
89+
90+
t.cleanup(os.Chdir, currentDir, internal.Offset(1))
91+
92+
err = os.Chdir(dir)
93+
if err != nil {
94+
t.fail(fmt.Sprintf("Failed to change directory: %v", err), 1)
95+
}
96+
}
97+
98+
func (t *ginkgoTestingTProxy) Context() context.Context {
99+
ctx, cancel := context.WithCancel(context.Background())
100+
t.cleanup(cancel, internal.Offset(1))
101+
return ctx
102+
}
103+
83104
func (t *ginkgoTestingTProxy) Error(args ...any) {
84105
t.fail(fmt.Sprintln(args...), t.offset)
85106
}

internal/testingtproxy/testingtproxy_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package testingtproxy_test
22

33
import (
4+
"fmt"
45
"os"
6+
"reflect"
57
"runtime"
8+
"testing"
69
"time"
710

811
. "github.com/onsi/ginkgo/v2"
@@ -87,6 +90,23 @@ var _ = Describe("Testingtproxy", func() {
8790
offset)
8891
})
8992

93+
It("satisfies the golang interfaces", func() {
94+
tbType := reflect.TypeFor[testing.TB]()
95+
gtType := reflect.TypeFor[GinkgoTInterface]()
96+
97+
fmt.Println("tbType: ", tbType.NumMethod())
98+
for i := 0; i < tbType.NumMethod(); i++ {
99+
expectedMethod := tbType.Method(i)
100+
if expectedMethod.Name == "private" {
101+
continue
102+
}
103+
actualMethod, hasMethod := gtType.MethodByName(expectedMethod.Name)
104+
Ω(hasMethod).Should(BeTrue())
105+
Ω(actualMethod.Name).Should(Equal(expectedMethod.Name))
106+
Ω(actualMethod.Type).Should(Equal(expectedMethod.Type))
107+
}
108+
})
109+
90110
Describe("Cleanup", Ordered, func() {
91111
var didCleanupAfter bool
92112
It("supports cleanup", func() {
@@ -143,6 +163,40 @@ var _ = Describe("Testingtproxy", func() {
143163
})
144164
})
145165

166+
Describe("Chdir", Ordered, func() {
167+
BeforeAll(func() {
168+
os.Mkdir("chdir_test_dir", 0755)
169+
DeferCleanup(os.RemoveAll, "chdir_test_dir")
170+
})
171+
172+
It("changes the working directory", func() {
173+
t.Chdir("chdir_test_dir")
174+
newDir, _ := os.Getwd()
175+
Ω(newDir).Should(ContainSubstring("chdir_test_dir"))
176+
})
177+
178+
It("cleans up after itself so the next test is back in the working directory", func() {
179+
dir, _ := os.Getwd()
180+
Ω(dir).ShouldNot(ContainSubstring("chdir_test_dir"))
181+
})
182+
})
183+
184+
Describe("Context", func() {
185+
It("returns a context that is closed in a canceled in a clean up", func() {
186+
c := make(chan struct{})
187+
DeferCleanup(func() {
188+
Eventually(c).Should(BeClosed())
189+
})
190+
191+
ctx := t.Context()
192+
Ω(ctx.Err()).Should(BeNil())
193+
go func() {
194+
<-ctx.Done()
195+
close(c)
196+
}()
197+
})
198+
})
199+
146200
Describe("TempDir", Ordered, func() {
147201
var tempDirA, tempDirB string
148202

0 commit comments

Comments
 (0)