Skip to content
This repository was archived by the owner on Sep 20, 2022. It is now read-only.

Commit 8fd7f23

Browse files
author
Jake Ginnivan
committed
Added test for extracting repo from remote
1 parent c29f75a commit 8fd7f23

6 files changed

Lines changed: 122 additions & 41 deletions

File tree

src/GitReleaseNotes.Tests/GitReleaseNotes.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
<Compile Include="ArgumentTests.cs" />
7171
<Compile Include="CommitGrouperTests.cs" />
7272
<Compile Include="IssueTrackers\GitHub\GitHubIssueTrackerTests.cs" />
73+
<Compile Include="IssueTrackers\GitHub\NetworkEx.cs" />
74+
<Compile Include="IssueTrackers\GitHub\RemoteCollectionEx.cs" />
75+
<Compile Include="IssueTrackers\GitHub\RemoteEx.cs" />
7376
<Compile Include="IssueTrackers\IssueNumberExtractor.cs" />
7477
<Compile Include="IssueTrackers\Jira\JiraIssueTrackerTests.cs" />
7578
<Compile Include="Properties\AssemblyInfo.cs" />

src/GitReleaseNotes.Tests/IssueTrackers/GitHub/GitHubIssueTrackerTests.cs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,63 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
using System.Threading.Tasks;
26
using GitReleaseNotes.IssueTrackers.GitHub;
37
using LibGit2Sharp;
48
using NSubstitute;
59
using Octokit;
610
using Xunit;
711
using Xunit.Extensions;
8-
using Commit = LibGit2Sharp.Commit;
9-
using Signature = LibGit2Sharp.Signature;
1012

1113
namespace GitReleaseNotes.Tests.IssueTrackers.GitHub
1214
{
1315
public class GitHubIssueTrackerTests
1416
{
1517
private readonly GitReleaseNotesArguments _arguments;
1618
private readonly IGitHubClient _gitHubClient;
19+
private readonly IIssuesClient _issuesClient;
1720
private readonly GitHubIssueTracker _sut;
1821
private readonly ILog _log;
19-
private IRepository _repo;
22+
private readonly IRepository _repo;
2023

2124
public GitHubIssueTrackerTests()
2225
{
2326
_log = Substitute.For<ILog>();
2427
_gitHubClient = Substitute.For<IGitHubClient>();
28+
_issuesClient = Substitute.For<IIssuesClient>();
29+
_gitHubClient.Issue.Returns(_issuesClient);
2530
_arguments = new GitReleaseNotesArguments
2631
{
2732
Repo = "Org/Repo",
2833
Token = "213"
2934
};
3035
_repo = Substitute.For<IRepository>();
36+
_repo.Network.Returns(new NetworkEx());
3137

3238
_sut = new GitHubIssueTracker(_repo, () => _gitHubClient, _log, _arguments);
3339
}
3440

35-
//[Fact]
36-
//public void CreatesReleaseNotesForClosedGitHubIssues()
37-
//{
38-
// var commit = CreateCommit("Fixes #1", DateTimeOffset.Now.AddDays(-1));
39-
// var commitsToScan = new List<Commit> { commit };
40-
// var toScan = new Dictionary<ReleaseInfo, List<Commit>>
41-
// {
42-
// {new ReleaseInfo(), commitsToScan}
43-
// };
44-
// _issuesClient
45-
// .GetForRepository("Org", "Repo", Arg.Any<RepositoryIssueRequest>())
46-
// .Returns(Task.FromResult<IReadOnlyList<Issue>>(new List<Issue>
47-
// {
48-
// new Issue
49-
// {
50-
// Number = 1,
51-
// Title = "Issue Title"
52-
// }
53-
// }.AsReadOnly()));
54-
// _gitHubClient.Issue.Returns(_issuesClient);
55-
56-
// var releaseNotes = _sut.GetClosedIssues(_gitReleaseNotesArguments, toScan);
57-
58-
// Assert.Equal("Issue Title", releaseNotes.Releases[0].ReleaseNoteItems[0].Title);
59-
//}
41+
[Fact]
42+
public void CreatesReleaseNotesForClosedGitHubIssues()
43+
{
44+
_issuesClient
45+
.GetForRepository("Org", "Repo", Arg.Any<RepositoryIssueRequest>())
46+
.Returns(Task.FromResult<IReadOnlyList<Issue>>(new List<Issue>
47+
{
48+
new Issue
49+
{
50+
Number = 1,
51+
Title = "Issue Title",
52+
Labels = new Collection<Label>(),
53+
ClosedAt = DateTimeOffset.Now
54+
}
55+
}.AsReadOnly()));
56+
57+
var closedIssues = _sut.GetClosedIssues(DateTimeOffset.Now.AddDays(-2));
58+
59+
Assert.Equal("Issue Title", closedIssues.Single().Title);
60+
}
6061

6162
[Fact]
6263
public void ErrorLoggedWhenRepoIsNotSpecified()
@@ -89,6 +90,18 @@ public void RepositoryMustBeInCorrectFormat(string repo, bool success)
8990
}
9091
}
9192

93+
[Fact]
94+
public void CanGetRepoFromRemote()
95+
{
96+
_repo.Network.Remotes.Add("upstream", "http://github.com/Org/Repo.With.Dots");
97+
98+
_sut.GetClosedIssues(DateTimeOffset.Now.AddDays(-2));
99+
100+
_issuesClient
101+
.Received()
102+
.GetForRepository("Org", "Repo.With.Dots", Arg.Any<RepositoryIssueRequest>());
103+
}
104+
92105
[Fact]
93106
public void MustSpecifyVersionWhenPublishFlagIsSet()
94107
{
@@ -114,14 +127,5 @@ public void CanCreateReleaseOnGitHub()
114127
.CreateRelease("Foo", "Baz",
115128
Arg.Is<ReleaseUpdate>(r => r.TagName == "1.2.0" && r.Body == releaseNotesOutput && r.Name == "1.2.0"));
116129
}
117-
118-
private static Commit CreateCommit(string message, DateTimeOffset when)
119-
{
120-
var commit = Substitute.For<Commit>();
121-
commit.Message.Returns(message);
122-
var commitSignature = new Signature("Jake", "", when);
123-
commit.Author.Returns(commitSignature);
124-
return commit;
125-
}
126130
}
127131
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using LibGit2Sharp;
2+
3+
namespace GitReleaseNotes.Tests.IssueTrackers.GitHub
4+
{
5+
public class NetworkEx : Network
6+
{
7+
private readonly RemoteCollection _remotes;
8+
9+
public NetworkEx()
10+
{
11+
_remotes = new RemoteCollectionEx();
12+
}
13+
14+
public override RemoteCollection Remotes
15+
{
16+
get { return _remotes; }
17+
}
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using LibGit2Sharp;
4+
5+
namespace GitReleaseNotes.Tests.IssueTrackers.GitHub
6+
{
7+
public class RemoteCollectionEx : RemoteCollection
8+
{
9+
readonly List<Remote> _remotes = new List<Remote>();
10+
11+
public override Remote Add(string name, string url)
12+
{
13+
var remoteEx = new RemoteEx(name, url);
14+
_remotes.Add(remoteEx);
15+
return remoteEx;
16+
}
17+
18+
public override IEnumerator<Remote> GetEnumerator()
19+
{
20+
return _remotes.GetEnumerator();
21+
}
22+
23+
public override Remote this[string name]
24+
{
25+
get { return _remotes.Single(r => r.Name == name); }
26+
}
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using LibGit2Sharp;
2+
3+
namespace GitReleaseNotes.Tests.IssueTrackers.GitHub
4+
{
5+
public class RemoteEx : Remote
6+
{
7+
private readonly string _url;
8+
private readonly string _name;
9+
10+
public override string Name
11+
{
12+
get { return _name; }
13+
}
14+
15+
public override string Url
16+
{
17+
get { return _url; }
18+
}
19+
20+
public RemoteEx(string name, string url)
21+
{
22+
_name = name;
23+
_url = url;
24+
}
25+
}
26+
}

src/GitReleaseNotes/IssueTrackers/GitHub/GitHubIssueTracker.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace GitReleaseNotes.IssueTrackers.GitHub
1111
public class GitHubIssueTracker : IIssueTracker
1212
{
1313
private readonly Func<IGitHubClient> _gitHubClientFactory;
14+
private readonly GitReleaseNotesArguments _arguments;
1415
private readonly IRepository _repository;
1516
private readonly ILog _log;
16-
private readonly GitReleaseNotesArguments _arguments;
1717

1818
public GitHubIssueTracker(IRepository repository, Func<IGitHubClient> gitHubClientFactory, ILog log, GitReleaseNotesArguments arguments)
1919
{
@@ -88,17 +88,18 @@ private void GetRepository(GitReleaseNotesArguments arguments, out string organi
8888
if (TryRemote(out organisation, out repository, remoteName))
8989
return;
9090
}
91+
9192
var repoParts = arguments.Repo.Split('/');
9293
organisation = repoParts[0];
9394
repository = repoParts[1];
9495
}
9596

9697
private bool TryRemote(out string organisation, out string repository, string remoteName)
9798
{
98-
var upstream = _repository.Network.Remotes[remoteName];
99-
if (upstream != null && upstream.Url.ToLower().Contains("github.com"))
99+
var remote = _repository.Network.Remotes[remoteName];
100+
if (remote != null && remote.Url.ToLower().Contains("github.com"))
100101
{
101-
var urlWithoutGitExtension = upstream.Url.EndsWith(".git") ? upstream.Url.Substring(0, upstream.Url.Length - 4) : upstream.Url;
102+
var urlWithoutGitExtension = remote.Url.EndsWith(".git") ? remote.Url.Substring(0, remote.Url.Length - 4) : remote.Url;
102103
var match = Regex.Match(urlWithoutGitExtension, "github.com[/:](?<org>.*?)/(?<repo>.*)");
103104
if (match.Success)
104105
{

0 commit comments

Comments
 (0)