1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
package test_helpers
import (
"fmt"
"reflect"
"strings"
"sync"
"time"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gcustom"
. "github.com/onsi/gomega/gstruct"
"github.com/onsi/ginkgo/v2/internal/interrupt_handler"
"github.com/onsi/ginkgo/v2/types"
)
type OmegaMatcherWithDescription struct {
OmegaMatcher
Description string
}
func (o OmegaMatcherWithDescription) GomegaString() string {
return o.Description
}
/*
A FakeReporter and collection of matchers to match against reported suite and spec summaries
*/
type Reports []types.SpecReport
func (s Reports) FindByLeafNodeType(nodeTypes types.NodeType) types.SpecReport {
for _, report := range s {
if report.LeafNodeType.Is(nodeTypes) {
return report
}
}
return types.SpecReport{}
}
func (s Reports) Find(name string) types.SpecReport {
for _, report := range s {
if report.LeafNodeText == name {
return report
}
}
return types.SpecReport{}
}
func (s Reports) FindByFullText(text string) types.SpecReport {
for _, report := range s {
if report.FullText() == text {
return report
}
}
return types.SpecReport{}
}
func (s Reports) Names() []string {
out := []string{}
for _, report := range s {
if report.LeafNodeText != "" {
out = append(out, report.LeafNodeText)
}
}
return out
}
func (s Reports) WithState(state types.SpecState) Reports {
out := Reports{}
for _, report := range s {
if report.State == state {
out = append(out, report)
}
}
return out
}
func (s Reports) WithLeafNodeType(nodeTypes types.NodeType) Reports {
out := Reports{}
for _, report := range s {
if report.LeafNodeType.Is(nodeTypes) {
out = append(out, report)
}
}
return out
}
type FakeReporter struct {
Begin types.Report
Will Reports
Did Reports
End types.Report
ProgressReports []types.ProgressReport
ReportEntries []types.ReportEntry
SpecEvents []types.SpecEvent
Failures []types.AdditionalFailure
lock *sync.Mutex
}
func NewFakeReporter() *FakeReporter {
return &FakeReporter{
lock: &sync.Mutex{},
}
}
func (r *FakeReporter) SuiteWillBegin(report types.Report) {
r.lock.Lock()
defer r.lock.Unlock()
r.Begin = report
}
func (r *FakeReporter) WillRun(report types.SpecReport) {
r.lock.Lock()
defer r.lock.Unlock()
r.Will = append(r.Will, report)
}
func (r *FakeReporter) DidRun(report types.SpecReport) {
r.lock.Lock()
defer r.lock.Unlock()
r.Did = append(r.Did, report)
}
func (r *FakeReporter) SuiteDidEnd(report types.Report) {
r.lock.Lock()
defer r.lock.Unlock()
r.End = report
}
func (r *FakeReporter) EmitProgressReport(progressReport types.ProgressReport) {
r.lock.Lock()
defer r.lock.Unlock()
r.ProgressReports = append(r.ProgressReports, progressReport)
}
func (r *FakeReporter) EmitFailure(state types.SpecState, failure types.Failure) {
r.lock.Lock()
defer r.lock.Unlock()
r.Failures = append(r.Failures, types.AdditionalFailure{Failure: failure, State: state})
}
func (r *FakeReporter) EmitReportEntry(reportEntry types.ReportEntry) {
r.lock.Lock()
defer r.lock.Unlock()
r.ReportEntries = append(r.ReportEntries, reportEntry)
}
func (r *FakeReporter) EmitSpecEvent(specEvent types.SpecEvent) {
r.lock.Lock()
defer r.lock.Unlock()
r.SpecEvents = append(r.SpecEvents, specEvent)
}
type NSpecs int
type NWillRun int
type NPassed int
type NSkipped int
type NFailed int
type NPending int
type NFlaked int
func BeASuiteSummary(options ...interface{}) OmegaMatcher {
type ReportStats struct {
Succeeded bool
TotalSpecs int
WillRunSpecs int
Passed int
Skipped int
Failed int
Pending int
Flaked int
}
fields := Fields{
"Passed": Equal(0),
"Skipped": Equal(0),
"Failed": Equal(0),
"Pending": Equal(0),
"Flaked": Equal(0),
"TotalSpecs": Equal(0),
}
for _, option := range options {
t := reflect.TypeOf(option)
if t.Kind() == reflect.Bool {
if option.(bool) {
fields["Succeeded"] = BeTrue()
} else {
fields["Succeeded"] = BeFalse()
}
} else if t == reflect.TypeOf(NSpecs(0)) {
fields["TotalSpecs"] = Equal(int(option.(NSpecs)))
} else if t == reflect.TypeOf(NWillRun(0)) {
fields["WillRunSpecs"] = Equal(int(option.(NWillRun)))
} else if t == reflect.TypeOf(NPassed(0)) {
fields["Passed"] = Equal(int(option.(NPassed)))
} else if t == reflect.TypeOf(NSkipped(0)) {
fields["Skipped"] = Equal(int(option.(NSkipped)))
} else if t == reflect.TypeOf(NFailed(0)) {
fields["Failed"] = Equal(int(option.(NFailed)))
} else if t == reflect.TypeOf(NPending(0)) {
fields["Pending"] = Equal(int(option.(NPending)))
} else if t == reflect.TypeOf(NFlaked(0)) {
fields["Flaked"] = Equal(int(option.(NFlaked)))
}
}
return WithTransform(func(report types.Report) ReportStats {
specs := report.SpecReports.WithLeafNodeType(types.NodeTypeIt)
return ReportStats{
Succeeded: report.SuiteSucceeded,
TotalSpecs: report.PreRunStats.TotalSpecs,
WillRunSpecs: report.PreRunStats.SpecsThatWillRun,
Passed: specs.CountWithState(types.SpecStatePassed),
Skipped: specs.CountWithState(types.SpecStateSkipped),
Failed: specs.CountWithState(types.SpecStateFailureStates),
Pending: specs.CountWithState(types.SpecStatePending),
Flaked: specs.CountOfFlakedSpecs(),
}
}, MatchFields(IgnoreExtras, fields))
}
type CapturedGinkgoWriterOutput string
type CapturedStdOutput string
type NumAttempts int
func HavePassed(options ...interface{}) OmegaMatcher {
matchers := []OmegaMatcher{
HaveField("State", types.SpecStatePassed),
HaveField("Failure", BeZero()),
}
for _, option := range options {
var matcher OmegaMatcher
switch v := option.(type) {
case CapturedGinkgoWriterOutput:
matcher = HaveField("CapturedGinkgoWriterOutput", string(v))
case CapturedStdOutput:
matcher = HaveField("CapturedStdOutErr", string(v))
case types.NodeType:
matcher = HaveField("LeafNodeType", v)
case NumAttempts:
matcher = HaveField("NumAttempts", int(v))
}
if matcher != nil {
matchers = append(matchers, matcher)
}
}
return And(matchers...)
}
func BePending() OmegaMatcher {
return And(
HaveField("State", types.SpecStatePending),
HaveField("Failure", BeZero()),
)
}
func HaveBeenSkipped() OmegaMatcher {
return And(
HaveField("State", types.SpecStateSkipped),
HaveField("Failure", BeZero()),
)
}
func HaveBeenSkippedWithMessage(message string, options ...interface{}) OmegaMatcher {
matchers := []OmegaMatcher{
HaveField("State", types.SpecStateSkipped),
HaveField("Failure.Message", Equal(message)),
}
for _, option := range options {
switch v := option.(type) {
case NumAttempts:
matchers = append(matchers, HaveField("NumAttempts", int(v)))
}
}
return And(matchers...)
}
func HaveBeenInterrupted(cause interrupt_handler.InterruptCause) OmegaMatcher {
return And(
HaveField("State", types.SpecStateInterrupted),
HaveField("Failure.Message", HavePrefix(cause.String())),
)
}
type FailureNodeType types.NodeType
func failureMatcherForState(state types.SpecState, messageField string, options ...interface{}) OmegaMatcher {
matchers := []OmegaMatcher{
HaveField("State", state),
}
for _, option := range options {
var matcher OmegaMatcher
switch v := option.(type) {
case CapturedGinkgoWriterOutput:
matcher = HaveField("CapturedGinkgoWriterOutput", string(v))
case CapturedStdOutput:
matcher = HaveField("CapturedStdOutErr", string(v))
case types.NodeType:
matcher = HaveField("LeafNodeType", v)
case types.FailureNodeContext:
matcher = HaveField("Failure.FailureNodeContext", v)
case string:
matcher = HaveField(messageField, ContainSubstring(v))
case OmegaMatcher:
matcher = HaveField(messageField, v)
case types.CodeLocation:
matcher = HaveField("Failure.Location", v)
case FailureNodeType:
matcher = HaveField("Failure.FailureNodeType", types.NodeType(v))
case NumAttempts:
matcher = HaveField("NumAttempts", int(v))
case types.TimelineLocation:
matcher = HaveField("Failure.TimelineLocation.Offset", v.Offset)
}
if matcher != nil {
matchers = append(matchers, matcher)
}
}
return And(matchers...)
}
func HaveFailed(options ...interface{}) OmegaMatcher {
return failureMatcherForState(types.SpecStateFailed, "Failure.Message", options...)
}
func HaveTimedOut(options ...interface{}) OmegaMatcher {
return failureMatcherForState(types.SpecStateTimedout, "Failure.Message", options...)
}
func HaveAborted(options ...interface{}) OmegaMatcher {
return failureMatcherForState(types.SpecStateAborted, "Failure.Message", options...)
}
func HavePanicked(options ...interface{}) OmegaMatcher {
return failureMatcherForState(types.SpecStatePanicked, "Failure.ForwardedPanic", options...)
}
func TLWithOffset[O int | string](o O) types.TimelineLocation {
t := types.TimelineLocation{}
switch x := any(o).(type) {
case int:
t.Offset = x
case string:
t.Offset = len(x)
}
return t
}
func BeSpecEvent(options ...interface{}) OmegaMatcher {
description := []string{"BeSpecEvent"}
matchers := []OmegaMatcher{}
for _, option := range options {
var matcher OmegaMatcher
switch x := option.(type) {
case types.SpecEventType:
matcher = HaveField("SpecEventType", x)
description = append(description, "["+x.String()+" SpecEvent]")
case types.CodeLocation:
matcher = HaveField("CodeLocation", x)
description = append(description, "CL="+x.String())
case types.TimelineLocation:
matcher = HaveField("TimelineLocation.Offset", x.Offset)
description = append(description, fmt.Sprintf("TL.Offset=%d", x.Offset))
case string:
matcher = HaveField("Message", ContainSubstring(x))
description = append(description, `Message="`+x+`"`)
case int:
matcher = HaveField("Attempt", x)
description = append(description, fmt.Sprintf("Attempt=%d", x))
case time.Duration:
matcher = HaveField("Duration", BeNumerically("~", x, time.Duration(float64(x)*0.2)))
description = append(description, "Duration="+x.String())
case types.NodeType:
matcher = HaveField("NodeType", x)
description = append(description, "NodeType="+x.String())
}
if matcher != nil {
matchers = append(matchers, matcher)
}
}
return OmegaMatcherWithDescription{OmegaMatcher: And(matchers...), Description: strings.Join(description, " ")}
}
func BeProgressReport(options ...interface{}) OmegaMatcher {
description := []string{"BeProgressReport"}
matchers := []OmegaMatcher{}
for _, option := range options {
var matcher OmegaMatcher
switch x := option.(type) {
case string:
matcher = HaveField("Message", ContainSubstring(x))
description = append(description, `Message="`+x+`"`)
case types.TimelineLocation:
matcher = HaveField("TimelineLocation.Offset", x.Offset)
description = append(description, fmt.Sprintf("TL.Offset=%d", x.Offset))
case types.CodeLocation:
matcher = HaveField("CurrentNodeLocation", x)
description = append(description, "CurrentNodeLocation="+x.String())
case types.NodeType:
matcher = HaveField("CurrentNodeType", x)
description = append(description, "CurrentNodeType="+x.String())
}
if matcher != nil {
matchers = append(matchers, matcher)
}
}
return OmegaMatcherWithDescription{OmegaMatcher: And(matchers...), Description: strings.Join(description, " ")}
}
func BeReportEntry(options ...interface{}) OmegaMatcher {
description := []string{"BeReportEntry"}
matchers := []OmegaMatcher{}
for _, option := range options {
var matcher OmegaMatcher
switch x := option.(type) {
case string:
matcher = HaveField("Name", ContainSubstring(x))
description = append(description, `Name="`+x+`"`)
case types.TimelineLocation:
matcher = HaveField("TimelineLocation.Offset", x.Offset)
description = append(description, fmt.Sprintf("TL.Offset=%d", x.Offset))
case types.ReportEntryVisibility:
matcher = HaveField("Visibility", x)
description = append(description, "Visibility="+x.String())
}
if matcher != nil {
matchers = append(matchers, matcher)
}
}
return OmegaMatcherWithDescription{OmegaMatcher: And(matchers...), Description: strings.Join(description, " ")}
}
func BeTimelineContaining(matchers ...OmegaMatcher) OmegaMatcher {
return gcustom.MakeMatcher(func(timeline types.Timeline) (bool, error) {
timelineIdx := 0
for _, matcher := range matchers {
for {
if timelineIdx >= len(timeline) {
return false, nil
}
event := timeline[timelineIdx]
timelineIdx += 1
success, err := matcher.Match(event)
if success && err == nil {
break
}
}
}
return true, nil
}).WithTemplate("Expected:\n{{.FormattedActual}}\n{{.To}} contain events matching (in order):\n{{format .Data 1}}", matchers)
}
func BeTimelineExactlyMatching(matchers ...OmegaMatcher) OmegaMatcher {
data := map[string]any{}
data["Matchers"] = matchers
return gcustom.MakeMatcher(func(timeline types.Timeline) (bool, error) {
for idx, matcher := range matchers {
if idx == len(timeline) {
data["LengthMismatch"] = "Not enough timeline entries"
return false, nil
}
event := timeline[idx]
success, err := matcher.Match(event)
if !(success && err == nil) {
data["Failure"] = matcher.FailureMessage(event)
data["FailedIndex"] = idx
return false, nil
}
}
if len(matchers) < len(timeline) {
data["LengthMismatch"] = "Not enough matcher entries"
return false, nil
}
return true, nil
}).WithTemplate(`Timeline failed to match:
{{if .Data.LengthMismatch}}
{{.Data.LengthMismatch}}
Timeline has {{len .Actual}} events:
{{ range .Actual }}{{ printf " %T\n" . }}{{ end }}
Matchers has {{len .Data.Matchers}} entries.
{{else}}
Failed at index {{.Data.FailedIndex}}:
{{format (index .Actual .Data.FailedIndex) 2}}
{{.Data.Failure}}
{{end}}`, data)
}
|