github.com/SAP/jenkins-library@v1.362.0/cmd/githubCreateIssue_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"testing"
     8  
     9  	piperGithub "github.com/SAP/jenkins-library/pkg/github"
    10  	"github.com/SAP/jenkins-library/pkg/mock"
    11  	github "github.com/google/go-github/v45/github"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestGetChunk(t *testing.T) {
    16  	tests := []struct {
    17  		name           string
    18  		chunkSize      int
    19  		largeString    string
    20  		expectedChunks []string
    21  	}{
    22  		{
    23  			name: "large string",
    24  			largeString: `The quick
    25  brown fox jumps
    26  over
    27  the lazy dog
    28  `,
    29  			chunkSize:      12,
    30  			expectedChunks: []string{"The quick\nbr", "own fox jump", "s\nover\nthe l", "azy dog\n"},
    31  		},
    32  		{
    33  			name:           "small string",
    34  			largeString:    `small`,
    35  			chunkSize:      12,
    36  			expectedChunks: []string{"small"},
    37  		},
    38  		{
    39  			name:           "exact size",
    40  			largeString:    `exact size12`,
    41  			chunkSize:      12,
    42  			expectedChunks: []string{"exact size12"},
    43  		},
    44  		{
    45  			name:           "empty string",
    46  			largeString:    ``,
    47  			chunkSize:      12,
    48  			expectedChunks: []string{""},
    49  		},
    50  	}
    51  	for _, test := range tests {
    52  		test := test
    53  		t.Run(test.name, func(t *testing.T) {
    54  			chunks := getChunks([]rune(test.largeString), test.chunkSize)
    55  			assert.ElementsMatch(t, test.expectedChunks, chunks)
    56  		})
    57  	}
    58  }
    59  
    60  func TestTransformConfig(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	t.Run("Success", func(t *testing.T) {
    64  		// init
    65  		filesMock := mock.FilesMock{}
    66  		config := githubCreateIssueOptions{
    67  			Owner:      "TEST",
    68  			Repository: "test",
    69  			Body:       "This is my test body",
    70  			Title:      "This is my title",
    71  			Assignees:  []string{"userIdOne", "userIdTwo"},
    72  			ChunkSize:  100,
    73  		}
    74  		options := piperGithub.CreateIssueOptions{}
    75  		resultChunks := []string{}
    76  		createIssue := func(options *piperGithub.CreateIssueOptions) (*github.Issue, error) {
    77  			resultChunks = append(resultChunks, string(options.Body))
    78  			return nil, nil
    79  		}
    80  
    81  		// test
    82  		err := runGithubCreateIssue(&config, nil, &options, &filesMock, createIssue)
    83  
    84  		// assert
    85  		assert.NoError(t, err)
    86  		assert.Equal(t, config.Token, options.Token)
    87  		assert.Equal(t, config.APIURL, options.APIURL)
    88  		assert.Equal(t, config.Owner, options.Owner)
    89  		assert.Equal(t, config.Repository, options.Repository)
    90  		assert.Equal(t, config.Title, options.Title)
    91  		assert.Equal(t, config.Assignees, options.Assignees)
    92  		assert.Equal(t, config.UpdateExisting, options.UpdateExisting)
    93  		assert.ElementsMatch(t, resultChunks, []string{string(config.Body)})
    94  	})
    95  
    96  	t.Run("Success bodyFilePath", func(t *testing.T) {
    97  		// init
    98  		filesMock := mock.FilesMock{}
    99  		filesMock.AddFile("test.md", []byte("Test markdown"))
   100  		config := githubCreateIssueOptions{
   101  			Owner:        "TEST",
   102  			Repository:   "test",
   103  			BodyFilePath: "test.md",
   104  			Title:        "This is my title",
   105  			Assignees:    []string{"userIdOne", "userIdTwo"},
   106  			ChunkSize:    100,
   107  		}
   108  		options := piperGithub.CreateIssueOptions{}
   109  		resultChunks := []string{}
   110  		createIssue := func(options *piperGithub.CreateIssueOptions) (*github.Issue, error) {
   111  			resultChunks = append(resultChunks, string(options.Body))
   112  			return nil, nil
   113  		}
   114  		// test
   115  		err := runGithubCreateIssue(&config, nil, &options, &filesMock, createIssue)
   116  
   117  		// assert
   118  		assert.NoError(t, err)
   119  		assert.Equal(t, config.Token, options.Token)
   120  		assert.Equal(t, config.APIURL, options.APIURL)
   121  		assert.Equal(t, config.Owner, options.Owner)
   122  		assert.Equal(t, config.Repository, options.Repository)
   123  		assert.Equal(t, config.Title, options.Title)
   124  		assert.Equal(t, config.Assignees, options.Assignees)
   125  		assert.Equal(t, config.UpdateExisting, options.UpdateExisting)
   126  		assert.ElementsMatch(t, resultChunks, []string{"Test markdown"})
   127  	})
   128  
   129  	t.Run("Error - missing issue body", func(t *testing.T) {
   130  		// init
   131  		filesMock := mock.FilesMock{}
   132  		config := githubCreateIssueOptions{ChunkSize: 100}
   133  		options := piperGithub.CreateIssueOptions{}
   134  		resultChunks := []string{}
   135  		createIssue := func(options *piperGithub.CreateIssueOptions) (*github.Issue, error) {
   136  			resultChunks = append(resultChunks, string(options.Body))
   137  			return nil, nil
   138  		}
   139  		// test
   140  		err := runGithubCreateIssue(&config, nil, &options, &filesMock, createIssue)
   141  
   142  		// assert
   143  		assert.EqualError(t, err, "either parameter `body` or parameter `bodyFilePath` is required")
   144  	})
   145  }