From fc6e1cd0cc44dca51f7441e402aaf0af2fdd204b Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Wed, 7 Feb 2024 11:12:18 -0500 Subject: [PATCH] test: unit test legacy java manifest helper Signed-off-by: Will Murphy --- syft/pkg/java_test.go | 101 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/syft/pkg/java_test.go b/syft/pkg/java_test.go index dd8679fef86..e0c4248573d 100644 --- a/syft/pkg/java_test.go +++ b/syft/pkg/java_test.go @@ -3,6 +3,7 @@ package pkg import ( "testing" + "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" ) @@ -109,3 +110,103 @@ func TestPomProperties_PkgTypeIndicated(t *testing.T) { }) } } + +func Test_legacyJavaManifest_toNewManifest(t *testing.T) { + tests := []struct { + name string + lm legacyJavaManifest + want JavaManifest + }{ + { + name: "empty", + lm: legacyJavaManifest{}, + want: JavaManifest{}, + }, + { + name: "main sections are sorted", + lm: legacyJavaManifest{ + Main: map[string]string{ + "a key": "a value", + "b key": "b value", + "c key": "c value", + }, + }, + want: JavaManifest{Main: KeyValues{ + { + Key: "a key", + Value: "a value", + }, + { + Key: "b key", + Value: "b value", + }, + { + Key: "c key", + Value: "c value", + }, + }}, + }, + { + name: "named sections have their name in the result", + lm: legacyJavaManifest{ + NamedSections: map[string]map[string]string{ + "a section": { + "a key": "a value", + "b key": "b value", + "c key": "c value", + }, + "b section": { + "d key": "d value", + "e key": "e value", + "f key": "f value", + }, + }, + }, + want: JavaManifest{Sections: []KeyValues{ + { + { + Key: "Name", + Value: "a section", + }, + { + Key: "a key", + Value: "a value", + }, + { + Key: "b key", + Value: "b value", + }, + { + Key: "c key", + Value: "c value", + }, + }, + { + { + Key: "Name", + Value: "b section", + }, + { + Key: "d key", + Value: "d value", + }, + { + Key: "e key", + Value: "e value", + }, + { + Key: "f key", + Value: "f value", + }, + }, + }}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if diff := cmp.Diff(tt.want, tt.lm.toNewManifest()); diff != "" { + t.Errorf("unexpected diff in manifest (-want +got):\n%s", diff) + } + }) + } +}