Skip to content

Commit e8f85aa

Browse files
committed
fix(tdutil): testing.T.output needs some inits starting go1.25
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
1 parent f32ba12 commit e8f85aa

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

helpers/tdutil/t.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ package tdutil
1414
import (
1515
"reflect"
1616
"testing"
17+
"unsafe"
1718
)
1819

20+
var testingT = reflect.TypeOf(testing.T{})
21+
1922
// T can be used in tests, to test [testing.T] behavior as it overrides
2023
// [testing.T.Run] method.
2124
type T struct {
@@ -28,7 +31,9 @@ type tFailedNow struct{}
2831
// NewT returns a new [*T] instance. name is the string returned by
2932
// method Name.
3033
func NewT(name string) *T {
31-
return &T{name: name}
34+
t := &T{name: name}
35+
t.prepareForLogs()
36+
return t
3237
}
3338

3439
// Run is a simplified version of [testing.T.Run] method, without edge
@@ -70,6 +75,11 @@ func (t *T) Fatalf(format string, args ...any) {
7075
t.FailNow()
7176
}
7277

78+
// Starting go1.18, unsafe.Add can be used instead.
79+
func ptrAdd(p unsafe.Pointer, x uintptr) unsafe.Pointer {
80+
return unsafe.Pointer(uintptr(p) + x)
81+
}
82+
7383
// CatchFailNow returns true if a [T.FailNow], [T.Fatal] or [T.Fatalf] call
7484
// occurred during the execution of fn.
7585
func (t *T) CatchFailNow(fn func()) (failNowOccurred bool) {
@@ -85,3 +95,34 @@ func (t *T) CatchFailNow(fn func()) (failNowOccurred bool) {
8595
fn()
8696
return
8797
}
98+
99+
func (t *T) prepareForLogs() {
100+
if _, ok := testingT.FieldByName("output"); !ok {
101+
panic("testing.T.output field not found!")
102+
}
103+
104+
// o is a new go1.25 testing.common field
105+
if o, ok := testingT.FieldByName("o"); ok {
106+
ot := o.Type.Elem()
107+
108+
// With go1.25 t.T.o needs to be set to feed t.T.output
109+
type outputWriter struct {
110+
c *testing.T
111+
partial []byte //nolint: unused
112+
}
113+
owt := reflect.TypeOf(outputWriter{})
114+
115+
if ot.NumField() != owt.NumField() ||
116+
ot.Field(0).Name != owt.Field(0).Name || // ot.c is *testing.common
117+
ot.Field(1).Type != owt.Field(1).Type {
118+
panic("testing.T.outputWriter changed")
119+
}
120+
121+
// Use ptrAdd to support go1.16 & go1.17, but starting go1.18 we can use:
122+
// oAddr := (**outputWriter)(unsafe.Add(unsafe.Pointer(&t.T), o.Offset))
123+
oAddr := (**outputWriter)(ptrAdd(unsafe.Pointer(&t.T), o.Offset))
124+
*oAddr = &outputWriter{
125+
c: &t.T,
126+
}
127+
}
128+
}

td/types_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
func TestSetlocation(t *testing.T) {
2020
//nolint: gocritic
2121
//line types_test.go:10
22-
tt := &tdutil.T{}
22+
tt := tdutil.NewT("test")
2323
ok := td.Cmp(tt, 12, 13)
2424
if !ok {
2525
test.EqualStr(t, tt.LogBuf(), ` types_test.go:11: Failed test
@@ -33,7 +33,7 @@ func TestSetlocation(t *testing.T) {
3333

3434
//nolint: gocritic
3535
//line types_test.go:20
36-
tt = &tdutil.T{}
36+
tt = tdutil.NewT("test")
3737
ok = td.Cmp(tt,
3838
12,
3939
td.Any(13, 14, 15))
@@ -52,7 +52,7 @@ func TestSetlocation(t *testing.T) {
5252

5353
//nolint: gocritic
5454
//line types_test.go:30
55-
tt = &tdutil.T{}
55+
tt = tdutil.NewT("test")
5656
ok = td.CmpAny(tt,
5757
12,
5858
[]any{13, 14, 15})
@@ -70,7 +70,7 @@ func TestSetlocation(t *testing.T) {
7070

7171
//nolint: gocritic
7272
//line types_test.go:40
73-
tt = &tdutil.T{}
73+
tt = tdutil.NewT("test")
7474
ttt := td.NewT(tt)
7575
ok = ttt.Cmp(
7676
12,
@@ -90,7 +90,7 @@ func TestSetlocation(t *testing.T) {
9090

9191
//nolint: gocritic
9292
//line types_test.go:50
93-
tt = &tdutil.T{}
93+
tt = tdutil.NewT("test")
9494
ttt = td.NewT(tt)
9595
ok = ttt.Any(
9696
12,
@@ -108,7 +108,7 @@ func TestSetlocation(t *testing.T) {
108108
}
109109

110110
//line /a/full/path/types_test.go:50
111-
tt = &tdutil.T{}
111+
tt = tdutil.NewT("test")
112112
ttt = td.NewT(tt)
113113
ok = ttt.Any(
114114
12,

0 commit comments

Comments
 (0)