2020-12-21 18:13:16 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-03-19 14:04:30 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
|
2020-12-21 18:13:16 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-11-08 15:54:39 +02:00
|
|
|
|
2022-01-21 11:52:17 +02:00
|
|
|
piperGithub "github.com/SAP/jenkins-library/pkg/github"
|
|
|
|
)
|
2021-11-08 15:54:39 +02:00
|
|
|
|
2022-01-21 11:52:17 +02:00
|
|
|
func TestTransformConfig(t *testing.T) {
|
2020-12-21 18:13:16 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
|
|
// init
|
2021-03-19 14:04:30 +02:00
|
|
|
filesMock := mock.FilesMock{}
|
2020-12-21 18:13:16 +02:00
|
|
|
config := githubCreateIssueOptions{
|
|
|
|
Owner: "TEST",
|
|
|
|
Repository: "test",
|
|
|
|
Body: "This is my test body",
|
|
|
|
Title: "This is my title",
|
2021-10-04 12:33:40 +02:00
|
|
|
Assignees: []string{"userIdOne", "userIdTwo"},
|
2020-12-21 18:13:16 +02:00
|
|
|
}
|
2022-01-21 11:52:17 +02:00
|
|
|
options := piperGithub.CreateIssueOptions{}
|
2020-12-21 18:13:16 +02:00
|
|
|
|
|
|
|
// test
|
2022-01-21 11:52:17 +02:00
|
|
|
err := transformConfig(&config, &options, filesMock.FileRead)
|
2020-12-21 18:13:16 +02:00
|
|
|
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
2022-01-21 11:52:17 +02:00
|
|
|
assert.Equal(t, config.Token, options.Token)
|
|
|
|
assert.Equal(t, config.APIURL, options.APIURL)
|
|
|
|
assert.Equal(t, config.Owner, options.Owner)
|
|
|
|
assert.Equal(t, config.Repository, options.Repository)
|
|
|
|
assert.Equal(t, []byte(config.Body), options.Body)
|
|
|
|
assert.Equal(t, config.Title, options.Title)
|
|
|
|
assert.Equal(t, config.Assignees, options.Assignees)
|
|
|
|
assert.Equal(t, config.UpdateExisting, options.UpdateExisting)
|
2020-12-21 18:13:16 +02:00
|
|
|
})
|
|
|
|
|
2022-01-21 11:52:17 +02:00
|
|
|
t.Run("Success bodyFilePath", func(t *testing.T) {
|
2021-03-19 14:04:30 +02:00
|
|
|
// init
|
|
|
|
filesMock := mock.FilesMock{}
|
|
|
|
filesMock.AddFile("test.md", []byte("Test markdown"))
|
|
|
|
config := githubCreateIssueOptions{
|
|
|
|
Owner: "TEST",
|
|
|
|
Repository: "test",
|
|
|
|
BodyFilePath: "test.md",
|
|
|
|
Title: "This is my title",
|
2022-01-21 11:52:17 +02:00
|
|
|
Assignees: []string{"userIdOne", "userIdTwo"},
|
2021-03-19 14:04:30 +02:00
|
|
|
}
|
2022-01-21 11:52:17 +02:00
|
|
|
options := piperGithub.CreateIssueOptions{}
|
2021-03-19 14:04:30 +02:00
|
|
|
|
|
|
|
// test
|
2022-01-21 11:52:17 +02:00
|
|
|
err := transformConfig(&config, &options, filesMock.FileRead)
|
2021-11-08 15:54:39 +02:00
|
|
|
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
2022-01-21 11:52:17 +02:00
|
|
|
assert.Equal(t, config.Token, options.Token)
|
|
|
|
assert.Equal(t, config.APIURL, options.APIURL)
|
|
|
|
assert.Equal(t, config.Owner, options.Owner)
|
|
|
|
assert.Equal(t, config.Repository, options.Repository)
|
|
|
|
assert.Equal(t, []byte("Test markdown"), options.Body)
|
|
|
|
assert.Equal(t, config.Title, options.Title)
|
|
|
|
assert.Equal(t, config.Assignees, options.Assignees)
|
|
|
|
assert.Equal(t, config.UpdateExisting, options.UpdateExisting)
|
2021-03-19 14:04:30 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Error - missing issue body", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
filesMock := mock.FilesMock{}
|
2020-12-21 18:13:16 +02:00
|
|
|
config := githubCreateIssueOptions{}
|
2022-01-21 11:52:17 +02:00
|
|
|
options := piperGithub.CreateIssueOptions{}
|
2020-12-21 18:13:16 +02:00
|
|
|
|
|
|
|
// test
|
2022-01-21 11:52:17 +02:00
|
|
|
err := transformConfig(&config, &options, filesMock.FileRead)
|
2021-03-19 14:04:30 +02:00
|
|
|
|
|
|
|
// assert
|
|
|
|
assert.EqualError(t, err, "either parameter `body` or parameter `bodyFilePath` is required")
|
|
|
|
})
|
2020-12-21 18:13:16 +02:00
|
|
|
}
|