2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2020-12-21 18:13:16 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-12-15 17:20:01 +02:00
|
|
|
piperGithub "github.com/SAP/jenkins-library/pkg/github"
|
2021-03-19 14:04:30 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
2022-12-15 17:20:01 +02:00
|
|
|
github "github.com/google/go-github/v45/github"
|
2020-12-21 18:13:16 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-01-21 11:52:17 +02:00
|
|
|
)
|
2021-11-08 15:54:39 +02:00
|
|
|
|
2022-12-15 17:20:01 +02:00
|
|
|
func TestGetChunk(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
chunkSize int
|
|
|
|
largeString string
|
|
|
|
expectedChunks []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "large string",
|
|
|
|
largeString: `The quick
|
|
|
|
brown fox jumps
|
|
|
|
over
|
|
|
|
the lazy dog
|
|
|
|
`,
|
|
|
|
chunkSize: 12,
|
|
|
|
expectedChunks: []string{"The quick\nbr", "own fox jump", "s\nover\nthe l", "azy dog\n"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "small string",
|
|
|
|
largeString: `small`,
|
|
|
|
chunkSize: 12,
|
|
|
|
expectedChunks: []string{"small"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exact size",
|
|
|
|
largeString: `exact size12`,
|
|
|
|
chunkSize: 12,
|
|
|
|
expectedChunks: []string{"exact size12"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty string",
|
|
|
|
largeString: ``,
|
|
|
|
chunkSize: 12,
|
|
|
|
expectedChunks: []string{""},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
chunks := getChunks([]rune(test.largeString), test.chunkSize)
|
|
|
|
assert.ElementsMatch(t, test.expectedChunks, chunks)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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"},
|
2022-12-15 17:20:01 +02:00
|
|
|
ChunkSize: 100,
|
2020-12-21 18:13:16 +02:00
|
|
|
}
|
2022-01-21 11:52:17 +02:00
|
|
|
options := piperGithub.CreateIssueOptions{}
|
2022-12-15 17:20:01 +02:00
|
|
|
resultChunks := []string{}
|
|
|
|
createIssue := func(options *piperGithub.CreateIssueOptions) (*github.Issue, error) {
|
|
|
|
resultChunks = append(resultChunks, string(options.Body))
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-12-21 18:13:16 +02:00
|
|
|
|
|
|
|
// test
|
2022-12-15 17:20:01 +02:00
|
|
|
err := runGithubCreateIssue(&config, nil, &options, &filesMock, createIssue)
|
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, config.Title, options.Title)
|
|
|
|
assert.Equal(t, config.Assignees, options.Assignees)
|
|
|
|
assert.Equal(t, config.UpdateExisting, options.UpdateExisting)
|
2022-12-15 17:20:01 +02:00
|
|
|
assert.ElementsMatch(t, resultChunks, []string{string(config.Body)})
|
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"},
|
2022-12-15 17:20:01 +02:00
|
|
|
ChunkSize: 100,
|
2021-03-19 14:04:30 +02:00
|
|
|
}
|
2022-01-21 11:52:17 +02:00
|
|
|
options := piperGithub.CreateIssueOptions{}
|
2022-12-15 17:20:01 +02:00
|
|
|
resultChunks := []string{}
|
|
|
|
createIssue := func(options *piperGithub.CreateIssueOptions) (*github.Issue, error) {
|
|
|
|
resultChunks = append(resultChunks, string(options.Body))
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-03-19 14:04:30 +02:00
|
|
|
// test
|
2022-12-15 17:20:01 +02:00
|
|
|
err := runGithubCreateIssue(&config, nil, &options, &filesMock, createIssue)
|
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, config.Title, options.Title)
|
|
|
|
assert.Equal(t, config.Assignees, options.Assignees)
|
|
|
|
assert.Equal(t, config.UpdateExisting, options.UpdateExisting)
|
2022-12-15 17:20:01 +02:00
|
|
|
assert.ElementsMatch(t, resultChunks, []string{"Test markdown"})
|
2021-03-19 14:04:30 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Error - missing issue body", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
filesMock := mock.FilesMock{}
|
2022-12-15 17:20:01 +02:00
|
|
|
config := githubCreateIssueOptions{ChunkSize: 100}
|
2022-01-21 11:52:17 +02:00
|
|
|
options := piperGithub.CreateIssueOptions{}
|
2022-12-15 17:20:01 +02:00
|
|
|
resultChunks := []string{}
|
|
|
|
createIssue := func(options *piperGithub.CreateIssueOptions) (*github.Issue, error) {
|
|
|
|
resultChunks = append(resultChunks, string(options.Body))
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-12-21 18:13:16 +02:00
|
|
|
// test
|
2022-12-15 17:20:01 +02:00
|
|
|
err := runGithubCreateIssue(&config, nil, &options, &filesMock, createIssue)
|
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
|
|
|
}
|