mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 15:06:36 +01:00
fix(java): skip comment lines in gradle.lockfile (#5372)
Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com> Co-authored-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
This commit is contained in:
co-authored by
Pujitha Paladugu
parent
4c500520d0
commit
722df28ed1
@@ -35,6 +35,11 @@ func parseGradleLockfile(_ context.Context, _ file.Resolver, _ *generic.Environm
|
|||||||
// Trim leading and trailing whitespace from the line
|
// Trim leading and trailing whitespace from the line
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
|
|
||||||
|
// skip comment lines (e.g. the header gradle writes), which may contain ':' and '=' characters
|
||||||
|
if strings.HasPrefix(line, "#") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
groupNameVersion := line
|
groupNameVersion := line
|
||||||
groupNameVersion = strings.Split(groupNameVersion, "=")[0]
|
groupNameVersion = strings.Split(groupNameVersion, "=")[0]
|
||||||
parts := strings.Split(groupNameVersion, ":")
|
parts := strings.Split(groupNameVersion, ":")
|
||||||
|
|||||||
@@ -87,3 +87,19 @@ func Test_parserGradleLockfile_lineTooLong(t *testing.T) {
|
|||||||
require.Len(t, pkgs, 1)
|
require.Len(t, pkgs, 1)
|
||||||
assert.Equal(t, "before", pkgs[0].Name)
|
assert.Equal(t, "before", pkgs[0].Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_parserGradleLockfile_skipsComments(t *testing.T) {
|
||||||
|
contents := `# This is a Gradle generated file for dependency locking.
|
||||||
|
# To regenerate this file, run: ./gradlew :dependencies --write-locks
|
||||||
|
# indented: comment=too
|
||||||
|
com.squareup.okhttp3:okhttp:4.12.0=compileClasspath,runtimeClasspath
|
||||||
|
empty=annotationProcessor
|
||||||
|
`
|
||||||
|
reader := file.NewLocationReadCloser(file.NewLocation("gradle.lockfile"), io.NopCloser(strings.NewReader(contents)))
|
||||||
|
|
||||||
|
pkgs, _, err := parseGradleLockfile(context.Background(), nil, nil, reader)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, pkgs, 1)
|
||||||
|
assert.Equal(t, "okhttp", pkgs[0].Name)
|
||||||
|
assert.Equal(t, "4.12.0", pkgs[0].Version)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user