-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_mock_test.go
More file actions
73 lines (66 loc) · 2.34 KB
/
client_mock_test.go
File metadata and controls
73 lines (66 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package scanner
import "context"
// MockGitHubClient implements GitHubClient with canned responses for testing.
// ListReposByAccount and ListReposByInstallation both return Repos/Err so
// tests don't have to care which listing path was taken.
type MockGitHubClient struct {
Repos []Repo
Err error
Tree map[string][]FileEntry // repo name -> file entries
TreeErr error // global tree error (used if TreeErrs is nil)
TreeErrs map[string]error // repo name -> per-repo tree error
Protection map[string]*BranchProtection // repo name -> classic branch protection
ProtectionErr error // global protection error (used if ProtectionErrs is nil)
ProtectionErrs map[string]error // repo name -> per-repo protection error
Rulesets map[string]*BranchProtection // repo name -> rulesets protection
RulesetsErr error // global rulesets error (used if RulesetsErrs is nil)
RulesetsErrs map[string]error // repo name -> per-repo rulesets error
}
func (m *MockGitHubClient) ListReposByAccount(ctx context.Context, name string) ([]Repo, error) {
return m.Repos, m.Err
}
func (m *MockGitHubClient) ListReposByInstallation(ctx context.Context) ([]Repo, error) {
return m.Repos, m.Err
}
func (m *MockGitHubClient) GetTree(ctx context.Context, owner, repo, branch string) ([]FileEntry, error) {
if m.TreeErrs != nil {
if err, ok := m.TreeErrs[repo]; ok {
return nil, err
}
}
if m.TreeErr != nil {
return nil, m.TreeErr
}
if m.Tree != nil {
return m.Tree[repo], nil
}
return nil, nil
}
func (m *MockGitHubClient) GetBranchProtection(ctx context.Context, owner, repo, branch string) (*BranchProtection, error) {
if m.ProtectionErrs != nil {
if err, ok := m.ProtectionErrs[repo]; ok {
return nil, err
}
}
if m.ProtectionErr != nil {
return nil, m.ProtectionErr
}
if m.Protection != nil {
return m.Protection[repo], nil
}
return nil, nil
}
func (m *MockGitHubClient) GetRulesets(ctx context.Context, owner, repo, branch string) (*BranchProtection, error) {
if m.RulesetsErrs != nil {
if err, ok := m.RulesetsErrs[repo]; ok {
return nil, err
}
}
if m.RulesetsErr != nil {
return nil, m.RulesetsErr
}
if m.Rulesets != nil {
return m.Rulesets[repo], nil
}
return nil, nil
}