|
| 1 | +// Copyright 2025 Google LLC |
| 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 | +// https://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 | + |
| 15 | +package librarian |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | + "github.com/googleapis/librarian/internal/github" |
| 24 | +) |
| 25 | + |
| 26 | +func TestNewGistOverflowHandler(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + for _, test := range []struct { |
| 29 | + name string |
| 30 | + client *mockGitHubClient |
| 31 | + maxContentSize int |
| 32 | + wantMaxContentSize int |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "default configs", |
| 36 | + client: &mockGitHubClient{}, |
| 37 | + wantMaxContentSize: maxPullRequestBodySize, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "custom length configs", |
| 41 | + client: &mockGitHubClient{}, |
| 42 | + maxContentSize: 1234, |
| 43 | + wantMaxContentSize: 1234, |
| 44 | + }, |
| 45 | + } { |
| 46 | + t.Run(test.name, func(t *testing.T) { |
| 47 | + got, err := NewGistOverflowHandler(test.client, test.maxContentSize) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("unexpected error in NewGistOverflowHandler() %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + if diff := cmp.Diff(test.wantMaxContentSize, got.maxContentSize); diff != "" { |
| 53 | + t.Errorf("%s: NewGistOverflowHandler() maxContentSize mismatch (-want +got):%s", test.name, diff) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestSavePullRequestBody(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + for _, test := range []struct { |
| 62 | + name string |
| 63 | + client *mockGitHubClient |
| 64 | + maxContentSize int |
| 65 | + content string |
| 66 | + want string |
| 67 | + wantErr bool |
| 68 | + wantErrMsg string |
| 69 | + wantCreateGistCalls int |
| 70 | + }{ |
| 71 | + { |
| 72 | + name: "short content", |
| 73 | + client: &mockGitHubClient{}, |
| 74 | + content: "some-content", |
| 75 | + want: "some-content", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "content too long content", |
| 79 | + client: &mockGitHubClient{ |
| 80 | + createdGist: &github.Gist{ |
| 81 | + ID: "abcd1234", |
| 82 | + Url: "https://gist.github.com/some-user/abcd1234", |
| 83 | + }, |
| 84 | + }, |
| 85 | + content: "super long content", |
| 86 | + maxContentSize: 5, // force overflow handling |
| 87 | + want: "See full release notes at: https://gist.github.com/some-user/abcd1234", |
| 88 | + wantCreateGistCalls: 1, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "content too long content, error creating gist", |
| 92 | + client: &mockGitHubClient{ |
| 93 | + createGistError: fmt.Errorf("some create gist error"), |
| 94 | + createdGist: &github.Gist{ |
| 95 | + ID: "abcd1234", |
| 96 | + Url: "https://gist.github.com/some-user/abcd1234", |
| 97 | + }, |
| 98 | + }, |
| 99 | + content: "super long content", |
| 100 | + maxContentSize: 5, // force overflow handling |
| 101 | + wantErr: true, |
| 102 | + wantErrMsg: "some create gist error", |
| 103 | + wantCreateGistCalls: 1, |
| 104 | + }, |
| 105 | + } { |
| 106 | + t.Run(test.name, func(t *testing.T) { |
| 107 | + handler, err := NewGistOverflowHandler(test.client, test.maxContentSize) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("unexpected error in NewGistOverflowHandler() %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + got, err := handler.SavePullRequestBody(t.Context(), test.content) |
| 113 | + if test.wantErr { |
| 114 | + if err == nil { |
| 115 | + t.Fatalf("SavePullRequestBody() error = %v, wantErr %v", err, test.wantErr) |
| 116 | + } |
| 117 | + |
| 118 | + if !strings.Contains(err.Error(), test.wantErrMsg) { |
| 119 | + t.Fatalf("want error message: %s, got: %s", test.wantErrMsg, err.Error()) |
| 120 | + } |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("unexpected error in SavePullRequestBody() %v", err) |
| 126 | + } |
| 127 | + |
| 128 | + if diff := cmp.Diff(got, test.want); diff != "" { |
| 129 | + t.Errorf("%s: SavePullRequestBody() mismatch (-want +got):%s", test.name, diff) |
| 130 | + } |
| 131 | + |
| 132 | + if diff := cmp.Diff(test.wantCreateGistCalls, test.client.createGistCalls); diff != "" { |
| 133 | + t.Errorf("%s: SavePullRequestBody() createGistCalls mismatch (-want +got):%s", test.name, diff) |
| 134 | + } |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestFetchPullRequestBody(t *testing.T) { |
| 140 | + t.Parallel() |
| 141 | + for _, test := range []struct { |
| 142 | + name string |
| 143 | + client *mockGitHubClient |
| 144 | + maxContentSize int |
| 145 | + content string |
| 146 | + want string |
| 147 | + wantErr bool |
| 148 | + wantErrMsg string |
| 149 | + wantGetGistContentCalls int |
| 150 | + }{ |
| 151 | + { |
| 152 | + name: "non overflow", |
| 153 | + client: &mockGitHubClient{}, |
| 154 | + content: "some release notes", |
| 155 | + want: "some release notes", |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "with overflow", |
| 159 | + client: &mockGitHubClient{ |
| 160 | + getGistContent: map[string]string{ |
| 161 | + "release-notes.md": "some release notes", |
| 162 | + }, |
| 163 | + }, |
| 164 | + content: "See full release notes at: https://gist.github.com/some-user/abcd1234", |
| 165 | + want: "some release notes", |
| 166 | + wantGetGistContentCalls: 1, |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "with overflow, error fetching gist", |
| 170 | + client: &mockGitHubClient{ |
| 171 | + getGistError: fmt.Errorf("some fetch gist error"), |
| 172 | + }, |
| 173 | + content: "See full release notes at: https://gist.github.com/some-user/abcd1234", |
| 174 | + wantErr: true, |
| 175 | + wantErrMsg: "some fetch gist error", |
| 176 | + wantGetGistContentCalls: 1, |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "with overflow, wrong file", |
| 180 | + client: &mockGitHubClient{ |
| 181 | + getGistContent: map[string]string{ |
| 182 | + "unexpected file": "some release notes", |
| 183 | + }, |
| 184 | + }, |
| 185 | + content: "See full release notes at: https://gist.github.com/some-user/abcd1234", |
| 186 | + wantErr: true, |
| 187 | + wantErrMsg: "unable to find", |
| 188 | + wantGetGistContentCalls: 1, |
| 189 | + }, |
| 190 | + } { |
| 191 | + t.Run(test.name, func(t *testing.T) { |
| 192 | + handler, err := NewGistOverflowHandler(test.client, test.maxContentSize) |
| 193 | + if err != nil { |
| 194 | + t.Fatalf("unexpected error in NewGistOverflowHandler() %v", err) |
| 195 | + } |
| 196 | + |
| 197 | + got, err := handler.FetchPullRequestBody(t.Context(), test.content) |
| 198 | + |
| 199 | + if test.wantErr { |
| 200 | + if err == nil { |
| 201 | + t.Fatalf("FetchPullRequestBody() error = %v, wantErr %v", err, test.wantErr) |
| 202 | + } |
| 203 | + |
| 204 | + if !strings.Contains(err.Error(), test.wantErrMsg) { |
| 205 | + t.Fatalf("want error message: %s, got: %s", test.wantErrMsg, err.Error()) |
| 206 | + } |
| 207 | + return |
| 208 | + } |
| 209 | + |
| 210 | + if err != nil { |
| 211 | + t.Fatalf("unexpected error in FetchPullRequestBody() %v", err) |
| 212 | + } |
| 213 | + |
| 214 | + if diff := cmp.Diff(got, test.want); diff != "" { |
| 215 | + t.Errorf("%s: FetchPullRequestBody() mismatch (-want +got):%s", test.name, diff) |
| 216 | + } |
| 217 | + |
| 218 | + if diff := cmp.Diff(test.wantGetGistContentCalls, test.client.getGistContentCalls); diff != "" { |
| 219 | + t.Errorf("%s: FetchPullRequestBody() createGistCalls mismatch (-want +got):%s", test.name, diff) |
| 220 | + } |
| 221 | + }) |
| 222 | + } |
| 223 | +} |
0 commit comments