From 1f9dfa311495847bfa52b802f34cc628cbd0fd64 Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Tue, 15 Oct 2024 23:10:25 +0200 Subject: [PATCH 1/4] feat(parser): ignore white space in pom.xml files It happens when creating the data structures and does not modify the file itself. It makes the parser behave in the same manner as maven. Signed-off-by: Samuel Gaist --- pkg/dependency/parser/java/pom/parse_test.go | 15 +++++++++++++++ pkg/dependency/parser/java/pom/pom.go | 2 +- .../parser/java/pom/testdata/with-spaces/pom.xml | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml diff --git a/pkg/dependency/parser/java/pom/parse_test.go b/pkg/dependency/parser/java/pom/parse_test.go index 7b77c0a7ef5e..2704835e828a 100644 --- a/pkg/dependency/parser/java/pom/parse_test.go +++ b/pkg/dependency/parser/java/pom/parse_test.go @@ -1974,6 +1974,21 @@ func TestPom_Parse(t *testing.T) { }, }, }, + { + name: "space at the start and/or end of the text nodes", + inputFile: filepath.Join("testdata", "with-spaces", "pom.xml"), + local: true, + want: []ftypes.Package{ + { + ID: "com.example : root-pom-with-spaces : 1.0.0", + Name: "com.example : root-pom-with-spaces ", + Version: " 1.0.0", + Relationship: ftypes.RelationshipRoot, + }, + }, + // wantDeps: []ftypes.Dependency{ + // }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/dependency/parser/java/pom/pom.go b/pkg/dependency/parser/java/pom/pom.go index 83c5d4fec609..8d61d26b5983 100644 --- a/pkg/dependency/parser/java/pom/pom.go +++ b/pkg/dependency/parser/java/pom/pom.go @@ -333,7 +333,7 @@ func (props *properties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error return xerrors.Errorf("XML decode error: %w", err) } - (*props)[p.XMLName.Local] = p.Value + (*props)[p.XMLName.Local] = strings.TrimSpace(p.Value) } return nil } diff --git a/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml b/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml new file mode 100644 index 000000000000..1193c6004ac8 --- /dev/null +++ b/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml @@ -0,0 +1,9 @@ + + 4.0.0 + + com.example + root-pom-with-spaces + 1.0.0 + + From 8ba7b54c5fd564efb6682c91d68531f859284c5a Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Wed, 16 Oct 2024 21:02:48 +0200 Subject: [PATCH 2/4] refactor(pom): ensure all fields are trimmed Signed-off-by: Samuel Gaist --- pkg/dependency/parser/java/pom/artifact.go | 2 +- pkg/dependency/parser/java/pom/parse_test.go | 59 +++++++++++++++++-- pkg/dependency/parser/java/pom/pom.go | 2 +- .../java/pom/testdata/with-spaces/pom.xml | 18 ++++++ 4 files changed, 74 insertions(+), 7 deletions(-) diff --git a/pkg/dependency/parser/java/pom/artifact.go b/pkg/dependency/parser/java/pom/artifact.go index 73ebe6b21407..8fc20b236b51 100644 --- a/pkg/dependency/parser/java/pom/artifact.go +++ b/pkg/dependency/parser/java/pom/artifact.go @@ -163,7 +163,7 @@ func evaluateVariable(s string, props map[string]string, seenProps []string) str } s = strings.ReplaceAll(s, m[0], newValue) } - return s + return strings.TrimSpace(s) } func printLoopedPropertiesStack(env string, usedProps []string) { diff --git a/pkg/dependency/parser/java/pom/parse_test.go b/pkg/dependency/parser/java/pom/parse_test.go index 2704835e828a..8b833839cad3 100644 --- a/pkg/dependency/parser/java/pom/parse_test.go +++ b/pkg/dependency/parser/java/pom/parse_test.go @@ -1974,20 +1974,69 @@ func TestPom_Parse(t *testing.T) { }, }, }, + //[INFO] com.example:root-pom-with-spaces:jar:1.0.0 + //[INFO] \- org.example:example-nested:jar:3.3.3:compile + //[INFO] \- org.example:example-dependency:jar:1.2.4:compile + //[INFO] \- org.example:example-api:jar:2.0.0:compile { name: "space at the start and/or end of the text nodes", inputFile: filepath.Join("testdata", "with-spaces", "pom.xml"), local: true, want: []ftypes.Package{ { - ID: "com.example : root-pom-with-spaces : 1.0.0", - Name: "com.example : root-pom-with-spaces ", - Version: " 1.0.0", + ID: "com.example:root-pom-with-spaces:1.0.0", + Name: "com.example:root-pom-with-spaces", + Version: "1.0.0", Relationship: ftypes.RelationshipRoot, }, + { + ID: "org.example:example-nested:3.3.3", + Name: "org.example:example-nested", + Version: "3.3.3", + Relationship: ftypes.RelationshipDirect, + Locations: ftypes.Locations{ + { + StartLine: 20, + EndLine: 24, + }, + }, + }, + { + ID: "org.example:example-api:2.0.0", + Name: "org.example:example-api", + Version: "2.0.0", + Licenses: []string{"The Apache Software License, Version 2.0"}, + Relationship: ftypes.RelationshipIndirect, + }, + // dependency version is taken from `com.example:root-pom-with-spaces` from dependencyManagement + // not from `com.example:example-nested` from `com.example:example-nested` + { + ID: "org.example:example-dependency:1.2.4", + Name: "org.example:example-dependency", + Version: "1.2.4", + Relationship: ftypes.RelationshipIndirect, + }, + }, + wantDeps: []ftypes.Dependency{ + { + ID: "com.example:root-pom-with-spaces:1.0.0", + DependsOn: []string{ + "org.example:example-nested:3.3.3", + }, + }, + { + ID: "org.example:example-dependency:1.2.4", + DependsOn: []string{ + "org.example:example-api:2.0.0", + }, + }, + { + ID: "org.example:example-nested:3.3.3", + DependsOn: []string{ + "org.example:example-dependency:1.2.4", + }, + }, }, - // wantDeps: []ftypes.Dependency{ - // }, }, } for _, tt := range tests { diff --git a/pkg/dependency/parser/java/pom/pom.go b/pkg/dependency/parser/java/pom/pom.go index 8d61d26b5983..83c5d4fec609 100644 --- a/pkg/dependency/parser/java/pom/pom.go +++ b/pkg/dependency/parser/java/pom/pom.go @@ -333,7 +333,7 @@ func (props *properties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error return xerrors.Errorf("XML decode error: %w", err) } - (*props)[p.XMLName.Local] = strings.TrimSpace(p.Value) + (*props)[p.XMLName.Local] = p.Value } return nil } diff --git a/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml b/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml index 1193c6004ac8..d60262fe4b92 100644 --- a/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml +++ b/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml @@ -6,4 +6,22 @@ root-pom-with-spaces 1.0.0 + + + + org.example + example-dependency + 1.2.4 + + + + + + + org.example + example-nested + 3.3.3 + + + From 164a7c43f7931883fb0086ca03105addd84a998e Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Thu, 17 Oct 2024 08:50:23 +0200 Subject: [PATCH 3/4] chore(pom): add properties to white space test Signed-off-by: Samuel Gaist --- pkg/dependency/parser/java/pom/parse_test.go | 4 ++-- pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/dependency/parser/java/pom/parse_test.go b/pkg/dependency/parser/java/pom/parse_test.go index 8b833839cad3..8d0bf002d6dc 100644 --- a/pkg/dependency/parser/java/pom/parse_test.go +++ b/pkg/dependency/parser/java/pom/parse_test.go @@ -1996,8 +1996,8 @@ func TestPom_Parse(t *testing.T) { Relationship: ftypes.RelationshipDirect, Locations: ftypes.Locations{ { - StartLine: 20, - EndLine: 24, + StartLine: 24, + EndLine: 28, }, }, }, diff --git a/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml b/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml index d60262fe4b92..c5e7c1900230 100644 --- a/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml +++ b/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml @@ -16,6 +16,10 @@ + + 1.2.4 + + org.example From a8a9d262d77c503e3ea388758f572a47c599869f Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Thu, 17 Oct 2024 10:28:20 +0200 Subject: [PATCH 4/4] fix(pom): fix white space test file The property was not correctly used. Signed-off-by: Samuel Gaist --- .../parser/java/pom/testdata/with-spaces/pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml b/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml index c5e7c1900230..7a0a1013aa31 100644 --- a/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml +++ b/pkg/dependency/parser/java/pom/testdata/with-spaces/pom.xml @@ -6,20 +6,20 @@ root-pom-with-spaces 1.0.0 + + 1.2.4 + + org.example example-dependency - 1.2.4 + ${dependency.version} - - 1.2.4 - - org.example