Skip to content

Commit

Permalink
fix(pom): mark child deps of test scope as Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Sep 11, 2024
1 parent fcffc4a commit dba9f9f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 26 deletions.
15 changes: 15 additions & 0 deletions pkg/dependency/parser/java/pom/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (

type options struct {
offline bool
includeTestScopes bool
releaseRemoteRepos []string
snapshotRemoteRepos []string
}
Expand All @@ -42,6 +43,11 @@ func WithOffline(offline bool) option {
opts.offline = offline
}
}
func WithIncludeTestScopes(includeTestScopes bool) option {
return func(opts *options) {
opts.includeTestScopes = includeTestScopes
}
}

func WithReleaseRemoteRepos(repos []string) option {
return func(opts *options) {
Expand All @@ -63,6 +69,7 @@ type Parser struct {
releaseRemoteRepos []string
snapshotRemoteRepos []string
offline bool
includeTestScopes bool
servers []Server
}

Expand Down Expand Up @@ -91,6 +98,7 @@ func NewParser(filePath string, opts ...option) *Parser {
releaseRemoteRepos: o.releaseRemoteRepos,
snapshotRemoteRepos: o.snapshotRemoteRepos,
offline: o.offline,
includeTestScopes: o.includeTestScopes,
servers: s.Servers,
}
}
Expand Down Expand Up @@ -303,6 +311,7 @@ func (p *Parser) resolve(art artifact, rootDepManagement []pomDependency) (analy
result, err := p.analyze(pomContent, analysisOptions{
exclusions: art.Exclusions,
depManagement: rootDepManagement,
testScope: art.Test,
})
if err != nil {
return analysisResult{}, xerrors.Errorf("analyze error: %w", err)
Expand All @@ -325,6 +334,7 @@ type analysisOptions struct {
exclusions map[string]struct{}
depManagement []pomDependency // from the root POM
lineNumber bool // Save line numbers
testScope bool
}

func (p *Parser) analyze(pom *pom, opts analysisOptions) (analysisResult, error) {
Expand Down Expand Up @@ -402,6 +412,11 @@ func (p *Parser) parseDependencies(deps []pomDependency, props map[string]string
// Resolve dependencies
d = d.Resolve(props, depManagement, rootDepManagement)

// Save dependencies with `test` scope only when `--include-dev-deps` flag is present
if d.Scope == "test" && !p.includeTestScopes {
continue
}

if (d.Scope != "" && d.Scope != "compile" && d.Scope != "runtime" && d.Scope != "test") || d.Optional {
continue
}
Expand Down
94 changes: 69 additions & 25 deletions pkg/dependency/parser/java/pom/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ import (

func TestPom_Parse(t *testing.T) {
tests := []struct {
name string
inputFile string
local bool
offline bool
want []ftypes.Package
wantDeps []ftypes.Dependency
wantErr string
name string
inputFile string
local bool
offline bool
includeTestScopes bool
want []ftypes.Package
wantDeps []ftypes.Dependency
wantErr string
}{
{
name: "local repository",
inputFile: filepath.Join("testdata", "happy", "pom.xml"),
local: true,
name: "local repository",
inputFile: filepath.Join("testdata", "happy", "pom.xml"),
local: true,
includeTestScopes: true,
want: []ftypes.Package{
{
ID: "com.example:happy:1.0.0",
Expand Down Expand Up @@ -123,27 +125,13 @@ func TestPom_Parse(t *testing.T) {
},
},
},
{
ID: "org.example:example-test:2.0.0",
Name: "org.example:example-test",
Version: "2.0.0",
Relationship: ftypes.RelationshipDirect,
Dev: true,
Locations: ftypes.Locations{
{
StartLine: 49,
EndLine: 54,
},
},
},
},
wantDeps: []ftypes.Dependency{
{
ID: "com.example:happy:1.0.0",
DependsOn: []string{
"org.example:example-api:1.7.30",
"org.example:example-runtime:1.0.0",
"org.example:example-test:2.0.0",
},
},
},
Expand Down Expand Up @@ -1527,6 +1515,56 @@ func TestPom_Parse(t *testing.T) {
},
},
},
{
name: "include dependencies with test scope",
inputFile: filepath.Join("testdata", "test-scope", "pom.xml"),
local: true,
includeTestScopes: true,
want: []ftypes.Package{
{
ID: "com.example:test-example:1.0.0",
Name: "com.example:test-example",
Version: "1.0.0",
Relationship: ftypes.RelationshipRoot,
},

{
ID: "org.example:example-dependency:1.2.3",
Name: "org.example:example-dependency",
Version: "1.2.3",
Relationship: ftypes.RelationshipDirect,
Dev: true,
Locations: ftypes.Locations{
{
StartLine: 13,
EndLine: 18,
},
},
},
{
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,
Dev: true,
},
},
wantDeps: []ftypes.Dependency{
{
ID: "com.example:test-example:1.0.0",
DependsOn: []string{
"org.example:example-dependency:1.2.3",
},
},
{
ID: "org.example:example-dependency:1.2.3",
DependsOn: []string{
"org.example:example-api:2.0.0",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -1545,7 +1583,13 @@ func TestPom_Parse(t *testing.T) {
remoteRepos = []string{ts.URL}
}

p := pom.NewParser(tt.inputFile, pom.WithReleaseRemoteRepos(remoteRepos), pom.WithSnapshotRemoteRepos(remoteRepos), pom.WithOffline(tt.offline))
p := pom.NewParser(
tt.inputFile,
pom.WithReleaseRemoteRepos(remoteRepos),
pom.WithSnapshotRemoteRepos(remoteRepos),
pom.WithOffline(tt.offline),
pom.WithIncludeTestScopes(tt.includeTestScopes),
)

gotPkgs, gotDeps, err := p.Parse(f)
if tt.wantErr != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/java/pom/pom.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (d pomDependency) ToArtifact(opts analysisOptions) artifact {
Exclusions: exclusions,
Locations: locations,
Relationship: ftypes.RelationshipIndirect, // default
Test: d.Scope == "test",
Test: d.Scope == "test" || opts.testScope,
}
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/dependency/parser/java/pom/testdata/test-scope/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>test-example</artifactId>
<version>1.0.0</version>

<name>test-example</name>
<description>Example</description>

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-dependency</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

0 comments on commit dba9f9f

Please sign in to comment.