github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/transportRequestDocIDFromGit_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "errors" 8 "github.com/stretchr/testify/assert" 9 "testing" 10 ) 11 12 func TestTrGitGetChangeDocumentID(t *testing.T) { 13 t.Parallel() 14 15 t.Run("good", func(t *testing.T) { 16 t.Parallel() 17 18 t.Run("getChangeDocumentID", func(t *testing.T) { 19 configMock := newCdIDConfigMock() 20 21 id, err := getChangeDocumentID(configMock.config, &transportRequestUtilsMock{cdID: "56781234"}) 22 23 if assert.NoError(t, err) { 24 assert.Equal(t, id, "56781234") 25 } 26 }) 27 t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) { 28 configMock := newCdIDConfigMock() 29 cpe := &transportRequestDocIDFromGitCommonPipelineEnvironment{} 30 31 err := runTransportRequestDocIDFromGit(configMock.config, nil, &transportRequestUtilsMock{cdID: "56781234"}, cpe) 32 33 if assert.NoError(t, err) { 34 assert.Equal(t, cpe.custom.changeDocumentID, "56781234") 35 } 36 }) 37 38 }) 39 t.Run("bad", func(t *testing.T) { 40 t.Parallel() 41 42 t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) { 43 configMock := newCdIDConfigMock() 44 cpe := &transportRequestDocIDFromGitCommonPipelineEnvironment{} 45 46 err := runTransportRequestDocIDFromGit(configMock.config, nil, &transportRequestUtilsMock{err: errors.New("fail")}, cpe) 47 48 assert.EqualError(t, err, "fail") 49 }) 50 51 }) 52 } 53 54 type cdIDConfigMock struct { 55 config *transportRequestDocIDFromGitOptions 56 } 57 58 func newCdIDConfigMock() *cdIDConfigMock { 59 return &cdIDConfigMock{ 60 config: &transportRequestDocIDFromGitOptions{ 61 GitFrom: "origin/master", 62 GitTo: "HEAD", 63 ChangeDocumentLabel: "ChangeDocument", 64 }, 65 } 66 }