mirror of
https://github.com/anchore/syft
synced 2024-11-10 06:14:16 +00:00
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:
parent
1860bab24b
commit
630c18e0d3
1 changed files with 15 additions and 1 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue