From 630c18e0d3151d9e13c43cdb2f5424d22334b50f Mon Sep 17 00:00:00 2001 From: William Murphy Date: Fri, 5 May 2023 15:57:13 -0400 Subject: [PATCH] Print package list when extra packages found (#1791) The tests in test/cli/packages_cmd_test.go are hard to debug when different packages are found in different environments. For example, CI runs and M1 macs have been observed to have different package counts. Therefore, if the test is about to fail, log a sorted list of the packages that were found, so that it is easy to compare failures of these tests. Signed-off-by: Will Murphy --- test/cli/trait_assertions_test.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/cli/trait_assertions_test.go b/test/cli/trait_assertions_test.go index d14dfca73..465c389d2 100644 --- a/test/cli/trait_assertions_test.go +++ b/test/cli/trait_assertions_test.go @@ -2,10 +2,12 @@ package cli import ( "encoding/json" + "fmt" "os" "os/exec" "path/filepath" "regexp" + "sort" "strings" "testing" @@ -104,8 +106,12 @@ func assertStdoutLengthGreaterThan(length uint) traitAssertion { func assertPackageCount(length uint) traitAssertion { return func(tb testing.TB, stdout, _ string, _ int) { tb.Helper() + type NameAndVersion struct { + Name string `json:"name"` + Version string `json:"version"` + } type partial struct { - Artifacts []interface{} `json:"artifacts"` + Artifacts []NameAndVersion `json:"artifacts"` } var data partial @@ -115,6 +121,14 @@ func assertPackageCount(length uint) traitAssertion { if uint(len(data.Artifacts)) != length { tb.Errorf("expected package count of %d, but found %d", length, len(data.Artifacts)) + debugArtifacts := make([]string, len(data.Artifacts)) + for i, a := range data.Artifacts { + debugArtifacts[i] = fmt.Sprintf("%s:%s", a.Name, a.Version) + } + sort.Strings(debugArtifacts) + for i, a := range debugArtifacts { + tb.Errorf("package %d: %s", i+1, a) + } } }