mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
124 lines
2.8 KiB
Go
124 lines
2.8 KiB
Go
package text
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"testing"
|
|
|
|
"github.com/anchore/go-testutils"
|
|
"github.com/anchore/imgbom/imgbom/pkg"
|
|
"github.com/anchore/imgbom/imgbom/scope"
|
|
"github.com/anchore/stereoscope/pkg/file"
|
|
"github.com/sergi/go-diff/diffmatchpatch"
|
|
)
|
|
|
|
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
|
|
|
func TestTextDirPresenter(t *testing.T) {
|
|
var buffer bytes.Buffer
|
|
|
|
catalog := pkg.NewCatalog()
|
|
|
|
// populate catalog with test data
|
|
catalog.Add(pkg.Package{
|
|
Name: "package-1",
|
|
Version: "1.0.1",
|
|
Type: pkg.DebPkg,
|
|
})
|
|
catalog.Add(pkg.Package{
|
|
Name: "package-2",
|
|
Version: "2.0.1",
|
|
Type: pkg.DebPkg,
|
|
})
|
|
|
|
s, err := scope.NewScopeFromDir("/some/path", scope.AllLayersScope)
|
|
if err != nil {
|
|
t.Fatalf("unable to create scope: %+v", err)
|
|
}
|
|
pres := NewPresenter(catalog, s)
|
|
|
|
// run presenter
|
|
err = pres.Present(&buffer)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actual := buffer.Bytes()
|
|
|
|
if *update {
|
|
testutils.UpdateGoldenFileContents(t, actual)
|
|
}
|
|
|
|
var expected = testutils.GetGoldenFileContents(t)
|
|
|
|
if !bytes.Equal(expected, actual) {
|
|
dmp := diffmatchpatch.New()
|
|
diffs := dmp.DiffMain(string(actual), string(expected), true)
|
|
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs))
|
|
}
|
|
|
|
}
|
|
|
|
type PackageInfo struct {
|
|
Name string
|
|
Version string
|
|
}
|
|
|
|
func TestTextImgPresenter(t *testing.T) {
|
|
var buffer bytes.Buffer
|
|
|
|
catalog := pkg.NewCatalog()
|
|
img, cleanup := testutils.GetFixtureImage(t, "docker-archive", "image-simple")
|
|
defer cleanup()
|
|
|
|
// populate catalog with test data
|
|
catalog.Add(pkg.Package{
|
|
Name: "package-1",
|
|
Version: "1.0.1",
|
|
Source: []file.Reference{
|
|
*img.SquashedTree().File("/somefile-1.txt"),
|
|
},
|
|
FoundBy: "dpkg",
|
|
Type: pkg.DebPkg,
|
|
})
|
|
catalog.Add(pkg.Package{
|
|
Name: "package-2",
|
|
Version: "2.0.1",
|
|
Source: []file.Reference{
|
|
*img.SquashedTree().File("/somefile-2.txt"),
|
|
},
|
|
FoundBy: "dpkg",
|
|
Metadata: PackageInfo{Name: "package-2", Version: "1.0.2"},
|
|
Type: pkg.DebPkg,
|
|
})
|
|
|
|
// stub out all the digests so that they don't affect tests comparisons
|
|
// TODO: update with go-testutils feature when issue #1 is resolved
|
|
for _, l := range img.Layers {
|
|
l.Metadata.Digest = "sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53"
|
|
}
|
|
|
|
s, err := scope.NewScopeFromImage(img, scope.AllLayersScope)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pres := NewPresenter(catalog, s)
|
|
// run presenter
|
|
err = pres.Present(&buffer)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actual := buffer.Bytes()
|
|
if *update {
|
|
testutils.UpdateGoldenFileContents(t, actual)
|
|
}
|
|
|
|
var expected = testutils.GetGoldenFileContents(t)
|
|
|
|
if !bytes.Equal(expected, actual) {
|
|
dmp := diffmatchpatch.New()
|
|
diffs := dmp.DiffMain(string(actual), string(expected), true)
|
|
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs))
|
|
}
|
|
|
|
}
|