1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/version_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* 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>
2023-05-03 21:02:11 +05:00

64 lines
1.2 KiB
Go

//go:build unit
// +build unit
package cmd
import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"os"
"testing"
)
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 = "" }()
//
//
version()
w.Close()
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
return buf.String()
}