github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/check-labels_test.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mungers 18 19 import ( 20 "reflect" 21 "runtime" 22 "testing" 23 24 "github.com/google/go-github/github" 25 ) 26 27 var ( 28 repoLabels = []*github.Label{ 29 {Name: stringPtr("team/abc"), Color: stringPtr("d4c5f9")}, 30 {Name: stringPtr("team/def"), Color: stringPtr("fef2c0")}, 31 {Name: stringPtr("team/ghi"), Color: stringPtr("c7def8")}, 32 {Name: stringPtr("label/rep"), Color: stringPtr("c7def8")}, 33 } 34 fileLabels = []*github.Label{ 35 {Name: stringPtr("release-note"), Color: stringPtr("d4c5f9")}, 36 {Name: stringPtr("dependency/rkt"), Color: stringPtr("fbfa04")}, 37 {Name: stringPtr("team/ghi"), Color: stringPtr("c7def8")}, 38 {Name: stringPtr("label/rep"), Color: stringPtr("def2c1")}, 39 } 40 ) 41 42 type LabelAccessorTest struct { 43 AddedLabels []*github.Label 44 RepoLabels []*github.Label 45 } 46 47 func (l *LabelAccessorTest) AddLabel(label *github.Label) error { 48 l.AddedLabels = append(l.AddedLabels, label) 49 return nil 50 } 51 52 func (l *LabelAccessorTest) GetLabels() ([]*github.Label, error) { 53 return l.RepoLabels, nil 54 } 55 56 func mockLabelReader() ([]byte, error) { 57 return []byte(`labels: 58 - name: release-note 59 color: d4c5f9 60 - name: dependency/rkt 61 color: fbfa04 62 - name: team/ghi 63 color: c7def8 64 - name: label/rep 65 color: def2c1`), nil 66 } 67 68 func makeCheckLabelsMunger() *CheckLabelsMunger { 69 return &CheckLabelsMunger{ 70 labelAccessor: &LabelAccessorTest{}, 71 readFunc: mockLabelReader, 72 } 73 } 74 75 func TestEachLoop(t *testing.T) { 76 runtime.GOMAXPROCS(runtime.NumCPU()) 77 tests := []struct { 78 name string 79 repoLabels []*github.Label 80 expected []*github.Label 81 }{ 82 { 83 name: "No labels in repository.", 84 repoLabels: []*github.Label{}, 85 expected: fileLabels, 86 }, 87 { 88 name: "Identical labels in repository and file.", 89 repoLabels: fileLabels, 90 expected: []*github.Label{}, 91 }, 92 { 93 name: "Adding label with the same name and a different color", 94 repoLabels: []*github.Label{repoLabels[3]}, 95 expected: fileLabels[0:3], 96 }, 97 { 98 name: "Adding new labels with existing labels in repository.", 99 repoLabels: repoLabels, 100 expected: fileLabels[0:2], 101 }, 102 } 103 104 for testNum, test := range tests { 105 c := makeCheckLabelsMunger() 106 l := &LabelAccessorTest{make([]*github.Label, 0, 0), test.repoLabels} 107 c.labelAccessor = l 108 c.EachLoop() 109 if len(l.AddedLabels) != len(test.expected) { 110 t.Errorf("%d:%s: len(expected):%d, len(l.AddedLabels):%d", testNum, test.name, len(test.expected), len(l.AddedLabels)) 111 } 112 for i := 0; i < len(test.expected); i++ { 113 if !reflect.DeepEqual(test.expected[i], l.AddedLabels[i]) { 114 t.Errorf("%d:%s: missing %v from AddedLabels", testNum, test.name, *test.expected[i].Name) 115 } 116 } 117 } 118 }