File: gojson_event_writer.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.27.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,340 kB
  • sloc: javascript: 65; makefile: 23; sh: 14
file content (111 lines) | stat: -rw-r--r-- 2,702 bytes parent folder | download
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
package reporters

type GoJSONEventWriter struct {
	enc encoder
	specSystemErrFn specSystemExtractFn
	specSystemOutFn specSystemExtractFn
}

func NewGoJSONEventWriter(enc encoder, errFn specSystemExtractFn, outFn specSystemExtractFn) *GoJSONEventWriter {
	return &GoJSONEventWriter{
		enc: enc,
		specSystemErrFn: errFn,
		specSystemOutFn: outFn,
	}
}

func (r *GoJSONEventWriter) writeEvent(e *gojsonEvent) error {
	return r.enc.Encode(e)
}

func (r *GoJSONEventWriter) WriteSuiteStart(report *gojsonReport) error {
	e := &gojsonEvent{
		Time:        &report.o.StartTime,
		Action:      GoJSONStart,
		Package:     report.goPkg,
		Output:      nil,
		FailedBuild: "",
	}
	return r.writeEvent(e)
}

func (r *GoJSONEventWriter) WriteSuiteResult(report *gojsonReport) error {
	var action GoJSONAction
	switch {
	case report.o.PreRunStats.SpecsThatWillRun == 0:
		action = GoJSONSkip
	case report.o.SuiteSucceeded:
		action = GoJSONPass
	default:
		action = GoJSONFail
	}
	e := &gojsonEvent{
		Time:        &report.o.EndTime,
		Action:      action,
		Package:     report.goPkg,
		Output:      nil,
		FailedBuild: "",
		Elapsed:     ptr(report.elapsed),
	}
	return r.writeEvent(e)
}

func (r *GoJSONEventWriter) WriteSpecStart(report *gojsonReport, specReport *gojsonSpecReport) error {
	e := &gojsonEvent{
		Time:        &specReport.o.StartTime,
		Action:      GoJSONRun,
		Test:        specReport.testName,
		Package:     report.goPkg,
		Output:      nil,
		FailedBuild: "",
	}
	return r.writeEvent(e)
}

func (r *GoJSONEventWriter) WriteSpecOut(report *gojsonReport, specReport *gojsonSpecReport) error {
	events := []*gojsonEvent{}

	stdErr := r.specSystemErrFn(specReport.o)
	if stdErr != "" {
		events = append(events, &gojsonEvent{
			Time:        &specReport.o.EndTime,
			Action:      GoJSONOutput,
			Test:        specReport.testName,
			Package:     report.goPkg,
			Output:      ptr(stdErr),
			FailedBuild: "",
		})
	}
	stdOut := r.specSystemOutFn(specReport.o)
	if stdOut != "" {
		events = append(events, &gojsonEvent{
			Time:        &specReport.o.EndTime,
			Action:      GoJSONOutput,
			Test:        specReport.testName,
			Package:     report.goPkg,
			Output:      ptr(stdOut),
			FailedBuild: "",
		})
	}

	for _, ev := range events {
		err := r.writeEvent(ev)
		if err != nil {
			return err
		}
	}
	return nil
}

func (r *GoJSONEventWriter) WriteSpecResult(report *gojsonReport, specReport *gojsonSpecReport) error {
	e := &gojsonEvent{
		Time:        &specReport.o.EndTime,
		Action:      specReport.action,
		Test:        specReport.testName,
		Package:     report.goPkg,
		Elapsed:     ptr(specReport.elapsed),
		Output:      nil,
		FailedBuild: "",
	}
	return r.writeEvent(e)
}