Skip to content

Commit 2bd5a3b

Browse files
committed
Expand GinkgoT() to include several Ginkgo-specific methods
this enables authors of third party libraries to have a richer integration point with Ginkgo
1 parent 28801fe commit 2bd5a3b

File tree

6 files changed

+282
-25
lines changed

6 files changed

+282
-25
lines changed

core_dsl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ and will simply log the passed in text to the GinkgoWriter. If By is handed a f
540540
541541
By will also generate and attach a ReportEntry to the spec. This will ensure that By annotations appear in Ginkgo's machine-readable reports.
542542
543-
Note that By does not generate a new Ginkgo node - rather it is simply synctactic sugar around GinkgoWriter and AddReportEntry
543+
Note that By does not generate a new Ginkgo node - rather it is simply syntactic sugar around GinkgoWriter and AddReportEntry
544544
You can learn more about By here: https://onsi.github.io/ginkgo/#documenting-complex-specs-by
545545
*/
546546
func By(text string, callback ...func()) {

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5340,6 +5340,8 @@ Since `GinkgoT()` implements `Cleanup()` (using `DeferCleanup()` under the hood)
53405340
53415341
When using Gomock you may want to run `ginkgo` with the `-trace` flag to print out stack traces for failures which will help you trace down where, in your code, invalid calls occurred.
53425342
5343+
`GinkgoT()` also provides additional methods that are Ginkgo-specific. This allows rich third-party integrations to be built on top of Ginkgo - with GinkgoT() serving as a single connection point.
5344+
53435345
### IDE Support
53445346
Ginkgo works best from the command-line, and [`ginkgo watch`](#watching-for-changes) makes it easy to rerun tests on the command line whenever changes are detected.
53455347

dsl/core/core_dsl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const GINKGO_VERSION = ginkgo.GINKGO_VERSION
2121
type GinkgoWriterInterface = ginkgo.GinkgoWriterInterface
2222
type GinkgoTestingT = ginkgo.GinkgoTestingT
2323
type GinkgoTInterface = ginkgo.GinkgoTInterface
24+
type FullGinkgoTInterface = ginkgo.FullGinkgoTInterface
2425
type SpecContext = ginkgo.SpecContext
2526

2627
var GinkgoWriter = ginkgo.GinkgoWriter

ginkgo_t_dsl.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
package ginkgo
22

3-
import "github.com/onsi/ginkgo/v2/internal/testingtproxy"
3+
import (
4+
"github.com/onsi/ginkgo/v2/internal/testingtproxy"
5+
)
46

57
/*
6-
GinkgoT() implements an interface analogous to *testing.T and can be used with
7-
third-party libraries that accept *testing.T through an interface.
8+
GinkgoT() implements an interface that allows third party libraries to integrate with and build on top of Ginkgo.
9+
10+
GinkgoT() is analogous to *testing.T and implements the majority of *testing.T's methods. It can be typically be used a a drop-in replacement with third-party libraries that accept *testing.T through an interface.
811
912
GinkgoT() takes an optional offset argument that can be used to get the
1013
correct line number associated with the failure - though you do not need to use this if you call GinkgoHelper() or GinkgoT().Helper() appropriately
1114
1215
You can learn more here: https://onsi.github.io/ginkgo/#using-third-party-libraries
1316
*/
14-
func GinkgoT(optionalOffset ...int) GinkgoTInterface {
17+
func GinkgoT(optionalOffset ...int) FullGinkgoTInterface {
1518
offset := 3
1619
if len(optionalOffset) > 0 {
1720
offset = optionalOffset[0]
1821
}
19-
return testingtproxy.New(GinkgoWriter, Fail, Skip, DeferCleanup, CurrentSpecReport, offset)
22+
return testingtproxy.New(
23+
GinkgoWriter,
24+
Fail,
25+
Skip,
26+
DeferCleanup,
27+
CurrentSpecReport,
28+
AddReportEntry,
29+
GinkgoRecover,
30+
AttachProgressReporter,
31+
suiteConfig.RandomSeed,
32+
suiteConfig.ParallelProcess,
33+
suiteConfig.ParallelTotal,
34+
reporterConfig.NoColor,
35+
offset)
2036
}
2137

2238
/*
@@ -43,3 +59,30 @@ type GinkgoTInterface interface {
4359
Skipped() bool
4460
TempDir() string
4561
}
62+
63+
type FullGinkgoTInterface interface {
64+
GinkgoTInterface
65+
66+
AddReportEntryVisibilityAlways(name string, args ...any)
67+
AddReportEntryVisibilityFailureOrVerbose(name string, args ...any)
68+
AddReportEntryVisibilityNever(name string, args ...any)
69+
70+
//Prints to the GinkgoWriter
71+
Print(a ...interface{})
72+
Printf(format string, a ...interface{})
73+
Println(a ...interface{})
74+
75+
//Provides access to Ginkgo's color formatting, correctly configured to match the color settings specified in the invocation of ginkgo
76+
F(format string, args ...any) string
77+
Fi(indentation uint, format string, args ...any) string
78+
Fiw(indentation uint, maxWidth uint, format string, args ...any) string
79+
80+
GinkgoRecover()
81+
DeferCleanup(args ...any)
82+
83+
RandomSeed() int64
84+
ParallelProcess() int
85+
ParallelTotal() int
86+
87+
AttachProgressReporter(func() string) func()
88+
}

internal/testingtproxy/testing_t_proxy.go

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,61 @@ import (
55
"io"
66
"os"
77

8+
"github.com/onsi/ginkgo/v2/formatter"
89
"github.com/onsi/ginkgo/v2/internal"
910
"github.com/onsi/ginkgo/v2/types"
1011
)
1112

1213
type failFunc func(message string, callerSkip ...int)
1314
type skipFunc func(message string, callerSkip ...int)
14-
type cleanupFunc func(args ...interface{})
15+
type cleanupFunc func(args ...any)
1516
type reportFunc func() types.SpecReport
17+
type addReportEntryFunc func(names string, args ...any)
18+
type ginkgoWriterInterface interface {
19+
io.Writer
1620

17-
func New(writer io.Writer, fail failFunc, skip skipFunc, cleanup cleanupFunc, report reportFunc, offset int) *ginkgoTestingTProxy {
21+
Print(a ...interface{})
22+
Printf(format string, a ...interface{})
23+
Println(a ...interface{})
24+
}
25+
type ginkgoRecoverFunc func()
26+
type attachProgressReporterFunc func(func() string) func()
27+
28+
func New(writer ginkgoWriterInterface, fail failFunc, skip skipFunc, cleanup cleanupFunc, report reportFunc, addReportEntry addReportEntryFunc, ginkgoRecover ginkgoRecoverFunc, attachProgressReporter attachProgressReporterFunc, randomSeed int64, parallelProcess int, parallelTotal int, noColor bool, offset int) *ginkgoTestingTProxy {
1829
return &ginkgoTestingTProxy{
19-
fail: fail,
20-
offset: offset,
21-
writer: writer,
22-
skip: skip,
23-
cleanup: cleanup,
24-
report: report,
30+
fail: fail,
31+
offset: offset,
32+
writer: writer,
33+
skip: skip,
34+
cleanup: cleanup,
35+
report: report,
36+
addReportEntry: addReportEntry,
37+
ginkgoRecover: ginkgoRecover,
38+
attachProgressReporter: attachProgressReporter,
39+
randomSeed: randomSeed,
40+
parallelProcess: parallelProcess,
41+
parallelTotal: parallelTotal,
42+
f: formatter.NewWithNoColorBool(noColor),
2543
}
2644
}
2745

2846
type ginkgoTestingTProxy struct {
29-
fail failFunc
30-
skip skipFunc
31-
cleanup cleanupFunc
32-
report reportFunc
33-
offset int
34-
writer io.Writer
35-
}
47+
fail failFunc
48+
skip skipFunc
49+
cleanup cleanupFunc
50+
report reportFunc
51+
offset int
52+
writer ginkgoWriterInterface
53+
addReportEntry addReportEntryFunc
54+
ginkgoRecover ginkgoRecoverFunc
55+
attachProgressReporter attachProgressReporterFunc
56+
randomSeed int64
57+
parallelProcess int
58+
parallelTotal int
59+
f formatter.Formatter
60+
}
61+
62+
// basic testing.T support
3663

3764
func (t *ginkgoTestingTProxy) Cleanup(f func()) {
3865
t.cleanup(f, internal.Offset(1))
@@ -126,3 +153,54 @@ func (t *ginkgoTestingTProxy) TempDir() string {
126153

127154
return tmpDir
128155
}
156+
157+
// FullGinkgoTInterface
158+
func (t *ginkgoTestingTProxy) AddReportEntryVisibilityAlways(name string, args ...any) {
159+
finalArgs := []any{internal.Offset(1), types.ReportEntryVisibilityAlways}
160+
t.addReportEntry(name, append(finalArgs, args...)...)
161+
}
162+
func (t *ginkgoTestingTProxy) AddReportEntryVisibilityFailureOrVerbose(name string, args ...any) {
163+
finalArgs := []any{internal.Offset(1), types.ReportEntryVisibilityFailureOrVerbose}
164+
t.addReportEntry(name, append(finalArgs, args...)...)
165+
}
166+
func (t *ginkgoTestingTProxy) AddReportEntryVisibilityNever(name string, args ...any) {
167+
finalArgs := []any{internal.Offset(1), types.ReportEntryVisibilityNever}
168+
t.addReportEntry(name, append(finalArgs, args...)...)
169+
}
170+
func (t *ginkgoTestingTProxy) Print(a ...any) {
171+
t.writer.Print(a...)
172+
}
173+
func (t *ginkgoTestingTProxy) Printf(format string, a ...any) {
174+
t.writer.Printf(format, a...)
175+
}
176+
func (t *ginkgoTestingTProxy) Println(a ...any) {
177+
t.writer.Println(a...)
178+
}
179+
func (t *ginkgoTestingTProxy) F(format string, args ...any) string {
180+
return t.f.F(format, args...)
181+
}
182+
func (t *ginkgoTestingTProxy) Fi(indentation uint, format string, args ...any) string {
183+
return t.f.Fi(indentation, format, args...)
184+
}
185+
func (t *ginkgoTestingTProxy) Fiw(indentation uint, maxWidth uint, format string, args ...any) string {
186+
return t.f.Fiw(indentation, maxWidth, format, args...)
187+
}
188+
func (t *ginkgoTestingTProxy) GinkgoRecover() {
189+
t.ginkgoRecover()
190+
}
191+
func (t *ginkgoTestingTProxy) DeferCleanup(args ...any) {
192+
finalArgs := []any{internal.Offset(1)}
193+
t.cleanup(append(finalArgs, args...)...)
194+
}
195+
func (t *ginkgoTestingTProxy) RandomSeed() int64 {
196+
return t.randomSeed
197+
}
198+
func (t *ginkgoTestingTProxy) ParallelProcess() int {
199+
return t.parallelProcess
200+
}
201+
func (t *ginkgoTestingTProxy) ParallelTotal() int {
202+
return t.parallelTotal
203+
}
204+
func (t *ginkgoTestingTProxy) AttachProgressReporter(f func() string) func() {
205+
return t.attachProgressReporter(f)
206+
}

0 commit comments

Comments
 (0)