github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/services/pull_request/github_test.go (about)

     1  package pull_request
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-github/v35/github"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func toPtr(s string) *string {
    11  	return &s
    12  }
    13  
    14  func TestContainLabels(t *testing.T) {
    15  	cases := []struct {
    16  		Name       string
    17  		Labels     []string
    18  		PullLabels []*github.Label
    19  		Expect     bool
    20  	}{
    21  		{
    22  			Name:   "Match labels",
    23  			Labels: []string{"label1", "label2"},
    24  			PullLabels: []*github.Label{
    25  				{Name: toPtr("label1")},
    26  				{Name: toPtr("label2")},
    27  				{Name: toPtr("label3")},
    28  			},
    29  			Expect: true,
    30  		},
    31  		{
    32  			Name:   "Not match labels",
    33  			Labels: []string{"label1", "label4"},
    34  			PullLabels: []*github.Label{
    35  				{Name: toPtr("label1")},
    36  				{Name: toPtr("label2")},
    37  				{Name: toPtr("label3")},
    38  			},
    39  			Expect: false,
    40  		},
    41  		{
    42  			Name:   "No specify",
    43  			Labels: []string{},
    44  			PullLabels: []*github.Label{
    45  				{Name: toPtr("label1")},
    46  				{Name: toPtr("label2")},
    47  				{Name: toPtr("label3")},
    48  			},
    49  			Expect: true,
    50  		},
    51  	}
    52  
    53  	for _, c := range cases {
    54  		t.Run(c.Name, func(t *testing.T) {
    55  			if got := containLabels(c.Labels, c.PullLabels); got != c.Expect {
    56  				t.Errorf("expect: %v, got: %v", c.Expect, got)
    57  			}
    58  		})
    59  	}
    60  }
    61  
    62  func TestGetGitHubPRLabelNames(t *testing.T) {
    63  	Tests := []struct {
    64  		Name           string
    65  		PullLabels     []*github.Label
    66  		ExpectedResult []string
    67  	}{
    68  		{
    69  			Name: "PR has labels",
    70  			PullLabels: []*github.Label{
    71  				{Name: toPtr("label1")},
    72  				{Name: toPtr("label2")},
    73  				{Name: toPtr("label3")},
    74  			},
    75  			ExpectedResult: []string{"label1", "label2", "label3"},
    76  		},
    77  		{
    78  			Name:           "PR does not have labels",
    79  			PullLabels:     []*github.Label{},
    80  			ExpectedResult: nil,
    81  		},
    82  	}
    83  	for _, test := range Tests {
    84  		t.Run(test.Name, func(t *testing.T) {
    85  			labels := getGithubPRLabelNames(test.PullLabels)
    86  			assert.Equal(t, test.ExpectedResult, labels)
    87  		})
    88  	}
    89  }