Skip to content

Commit

Permalink
support for sub-module in maven build (#4950)
Browse files Browse the repository at this point in the history
* support for sub-module in maven build

* fixed test-cases

Signed-off-by: Vijayan T <[email protected]>

* fixed test-cases

Signed-off-by: Vijayan T <[email protected]>

* changed the function name

---------

Signed-off-by: Vijayan T <[email protected]>
Co-authored-by: Vyacheslav Starostin <[email protected]>
  • Loading branch information
vijayanjay and vstarostin authored Jun 19, 2024
1 parent c3ec3d6 commit 4827785
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 39 deletions.
79 changes: 65 additions & 14 deletions cmd/codeqlExecuteScan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -53,7 +54,10 @@ func TestGetMavenSettings(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", ProjectSettingsFile: "test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --settings=test.xml", params)
dir, _ := os.Getwd()
projectSettingsPath := filepath.Join(dir, "test.xml")
expectedCommand := fmt.Sprintf(" --settings=%s", projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("Skip Project Settings file in case already used", func(t *testing.T) {
Expand All @@ -67,84 +71,127 @@ func TestGetMavenSettings(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", GlobalSettingsFile: "global.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=global.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, "global.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s", globalSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("Project and Global Settings file", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", ProjectSettingsFile: "test.xml", GlobalSettingsFile: "global.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=global.xml --settings=test.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, "global.xml")
projectSettingsPath := filepath.Join(dir, "test.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s --settings=%s", globalSettingsPath, projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("ProjectSettingsFile https url", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", ProjectSettingsFile: "https://jenkins-sap-test.com/test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --settings=.pipeline/mavenProjectSettings.xml", params)
dir, _ := os.Getwd()
projectSettingsPath := filepath.Join(dir, ".pipeline/mavenProjectSettings.xml")
expectedCommand := fmt.Sprintf(" --settings=%s", projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("ProjectSettingsFile http url", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --settings=.pipeline/mavenProjectSettings.xml", params)
dir, _ := os.Getwd()
projectSettingsPath := filepath.Join(dir, ".pipeline/mavenProjectSettings.xml")
expectedCommand := fmt.Sprintf(" --settings=%s", projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("GlobalSettingsFile https url", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", GlobalSettingsFile: "https://jenkins-sap-test.com/test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, ".pipeline/mavenGlobalSettings.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s", globalSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("GlobalSettingsFile http url", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", GlobalSettingsFile: "http://jenkins-sap-test.com/test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, ".pipeline/mavenGlobalSettings.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s", globalSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("ProjectSettingsFile and GlobalSettingsFile https url", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", GlobalSettingsFile: "https://jenkins-sap-test.com/test.xml", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml --settings=.pipeline/mavenProjectSettings.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, ".pipeline/mavenGlobalSettings.xml")
projectSettingsPath := filepath.Join(dir, ".pipeline/mavenProjectSettings.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s --settings=%s", globalSettingsPath, projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("ProjectSettingsFile and GlobalSettingsFile http url", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", GlobalSettingsFile: "http://jenkins-sap-test.com/test.xml", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml --settings=.pipeline/mavenProjectSettings.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, ".pipeline/mavenGlobalSettings.xml")
projectSettingsPath := filepath.Join(dir, ".pipeline/mavenProjectSettings.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s --settings=%s", globalSettingsPath, projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("ProjectSettingsFile file and GlobalSettingsFile https url", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", GlobalSettingsFile: "https://jenkins-sap-test.com/test.xml", ProjectSettingsFile: "test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml --settings=test.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, ".pipeline/mavenGlobalSettings.xml")
projectSettingsPath := filepath.Join(dir, "test.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s --settings=%s", globalSettingsPath, projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("ProjectSettingsFile file and GlobalSettingsFile https url", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", GlobalSettingsFile: "http://jenkins-sap-test.com/test.xml", ProjectSettingsFile: "test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml --settings=test.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, ".pipeline/mavenGlobalSettings.xml")
projectSettingsPath := filepath.Join(dir, "test.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s --settings=%s", globalSettingsPath, projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("ProjectSettingsFile https url and GlobalSettingsFile file", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", GlobalSettingsFile: "global.xml", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=global.xml --settings=.pipeline/mavenProjectSettings.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, "global.xml")
projectSettingsPath := filepath.Join(dir, ".pipeline/mavenProjectSettings.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s --settings=%s", globalSettingsPath, projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})

t.Run("ProjectSettingsFile http url and GlobalSettingsFile file", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", GlobalSettingsFile: "global.xml", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
buildCmd := "mvn clean install"
params := getMavenSettings(buildCmd, &config, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, " --global-settings=global.xml --settings=.pipeline/mavenProjectSettings.xml", params)
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, "global.xml")
projectSettingsPath := filepath.Join(dir, ".pipeline/mavenProjectSettings.xml")
expectedCommand := fmt.Sprintf(" --global-settings=%s --settings=%s", globalSettingsPath, projectSettingsPath)
assert.Equal(t, expectedCommand, params)
})
}

Expand Down Expand Up @@ -196,7 +243,11 @@ func TestUpdateCmdFlag(t *testing.T) {
"--command": "mvn clean install",
}
updateCmdFlag(&config, customFlags, newCodeqlExecuteScanTestsUtils())
assert.Equal(t, "mvn clean install --global-settings=global.xml --settings=test.xml", customFlags["--command"])
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, "global.xml")
projectSettingsPath := filepath.Join(dir, "test.xml")
expectedCommand := fmt.Sprintf("mvn clean install --global-settings=%s --settings=%s", globalSettingsPath, projectSettingsPath)
assert.Equal(t, expectedCommand, customFlags["--command"])
assert.Equal(t, "", customFlags["-c"])
})

Expand Down
6 changes: 4 additions & 2 deletions cmd/detectExecuteScan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,10 @@ func TestRunDetect(t *testing.T) {
assert.Equal(t, ".", utilsMock.Dir, "Wrong execution directory used")
assert.Equal(t, "/bin/bash", utilsMock.Shell[0], "Bash shell expected")
absoluteLocalPath := string(os.PathSeparator) + filepath.Join("root_folder", ".pipeline", "local_repo")

expectedParam := "\"--detect.maven.build.command=--global-settings global-settings.xml --settings project-settings.xml -Dmaven.repo.local=" + absoluteLocalPath + "\""
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, "global-settings.xml")
projectSettingsPath := filepath.Join(dir, "project-settings.xml")
expectedParam := "\"--detect.maven.build.command=--global-settings " + globalSettingsPath + " --settings " + projectSettingsPath + " -Dmaven.repo.local=" + absoluteLocalPath + "\""
assert.Contains(t, utilsMock.Calls[0], expectedParam)
})

Expand Down
13 changes: 8 additions & 5 deletions cmd/nexusUpload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ package cmd
import (
"errors"
"fmt"
"github.com/SAP/jenkins-library/pkg/maven"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/nexus"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"path/filepath"
"strings"
"testing"

"github.com/SAP/jenkins-library/pkg/maven"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/nexus"
"github.com/stretchr/testify/assert"
)

type mockUtilsBundle struct {
Expand Down Expand Up @@ -679,9 +680,11 @@ func TestUploadMavenProjects(t *testing.T) {
assert.NoError(t, err, "expected Maven upload to work")

assert.Equal(t, 1, len(utils.Calls))
dir, _ := os.Getwd()
absoluteSettingsPath := filepath.Join(dir, settingsPath)
expectedParameters1 := []string{
"--settings",
settingsPath,
absoluteSettingsPath,
"-Durl=http://localhost:8081/repository/maven-releases/",
"-DgroupId=com.mycompany.app",
"-Dversion=1.0",
Expand Down
38 changes: 25 additions & 13 deletions pkg/maven/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ package maven

import (
"errors"
"github.com/SAP/jenkins-library/pkg/mock"
"os"
"path/filepath"

"github.com/SAP/jenkins-library/pkg/mock"

"net/http"
"testing"

Expand Down Expand Up @@ -78,7 +80,10 @@ func TestExecute(t *testing.T) {
Goals: []string{"flatten", "install"}, Defines: []string{"-Da=b"},
Flags: []string{"-q"}, LogSuccessfulMavenTransfers: true,
ReturnStdout: false}
expectedParameters := []string{"--global-settings", "anotherSettings.xml", "--settings", "settings.xml",
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, "anotherSettings.xml")
projectSettingsPath := filepath.Join(dir, "settings.xml")
expectedParameters := []string{"--global-settings", globalSettingsPath, "--settings", projectSettingsPath,
"-Dmaven.repo.local=.m2/", "--file", "pom.xml", "-q", "-Da=b", "--batch-mode",
"flatten", "install"}

Expand Down Expand Up @@ -115,9 +120,12 @@ func TestGetParameters(t *testing.T) {
t.Run("should resolve configured parameters and download the settings files", func(t *testing.T) {
utils := NewMockUtils(false)
opts := ExecuteOptions{PomPath: "pom.xml", GlobalSettingsFile: "https://mysettings.com", ProjectSettingsFile: "http://myprojectsettings.com", ReturnStdout: false}
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, ".pipeline", "mavenGlobalSettings.xml")
projectSettingsPath := filepath.Join(dir, ".pipeline", "mavenProjectSettings.xml")
expectedParameters := []string{
"--global-settings", ".pipeline/mavenGlobalSettings.xml",
"--settings", ".pipeline/mavenProjectSettings.xml",
"--global-settings", globalSettingsPath,
"--settings", projectSettingsPath,
"--file", "pom.xml",
"-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn",
"--batch-mode"}
Expand All @@ -139,9 +147,12 @@ func TestGetParameters(t *testing.T) {
utils.AddFile(".pipeline/mavenGlobalSettings.xml", []byte("dummyContent"))
utils.AddFile(".pipeline/mavenProjectSettings.xml", []byte("dummyContent"))
opts := ExecuteOptions{PomPath: "pom.xml", GlobalSettingsFile: "https://mysettings.com", ProjectSettingsFile: "http://myprojectsettings.com", ReturnStdout: false}
dir, _ := os.Getwd()
globalSettingsPath := filepath.Join(dir, ".pipeline", "mavenGlobalSettings.xml")
projectSettingsPath := filepath.Join(dir, ".pipeline", "mavenProjectSettings.xml")
expectedParameters := []string{
"--global-settings", ".pipeline/mavenGlobalSettings.xml",
"--settings", ".pipeline/mavenProjectSettings.xml",
"--global-settings", globalSettingsPath,
"--settings", projectSettingsPath,
"--file", "pom.xml",
"-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn",
"--batch-mode"}
Expand Down Expand Up @@ -207,16 +218,17 @@ func TestMavenInstall(t *testing.T) {

options := EvaluateOptions{}
options.ProjectSettingsFile = "settings.xml"
utils.StdoutReturn = map[string]string{"mvn --settings settings.xml --file pom.xml -Dexpression=project.build.finalName -DforceStdout -q -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate": "foo"}
dir, _ := os.Getwd()
projectSettingsPath := filepath.Join(dir, "settings.xml")
utils.StdoutReturn = map[string]string{"mvn --settings " + projectSettingsPath + " --file pom.xml -Dexpression=project.build.finalName -DforceStdout -q -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate": "foo"}
err := doInstallMavenArtifacts(&options, &utils)

assert.NoError(t, err)
if assert.Equal(t, 5, len(utils.Calls)) {
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", "settings.xml", "-Dflatten.mode=resolveCiFriendliesOnly", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "flatten:flatten"}}, utils.Calls[0])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", "settings.xml", "--file", "pom.xml", "-Dexpression=project.packaging", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, utils.Calls[1])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", "settings.xml", "--file", "pom.xml", "-Dexpression=project.build.finalName", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, utils.Calls[2])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", "settings.xml", "-Dfile=" + filepath.Join(".", "target", "foo.jar"), "-Dpackaging=jar", "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}}, utils.Calls[3])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", "settings.xml", "-Dfile=" + filepath.Join(".", "target", "foo.war"), "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}}, utils.Calls[4])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", projectSettingsPath, "-Dflatten.mode=resolveCiFriendliesOnly", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "flatten:flatten"}}, utils.Calls[0])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", projectSettingsPath, "--file", "pom.xml", "-Dexpression=project.packaging", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, utils.Calls[1])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", projectSettingsPath, "--file", "pom.xml", "-Dexpression=project.build.finalName", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, utils.Calls[2])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", projectSettingsPath, "-Dfile=" + filepath.Join(".", "target", "foo.jar"), "-Dpackaging=jar", "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}}, utils.Calls[3])
assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--settings", projectSettingsPath, "-Dfile=" + filepath.Join(".", "target", "foo.war"), "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}}, utils.Calls[4])
}
})

Expand Down
Loading

0 comments on commit 4827785

Please sign in to comment.