1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/version_test.go

65 lines
1.3 KiB
Go
Raw Normal View History

2019-10-31 14:57:29 +02:00
package cmd
import (
"bytes"
"github.com/stretchr/testify/assert"
2019-11-05 22:51:44 +02:00
"io"
"os"
"testing"
2019-10-31 14:57:29 +02:00
)
func TestVersion(t *testing.T) {
t.Run("versionAndTagInitialValues", func(t *testing.T) {
result := runVersionCommand(t, "", "")
assert.Contains(t, result, "commit: \"<n/a>\"")
assert.Contains(t, result, "tag: \"<n/a>\"")
})
t.Run("versionAndTagSet", func(t *testing.T) {
result := runVersionCommand(t, "16bafe", "v1.2.3")
assert.Contains(t, result, "commit: \"16bafe\"")
assert.Contains(t, result, "tag: \"v1.2.3\"")
})
}
func runVersionCommand(t *testing.T, commitID, tag string) string {
orig := os.Stdout
2019-11-05 22:51:44 +02:00
defer func() { os.Stdout = orig }()
2019-10-31 14:57:29 +02:00
2019-11-05 22:51:44 +02:00
r, w, e := os.Pipe()
2019-10-31 14:57:29 +02:00
if e != nil {
t.Error("Cannot setup pipes.")
}
os.Stdout = w
//
// needs to be set in the free wild by the build process:
// go build -ldflags "-X github.com/SAP/jenkins-library/cmd.GitCommit=${GIT_COMMIT} -X github.com/SAP/jenkins-library/cmd.GitTag=${GIT_TAG}"
2019-11-05 22:51:44 +02:00
if len(commitID) > 0 {
GitCommit = commitID
}
if len(tag) > 0 {
GitTag = tag
}
2019-10-31 14:57:29 +02:00
defer func() { GitCommit = ""; GitTag = "" }()
//
//
var myVersionOptions versionOptions
e = version(myVersionOptions)
if e != nil {
t.Error("Version command failed.")
}
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
2019-11-05 22:51:44 +02:00
}