|
| 1 | +//go:build tools |
| 2 | +// +build tools |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2025 The Kubernetes Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "time" |
| 25 | +) |
| 26 | + |
| 27 | +// mockGithubClient is a mock implementation of githubClientInterface for testing. |
| 28 | +type mockGithubClient struct { |
| 29 | + // Mock responses |
| 30 | + diffResponse *githubDiff |
| 31 | + refResponse githubRef |
| 32 | + tagResponse githubTag |
| 33 | + commitResponse githubCommit |
| 34 | + prsResponse []githubPR |
| 35 | + |
| 36 | + // Mock errors |
| 37 | + diffError error |
| 38 | + refError error |
| 39 | + tagError error |
| 40 | + commitError error |
| 41 | + prsError error |
| 42 | +} |
| 43 | + |
| 44 | +// Ensure mockGithubClient implements githubClientInterface. |
| 45 | +var _ githubClientInterface = (*mockGithubClient)(nil) |
| 46 | + |
| 47 | +func (m *mockGithubClient) getDiffAllCommits(_, _ string) (*githubDiff, error) { |
| 48 | + if m.diffError != nil { |
| 49 | + return nil, m.diffError |
| 50 | + } |
| 51 | + return m.diffResponse, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (m *mockGithubClient) getRef(_ string) (githubRef, error) { |
| 55 | + if m.refError != nil { |
| 56 | + return githubRef{}, m.refError |
| 57 | + } |
| 58 | + return m.refResponse, nil |
| 59 | +} |
| 60 | + |
| 61 | +func (m *mockGithubClient) getTag(_ string) (githubTag, error) { |
| 62 | + if m.tagError != nil { |
| 63 | + return githubTag{}, m.tagError |
| 64 | + } |
| 65 | + return m.tagResponse, nil |
| 66 | +} |
| 67 | + |
| 68 | +func (m *mockGithubClient) getCommit(_ string) (githubCommit, error) { |
| 69 | + if m.commitError != nil { |
| 70 | + return githubCommit{}, m.commitError |
| 71 | + } |
| 72 | + return m.commitResponse, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (m *mockGithubClient) listMergedPRs(_, _ time.Time, _ ...string) ([]githubPR, error) { |
| 76 | + if m.prsError != nil { |
| 77 | + return nil, m.prsError |
| 78 | + } |
| 79 | + return m.prsResponse, nil |
| 80 | +} |
| 81 | + |
| 82 | +// newMockGithubClient creates a new mock client with default responses. |
| 83 | +func newMockGithubClient() *mockGithubClient { |
| 84 | + return &mockGithubClient{ |
| 85 | + diffResponse: &githubDiff{ |
| 86 | + MergeBaseCommit: githubCommitNode{ |
| 87 | + Commit: githubCommit{ |
| 88 | + Message: "Merge commit", |
| 89 | + Committer: githubCommitter{ |
| 90 | + Date: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + Commits: []githubCommitNode{ |
| 95 | + { |
| 96 | + Commit: githubCommit{ |
| 97 | + Message: "Merge pull request #1234 from test/branch", |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + refResponse: githubRef{ |
| 103 | + Object: githubObject{ |
| 104 | + ObjectType: commitType, |
| 105 | + SHA: "abc123", |
| 106 | + }, |
| 107 | + }, |
| 108 | + tagResponse: githubTag{ |
| 109 | + Object: githubObject{ |
| 110 | + ObjectType: tagType, |
| 111 | + SHA: "def456", |
| 112 | + }, |
| 113 | + }, |
| 114 | + commitResponse: githubCommit{ |
| 115 | + Message: "Test commit", |
| 116 | + Committer: githubCommitter{ |
| 117 | + Date: time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC), |
| 118 | + }, |
| 119 | + }, |
| 120 | + prsResponse: []githubPR{ |
| 121 | + { |
| 122 | + Number: 1234, |
| 123 | + Title: "Test PR", |
| 124 | + Labels: []githubLabel{ |
| 125 | + {Name: "area/testing"}, |
| 126 | + }, |
| 127 | + User: githubUser{ |
| 128 | + Login: "testuser", |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// newMockGithubClientWithError creates a mock client that returns an error for specific operations. |
| 136 | +func newMockGithubClientWithError(operation string, err error) *mockGithubClient { |
| 137 | + mock := newMockGithubClient() |
| 138 | + switch operation { |
| 139 | + case "diff": |
| 140 | + mock.diffError = err |
| 141 | + case "ref": |
| 142 | + mock.refError = err |
| 143 | + case "tag": |
| 144 | + mock.tagError = err |
| 145 | + case "commit": |
| 146 | + mock.commitError = err |
| 147 | + case "prs": |
| 148 | + mock.prsError = err |
| 149 | + } |
| 150 | + return mock |
| 151 | +} |
| 152 | + |
| 153 | +// newMockGithubClientForInvalidRef creates a mock client that simulates invalid ref scenarios. |
| 154 | +func newMockGithubClientForInvalidRef() *mockGithubClient { |
| 155 | + mock := newMockGithubClient() |
| 156 | + mock.diffError = fmt.Errorf("invalid ref") |
| 157 | + return mock |
| 158 | +} |
0 commit comments