|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** Type: Handwritten *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This code is generated by Magic Modules using the following: |
| 8 | +// |
| 9 | +// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/scripts/teamcitytestscripts/teamcity.go |
| 10 | +// |
| 11 | +// DO NOT EDIT this file directly. Any changes made to this file will be |
| 12 | +// overwritten during the next generation cycle. |
| 13 | +// |
| 14 | +// ---------------------------------------------------------------------------- |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "fmt" |
| 20 | + "regexp" |
| 21 | + "strings" |
| 22 | + "time" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + TeamCityTimestampFormat = "2006-01-02T15:04:05.000" |
| 27 | + TeamCityTestStarted = "##teamcity[testStarted timestamp='%s' name='%s']\n" |
| 28 | + TeamCityTestFailed = "##teamcity[testFailed timestamp='%s' name='%s']\n" |
| 29 | + TeamCityTestFinished = "##teamcity[testFinished timestamp='%s' name='%s']\n" |
| 30 | + TeamCityTestFailedRace = "##teamcity[testFailed timestamp='%s' name='%s' message='Race detected!']\n" |
| 31 | + TeamCityTestIgnored = "##teamcity[testIgnored timestamp='%s' name='%s']\n" |
| 32 | + TeamCityTestFailedPanic = "##teamcity[testFailed timestamp='%s' name='%s' message='Test ended in panic.']\n" |
| 33 | + TeamCityTestDiffFailed = "##teamcity[testDiffFailed timestamp='%s' name='%s']\n" |
| 34 | + TeamCityTestStdOut = "##teamcity[testStdOut name='%s' out='%s']\n" |
| 35 | + TeamCityTestStdErr = "##teamcity[testStdErr name='%s' out='%s']\n" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + end = regexp.MustCompile(`--- (PASS|SKIP|FAIL):\s+([a-zA-Z_]\S*) \(([\.\d]+)\)`) |
| 40 | + diff = regexp.MustCompile(`\[Diff\] (.*)`) |
| 41 | + paniced = regexp.MustCompile(`panic:\s+(.*)\s+\[recovered\]\n`) |
| 42 | + //suite = regexp.MustCompile("^(ok|FAIL)\\s+([^\\s]+)\\s+([\\.\\d]+)s") |
| 43 | + race = regexp.MustCompile("^WARNING: DATA RACE") |
| 44 | +) |
| 45 | + |
| 46 | +type TeamCityTest struct { |
| 47 | + Name, Output, ErrOutput, Duration string |
| 48 | + Race, Fail, Skip, Pass, Diff bool |
| 49 | + Started time.Time |
| 50 | +} |
| 51 | + |
| 52 | +func NewTeamCityTest(testName string) *TeamCityTest { |
| 53 | + return &TeamCityTest{ |
| 54 | + Name: testName, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (test *TeamCityTest) ParseTestRunnerOutput(testOutput string, errOutput string) { |
| 59 | + hasDataRace := race.MatchString(testOutput) |
| 60 | + test.Race = hasDataRace |
| 61 | + |
| 62 | + resultDiff := diff.FindStringSubmatch(testOutput) |
| 63 | + if resultDiff != nil { |
| 64 | + test.Diff = true |
| 65 | + } else { |
| 66 | + resultLines := end.FindStringSubmatch(testOutput) |
| 67 | + if resultLines != nil { |
| 68 | + switch resultLines[1] { |
| 69 | + case "PASS": |
| 70 | + test.Pass = true |
| 71 | + case "SKIP": |
| 72 | + test.Skip = true |
| 73 | + case "FAIL": |
| 74 | + test.Fail = true |
| 75 | + } |
| 76 | + test.Duration = resultLines[3] |
| 77 | + } |
| 78 | + } |
| 79 | + test.Output = testOutput |
| 80 | + test.ErrOutput = errOutput |
| 81 | +} |
| 82 | + |
| 83 | +func (test *TeamCityTest) FormatTestOutput() string { |
| 84 | + now := time.Now().Format(TeamCityTimestampFormat) |
| 85 | + |
| 86 | + var output bytes.Buffer |
| 87 | + |
| 88 | + output.WriteString(fmt.Sprintf(TeamCityTestStarted, test.Started.Format(TeamCityTimestampFormat), test.Name)) |
| 89 | + |
| 90 | + output.WriteString(fmt.Sprintf(TeamCityTestStdOut, test.Name, escapeOutput(test.Output))) |
| 91 | + output.WriteString(fmt.Sprintf(TeamCityTestStdErr, test.Name, escapeOutput(test.ErrOutput))) |
| 92 | + |
| 93 | + if test.Diff { |
| 94 | + output.WriteString(fmt.Sprintf(TeamCityTestDiffFailed, now, test.Name)) |
| 95 | + // have to fail so that teamcity catches failure correctly |
| 96 | + output.WriteString(fmt.Sprintf(TeamCityTestFailedPanic, now, test.Name)) |
| 97 | + output.WriteString(fmt.Sprintf(TeamCityTestFinished, now, test.Name)) |
| 98 | + return output.String() |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + if test.Fail { |
| 103 | + output.WriteString(fmt.Sprintf(TeamCityTestFailed, now, test.Name)) |
| 104 | + output.WriteString(fmt.Sprintf(TeamCityTestFinished, now, test.Name)) |
| 105 | + return output.String() |
| 106 | + } |
| 107 | + |
| 108 | + if test.Race { |
| 109 | + output.WriteString(fmt.Sprintf(TeamCityTestFailedRace, now, test.Name)) |
| 110 | + output.WriteString(fmt.Sprintf(TeamCityTestFinished, now, test.Name)) |
| 111 | + return output.String() |
| 112 | + } |
| 113 | + |
| 114 | + if test.Skip { |
| 115 | + output.WriteString(fmt.Sprintf(TeamCityTestIgnored, now, test.Name)) |
| 116 | + return output.String() |
| 117 | + } |
| 118 | + |
| 119 | + if test.Pass { |
| 120 | + output.WriteString(fmt.Sprintf(TeamCityTestFinished, now, test.Name)) |
| 121 | + return output.String() |
| 122 | + } |
| 123 | + |
| 124 | + // test passes if no diff, even if failure (failure artifacts will be in regular_failure_file.log) |
| 125 | + output.WriteString(fmt.Sprintf(TeamCityTestFinished, now, test.Name)) |
| 126 | + |
| 127 | + return output.String() |
| 128 | +} |
| 129 | + |
| 130 | +func escapeOutput(outputLines string) string { |
| 131 | + newOutput := strings.Replace(outputLines, "|", "||", -1) |
| 132 | + newOutput = strings.Replace(newOutput, "\n", "|n", -1) |
| 133 | + newOutput = strings.Replace(newOutput, "'", "|'", -1) |
| 134 | + newOutput = strings.Replace(newOutput, "]", "|]", -1) |
| 135 | + newOutput = strings.Replace(newOutput, "[", "|[", -1) |
| 136 | + return newOutput |
| 137 | +} |
0 commit comments