-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubRepoConnectorTests.cs
More file actions
409 lines (352 loc) · 13.8 KB
/
GitHubRepoConnectorTests.cs
File metadata and controls
409 lines (352 loc) · 13.8 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright (c) DEMA Consulting
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace DemaConsulting.BuildMark.Tests;
/// <summary>
/// Testable GitHub repository connector that allows injecting command results.
/// </summary>
internal class TestableGitHubRepoConnector : GitHubRepoConnector
{
private readonly Dictionary<string, string> _commandResults = new();
/// <summary>
/// Adds a command result for testing.
/// </summary>
/// <param name="command">Command name.</param>
/// <param name="arguments">Command arguments.</param>
/// <param name="result">Expected result.</param>
public void AddCommandResult(string command, string arguments, string result)
{
_commandResults[$"{command} {arguments}"] = result;
}
/// <summary>
/// Runs a command and returns its output.
/// </summary>
/// <param name="command">Command to run.</param>
/// <param name="arguments">Command arguments.</param>
/// <returns>Command output.</returns>
protected override Task<string> RunCommandAsync(string command, string arguments)
{
var key = $"{command} {arguments}";
if (_commandResults.TryGetValue(key, out var result))
{
return Task.FromResult(result);
}
throw new InvalidOperationException($"No result configured for command: {key}");
}
}
/// <summary>
/// Tests for the GitHubRepoConnector class.
/// </summary>
[TestClass]
public class GitHubRepoConnectorTests
{
/// <summary>
/// Test that GetTagHistoryAsync returns expected tags.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetTagHistoryAsync_ReturnsExpectedTags()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult("git", "tag --sort=creatordate --merged HEAD", "v1.0.0\nv1.1.0\nv2.0.0");
// Act
var tags = await connector.GetTagHistoryAsync();
// Assert
Assert.HasCount(3, tags);
Assert.AreEqual("v1.0.0", tags[0].Tag);
Assert.AreEqual("v1.1.0", tags[1].Tag);
Assert.AreEqual("v2.0.0", tags[2].Tag);
}
/// <summary>
/// Test that GetTagHistoryAsync returns empty list when no tags.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetTagHistoryAsync_ReturnsEmptyListWhenNoTags()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult("git", "tag --sort=creatordate --merged HEAD", "");
// Act
var tags = await connector.GetTagHistoryAsync();
// Assert
Assert.IsEmpty(tags);
}
/// <summary>
/// Test that GetPullRequestsBetweenTagsAsync returns expected PRs.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetPullRequestsBetweenTagsAsync_ReturnsExpectedPRs()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult(
"git",
"log --oneline --merges v1.0.0..v2.0.0",
"abc123 Merge pull request #10 from feature/x\ndef456 Merge pull request #11 from bugfix/y");
// Act
var prs = await connector.GetPullRequestsBetweenTagsAsync(
Version.Create("v1.0.0"),
Version.Create("v2.0.0"));
// Assert
Assert.HasCount(2, prs);
Assert.AreEqual("10", prs[0]);
Assert.AreEqual("11", prs[1]);
}
/// <summary>
/// Test that GetPullRequestsBetweenTagsAsync handles null fromTag.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetPullRequestsBetweenTagsAsync_HandlesNullFromTag()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult(
"git",
"log --oneline --merges v1.0.0",
"abc123 Merge pull request #10 from feature/x");
// Act
var prs = await connector.GetPullRequestsBetweenTagsAsync(null, Version.Create("v1.0.0"));
// Assert
Assert.HasCount(1, prs);
Assert.AreEqual("10", prs[0]);
}
/// <summary>
/// Test that GetPullRequestsBetweenTagsAsync handles null toTag.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetPullRequestsBetweenTagsAsync_HandlesNullToTag()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult(
"git",
"log --oneline --merges v1.0.0..HEAD",
"abc123 Merge pull request #11 from feature/y");
// Act
var prs = await connector.GetPullRequestsBetweenTagsAsync(Version.Create("v1.0.0"), null);
// Assert
Assert.HasCount(1, prs);
Assert.AreEqual("11", prs[0]);
}
/// <summary>
/// Test that GetPullRequestsBetweenTagsAsync handles both null tags.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetPullRequestsBetweenTagsAsync_HandlesBothNullTags()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult(
"git",
"log --oneline --merges HEAD",
"abc123 Merge pull request #12 from feature/z");
// Act
var prs = await connector.GetPullRequestsBetweenTagsAsync(null, null);
// Assert
Assert.HasCount(1, prs);
Assert.AreEqual("12", prs[0]);
}
/// <summary>
/// Test that GetIssuesForPullRequestAsync returns expected issues.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetIssuesForPullRequestAsync_ReturnsExpectedIssues()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult(
"gh",
"pr view 10 --json body --jq .body",
"This PR fixes #123 and resolves #456");
// Act
var issues = await connector.GetIssuesForPullRequestAsync("10");
// Assert
Assert.HasCount(2, issues);
Assert.AreEqual("123", issues[0]);
Assert.AreEqual("456", issues[1]);
}
/// <summary>
/// Test that GetIssuesForPullRequestAsync returns empty when no issues.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetIssuesForPullRequestAsync_ReturnsEmptyWhenNoIssues()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult(
"gh",
"pr view 10 --json body --jq .body",
"This PR has no issue references");
// Act
var issues = await connector.GetIssuesForPullRequestAsync("10");
// Assert
Assert.IsEmpty(issues);
}
/// <summary>
/// Test that GetIssueTitleAsync returns expected title.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetIssueTitleAsync_ReturnsExpectedTitle()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult("gh", "issue view 123 --json title --jq .title", "Add new feature");
// Act
var title = await connector.GetIssueTitleAsync("123");
// Assert
Assert.AreEqual("Add new feature", title);
}
/// <summary>
/// Test that GetIssueTypeAsync returns bug for bug label.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetIssueTypeAsync_ReturnsBugForBugLabel()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult("gh", "issue view 123 --json labels --jq '.labels[].name'", "bug\npriority:high");
// Act
var type = await connector.GetIssueTypeAsync("123");
// Assert
Assert.AreEqual("bug", type);
}
/// <summary>
/// Test that GetIssueTypeAsync returns feature for enhancement label.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetIssueTypeAsync_ReturnsFeatureForEnhancementLabel()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult("gh", "issue view 123 --json labels --jq '.labels[].name'", "enhancement");
// Act
var type = await connector.GetIssueTypeAsync("123");
// Assert
Assert.AreEqual("feature", type);
}
/// <summary>
/// Test that GetIssueTypeAsync returns other for unknown label.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetIssueTypeAsync_ReturnsOtherForUnknownLabel()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult("gh", "issue view 123 --json labels --jq '.labels[].name'", "question");
// Act
var type = await connector.GetIssueTypeAsync("123");
// Assert
Assert.AreEqual("other", type);
}
/// <summary>
/// Test that GetHashForTagAsync returns expected hash.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetHashForTagAsync_ReturnsExpectedHash()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult("git", "rev-parse v1.0.0", "abc123def456789");
// Act
var hash = await connector.GetHashForTagAsync(Version.Create("v1.0.0").Tag);
// Assert
Assert.AreEqual("abc123def456789", hash);
}
/// <summary>
/// Test that GetHashForTagAsync returns current hash for null tag.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetHashForTagAsync_ReturnsCurrentHashForNullTag()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
connector.AddCommandResult("git", "rev-parse HEAD", "current123hash456");
// Act
var hash = await connector.GetHashForTagAsync(null);
// Assert
Assert.AreEqual("current123hash456", hash);
}
/// <summary>
/// Test that GetPullRequestsBetweenTagsAsync throws for invalid tag names.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetPullRequestsBetweenTagsAsync_ThrowsForInvalidTagName()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
// Create a Version with an invalid tag name for testing validation
var invalidVersion = new Version("v1.0.0; rm -rf /", "1.0.0", "1.0.0", string.Empty, string.Empty, false);
// Act & Assert
var ex = await Assert.ThrowsAsync<ArgumentException>(
async () => await connector.GetPullRequestsBetweenTagsAsync(invalidVersion, null));
Assert.Contains("Invalid tag name", ex.Message);
}
/// <summary>
/// Test that GetIssuesForPullRequestAsync throws for invalid PR ID.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetIssuesForPullRequestAsync_ThrowsForInvalidPRId()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
// Act & Assert
var ex = await Assert.ThrowsAsync<ArgumentException>(
async () => await connector.GetIssuesForPullRequestAsync("10; cat /etc/passwd"));
Assert.Contains("Invalid ID", ex.Message);
}
/// <summary>
/// Test that GetIssueTitleAsync throws for invalid issue ID.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetIssueTitleAsync_ThrowsForInvalidIssueId()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
// Act & Assert
var ex = await Assert.ThrowsAsync<ArgumentException>(
async () => await connector.GetIssueTitleAsync("123; whoami"));
Assert.Contains("Invalid ID", ex.Message);
}
/// <summary>
/// Test that GetIssueTypeAsync throws for invalid issue ID.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetIssueTypeAsync_ThrowsForInvalidIssueId()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
// Act & Assert
var ex = await Assert.ThrowsAsync<ArgumentException>(
async () => await connector.GetIssueTypeAsync("456 && echo hacked"));
Assert.Contains("Invalid ID", ex.Message);
}
/// <summary>
/// Test that GetHashForTagAsync throws for invalid tag name.
/// </summary>
[TestMethod]
public async Task GitHubRepoConnector_GetHashForTagAsync_ThrowsForInvalidTagName()
{
// Arrange
var connector = new TestableGitHubRepoConnector();
// Act & Assert
var ex = await Assert.ThrowsAsync<ArgumentException>(
async () => await connector.GetHashForTagAsync("v1.0.0 | echo pwned"));
Assert.Contains("Invalid tag name", ex.Message);
}
}