mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
ffc931aad1
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files. * Added unit build tag to all unit test files. * added to new unit test unit build tag * Update verify-go.yml * small fix --------- Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
//go:build unit
|
|
// +build unit
|
|
|
|
package mock_test
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
type ExecRunner interface {
|
|
SetDir(d string)
|
|
SetEnv(e []string)
|
|
Stdout(out io.Writer)
|
|
Stderr(err io.Writer)
|
|
RunExecutable(e string, p ...string) error
|
|
}
|
|
|
|
func getMavenVersion(runner ExecRunner) (string, error) {
|
|
output := bytes.Buffer{}
|
|
runner.Stdout(&output)
|
|
err := runner.RunExecutable("mvn", "--version")
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to run maven: %w", err)
|
|
}
|
|
logLines := strings.Split(output.String(), "\n")
|
|
if len(logLines) < 1 {
|
|
return "", fmt.Errorf("failed to obtain maven output")
|
|
}
|
|
return logLines[0], nil
|
|
}
|
|
|
|
func ExampleDockerExecRunner_RunExecutable() {
|
|
// getMavenVersion(runner ExecRunner) executes the command "mvn --version"
|
|
// and returns the command output as string
|
|
runner := command.Command{}
|
|
localMavenVersion, _ := getMavenVersion(&runner)
|
|
|
|
dockerRunner := mock.DockerExecRunner{Runner: &runner}
|
|
_ = dockerRunner.AddExecConfig("mvn", mock.DockerExecConfig{
|
|
Image: "maven:3.6.1-jdk-8",
|
|
})
|
|
|
|
dockerMavenVersion, _ := getMavenVersion(&dockerRunner)
|
|
|
|
fmt.Printf("Your local mvn version is %v, while the version in docker is %v", localMavenVersion, dockerMavenVersion)
|
|
}
|