Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 0e746fc

Browse files
committed
Added tests for PullRequestService.GetLocalBranches.
1 parent c24604a commit 0e746fc

File tree

1 file changed

+89
-8
lines changed

1 file changed

+89
-8
lines changed

src/UnitTests/GitHub.App/Services/PullRequestServiceTests.cs

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
using System.Reactive.Linq;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reactive.Linq;
24
using System.Threading.Tasks;
3-
using NSubstitute;
4-
using Xunit;
5-
using UnitTests;
65
using GitHub.Models;
7-
using System;
6+
using GitHub.Primitives;
87
using GitHub.Services;
9-
using Rothko;
108
using LibGit2Sharp;
11-
using System.Collections.Generic;
9+
using NSubstitute;
10+
using Octokit;
11+
using Rothko;
12+
using UnitTests;
13+
using Xunit;
1214

1315
public class PullRequestServiceTests : TestBaseClass
1416
{
@@ -94,13 +96,92 @@ static IGitService MockGitService()
9496
}
9597
}
9698

99+
public class TheGetLocalBranchesMethod
100+
{
101+
[Fact]
102+
public async Task ShouldReturnPullRequestBranchForPullRequestFromSameRepository()
103+
{
104+
var service = new PullRequestService(
105+
Substitute.For<IGitClient>(),
106+
MockGitService(),
107+
Substitute.For<IOperatingSystem>(),
108+
Substitute.For<IUsageTracker>());
109+
110+
var localRepo = Substitute.For<ILocalRepositoryModel>();
111+
localRepo.CloneUrl.Returns(new UriString("https://github.com/foo/bar"));
112+
113+
var result = await service.GetLocalBranches(localRepo, CreatePullRequest());
114+
115+
Assert.Equal("source", result.Name);
116+
}
117+
118+
[Fact]
119+
public async Task ShouldReturnMarkedBranchForPullRequestFromFork()
120+
{
121+
var repo = Substitute.For<IRepository>();
122+
var config = Substitute.For<Configuration>();
123+
124+
var configEntry1 = Substitute.For<ConfigurationEntry<string>>();
125+
configEntry1.Key.Returns("branch.pr/1-foo.ghfvs-pr");
126+
configEntry1.Value.Returns("1");
127+
var configEntry2 = Substitute.For<ConfigurationEntry<string>>();
128+
configEntry2.Key.Returns("branch.pr/2-bar.ghfvs-pr");
129+
configEntry2.Value.Returns("2");
130+
131+
config.GetEnumerator().Returns(new List<ConfigurationEntry<string>>
132+
{
133+
configEntry1,
134+
configEntry2,
135+
}.GetEnumerator());
136+
137+
repo.Config.Returns(config);
138+
139+
var service = new PullRequestService(
140+
Substitute.For<IGitClient>(),
141+
MockGitService(repo),
142+
Substitute.For<IOperatingSystem>(),
143+
Substitute.For<IUsageTracker>());
144+
145+
var localRepo = Substitute.For<ILocalRepositoryModel>();
146+
localRepo.CloneUrl.Returns(new UriString("https://github.com/baz/bar"));
147+
148+
var result = await service.GetLocalBranches(localRepo, CreatePullRequest());
149+
150+
Assert.Equal("pr/1-foo", result.Name);
151+
}
152+
153+
static PullRequest CreatePullRequest()
154+
{
155+
var uri = new Uri("http://github.com/foo/bar.git");
156+
var repository = CreateRepository("foo", "bar");
157+
var user = CreateUserAndScopes("foo").User;
158+
159+
return new PullRequest(
160+
uri, uri, uri, uri, uri, uri,
161+
1, ItemState.Open, "PR 1", string.Empty,
162+
DateTimeOffset.Now, DateTimeOffset.Now, null, null,
163+
new GitReference(string.Empty, "foo:bar", "source", string.Empty, user, repository),
164+
new GitReference(string.Empty, "foo:baz", "dest", string.Empty, user, repository),
165+
user, user,
166+
true, null,
167+
0, 0, 0, 0, 0, 0, null, false);
168+
}
169+
170+
static IGitService MockGitService(IRepository repository = null)
171+
{
172+
var result = Substitute.For<IGitService>();
173+
result.GetRepository(Arg.Any<string>()).Returns(repository ?? Substitute.For<IRepository>());
174+
return result;
175+
}
176+
}
177+
97178
static BranchCollection MockBranches(params string[] names)
98179
{
99180
var result = Substitute.For<BranchCollection>();
100181

101182
foreach (var name in names)
102183
{
103-
var branch = Substitute.For<Branch>();
184+
var branch = Substitute.For<LibGit2Sharp.Branch>();
104185
branch.CanonicalName.Returns("refs/heads/" + name);
105186
result[name].Returns(branch);
106187
}

0 commit comments

Comments
 (0)