| skipped 14 lines |
15 | 15 | | package raw |
16 | 16 | | |
17 | 17 | | import ( |
| 18 | + | "errors" |
18 | 19 | | "testing" |
19 | 20 | | |
| 21 | + | "github.com/golang/mock/gomock" |
20 | 22 | | "github.com/google/go-cmp/cmp" |
21 | 23 | | |
| 24 | + | "github.com/ossf/scorecard/v4/checker" |
22 | 25 | | "github.com/ossf/scorecard/v4/clients" |
| 26 | + | mockrepo "github.com/ossf/scorecard/v4/clients/mockclients" |
23 | 27 | | ) |
24 | 28 | | |
25 | | - | var branch = "master" |
| 29 | + | var ( |
| 30 | + | errBPTest = errors.New("test error") |
| 31 | + | defaultBranchName = "default" |
| 32 | + | releaseBranchName = "release-branch" |
| 33 | + | mainBranchName = "main" |
| 34 | + | ) |
26 | 35 | | |
27 | | - | func Test_getBranchName(t *testing.T) { |
28 | | - | t.Parallel() |
29 | | - | type args struct { |
30 | | - | branch *clients.BranchRef |
| 36 | + | // nolint: govet |
| 37 | + | type branchArg struct { |
| 38 | + | err error |
| 39 | + | name string |
| 40 | + | branchRef *clients.BranchRef |
| 41 | + | defaultBranch bool |
| 42 | + | } |
| 43 | + | |
| 44 | + | type branchesArg []branchArg |
| 45 | + | |
| 46 | + | func (ba branchesArg) getDefaultBranch() (*clients.BranchRef, error) { |
| 47 | + | for _, branch := range ba { |
| 48 | + | if branch.defaultBranch { |
| 49 | + | return branch.branchRef, branch.err |
| 50 | + | } |
31 | 51 | | } |
| 52 | + | return nil, nil |
| 53 | + | } |
| 54 | + | |
| 55 | + | func (ba branchesArg) getBranch(b string) (*clients.BranchRef, error) { |
| 56 | + | for _, branch := range ba { |
| 57 | + | if branch.name == b { |
| 58 | + | return branch.branchRef, branch.err |
| 59 | + | } |
| 60 | + | } |
| 61 | + | return nil, nil |
| 62 | + | } |
| 63 | + | |
| 64 | + | func TestBranchProtection(t *testing.T) { |
| 65 | + | t.Parallel() |
| 66 | + | // nolint: govet |
32 | 67 | | tests := []struct { |
33 | | - | name string |
34 | | - | args args |
35 | | - | want string |
| 68 | + | name string |
| 69 | + | branches branchesArg |
| 70 | + | releases []clients.Release |
| 71 | + | releasesErr error |
| 72 | + | want checker.BranchProtectionsData |
| 73 | + | wantErr error |
36 | 74 | | }{ |
37 | 75 | | { |
38 | | - | name: "simple", |
39 | | - | args: args{ |
40 | | - | branch: &clients.BranchRef{ |
41 | | - | Name: &branch, |
| 76 | + | name: "default-branch-err", |
| 77 | + | branches: branchesArg{ |
| 78 | + | { |
| 79 | + | name: defaultBranchName, |
| 80 | + | err: errBPTest, |
42 | 81 | | }, |
43 | 82 | | }, |
44 | | - | want: master, |
45 | 83 | | }, |
46 | 84 | | { |
47 | | - | name: "empty name", |
48 | | - | args: args{ |
49 | | - | branch: &clients.BranchRef{}, |
| 85 | + | name: "null-default-branch-only", |
| 86 | + | branches: branchesArg{ |
| 87 | + | { |
| 88 | + | name: defaultBranchName, |
| 89 | + | defaultBranch: true, |
| 90 | + | branchRef: nil, |
| 91 | + | }, |
50 | 92 | | }, |
51 | | - | want: "", |
52 | 93 | | }, |
53 | | - | } |
54 | | - | for _, tt := range tests { |
55 | | - | tt := tt |
56 | | - | t.Run(tt.name, func(t *testing.T) { |
57 | | - | t.Parallel() |
58 | | - | if got := getBranchName(tt.args.branch); got != tt.want { |
59 | | - | t.Errorf("getBranchName() = %v, want %v", got, tt.want) |
60 | | - | } |
61 | | - | }) |
62 | | - | } |
63 | | - | } |
64 | | - | |
65 | | - | func Test_getBranchMapFrom(t *testing.T) { |
66 | | - | t.Parallel() |
67 | | - | type args struct { |
68 | | - | branches []*clients.BranchRef |
69 | | - | } |
70 | | - | //nolint |
71 | | - | tests := []struct { |
72 | | - | name string |
73 | | - | args args |
74 | | - | want branchMap |
75 | | - | }{ |
76 | 94 | | { |
77 | | - | name: "simple", |
78 | | - | args: args{ |
79 | | - | branches: []*clients.BranchRef{ |
| 95 | + | name: "default-branch-only", |
| 96 | + | branches: branchesArg{ |
| 97 | + | { |
| 98 | + | name: defaultBranchName, |
| 99 | + | defaultBranch: true, |
| 100 | + | branchRef: &clients.BranchRef{ |
| 101 | + | Name: &defaultBranchName, |
| 102 | + | }, |
| 103 | + | }, |
| 104 | + | }, |
| 105 | + | want: checker.BranchProtectionsData{ |
| 106 | + | Branches: []clients.BranchRef{ |
80 | 107 | | { |
81 | | - | Name: &branch, |
| 108 | + | Name: &defaultBranchName, |
82 | 109 | | }, |
83 | 110 | | }, |
84 | 111 | | }, |
85 | | - | want: branchMap{ |
86 | | - | master: &clients.BranchRef{ |
87 | | - | Name: &branch, |
| 112 | + | }, |
| 113 | + | { |
| 114 | + | name: "list-releases-error", |
| 115 | + | releasesErr: errBPTest, |
| 116 | + | wantErr: errBPTest, |
| 117 | + | }, |
| 118 | + | { |
| 119 | + | name: "no-releases", |
| 120 | + | }, |
| 121 | + | { |
| 122 | + | name: "empty-targetcommitish", |
| 123 | + | releases: []clients.Release{ |
| 124 | + | { |
| 125 | + | TargetCommitish: "", |
88 | 126 | | }, |
89 | 127 | | }, |
| 128 | + | wantErr: errInternalCommitishNil, |
90 | 129 | | }, |
91 | | - | } |
92 | | - | for _, tt := range tests { |
93 | | - | tt := tt |
94 | | - | t.Run(tt.name, func(t *testing.T) { |
95 | | - | t.Parallel() |
96 | | - | if got := getBranchMapFrom(tt.args.branches); !cmp.Equal(got, tt.want) { |
97 | | - | t.Errorf("getBranchMapFrom() = %v, want %v", got, tt.want) |
98 | | - | } |
99 | | - | }) |
100 | | - | } |
101 | | - | } |
102 | | - | |
103 | | - | func Test_branchMap_getBranchByName(t *testing.T) { |
104 | | - | main := "main" |
105 | | - | t.Parallel() |
106 | | - | type args struct { |
107 | | - | name string |
108 | | - | } |
109 | | - | //nolint |
110 | | - | tests := []struct { |
111 | | - | name string |
112 | | - | b branchMap |
113 | | - | args args |
114 | | - | want *clients.BranchRef |
115 | | - | wantErr bool |
116 | | - | }{ |
117 | 130 | | { |
118 | | - | name: "simple", |
119 | | - | b: branchMap{ |
120 | | - | master: &clients.BranchRef{ |
121 | | - | Name: &branch, |
| 131 | + | name: "release-branch-err", |
| 132 | + | releases: []clients.Release{ |
| 133 | + | { |
| 134 | + | TargetCommitish: releaseBranchName, |
122 | 135 | | }, |
123 | 136 | | }, |
124 | | - | args: args{ |
125 | | - | name: master, |
| 137 | + | branches: branchesArg{ |
| 138 | + | { |
| 139 | + | name: releaseBranchName, |
| 140 | + | err: errBPTest, |
| 141 | + | }, |
126 | 142 | | }, |
127 | | - | want: &clients.BranchRef{ |
128 | | - | Name: &branch, |
| 143 | + | wantErr: errBPTest, |
| 144 | + | }, |
| 145 | + | { |
| 146 | + | name: "nil-release-branch", |
| 147 | + | releases: []clients.Release{ |
| 148 | + | { |
| 149 | + | TargetCommitish: releaseBranchName, |
| 150 | + | }, |
| 151 | + | }, |
| 152 | + | branches: branchesArg{ |
| 153 | + | { |
| 154 | + | name: releaseBranchName, |
| 155 | + | branchRef: nil, |
| 156 | + | }, |
129 | 157 | | }, |
130 | 158 | | }, |
131 | 159 | | { |
132 | | - | name: "main", |
133 | | - | b: branchMap{ |
134 | | - | master: &clients.BranchRef{ |
135 | | - | Name: &main, |
| 160 | + | name: "add-release-branch", |
| 161 | + | releases: []clients.Release{ |
| 162 | + | { |
| 163 | + | TargetCommitish: releaseBranchName, |
136 | 164 | | }, |
137 | | - | main: &clients.BranchRef{ |
138 | | - | Name: &main, |
| 165 | + | }, |
| 166 | + | branches: branchesArg{ |
| 167 | + | { |
| 168 | + | name: releaseBranchName, |
| 169 | + | branchRef: &clients.BranchRef{ |
| 170 | + | Name: &releaseBranchName, |
| 171 | + | }, |
139 | 172 | | }, |
140 | 173 | | }, |
141 | | - | args: args{ |
142 | | - | name: "main", |
| 174 | + | want: checker.BranchProtectionsData{ |
| 175 | + | Branches: []clients.BranchRef{ |
| 176 | + | { |
| 177 | + | Name: &releaseBranchName, |
| 178 | + | }, |
| 179 | + | }, |
| 180 | + | }, |
| 181 | + | }, |
| 182 | + | { |
| 183 | + | name: "master-to-main-redirect", |
| 184 | + | releases: []clients.Release{ |
| 185 | + | { |
| 186 | + | TargetCommitish: "master", |
| 187 | + | }, |
| 188 | + | }, |
| 189 | + | branches: branchesArg{ |
| 190 | + | { |
| 191 | + | name: mainBranchName, |
| 192 | + | branchRef: &clients.BranchRef{ |
| 193 | + | Name: &mainBranchName, |
| 194 | + | }, |
| 195 | + | }, |
143 | 196 | | }, |
144 | | - | want: &clients.BranchRef{ |
145 | | - | Name: &main, |
| 197 | + | want: checker.BranchProtectionsData{ |
| 198 | + | Branches: []clients.BranchRef{ |
| 199 | + | { |
| 200 | + | Name: &mainBranchName, |
| 201 | + | }, |
| 202 | + | }, |
146 | 203 | | }, |
147 | 204 | | }, |
148 | 205 | | { |
149 | | - | name: "not found", |
150 | | - | b: branchMap{ |
151 | | - | master: &clients.BranchRef{ |
152 | | - | Name: &branch, |
| 206 | + | name: "default-and-release-branches", |
| 207 | + | releases: []clients.Release{ |
| 208 | + | { |
| 209 | + | TargetCommitish: releaseBranchName, |
153 | 210 | | }, |
154 | 211 | | }, |
155 | | - | args: args{ |
156 | | - | name: "not-found", |
| 212 | + | branches: branchesArg{ |
| 213 | + | { |
| 214 | + | name: defaultBranchName, |
| 215 | + | defaultBranch: true, |
| 216 | + | branchRef: &clients.BranchRef{ |
| 217 | + | Name: &defaultBranchName, |
| 218 | + | }, |
| 219 | + | }, |
| 220 | + | { |
| 221 | + | name: releaseBranchName, |
| 222 | + | branchRef: &clients.BranchRef{ |
| 223 | + | Name: &releaseBranchName, |
| 224 | + | }, |
| 225 | + | }, |
| 226 | + | }, |
| 227 | + | want: checker.BranchProtectionsData{ |
| 228 | + | Branches: []clients.BranchRef{ |
| 229 | + | { |
| 230 | + | Name: &defaultBranchName, |
| 231 | + | }, |
| 232 | + | { |
| 233 | + | Name: &releaseBranchName, |
| 234 | + | }, |
| 235 | + | }, |
157 | 236 | | }, |
158 | | - | wantErr: true, |
159 | 237 | | }, |
| 238 | + | // TODO: Add tests for commitSHA regex matching. |
160 | 239 | | } |
161 | 240 | | for _, tt := range tests { |
162 | 241 | | tt := tt |
163 | 242 | | t.Run(tt.name, func(t *testing.T) { |
164 | 243 | | t.Parallel() |
165 | | - | got, err := tt.b.getBranchByName(tt.args.name) |
166 | | - | if (err != nil) != tt.wantErr { |
167 | | - | t.Errorf("branchMap.getBranchByName() error = %v, wantErr %v", err, tt.wantErr) |
168 | | - | return |
| 244 | + | ctrl := gomock.NewController(t) |
| 245 | + | mockRepoClient := mockrepo.NewMockRepoClient(ctrl) |
| 246 | + | mockRepoClient.EXPECT().GetDefaultBranch(). |
| 247 | + | AnyTimes().DoAndReturn(func() (*clients.BranchRef, error) { |
| 248 | + | return tt.branches.getDefaultBranch() |
| 249 | + | }) |
| 250 | + | mockRepoClient.EXPECT().GetBranch(gomock.Any()).AnyTimes(). |
| 251 | + | DoAndReturn(func(branch string) (*clients.BranchRef, error) { |
| 252 | + | return tt.branches.getBranch(branch) |
| 253 | + | }) |
| 254 | + | mockRepoClient.EXPECT().ListReleases().AnyTimes(). |
| 255 | + | DoAndReturn(func() ([]clients.Release, error) { |
| 256 | + | return tt.releases, tt.releasesErr |
| 257 | + | }) |
| 258 | + | |
| 259 | + | rawData, err := BranchProtection(mockRepoClient) |
| 260 | + | if !errors.Is(err, tt.wantErr) { |
| 261 | + | t.Errorf("failed. expected: %v, got: %v", tt.wantErr, err) |
| 262 | + | t.Fail() |
169 | 263 | | } |
170 | | - | if !cmp.Equal(got, tt.want) { |
171 | | - | t.Errorf("branchMap.getBranchByName() = %v, want %v", got, tt.want) |
| 264 | + | if !cmp.Equal(rawData, tt.want) { |
| 265 | + | t.Errorf("failed. expected: %v, got: %v", tt.want, rawData) |
| 266 | + | t.Fail() |
172 | 267 | | } |
173 | 268 | | }) |
174 | 269 | | } |
| skipped 2 lines |