package packages import ( "bytes" "flag" "testing" "github.com/anchore/stereoscope/pkg/filetree" "github.com/go-test/deep" "github.com/anchore/go-testutils" "github.com/anchore/stereoscope/pkg/imagetest" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/source" "github.com/sergi/go-diff/diffmatchpatch" ) var updateTablePresenterGoldenFiles = flag.Bool("update-table", false, "update the *.golden files for table presenters") func TestTablePresenter(t *testing.T) { var buffer bytes.Buffer testImage := "image-simple" catalog := pkg.NewCatalog() img := imagetest.GetFixtureImage(t, "docker-archive", testImage) _, ref1, _ := img.SquashedTree().File("/somefile-1.txt", filetree.FollowBasenameLinks) _, ref2, _ := img.SquashedTree().File("/somefile-2.txt", filetree.FollowBasenameLinks) // populate catalog with test data catalog.Add(pkg.Package{ Name: "package-1", Version: "1.0.1", Locations: []source.Location{ source.NewLocationFromImage(string(ref1.RealPath), *ref1, img), }, Type: pkg.DebPkg, }) catalog.Add(pkg.Package{ Name: "package-2", Version: "2.0.1", Locations: []source.Location{ source.NewLocationFromImage(string(ref2.RealPath), *ref2, img), }, Type: pkg.DebPkg, }) pres := NewTablePresenter(catalog) // run presenter err := pres.Present(&buffer) if err != nil { t.Fatal(err) } actual := buffer.Bytes() if *updateTablePresenterGoldenFiles { 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)) } } func TestRemoveDuplicateRows(t *testing.T) { data := [][]string{ {"1", "2", "3"}, {"a", "b", "c"}, {"1", "2", "3"}, {"a", "b", "c"}, {"1", "2", "3"}, {"4", "5", "6"}, {"1", "2", "1"}, } expected := [][]string{ {"1", "2", "3"}, {"a", "b", "c"}, {"4", "5", "6"}, {"1", "2", "1"}, } actual := removeDuplicateRows(data) if diffs := deep.Equal(expected, actual); len(diffs) > 0 { t.Errorf("found diffs!") for _, d := range diffs { t.Errorf(" diff: %+v", d) } } }