Skip to content

Commit

Permalink
Polish maven test for convenient debug
Browse files Browse the repository at this point in the history
  • Loading branch information
kezhenxu94 committed Sep 3, 2022
1 parent cbb6f55 commit 3f618dd
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 15 deletions.
11 changes: 11 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,14 @@ The following components are provided under the MIT license. See project link fo
The text of each license is also included at licenses/LICENSE-[project].txt.

languages.yaml from GitHub: https://github.com/github/linguist/blob/master/lib/linguist/languages.yml MIT


========================================================================
Apache 2.0 licenses
========================================================================

The following components are provided under the Apache License. See project link for details.
The text of each license is the standard Apache 2.0 license.

mvnw files from https://github.com/apache/maven-wrapper Apache 2.0
pkg/deps/testdata/maven/.mvn/wrapper/maven-wrapper.properties from https://github.com/apache/maven-wrapper Apache 2.0
2 changes: 1 addition & 1 deletion pkg/deps/jar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func copyJars(t *testing.T, pomFile, content string) ([]string, error) {
return nil, err
}

if err := dumpPomFile(pomFile, content); err != nil {
if err := writeFile(pomFile, content); err != nil {
return nil, err
}

Expand Down
62 changes: 48 additions & 14 deletions pkg/deps/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package deps_test

import (
"bufio"
"embed"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

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

Expand All @@ -47,7 +48,7 @@ func TestCanResolvePomFile(t *testing.T) {
}
}

func dumpPomFile(fileName, content string) error {
func writeFile(fileName, content string) error {
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return err
Expand All @@ -64,15 +65,40 @@ func dumpPomFile(fileName, content string) error {
return nil
}

func TestResolveMaven(t *testing.T) {
if _, err := exec.Command("mvn", "--version").Output(); err != nil {
logger.Log.Warnf("Failed to find mvn, the test `TestResolveMaven` was skipped")
return
}
func ensureDir(dirName string) error {
return os.MkdirAll(dirName, 0777)
}

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

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

pomFile := filepath.Join(t.TempDir(), "pom.xml")
fs.WalkDir(testAssets, base, 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))
if err := ensureDir(filepath.Dir(filename)); err != nil {
return err
}

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

return nil
})

pomFile := filepath.Join(tempDir, "pom.xml")

for _, test := range []struct {
pomContent string
Expand All @@ -82,11 +108,11 @@ func TestResolveMaven(t *testing.T) {
<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>
Expand All @@ -106,14 +132,22 @@ func TestResolveMaven(t *testing.T) {
<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>`, 107},
</project>`, 110},
} {
_ = dumpPomFile(pomFile, test.pomContent)
_ = writeFile(pomFile, test.pomContent)

config := deps.ConfigDeps{}
config.Finalize("")

if resolver.CanResolve(pomFile) {
report := deps.Report{}
if err := resolver.Resolve(pomFile, nil, &report); err != nil {
if err := resolver.Resolve(pomFile, &config, &report); err != nil {
t.Error(err)
return
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/deps/testdata/maven/.mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
Loading

0 comments on commit 3f618dd

Please sign in to comment.