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 <will.murphy@anchore.com>
This commit is contained in:
William Murphy 2023-05-05 15:57:13 -04:00 committed by GitHub
parent 1860bab24b
commit 630c18e0d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)
}
}
}