Skip to content

Commit

Permalink
Add some tests for maven resovler
Browse files Browse the repository at this point in the history
  • Loading branch information
kezhenxu94 committed Sep 8, 2022
1 parent df92796 commit 04cf948
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 54 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@
assets/assets.gen.go
.DS_Store
coverage.txt
plantuml.jar

target/
*.jar
4 changes: 3 additions & 1 deletion pkg/deps/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func (config *ConfigDeps) Finalize(configFile string) error {
}

for i, file := range config.Files {
config.Files[i] = filepath.Join(filepath.Dir(configFileAbsPath), file)
if !strings.HasPrefix(file, "/") {
config.Files[i] = filepath.Join(filepath.Dir(configFileAbsPath), file)
}
}

if config.Threshold <= 0 {
Expand Down
3 changes: 3 additions & 0 deletions pkg/deps/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ func LoadDependencies(data []byte, config *ConfigDeps) []*Dependency {
queue = append(queue, depTree)
} else if recursive {
continue
} else {
queue = append(queue, depTree.TransitiveDeps...)
}

for len(queue) > 0 {
dep := queue[0]
queue = queue[1:]
Expand Down
78 changes: 26 additions & 52 deletions pkg/deps/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ package deps_test
import (
"bufio"
"embed"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"

"github.com/apache/skywalking-eyes/pkg/config"
"github.com/apache/skywalking-eyes/pkg/deps"
)

Expand Down Expand Up @@ -69,93 +69,67 @@ func ensureDir(dirName string) error {
return os.MkdirAll(dirName, 0777)
}

//go:embed testdata/maven/*
//go:embed testdata/maven/**/*
var testAssets embed.FS

func TestResolveMaven(t *testing.T) {
resolver := new(deps.MavenPomResolver)
tempDir := t.TempDir()
base := "testdata/maven"

fs.WalkDir(testAssets, base, func(path string, d fs.DirEntry, err error) error {
func copy(assetDir, destination string) error {
return fs.WalkDir(testAssets, assetDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
filename := filepath.Join(tempDir, strings.Replace(path, base, "", 1))
filename := filepath.Join(destination, strings.Replace(path, assetDir, "", 1))
if err := ensureDir(filepath.Dir(filename)); err != nil {
return err
}

content, err := testAssets.ReadFile(path)
if err != nil {
t.Error(err)
return err
}
writeFile(filename, string(content))

return nil
})
}

pomFile := filepath.Join(tempDir, "pom.xml")
func TestResolveMaven(t *testing.T) {
resolver := new(deps.MavenPomResolver)

for _, test := range []struct {
pomContent string
workingDir string
testCase string
cnt int
}{
{`<?xml version="1.0" encoding="UTF-8"?>
<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>apache</groupId>
<artifactId>skywalking-eyes</artifactId>
<version>1.0</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.skywalking/skywalking-sharing-server-plugin -->
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>skywalking-sharing-server-plugin</artifactId>
<version>8.6.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
</project>`, 110},
{t.TempDir(), "normal", 110},
{t.TempDir(), "exclude", 109},
{t.TempDir(), "exclude-recursive", 7},
} {
_ = writeFile(pomFile, test.pomContent)
if err := copy("testdata/maven/base", test.workingDir); err != nil {
t.Error(err)
}
if err := copy(filepath.Join("testdata/maven/cases", test.testCase), test.workingDir); err != nil {
t.Error(err)
}

config := deps.ConfigDeps{}
config.Finalize("")
config, err := config.NewConfigFromFile(filepath.Join(test.workingDir, "licenserc.yaml"))
if err != nil {
t.Error(err)
}

pomFile := filepath.Join(test.workingDir, "pom.xml")
if resolver.CanResolve(pomFile) {
report := deps.Report{}
if err := resolver.Resolve(pomFile, &config, &report); err != nil {
if err := resolver.Resolve(pomFile, config.Dependencies(), &report); err != nil {
t.Error(err)
return
}

if len(report.Resolved)+len(report.Skipped) != test.cnt {
t.Errorf("the expected number of jar packages is: %d, but actually: %d. result:\n%v", test.cnt, len(report.Resolved)+len(report.Skipped), report.String())
}
fmt.Println(report.String())
}
}
}
File renamed without changes.
31 changes: 31 additions & 0 deletions pkg/deps/testdata/maven/base/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>apache</groupId>
<artifactId>skywalking-eyes</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>skywalking-sharing-server-plugin</artifactId>
<version>8.6.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependency:
files:
- pom.xml
excludes:
- name: org.apache.skywalking:skywalking-sharing-server-plugin
recursive: true
6 changes: 6 additions & 0 deletions pkg/deps/testdata/maven/cases/exclude/licenserc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependency:
files:
- pom.xml
excludes:
- name: org.apache.skywalking:skywalking-sharing-server-plugin
recursive: false
3 changes: 3 additions & 0 deletions pkg/deps/testdata/maven/cases/normal/licenserc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependency:
files:
- pom.xml

0 comments on commit 04cf948

Please sign in to comment.