diff --git a/.gitignore b/.gitignore index 16cbe49..e5df3f6 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,6 @@ assets/assets.gen.go .DS_Store coverage.txt -plantuml.jar + +target/ +*.jar diff --git a/pkg/deps/config.go b/pkg/deps/config.go index 89219ae..fcdc0f1 100644 --- a/pkg/deps/config.go +++ b/pkg/deps/config.go @@ -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 { diff --git a/pkg/deps/maven.go b/pkg/deps/maven.go index cd7c067..0c5b146 100644 --- a/pkg/deps/maven.go +++ b/pkg/deps/maven.go @@ -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:] diff --git a/pkg/deps/maven_test.go b/pkg/deps/maven_test.go index 6a835bb..b9da207 100644 --- a/pkg/deps/maven_test.go +++ b/pkg/deps/maven_test.go @@ -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" ) @@ -69,85 +69,60 @@ 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 }{ - {` - - 4.0.0 - - apache - skywalking-eyes - 1.0 - - - - - junit - junit - 4.12 - - - - commons-logging - commons-logging - 1.2 - - - - org.apache.skywalking - skywalking-sharing-server-plugin - 8.6.0 - - - com.fasterxml.jackson.datatype - jackson-datatype-jsr310 - 2.13.3 - - - `, 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 } @@ -155,7 +130,6 @@ func TestResolveMaven(t *testing.T) { 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()) } } } diff --git a/pkg/deps/testdata/maven/.mvn/wrapper/maven-wrapper.properties b/pkg/deps/testdata/maven/base/.mvn/wrapper/maven-wrapper.properties similarity index 100% rename from pkg/deps/testdata/maven/.mvn/wrapper/maven-wrapper.properties rename to pkg/deps/testdata/maven/base/.mvn/wrapper/maven-wrapper.properties diff --git a/pkg/deps/testdata/maven/mvnw b/pkg/deps/testdata/maven/base/mvnw similarity index 100% rename from pkg/deps/testdata/maven/mvnw rename to pkg/deps/testdata/maven/base/mvnw diff --git a/pkg/deps/testdata/maven/base/pom.xml b/pkg/deps/testdata/maven/base/pom.xml new file mode 100644 index 0000000..4b1dea9 --- /dev/null +++ b/pkg/deps/testdata/maven/base/pom.xml @@ -0,0 +1,48 @@ + + + + 4.0.0 + + apache + skywalking-eyes + 1.0 + + + + junit + junit + 4.12 + + + commons-logging + commons-logging + 1.2 + + + org.apache.skywalking + skywalking-sharing-server-plugin + 8.6.0 + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.13.3 + + + diff --git a/pkg/deps/testdata/maven/cases/exclude-recursive/licenserc.yaml b/pkg/deps/testdata/maven/cases/exclude-recursive/licenserc.yaml new file mode 100644 index 0000000..8ea9207 --- /dev/null +++ b/pkg/deps/testdata/maven/cases/exclude-recursive/licenserc.yaml @@ -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 diff --git a/pkg/deps/testdata/maven/cases/exclude/licenserc.yaml b/pkg/deps/testdata/maven/cases/exclude/licenserc.yaml new file mode 100644 index 0000000..a58a8bc --- /dev/null +++ b/pkg/deps/testdata/maven/cases/exclude/licenserc.yaml @@ -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 diff --git a/pkg/deps/testdata/maven/cases/normal/licenserc.yaml b/pkg/deps/testdata/maven/cases/normal/licenserc.yaml new file mode 100644 index 0000000..1136984 --- /dev/null +++ b/pkg/deps/testdata/maven/cases/normal/licenserc.yaml @@ -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