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

Commit 40525e0

Browse files
committed
Update tests to expect NotFoundException instead of null
1 parent 90b703f commit 40525e0

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public async Task PushesToDefaultOrigin()
5454

5555
await gitClient.Push(repository, "master", "origin");
5656

57-
repository.Network.Received().Push(origin,"HEAD", @"refs/heads/master", Arg.Any<PushOptions>());
57+
repository.Network.Received().Push(origin, "HEAD", @"refs/heads/master", Arg.Any<PushOptions>());
5858
}
5959

6060
[Fact]
@@ -194,7 +194,11 @@ public async Task WhenToFetch(string baseUrl, string headUrl, string baseSha, st
194194
repo.Network.Remotes.Add(null, null).ReturnsForAnyArgs(remote);
195195
var gitClient = new GitClient(Substitute.For<IGitHubCredentialProvider>());
196196

197-
await gitClient.GetPullRequestMergeBase(repo, baseUri, headUri, baseSha, headSha, baseRef, headRef);
197+
try
198+
{
199+
await gitClient.GetPullRequestMergeBase(repo, baseUri, headUri, baseSha, headSha, baseRef, headRef);
200+
}
201+
catch (NotFoundException) { }
198202

199203
repo.Network.Received(receivedFetch).Fetch(Arg.Any<Remote>(), Arg.Any<string[]>(), Arg.Any<FetchOptions>());
200204
}
@@ -210,7 +214,11 @@ public async Task WhatToFetch(string baseSha, string headSha, string mergeBaseSh
210214
var headUrl = new UriString("https://github.com/owner/repo");
211215
var gitClient = new GitClient(Substitute.For<IGitHubCredentialProvider>());
212216

213-
await gitClient.GetPullRequestMergeBase(repo, baseUrl, headUrl, baseSha, headSha, baseRef, headRef);
217+
try
218+
{
219+
await gitClient.GetPullRequestMergeBase(repo, baseUrl, headUrl, baseSha, headSha, baseRef, headRef);
220+
}
221+
catch (NotFoundException) { }
214222

215223
repo.Network.Received(1).Fetch(Arg.Any<Remote>(), Arg.Is<IEnumerable<string>>(x => x.Contains(expectRefSpec)), Arg.Any<FetchOptions>());
216224
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ public async void MergeBaseNotAvailable_ThrowsFileNotFoundException()
6565
var mergeBaseSha = null as string;
6666
var head = false;
6767
var expectMessage = $"Couldn't find merge base between {baseSha} and {headSha}.";
68+
var mergeBaseException = new NotFoundException(expectMessage);
6869

6970
var ex = await Assert.ThrowsAsync<NotFoundException>(() => ExtractFile(baseSha, baseFileContent, headSha, headFileContent, mergeBaseSha, mergeBaseFileContent,
70-
fileName, head, Encoding.UTF8));
71+
fileName, head, Encoding.UTF8, mergeBaseException: mergeBaseException));
7172
Assert.Equal(expectMessage, ex.Message);
7273
}
7374

@@ -150,7 +151,8 @@ static bool HasPreamble(string file, Encoding encoding)
150151

151152
static async Task<string> ExtractFile(
152153
string baseSha, object baseFileContent, string headSha, object headFileContent, string mergeBaseSha, object mergeBaseFileContent,
153-
string fileName, bool head, Encoding encoding, string repoDir = "repoDir", int pullNumber = 666, string baseRef = "baseRef", string headRef = "headRef")
154+
string fileName, bool head, Encoding encoding, string repoDir = "repoDir", int pullNumber = 666, string baseRef = "baseRef", string headRef = "headRef",
155+
Exception mergeBaseException = null)
154156
{
155157
var repositoryModel = Substitute.For<ILocalRepositoryModel>();
156158
repositoryModel.LocalPath.Returns(repoDir);
@@ -166,7 +168,15 @@ static async Task<string> ExtractFile(
166168
var gitService = serviceProvider.GetGitService();
167169
var service = new PullRequestService(gitClient, gitService, serviceProvider.GetOperatingSystem(), Substitute.For<IUsageTracker>());
168170

169-
gitClient.GetPullRequestMergeBase(Arg.Any<IRepository>(), Arg.Any<UriString>(), Arg.Any<UriString>(), baseSha, headSha, baseRef, headRef).ReturnsForAnyArgs(Task.FromResult(mergeBaseSha));
171+
if (mergeBaseException == null)
172+
{
173+
gitClient.GetPullRequestMergeBase(Arg.Any<IRepository>(), Arg.Any<UriString>(), Arg.Any<UriString>(), baseSha, headSha, baseRef, headRef).ReturnsForAnyArgs(Task.FromResult(mergeBaseSha));
174+
}
175+
else
176+
{
177+
gitClient.GetPullRequestMergeBase(Arg.Any<IRepository>(), Arg.Any<UriString>(), Arg.Any<UriString>(), baseSha, headSha, baseRef, headRef).ReturnsForAnyArgs(Task.FromException<string>(mergeBaseException));
178+
}
179+
170180
gitClient.ExtractFile(Arg.Any<IRepository>(), mergeBaseSha, fileName).Returns(GetFileTask(mergeBaseFileContent));
171181
gitClient.ExtractFile(Arg.Any<IRepository>(), baseSha, fileName).Returns(GetFileTask(baseFileContent));
172182
gitClient.ExtractFile(Arg.Any<IRepository>(), headSha, fileName).Returns(GetFileTask(headFileContent));
@@ -561,7 +571,7 @@ static IGitService MockGitService()
561571
var branches = MockBranches("pr/123-foo1", "pr/123-foo1-2");
562572
repository.Branches.Returns(branches);
563573

564-
var result = Substitute.For<IGitService>();
574+
var result = Substitute.For<IGitService>();
565575
result.GetRepository(Arg.Any<string>()).Returns(repository);
566576
return result;
567577
}

0 commit comments

Comments
 (0)