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

Commit 1fe4009

Browse files
committed
Split GitService unit and integration tests
Keep tests that mock IRepository and create a real Repository separate.
1 parent da21ea7 commit 1fe4009

File tree

2 files changed

+215
-216
lines changed

2 files changed

+215
-216
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using GitHub.Services;
5+
using LibGit2Sharp;
6+
using NUnit.Framework;
7+
8+
public class GitServiceIntegrationTests
9+
{
10+
public class TheGetLatestPushedShaMethod : TestBaseClass
11+
{
12+
[Test]
13+
public async Task EmptyRepository_ReturnsNull()
14+
{
15+
using (var temp = new TempDirectory())
16+
{
17+
string expectSha;
18+
var dir = temp.Directory.FullName;
19+
using (var repo = new Repository(Repository.Init(dir)))
20+
{
21+
expectSha = null;
22+
}
23+
24+
var target = new GitService(new RepositoryFacade());
25+
26+
var sha = await target.GetLatestPushedSha(dir).ConfigureAwait(false);
27+
28+
Assert.That(sha, Is.EqualTo(expectSha));
29+
}
30+
}
31+
32+
[Test]
33+
public async Task HeadAndRemoteOnSameCommit_ReturnCommitSha()
34+
{
35+
using (var temp = new TempDirectory())
36+
{
37+
string expectSha;
38+
var dir = temp.Directory.FullName;
39+
using (var repo = new Repository(Repository.Init(dir)))
40+
{
41+
AddCommit(repo); // First commit
42+
var commit = AddCommit(repo);
43+
expectSha = commit.Sha;
44+
AddTrackedBranch(repo, repo.Head, commit);
45+
}
46+
47+
var target = new GitService(new RepositoryFacade());
48+
49+
var sha = await target.GetLatestPushedSha(dir).ConfigureAwait(false);
50+
51+
Assert.That(sha, Is.EqualTo(expectSha));
52+
}
53+
}
54+
55+
[Test]
56+
public async Task LocalAheadOfRemote_ReturnRemoteCommitSha()
57+
{
58+
using (var temp = new TempDirectory())
59+
{
60+
string expectSha;
61+
var dir = temp.Directory.FullName;
62+
using (var repo = new Repository(Repository.Init(dir)))
63+
{
64+
AddCommit(repo); // First commit
65+
var commit = AddCommit(repo);
66+
expectSha = commit.Sha;
67+
AddTrackedBranch(repo, repo.Head, commit);
68+
AddCommit(repo);
69+
}
70+
71+
var target = new GitService(new RepositoryFacade());
72+
73+
var sha = await target.GetLatestPushedSha(dir).ConfigureAwait(false);
74+
75+
Assert.That(sha, Is.EqualTo(expectSha));
76+
}
77+
}
78+
79+
[Test]
80+
public async Task LocalBehindRemote_ReturnRemoteCommitSha()
81+
{
82+
using (var temp = new TempDirectory())
83+
{
84+
string expectSha;
85+
var dir = temp.Directory.FullName;
86+
using (var repo = new Repository(Repository.Init(dir)))
87+
{
88+
AddCommit(repo); // First commit
89+
var commit1 = AddCommit(repo);
90+
var commit2 = AddCommit(repo);
91+
repo.Reset(ResetMode.Hard, commit1);
92+
expectSha = commit1.Sha;
93+
AddTrackedBranch(repo, repo.Head, commit2);
94+
}
95+
96+
var target = new GitService(new RepositoryFacade());
97+
98+
var sha = await target.GetLatestPushedSha(dir).ConfigureAwait(false);
99+
100+
Assert.That(sha, Is.EqualTo(expectSha));
101+
}
102+
}
103+
104+
[Test]
105+
public async Task BranchForkedFromMaster_ReturnRemoteCommitSha()
106+
{
107+
using (var temp = new TempDirectory())
108+
{
109+
string expectSha;
110+
var dir = temp.Directory.FullName;
111+
using (var repo = new Repository(Repository.Init(dir)))
112+
{
113+
AddCommit(repo); // First commit
114+
var commit1 = AddCommit(repo);
115+
AddTrackedBranch(repo, repo.Head, commit1);
116+
var branch = repo.Branches.Add("branch", commit1);
117+
Commands.Checkout(repo, branch);
118+
var commit2 = AddCommit(repo);
119+
expectSha = commit1.Sha;
120+
}
121+
122+
var target = new GitService(new RepositoryFacade());
123+
124+
var sha = await target.GetLatestPushedSha(dir).ConfigureAwait(false);
125+
126+
Assert.That(sha, Is.EqualTo(expectSha));
127+
}
128+
}
129+
130+
[Test]
131+
public async Task TowPossibleRemoteBranches_ReturnNearestCommitSha()
132+
{
133+
using (var temp = new TempDirectory())
134+
{
135+
string expectSha;
136+
var dir = temp.Directory.FullName;
137+
using (var repo = new Repository(Repository.Init(dir)))
138+
{
139+
AddCommit(repo); // First commit
140+
var commit1 = AddCommit(repo);
141+
var commit2 = AddCommit(repo);
142+
var commit3 = AddCommit(repo);
143+
var branch1 = repo.Branches.Add("branch1", commit1);
144+
AddTrackedBranch(repo, branch1, commit1);
145+
var branch2 = repo.Branches.Add("branch2", commit2);
146+
AddTrackedBranch(repo, branch2, commit2);
147+
expectSha = commit2.Sha;
148+
}
149+
150+
var target = new GitService(new RepositoryFacade());
151+
152+
var sha = await target.GetLatestPushedSha(dir).ConfigureAwait(false);
153+
154+
Assert.That(sha, Is.EqualTo(expectSha));
155+
}
156+
}
157+
158+
[TestCase("origin", true)]
159+
[TestCase("jcansdale", true, Description = "Search all remotes")]
160+
public async Task BehindRemoteBranch_ReturnRemoteCommitSha(string remoteName, bool expectFound)
161+
{
162+
using (var temp = new TempDirectory())
163+
{
164+
string expectSha;
165+
var dir = temp.Directory.FullName;
166+
using (var repo = new Repository(Repository.Init(dir)))
167+
{
168+
AddCommit(repo); // First commit
169+
var commit1 = AddCommit(repo);
170+
var commit2 = AddCommit(repo);
171+
var branchA = repo.Branches.Add("branchA", commit2);
172+
repo.Reset(ResetMode.Hard, commit1);
173+
expectSha = expectFound ? commit1.Sha : null;
174+
AddTrackedBranch(repo, branchA, commit2, remoteName: remoteName);
175+
}
176+
177+
var target = new GitService(new RepositoryFacade());
178+
179+
var sha = await target.GetLatestPushedSha(dir).ConfigureAwait(false);
180+
181+
Assert.That(sha, Is.EqualTo(expectSha));
182+
}
183+
}
184+
185+
static Commit AddCommit(Repository repo)
186+
{
187+
var dir = repo.Info.WorkingDirectory;
188+
var path = "file.txt";
189+
var file = Path.Combine(dir, path);
190+
var guidString = Guid.NewGuid().ToString();
191+
File.WriteAllText(file, guidString);
192+
Commands.Stage(repo, path);
193+
var signature = new Signature("foobar", "[email protected]", DateTime.Now);
194+
var commit = repo.Commit("message", signature, signature);
195+
return commit;
196+
}
197+
198+
static void AddTrackedBranch(Repository repo, Branch branch, Commit commit,
199+
string trackedBranchName = null, string remoteName = "origin")
200+
{
201+
trackedBranchName = trackedBranchName ?? branch.FriendlyName;
202+
203+
if (repo.Network.Remotes[remoteName] == null)
204+
{
205+
repo.Network.Remotes.Add(remoteName, "https://github.com/owner/repo");
206+
}
207+
var canonicalName = $"refs/remotes/{remoteName}/{trackedBranchName}";
208+
repo.Refs.Add(canonicalName, commit.Id);
209+
repo.Branches.Update(branch, b => b.TrackedBranch = canonicalName);
210+
}
211+
}
212+
}

0 commit comments

Comments
 (0)