Skip to content

Commit

Permalink
chore: move formatting out of the FailedTestCases
Browse files Browse the repository at this point in the history
  • Loading branch information
psturc committed Nov 13, 2024
1 parent 5b9449f commit 970f1f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cmd/analyzetestresults/analyzetestresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var AnalyzeTestResultsCmd = &cobra.Command{
failedTCReport := testresults.FailedTestCasesReport{}
failedTCReport.CollectTestFilesData(scanner.FilesPathMap, jUnitFilename, e2eTestRunLogFilename, clusterProvisionLogFilename)

if err := os.WriteFile(outputFilename, []byte(failedTCReport.GetFormattedReport()), 0o600); err != nil {
if err := os.WriteFile(outputFilename, []byte(testresults.GetFormattedReport(failedTCReport)), 0o600); err != nil {
return fmt.Errorf("failed to create a file with the test result analysis: %+v", err)
}
klog.Infof("analysis saved to %s", outputFilename)
Expand Down
43 changes: 19 additions & 24 deletions pkg/testresults/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import (
"fmt"
)

const dropdownSummaryString = "Click to view logs"

// extractFailedTestCasesBody initialises the FailedTestCasesReport struct's
// 'failedTestCaseNames' field with the names of failed test cases
// within given JUnitTestSuites -- if the given JUnitTestSuites is !nil.
func (f *FailedTestCasesReport) extractFailedTestCasesBody() {
func extractFailedTestCasesBody(f FailedTestCasesReport) (failedTestCasesBody []string) {
switch f.FailureType {
case OtherFailure:
return
case ClusterCreationFailure:
testCaseEntry := returnContentWrappedInDropdown(dropdownSummaryString, f.ClusterProvisionLog)
f.FailedTestCaseNames = append(f.FailedTestCaseNames, testCaseEntry)
return
return []string{returnContentWrappedInDropdown(dropdownSummaryString, f.ClusterProvisionLog)}
case TestRunFailure:
testCaseEntry := returnContentWrappedInDropdown(dropdownSummaryString, f.E2ETestLog)
f.FailedTestCaseNames = append(f.FailedTestCaseNames, testCaseEntry)
return
return []string{returnContentWrappedInDropdown(dropdownSummaryString, f.E2ETestLog)}
}
ftc := f.GetFailedTestCases()
for _, tc := range ftc {
Expand All @@ -33,36 +31,33 @@ func (f *FailedTestCasesReport) extractFailedTestCasesBody() {
}

testCaseEntry := "* :arrow_right: " + "[**`" + tc.Status + "`**] " + tc.Name + "\n" + tcMessage
f.FailedTestCaseNames = append(f.FailedTestCaseNames, testCaseEntry)
failedTestCasesBody = append(failedTestCasesBody, testCaseEntry)
}
return
}

// setHeaderString initialises sets 'headerString' field for the report summary
// getHeaderStringForFailureType returns 'headerString' for the report summary
// based on phase at which PipelineRun failed
func (f *FailedTestCasesReport) setHeaderString() {
switch f.FailureType {
func getHeaderStringForFailureType(ft FailureType) string {
switch ft {
case OtherFailure:
f.HeaderString = ":rotating_light: **Couldn't detect a specific failure, see the related PipelineRun for more details or consult with Konflux DevProd team.**\n"
return
return ":rotating_light: **Couldn't detect a specific failure, see the related PipelineRun for more details or consult with Konflux DevProd team.**\n"
case TestRunFailure:
f.HeaderString = ":rotating_light: **No JUnit file found, see the log from running tests**: \n"
return
return ":rotating_light: **No JUnit file found, see the log from running tests**: \n"
case ClusterCreationFailure:
f.HeaderString = ":rotating_light: **Failed to provision a cluster, see the log for more details**: \n"
return
return ":rotating_light: **Failed to provision a cluster, see the log for more details**: \n"
case TestCaseFailure:
f.HeaderString = ":rotating_light: **Error occurred while running the E2E tests, list of failed Spec(s)**: \n"
return ":rotating_light: **Error occurred while running the E2E tests, list of failed Spec(s)**: \n"
}
return ""
}

// GetFormattedReport returns the full report (test run analysis) as a string
func (f *FailedTestCasesReport) GetFormattedReport() (report string) {
f.setHeaderString()
f.extractFailedTestCasesBody()
func GetFormattedReport(report FailedTestCasesReport) (formattedReport string) {
formattedReport = getHeaderStringForFailureType(report.FailureType)

report = f.HeaderString
for _, failedTCName := range f.FailedTestCaseNames {
report += fmt.Sprintf("\n %s\n", failedTCName)
for _, failedTCName := range extractFailedTestCasesBody(report) {
formattedReport += fmt.Sprintf("\n %s\n", failedTCName)
}

return
Expand Down
5 changes: 0 additions & 5 deletions pkg/testresults/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
type FailureType string

const (
dropdownSummaryString = "Click to view logs"

// OtherFailure represents the type failure that hasn't been identified by the analyzer
OtherFailure FailureType = "otherFailure"
// ClusterCreationFailure represents the issue with provisioning a test cluster
Expand All @@ -28,9 +26,6 @@ const (
type FailedTestCasesReport struct {
JUnitTestSuites *reporters.JUnitTestSuites

HeaderString string
FailedTestCaseNames []string

ClusterProvisionLog string
E2ETestLog string

Expand Down

0 comments on commit 970f1f8

Please sign in to comment.