Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gradle): add *gradle.lockfile parser #127

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions pkg/gradle/lockfile/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lockfile

import (
"bufio"
"strings"

dio "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/aquasecurity/go-dep-parser/pkg/utils"
)

type Parser struct{}

func NewParser() types.Parser {
return &Parser{}
}

func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var libs []types.Library
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") { // skip comments
knqyf263 marked this conversation as resolved.
Show resolved Hide resolved
continue
}

// dependency format: group:artifact:version=classPaths
dep := strings.Split(line, ":")
if len(dep) != 3 { // skip the last line with lists of empty configurations
knqyf263 marked this conversation as resolved.
Show resolved Hide resolved
continue
}
libs = append(libs, types.Library{
Name: strings.Join(dep[:2], ":"),
Version: strings.Split(dep[2], "=")[0], // remove classPaths
})

}
return utils.UniqueLibraries(libs), nil, nil
}
52 changes: 52 additions & 0 deletions pkg/gradle/lockfile/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package lockfile

import (
"os"
"testing"

"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/stretchr/testify/assert"
)

func TestParser_Parse(t *testing.T) {
tests := []struct {
name string
inputFile string
want []types.Library
}{
{
name: "happy path",
inputFile: "testdata/happy.lockfile",
want: []types.Library{
{
Name: "cglib:cglib-nodep",
Version: "2.1.2",
},
{
Name: "org.springframework:spring-asm",
Version: "3.1.3.RELEASE",
},
{
Name: "org.springframework:spring-beans",
Version: "5.0.5.RELEASE",
},
},
},
{
name: "empty",
inputFile: "testdata/empty.lockfile",
want: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := NewParser()
f, err := os.Open(tt.inputFile)
assert.NoError(t, err)

libs, _, _ := parser.Parse(f)
assert.Equal(t, tt.want, libs)
})
}
}
4 changes: 4 additions & 0 deletions pkg/gradle/lockfile/testdata/empty.lockfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is a Gradle generated file for dependency locking.
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
empty=incomingCatalog
8 changes: 8 additions & 0 deletions pkg/gradle/lockfile/testdata/happy.lockfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is a Gradle generated file for dependency locking.
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
cglib:cglib-nodep:2.1.2=testRuntimeClasspath,classpath
org.springframework:spring-asm:3.1.3.RELEASE=classpath
org.springframework:spring-beans:5.0.5.RELEASE=compileClasspath, runtimeClasspath
# io.grpc:grpc-api:1.21.1=classpath
empty=