mirror-to-gitea/vendor/github.com/onsi/ginkgo/reporters/teamcity_reporter.go
vendor/github.com/onsi/ginkgo/reporters/teamcity_reporter.goRaw
1/*
2
3TeamCity Reporter for Ginkgo
4
5Makes use of TeamCity's support for Service Messages
6http://confluence.jetbrains.com/display/TCD7/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-ReportingTests
7*/
8
9package reporters
10
11import (
12 "fmt"
13 "io"
14 "strings"
15
16 "github.com/onsi/ginkgo/config"
17 "github.com/onsi/ginkgo/types"
18)
19
20const (
21 messageId = "##teamcity"
22)
23
24type TeamCityReporter struct {
25 writer io.Writer
26 testSuiteName string
27 ReporterConfig config.DefaultReporterConfigType
28}
29
30func NewTeamCityReporter(writer io.Writer) *TeamCityReporter {
31 return &TeamCityReporter{
32 writer: writer,
33 }
34}
35
36func (reporter *TeamCityReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
37 reporter.testSuiteName = escape(summary.SuiteDescription)
38 fmt.Fprintf(reporter.writer, "%s[testSuiteStarted name='%s']\n", messageId, reporter.testSuiteName)
39}
40
41func (reporter *TeamCityReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {
42 reporter.handleSetupSummary("BeforeSuite", setupSummary)
43}
44
45func (reporter *TeamCityReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {
46 reporter.handleSetupSummary("AfterSuite", setupSummary)
47}
48
49func (reporter *TeamCityReporter) handleSetupSummary(name string, setupSummary *types.SetupSummary) {
50 if setupSummary.State != types.SpecStatePassed {
51 testName := escape(name)
52 fmt.Fprintf(reporter.writer, "%s[testStarted name='%s']\n", messageId, testName)
53 message := reporter.failureMessage(setupSummary.Failure)
54 details := reporter.failureDetails(setupSummary.Failure)
55 fmt.Fprintf(reporter.writer, "%s[testFailed name='%s' message='%s' details='%s']\n", messageId, testName, message, details)
56 durationInMilliseconds := setupSummary.RunTime.Seconds() * 1000
57 fmt.Fprintf(reporter.writer, "%s[testFinished name='%s' duration='%v']\n", messageId, testName, durationInMilliseconds)
58 }
59}
60
61func (reporter *TeamCityReporter) SpecWillRun(specSummary *types.SpecSummary) {
62 testName := escape(strings.Join(specSummary.ComponentTexts[1:], " "))
63 fmt.Fprintf(reporter.writer, "%s[testStarted name='%s']\n", messageId, testName)
64}
65
66func (reporter *TeamCityReporter) SpecDidComplete(specSummary *types.SpecSummary) {
67 testName := escape(strings.Join(specSummary.ComponentTexts[1:], " "))
68
69 if reporter.ReporterConfig.ReportPassed && specSummary.State == types.SpecStatePassed {
70 details := escape(specSummary.CapturedOutput)
71 fmt.Fprintf(reporter.writer, "%s[testPassed name='%s' details='%s']\n", messageId, testName, details)
72 }
73 if specSummary.State == types.SpecStateFailed || specSummary.State == types.SpecStateTimedOut || specSummary.State == types.SpecStatePanicked {
74 message := reporter.failureMessage(specSummary.Failure)
75 details := reporter.failureDetails(specSummary.Failure)
76 fmt.Fprintf(reporter.writer, "%s[testFailed name='%s' message='%s' details='%s']\n", messageId, testName, message, details)
77 }
78 if specSummary.State == types.SpecStateSkipped || specSummary.State == types.SpecStatePending {
79 fmt.Fprintf(reporter.writer, "%s[testIgnored name='%s']\n", messageId, testName)
80 }
81
82 durationInMilliseconds := specSummary.RunTime.Seconds() * 1000
83 fmt.Fprintf(reporter.writer, "%s[testFinished name='%s' duration='%v']\n", messageId, testName, durationInMilliseconds)
84}
85
86func (reporter *TeamCityReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
87 fmt.Fprintf(reporter.writer, "%s[testSuiteFinished name='%s']\n", messageId, reporter.testSuiteName)
88}
89
90func (reporter *TeamCityReporter) failureMessage(failure types.SpecFailure) string {
91 return escape(failure.ComponentCodeLocation.String())
92}
93
94func (reporter *TeamCityReporter) failureDetails(failure types.SpecFailure) string {
95 return escape(fmt.Sprintf("%s\n%s", failure.Message, failure.Location.String()))
96}
97
98func escape(output string) string {
99 output = strings.Replace(output, "|", "||", -1)
100 output = strings.Replace(output, "'", "|'", -1)
101 output = strings.Replace(output, "\n", "|n", -1)
102 output = strings.Replace(output, "\r", "|r", -1)
103 output = strings.Replace(output, "[", "|[", -1)
104 output = strings.Replace(output, "]", "|]", -1)
105 return output
106}