Skip to content

Commit c3bcfa7

Browse files
🌱 Unit tests for checks/raw/contributors.go (#2998)
- Add tests and fix casing for Contributors function in checks/raw/contributors_test.go Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
1 parent 157a509 commit c3bcfa7

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

checks/raw/contributors_test.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright 2023 OpenSSF Scorecard Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package raw
15+
16+
import (
17+
"testing"
18+
19+
"github.com/golang/mock/gomock"
20+
"github.com/google/go-cmp/cmp"
21+
22+
"github.com/ossf/scorecard/v4/clients"
23+
mockrepo "github.com/ossf/scorecard/v4/clients/mockclients"
24+
)
25+
26+
func TestCompanyContains(t *testing.T) {
27+
t.Parallel()
28+
testCases := []struct { //nolint:govet
29+
name string
30+
cs []string
31+
company string
32+
expected bool
33+
}{
34+
{
35+
name: "company is present",
36+
cs: []string{"Google", "Facebook", "OpenAI"},
37+
company: "OpenAI",
38+
expected: true,
39+
},
40+
{
41+
name: "company is not present",
42+
cs: []string{"Google", "Facebook", "OpenAI"},
43+
company: "Microsoft",
44+
expected: false,
45+
},
46+
{
47+
name: "empty slice",
48+
cs: []string{},
49+
company: "Microsoft",
50+
expected: false,
51+
},
52+
}
53+
54+
for _, tc := range testCases {
55+
tc := tc
56+
t.Run(tc.name, func(t *testing.T) {
57+
t.Parallel()
58+
result := companyContains(tc.cs, tc.company)
59+
if result != tc.expected {
60+
t.Errorf("expected %v, got %v", tc.expected, result)
61+
}
62+
})
63+
}
64+
}
65+
66+
func TestOrgContains(t *testing.T) {
67+
t.Parallel()
68+
testCases := []struct { //nolint:govet
69+
name string
70+
os []clients.User
71+
login string
72+
expected bool
73+
}{
74+
{
75+
name: "user is present",
76+
os: []clients.User{{Login: "alice"}, {Login: "bob"}, {Login: "charlie"}},
77+
login: "bob",
78+
expected: true,
79+
},
80+
{
81+
name: "user is not present",
82+
os: []clients.User{{Login: "alice"}, {Login: "bob"}, {Login: "charlie"}},
83+
login: "david",
84+
expected: false,
85+
},
86+
{
87+
name: "empty slice",
88+
os: []clients.User{},
89+
login: "alice",
90+
expected: false,
91+
},
92+
}
93+
94+
for _, tc := range testCases {
95+
tc := tc
96+
t.Run(tc.name, func(t *testing.T) {
97+
t.Parallel()
98+
result := orgContains(tc.os, tc.login)
99+
if result != tc.expected {
100+
t.Errorf("expected %v, got %v", tc.expected, result)
101+
}
102+
})
103+
}
104+
}
105+
106+
func TestContributors(t *testing.T) {
107+
ctrl := gomock.NewController(t)
108+
defer ctrl.Finish()
109+
110+
mockRepoClient := mockrepo.NewMockRepoClient(ctrl)
111+
contributors := []clients.User{
112+
{
113+
Login: "user1",
114+
NumContributions: 5,
115+
Companies: []string{"Company1", "Company2"},
116+
Organizations: []clients.User{
117+
{Login: "org1"},
118+
{Login: "org2"},
119+
},
120+
},
121+
{
122+
Login: "user2",
123+
NumContributions: 3,
124+
Companies: []string{"Company3", "Company4"},
125+
Organizations: []clients.User{
126+
{Login: "org3"},
127+
{Login: "org4"},
128+
},
129+
},
130+
}
131+
132+
mockRepoClient.EXPECT().ListContributors().Return(contributors, nil)
133+
134+
data, err := Contributors(mockRepoClient)
135+
if err != nil {
136+
t.Fatalf("unexpected error: %v", err)
137+
}
138+
139+
expectedUsers := []clients.User{
140+
{
141+
Login: "user1",
142+
NumContributions: 5,
143+
Companies: []string{"company1", "company2"},
144+
Organizations: []clients.User{
145+
{Login: "org1"},
146+
{Login: "org2"},
147+
},
148+
},
149+
{
150+
Login: "user2",
151+
NumContributions: 3,
152+
Companies: []string{"company3", "company4"},
153+
Organizations: []clients.User{
154+
{Login: "org3"},
155+
{Login: "org4"},
156+
},
157+
},
158+
}
159+
160+
if diff := cmp.Diff(expectedUsers, data.Users); diff != "" {
161+
t.Errorf("unexpected contributors data (-want +got):\n%s", diff)
162+
}
163+
}

0 commit comments

Comments
 (0)