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

Add some tests for maven resovler #138

Merged
merged 1 commit into from
Sep 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
48 changes: 48 additions & 0 deletions pkg/deps/testdata/maven/base/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->
<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>
23 changes: 23 additions & 0 deletions pkg/deps/testdata/maven/cases/exclude-recursive/licenserc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

dependency:
files:
- pom.xml
excludes:
- name: org.apache.skywalking:skywalking-sharing-server-plugin
recursive: true
23 changes: 23 additions & 0 deletions pkg/deps/testdata/maven/cases/exclude/licenserc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

dependency:
files:
- pom.xml
excludes:
- name: org.apache.skywalking:skywalking-sharing-server-plugin
recursive: false
20 changes: 20 additions & 0 deletions pkg/deps/testdata/maven/cases/normal/licenserc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

dependency:
files:
- pom.xml