1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/transportRequestReqIDFromGit_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

88 lines
2.0 KiB
Go

//go:build unit
// +build unit
package cmd
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
type transportRequestUtilsMock struct {
err error
trID string
cdID string
}
func (m *transportRequestUtilsMock) FindIDInRange(label, from, to string) (string, error) {
if m.err != nil {
return "", m.err
}
if strings.HasPrefix(label, "TransportRequest") {
return m.trID, nil
}
if strings.HasPrefix(label, "ChangeDocument") {
return m.cdID, nil
}
return "invalid", fmt.Errorf("invalid label passed: %s", label)
}
func TestTrGitGetTransportRequestID(t *testing.T) {
t.Parallel()
t.Run("good", func(t *testing.T) {
t.Parallel()
t.Run("getTransportRequestID", func(t *testing.T) {
configMock := newTrIDConfigMock()
id, err := getTransportRequestID(configMock.config, &transportRequestUtilsMock{trID: "43218765"})
if assert.NoError(t, err) {
assert.Equal(t, id, "43218765")
}
})
t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) {
configMock := newTrIDConfigMock()
cpe := &transportRequestReqIDFromGitCommonPipelineEnvironment{}
err := runTransportRequestReqIDFromGit(configMock.config, nil, &transportRequestUtilsMock{trID: "43218765"}, cpe)
if assert.NoError(t, err) {
assert.Equal(t, cpe.custom.transportRequestID, "43218765")
}
})
})
t.Run("bad", func(t *testing.T) {
t.Parallel()
t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) {
configMock := newTrIDConfigMock()
cpe := &transportRequestReqIDFromGitCommonPipelineEnvironment{}
err := runTransportRequestReqIDFromGit(configMock.config, nil, &transportRequestUtilsMock{err: errors.New("fail")}, cpe)
assert.EqualError(t, err, "fail")
})
})
}
type trIDConfigMock struct {
config *transportRequestReqIDFromGitOptions
}
func newTrIDConfigMock() *trIDConfigMock {
return &trIDConfigMock{
config: &transportRequestReqIDFromGitOptions{
GitFrom: "origin/master",
GitTo: "HEAD",
TransportRequestLabel: "TransportRequest",
},
}
}