github.com/SAP/jenkins-library@v1.362.0/cmd/transportRequestReqIDFromGit_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "errors" 8 "fmt" 9 "github.com/stretchr/testify/assert" 10 "strings" 11 "testing" 12 ) 13 14 type transportRequestUtilsMock struct { 15 err error 16 trID string 17 cdID string 18 } 19 20 func (m *transportRequestUtilsMock) FindIDInRange(label, from, to string) (string, error) { 21 if m.err != nil { 22 return "", m.err 23 } 24 if strings.HasPrefix(label, "TransportRequest") { 25 return m.trID, nil 26 } 27 if strings.HasPrefix(label, "ChangeDocument") { 28 return m.cdID, nil 29 } 30 31 return "invalid", fmt.Errorf("invalid label passed: %s", label) 32 } 33 34 func TestTrGitGetTransportRequestID(t *testing.T) { 35 t.Parallel() 36 37 t.Run("good", func(t *testing.T) { 38 t.Parallel() 39 40 t.Run("getTransportRequestID", func(t *testing.T) { 41 configMock := newTrIDConfigMock() 42 43 id, err := getTransportRequestID(configMock.config, &transportRequestUtilsMock{trID: "43218765"}) 44 45 if assert.NoError(t, err) { 46 assert.Equal(t, id, "43218765") 47 } 48 }) 49 t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) { 50 configMock := newTrIDConfigMock() 51 cpe := &transportRequestReqIDFromGitCommonPipelineEnvironment{} 52 53 err := runTransportRequestReqIDFromGit(configMock.config, nil, &transportRequestUtilsMock{trID: "43218765"}, cpe) 54 55 if assert.NoError(t, err) { 56 assert.Equal(t, cpe.custom.transportRequestID, "43218765") 57 } 58 }) 59 }) 60 t.Run("bad", func(t *testing.T) { 61 t.Parallel() 62 63 t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) { 64 configMock := newTrIDConfigMock() 65 cpe := &transportRequestReqIDFromGitCommonPipelineEnvironment{} 66 67 err := runTransportRequestReqIDFromGit(configMock.config, nil, &transportRequestUtilsMock{err: errors.New("fail")}, cpe) 68 69 assert.EqualError(t, err, "fail") 70 }) 71 72 }) 73 } 74 75 type trIDConfigMock struct { 76 config *transportRequestReqIDFromGitOptions 77 } 78 79 func newTrIDConfigMock() *trIDConfigMock { 80 return &trIDConfigMock{ 81 config: &transportRequestReqIDFromGitOptions{ 82 GitFrom: "origin/master", 83 GitTo: "HEAD", 84 TransportRequestLabel: "TransportRequest", 85 }, 86 } 87 }