Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

massive amounts of duplicates shown by formatter due to labels being duplicated #310

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions pkg/runs/runsConverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package runs

import (
"log"
"sort"

"github.com/galasa-dev/cli/pkg/galasaapi"
Expand All @@ -18,6 +19,8 @@ func orderFormattableTests(formattableTest []runsformatter.FormattableTest) []ru
//get slice of all result labels in ordered form
orderedResultLabels := getAvailableResultLabelsinOrder(formattableTest)

log.Printf("orderFormattableTests: ordered result labels: '%v': %v\n", len(orderedResultLabels), orderedResultLabels)

//formattableTest runs grouped by results
//map["passed"] = [run1, run2, ...]
runsGroupedByResultsMap := make(map[string][]runsformatter.FormattableTest)
Expand All @@ -27,52 +30,78 @@ func orderFormattableTests(formattableTest []runsformatter.FormattableTest) []ru

//append tests in order
for _, result := range orderedResultLabels {
log.Printf("Gathering test results under the '%v' label\n", result)
orderedFormattableTest = append(orderedFormattableTest, runsGroupedByResultsMap[result]...)
log.Printf("Now there are %v results in total\n", len(orderedFormattableTest))
}

log.Printf("Returning %v test results\n", len(orderedFormattableTest))
return orderedFormattableTest
}

// getAvailableResultLabelsinOrder - returns a slice of all available result labels in the order they should be displayed
// The order is important as it determines how the tests will be displayed on the screen
// The standard labels are shown first, followed by any extra labels present in the test data.
// These laels can be used as columns in the eventual output.
func getAvailableResultLabelsinOrder(formattableTest []runsformatter.FormattableTest) []string {
var orderedResultLabels []string
var orderedResultLabels []string = make([]string, 0)
orderedResultLabels = append(orderedResultLabels, RESULT_PASSED)
orderedResultLabels = append(orderedResultLabels, RESULT_PASSED_WITH_DEFECTS)
orderedResultLabels = append(orderedResultLabels, RESULT_FAILED)
orderedResultLabels = append(orderedResultLabels, RESULT_FAILED_WITH_DEFECTS)
orderedResultLabels = append(orderedResultLabels, RESULT_ENVFAIL)

//Build a list of standard labels to prevent duplication
var standardResultLabels = make(map[string]struct{})
var standardResultLabels = make(map[string]struct{}, 0)
for _, key := range orderedResultLabels {
//'struct{}{}' allocates no storage. In Go 1.19 we can use just '{}' instead
standardResultLabels[key] = struct{}{}
}

log.Printf("There are %v standard labels: %v\n", len(standardResultLabels), standardResultLabels)

//Gathering custom labels
var customResultLabels []string
var customResultLabels []string = make([]string, 0)
// A map to make sure we never add the same custom label to the list twice.
customResultLabelMap := make(map[string]string, 0)
for _, run := range formattableTest {
_, isStandardLabel := standardResultLabels[run.Result]
if !isStandardLabel {
customResultLabels = append(customResultLabels, run.Result)

_, isCustomLabelWeAlreadyKnowAbout := customResultLabelMap[run.Result]
if !isCustomLabelWeAlreadyKnowAbout {
log.Printf("Label '%v' is not a standard result label\n", run.Result)
customResultLabels = append(customResultLabels, run.Result)
customResultLabelMap[run.Result] = "known"
}
}
}

sort.Strings(customResultLabels)
orderedResultLabels = append(orderedResultLabels, customResultLabels...)

log.Printf("There are %v labels overall: %v", len(orderedResultLabels), orderedResultLabels)

return orderedResultLabels
}

func FormattableTestFromGalasaApi(runs []galasaapi.Run, apiServerUrl string) []runsformatter.FormattableTest {
var formattableTest []runsformatter.FormattableTest

log.Printf("FormattableTestFromGalasaApi: There are %v runs passed\n", len(runs))

for _, run := range runs {
//Get the data for each TestStructure in runs
newFormattableTest := getTestStructureData(run, apiServerUrl)
formattableTest = append(formattableTest, newFormattableTest)
}

log.Printf("FormattableTestFromGalasaApi: There are %v runs to format\n", len(formattableTest))

orderedFormattableTest := orderFormattableTests(formattableTest)

log.Printf("FormattableTestFromGalasaApi: There are %v runs returned\n", len(orderedFormattableTest))

return orderedFormattableTest
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/runs/runsConverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,41 @@ func TestGherkinRunReturnsCorrectFormattableTestly(t *testing.T) {
assert.Equal(t, "Passed", output.Result)
assert.Equal(t, "myStatus", output.Status)
}

func TestGetAvailableResultLabelsInOrderWithNo2ExtraLabels(t *testing.T) {
testRun1 := runsformatter.FormattableTest{
Name: "",
Bundle: "",
Status: "myStatus",
Result: "Passed",
}
testRun2 := runsformatter.FormattableTest{
Name: "",
Bundle: "",
Status: "myStatus",
Result: "MyWeirdResultCode",
}
testRun3 := runsformatter.FormattableTest{
Name: "",
Bundle: "",
Status: "myStatus",
Result: "MyWeirdResultalternativeCode",
}
testRun4 := runsformatter.FormattableTest{
Name: "",
Bundle: "",
Status: "myStatus",
Result: "MyWeirdResultalternativeCode",
}
testRun5 := runsformatter.FormattableTest{
Name: "",
Bundle: "",
Status: "myStatus",
Result: "MyWeirdResultalternativeCode",
}
testRunsArray := [5]runsformatter.FormattableTest{testRun1, testRun2, testRun3, testRun4, testRun5}
labelsGotBack := getAvailableResultLabelsinOrder(testRunsArray[:])

assert.NotEmpty(t, labelsGotBack)
assert.Equal(t, 7, len(labelsGotBack))
}
14 changes: 14 additions & 0 deletions pkg/runs/runsGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@ func GetRuns(
if err == nil {
// Some formatters need extra fields filled-in so they can be displayed.
if chosenFormatter.IsNeedingMethodDetails() {
log.Println("This type of formatter needs extra detail about each run to display")
runJson, err = GetRunDetailsFromRasSearchRuns(runJson, apiClient)
}

if err == nil {
var outputText string

log.Printf("There are %v results to display in total.\n", len(runJson))

//convert galsaapi.Runs tests into formattable data
formattableTest := FormattableTestFromGalasaApi(runJson, apiServerUrl)
outputText, err = chosenFormatter.FormatRuns(formattableTest)
Expand Down Expand Up @@ -179,6 +182,7 @@ func GetRunDetailsFromRasSearchRuns(runs []galasaapi.Run, apiClient *galasaapi.A

for _, run := range runs {
runid := run.GetRunId()
log.Printf("Getting details for run %v\n", runid)
details, httpResponse, err = apiClient.ResultArchiveStoreAPIApi.GetRasRunById(context, runid).ClientApiVersion(restApiVersion).Execute()
if err != nil {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_QUERY_RUNS_FAILED, err.Error())
Expand All @@ -187,7 +191,9 @@ func GetRunDetailsFromRasSearchRuns(runs []galasaapi.Run, apiClient *galasaapi.A

err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_QUERY_RUNS_NON_OK_STATUS, strconv.Itoa(httpResponse.StatusCode))
} else {
log.Printf("Adding those details into the list of runs. Length")
runsDetails = append(runsDetails, *details)
log.Printf("Now there are %v runs\n", len(runsDetails))
}
}
}
Expand Down Expand Up @@ -267,12 +273,18 @@ func GetRunsFromRestApi(
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_QUERY_RUNS_FAILED, errString)
} else {

log.Printf("HTTP status was OK")

// Copy the results from this page into our bigger list of results.
runsOnThisPage := runData.GetRuns()
log.Printf("runsOnThisPage: %v", len(runsOnThisPage))

// Add all the runs into our set of results.
// Note: The ... syntax means 'all of the array', so they all get appended at once.
results = append(results, runsOnThisPage...)

log.Printf("total runs: %v", len(results))

// Have we processed the last page ?
if !runData.HasNextCursor() || len(runsOnThisPage) < int(runData.GetPageSize()) {
gotAllResults = true
Expand All @@ -285,6 +297,8 @@ func GetRunsFromRestApi(
}
}

log.Printf("total runs returned: %v", len(results))

return results, err
}

Expand Down
9 changes: 7 additions & 2 deletions pkg/runsformatter/summaryFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/
package runsformatter

import "strings"
import (
"log"
"strings"
)

// -----------------------------------------------------
// Summary format.
Expand All @@ -29,12 +32,14 @@ func (*SummaryFormatter) IsNeedingMethodDetails() bool {
}

func (*SummaryFormatter) FormatRuns(testResultsData []FormattableTest) (string, error) {
var result string = ""
var result string
var err error
buff := strings.Builder{}
totalResults := len(testResultsData)
resultCountsMap := initialiseResultMap()

log.Printf("Formatter passed %v runs to show.\n", len(testResultsData))

if totalResults > 0 {
var table [][]string

Expand Down