Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 1756035

Browse files
Adding tests for Repository
1 parent d6c22fe commit 1756035

File tree

7 files changed

+264
-51
lines changed

7 files changed

+264
-51
lines changed

src/GitHub.Api/Git/IRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ interface IRepository : IEquatable<IRepository>
6464
event Action OnHeadChanged;
6565
event Action<IEnumerable<GitLock>> OnLocksChanged;
6666
event Action OnRepositoryInfoChanged;
67+
event Action OnRemoteBranchListChanged;
6768
}
6869
}

src/GitHub.Api/Git/Repository.cs

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ private void RepositoryManager_OnLocksUpdated(IEnumerable<GitLock> locks)
168168

169169
private void RepositoryManager_OnHeadUpdated(string h)
170170
{
171+
Logger.Trace("HeadUpdated");
172+
171173
if (head != h)
172174
{
173175
head = h;
@@ -177,30 +179,49 @@ private void RepositoryManager_OnHeadUpdated(string h)
177179

178180
private void UpdateCurrentBranchAndRemote()
179181
{
180-
var branch = GetCurrentBranch();
181-
var remote = GetCurrentRemote();
182+
ConfigBranch? branch = null;
183+
184+
if (head.StartsWith("ref:"))
185+
{
186+
var branchName = head.Substring(head.IndexOf("refs/heads/") + "refs/heads/".Length);
187+
branch = GetBranch(branchName);
188+
}
189+
190+
CurrentBranch = branch;
191+
192+
var defaultRemote = "origin";
193+
ConfigRemote? remote = null;
194+
195+
if (currentBranch.HasValue && currentBranch.Value.IsTracking)
196+
{
197+
remote = currentBranch.Value.Remote;
198+
}
182199

183-
if (!Nullable.Equals(currentBranch, branch))
200+
if (!remote.HasValue)
184201
{
185-
currentBranch = branch;
186-
OnCurrentBranchChanged?.Invoke(currentBranch.HasValue ? currentBranch.Value.Name : null);
202+
remote = repositoryManager.Config.GetRemote(defaultRemote);
187203
}
188204

189-
if (!Nullable.Equals(currentRemote, remote))
205+
if (!remote.HasValue)
190206
{
191-
CurrentRemote = remote;
192-
OnCurrentRemoteChanged?.Invoke(currentRemote.HasValue ? currentRemote.Value.Name : null);
207+
remote = repositoryManager.Config.GetRemotes().FirstOrDefault();
193208
}
209+
210+
CurrentRemote = remote;
194211
}
195212

196213
private void RepositoryManager_OnRemoteBranchListUpdated(Dictionary<string, Dictionary<string, ConfigBranch>> branches)
197214
{
215+
Logger.Trace("RemoveBranchListUpdated");
216+
198217
remoteBranches = branches;
199218
OnRemoteBranchListChanged?.Invoke();
200219
}
201220

202221
private void RepositoryManager_OnLocalBranchListUpdated(Dictionary<string, ConfigBranch> branches)
203222
{
223+
Logger.Trace("LocalBranchListUpdated");
224+
204225
localBranches = branches;
205226
OnLocalBranchListChanged?.Invoke();
206227
}
@@ -263,20 +284,6 @@ private void RepositoryManager_OnRemoteBranchRemoved(string remote, string name)
263284
}
264285
}
265286
}
266-
267-
private ConfigBranch? GetCurrentBranch()
268-
{
269-
if (head.StartsWith("ref:"))
270-
{
271-
var branch = head.Substring(head.IndexOf("refs/heads/") + "refs/heads/".Length);
272-
currentBranch = GetBranch(branch);
273-
}
274-
else
275-
{
276-
currentBranch = null;
277-
}
278-
return currentBranch;
279-
}
280287

281288
private ConfigBranch? GetBranch(string name)
282289
{
@@ -288,30 +295,6 @@ private void RepositoryManager_OnRemoteBranchRemoved(string remote, string name)
288295
return null;
289296
}
290297

291-
private ConfigRemote? GetCurrentRemote(string defaultRemote = "origin")
292-
{
293-
if (currentBranch.HasValue && currentBranch.Value.IsTracking)
294-
{
295-
return currentBranch.Value.Remote;
296-
}
297-
298-
var remote = repositoryManager.Config.GetRemote(defaultRemote);
299-
if (remote.HasValue)
300-
{
301-
return remote;
302-
}
303-
304-
using (var remoteEnumerator = repositoryManager.Config.GetRemotes().GetEnumerator())
305-
{
306-
if (remoteEnumerator.MoveNext())
307-
{
308-
return remoteEnumerator.Current;
309-
}
310-
}
311-
312-
return null;
313-
}
314-
315298
/// <summary>
316299
/// Note: We don't consider CloneUrl a part of the hash code because it can change during the lifetime
317300
/// of a repository. Equals takes care of any hash collisions because of this
@@ -348,10 +331,10 @@ public ConfigBranch? CurrentBranch
348331
get { return currentBranch; }
349332
set
350333
{
351-
if (currentBranch.HasValue != value.HasValue || (currentBranch.HasValue && !currentBranch.Value.Equals(value.Value)))
334+
if (!Nullable.Equals(currentBranch, value))
352335
{
353336
currentBranch = value;
354-
Logger.Trace("OnActiveBranchChanged: {0}", value?.ToString() ?? "NULL");
337+
Logger.Trace("OnCurrentBranchChanged: {0}", value?.ToString() ?? "NULL");
355338
OnCurrentBranchChanged?.Invoke(CurrentBranch.HasValue ? CurrentBranch.Value.Name : null);
356339
}
357340
}
@@ -370,11 +353,11 @@ public ConfigRemote? CurrentRemote
370353
get { return currentRemote; }
371354
set
372355
{
373-
if (currentRemote.HasValue != value.HasValue || (currentRemote.HasValue && !currentRemote.Value.Equals(value.Value)))
356+
if (!Nullable.Equals(currentRemote, value))
374357
{
375358
currentRemote = value;
376359
SetCloneUrl();
377-
Logger.Trace("OnActiveRemoteChanged: {0}", value?.ToString() ?? "NULL");
360+
Logger.Trace("OnCurrentRemoteChanged: {0}", value?.ToString() ?? "NULL");
378361
OnCurrentRemoteChanged?.Invoke(CurrentRemote.HasValue ? CurrentRemote.Value.Name : null);
379362
}
380363
}

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ public void Start()
158158
{
159159
Logger.Trace("Start");
160160

161-
UpdateHead();
162161
UpdateConfigData();
162+
UpdateHead();
163163
LoadGitUser();
164164
watcher.Start();
165165
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading;
4+
using GitHub.Unity;
5+
using NSubstitute;
6+
7+
namespace TestUtils.Events
8+
{
9+
interface IRepositoryListener
10+
{
11+
void OnStatusChanged(GitStatus status);
12+
void OnCurrentBranchChanged(string branch);
13+
void OnCurrentRemoteChanged(string remote);
14+
void OnLocalBranchListChanged();
15+
void OnRemoteBranchListChanged();
16+
void OnHeadChanged();
17+
void OnLocksChanged(IEnumerable<GitLock> locks);
18+
void OnRepositoryInfoChanged();
19+
}
20+
21+
class RepositoryEvents
22+
{
23+
public EventWaitHandle OnStatusChanged { get; } = new AutoResetEvent(false);
24+
public EventWaitHandle OnCurrentBranchChanged { get; } = new AutoResetEvent(false);
25+
public EventWaitHandle OnCurrentRemoteChanged { get; } = new AutoResetEvent(false);
26+
public EventWaitHandle OnLocalBranchListChanged { get; } = new AutoResetEvent(false);
27+
public EventWaitHandle OnRemoteBranchListChanged { get; } = new AutoResetEvent(false);
28+
public EventWaitHandle OnHeadChanged { get; } = new AutoResetEvent(false);
29+
public EventWaitHandle OnLocksChanged { get; } = new AutoResetEvent(false);
30+
public EventWaitHandle OnRepositoryInfoChanged { get; } = new AutoResetEvent(false);
31+
32+
public void Reset()
33+
{
34+
OnStatusChanged.Reset();
35+
OnCurrentBranchChanged.Reset();
36+
OnCurrentRemoteChanged.Reset();
37+
OnLocalBranchListChanged.Reset();
38+
OnRemoteBranchListChanged.Reset();
39+
OnHeadChanged.Reset();
40+
OnLocksChanged.Reset();
41+
OnRepositoryInfoChanged.Reset();
42+
}
43+
}
44+
45+
static class RepositoryListenerExtensions
46+
{
47+
public static void AttachListener(this IRepositoryListener listener,
48+
IRepository repository, RepositoryEvents repositoryEvents = null, bool trace = true)
49+
{
50+
var logger = trace ? Logging.GetLogger<IRepositoryListener>() : null;
51+
52+
repository.OnStatusChanged += gitStatus =>
53+
{
54+
logger?.Trace("OnStatusChanged: {0}", gitStatus);
55+
listener.OnStatusChanged(gitStatus);
56+
repositoryEvents?.OnStatusChanged.Set();
57+
};
58+
59+
repository.OnCurrentBranchChanged += name =>
60+
{
61+
logger?.Debug("OnCurrentBranchChanged: {0}", name);
62+
listener.OnCurrentBranchChanged(name);
63+
repositoryEvents?.OnCurrentBranchChanged.Set();
64+
};
65+
66+
repository.OnCurrentRemoteChanged += name =>
67+
{
68+
logger?.Debug("OnCurrentRemoteChanged: {0}", name);
69+
listener.OnCurrentRemoteChanged(name);
70+
repositoryEvents?.OnCurrentRemoteChanged.Set();
71+
};
72+
73+
repository.OnLocalBranchListChanged += () =>
74+
{
75+
logger?.Debug("OnLocalBranchListChanged");
76+
listener.OnLocalBranchListChanged();
77+
repositoryEvents?.OnLocalBranchListChanged.Set();
78+
};
79+
80+
repository.OnRemoteBranchListChanged += () =>
81+
{
82+
logger?.Debug("OnRemoteBranchListChanged");
83+
listener.OnRemoteBranchListChanged();
84+
repositoryEvents?.OnRemoteBranchListChanged.Set();
85+
};
86+
87+
repository.OnHeadChanged += () =>
88+
{
89+
logger?.Debug("OnHeadChanged");
90+
listener.OnHeadChanged();
91+
repositoryEvents?.OnHeadChanged.Set();
92+
};
93+
94+
repository.OnLocksChanged += locks =>
95+
{
96+
logger?.Debug("OnLocksChanged: {0}", locks);
97+
listener.OnLocksChanged(locks);
98+
repositoryEvents?.OnLocksChanged.Set();
99+
};
100+
101+
repository.OnRepositoryInfoChanged += () =>
102+
{
103+
logger?.Debug("OnRepositoryInfoChanged");
104+
listener.OnRepositoryInfoChanged();
105+
repositoryEvents?.OnRepositoryInfoChanged.Set();
106+
};
107+
}
108+
109+
public static void AssertDidNotReceiveAnyCalls(this IRepositoryListener repositoryListener)
110+
{
111+
repositoryListener.DidNotReceive().OnStatusChanged(Args.GitStatus);
112+
repositoryListener.DidNotReceive().OnCurrentBranchChanged(Args.String);
113+
repositoryListener.DidNotReceive().OnCurrentRemoteChanged(Args.String);
114+
repositoryListener.DidNotReceive().OnLocalBranchListChanged();
115+
repositoryListener.DidNotReceive().OnHeadChanged();
116+
repositoryListener.DidNotReceive().OnLocksChanged(Arg.Any<IEnumerable<GitLock>>());
117+
repositoryListener.DidNotReceive().OnRepositoryInfoChanged();
118+
}
119+
}
120+
};

src/tests/TestUtils/TestUtils.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Reference Include="System.Xml" />
5959
</ItemGroup>
6060
<ItemGroup>
61+
<Compile Include="Events\IRepositoryListener.cs" />
6162
<Compile Include="Events\IRepositoryManagerListener.cs" />
6263
<Compile Include="Helpers\Args.cs" />
6364
<Compile Include="Helpers\AssertExtensions.cs" />

0 commit comments

Comments
 (0)