|
1 | | -using System.Reactive.Linq; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Reactive.Linq; |
2 | 4 | using System.Threading.Tasks; |
3 | | -using NSubstitute; |
4 | | -using Xunit; |
5 | | -using UnitTests; |
6 | 5 | using GitHub.Models; |
7 | | -using System; |
| 6 | +using GitHub.Primitives; |
8 | 7 | using GitHub.Services; |
9 | | -using Rothko; |
10 | 8 | using LibGit2Sharp; |
11 | | -using System.Collections.Generic; |
| 9 | +using NSubstitute; |
| 10 | +using Octokit; |
| 11 | +using Rothko; |
| 12 | +using UnitTests; |
| 13 | +using Xunit; |
12 | 14 |
|
13 | 15 | public class PullRequestServiceTests : TestBaseClass |
14 | 16 | { |
@@ -94,13 +96,92 @@ static IGitService MockGitService() |
94 | 96 | } |
95 | 97 | } |
96 | 98 |
|
| 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 | + |
97 | 178 | static BranchCollection MockBranches(params string[] names) |
98 | 179 | { |
99 | 180 | var result = Substitute.For<BranchCollection>(); |
100 | 181 |
|
101 | 182 | foreach (var name in names) |
102 | 183 | { |
103 | | - var branch = Substitute.For<Branch>(); |
| 184 | + var branch = Substitute.For<LibGit2Sharp.Branch>(); |
104 | 185 | branch.CanonicalName.Returns("refs/heads/" + name); |
105 | 186 | result[name].Returns(branch); |
106 | 187 | } |
|
0 commit comments