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
2019-10-31 13:57:29 +01:00

61 lines
1.3 KiB
Go

package cmd
import (
"testing"
"os"
"bytes"
"io"
"github.com/stretchr/testify/assert"
)
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
defer func() {os.Stdout = orig}()
r,w,e := os.Pipe()
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}"
if len(commitID) > 0 { GitCommit = commitID; }
if len(tag) > 0 { GitTag = tag }
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()
}