This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathVSGitExtTests.cs
More file actions
312 lines (261 loc) · 10.8 KB
/
VSGitExtTests.cs
File metadata and controls
312 lines (261 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Linq;
using System.Threading;
using System.ComponentModel;
using System.Collections.Generic;
using GitHub.Models;
using GitHub.Services;
using GitHub.VisualStudio;
using GitHub.VisualStudio.Base;
using NUnit.Framework;
using NSubstitute;
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;
using Task = System.Threading.Tasks.Task;
using static Microsoft.VisualStudio.VSConstants;
public class VSGitExtTests
{
public class TheConstructor : TestBaseClass
{
[TestCase(true, Guids.GitSccProviderId, 1)]
[TestCase(true, UICONTEXT.RepositoryOpen_string, 0, Description = "No longer using RepositoryOpen")]
[TestCase(false, Guids.GitSccProviderId, 0)]
public void GetServiceIGitExt_WhenGitSccProviderIsActive(bool isActive, string contextGuidString, int expectCalls)
{
var context = CreateVSUIContext(isActive);
var sp = Substitute.For<IAsyncServiceProvider>();
var target = CreateVSGitExt(context, sp: sp, contextGuidString: contextGuidString);
sp.Received(expectCalls).GetServiceAsync(typeof(IGitExt));
}
[TestCase(true, 1)]
[TestCase(false, 0)]
public void GetServiceIGitExt_WhenUIContextChanged(bool activated, int expectCalls)
{
var context = CreateVSUIContext(false);
var sp = Substitute.For<IAsyncServiceProvider>();
var target = CreateVSGitExt(context, sp: sp);
context.IsActive = activated;
target.JoinTillEmpty();
sp.Received(expectCalls).GetServiceAsync(typeof(IGitExt));
}
[Test]
public void ActiveRepositories_ReadUsingThreadPoolThread()
{
var gitExt = Substitute.For<IGitExt>();
bool? threadPool = null;
gitExt.ActiveRepositories.Returns(x =>
{
threadPool = Thread.CurrentThread.IsThreadPoolThread;
return new IGitRepositoryInfo[0];
});
var target = CreateVSGitExt(gitExt: gitExt);
target.JoinTillEmpty();
Assert.That(threadPool, Is.True);
}
}
public class TheActiveRepositoriesChangedEvent : TestBaseClass
{
[Test]
public void GitExtPropertyChangedEvent_ActiveRepositoriesChangedIsFired()
{
var context = CreateVSUIContext(true);
var gitExt = CreateGitExt();
var target = CreateVSGitExt(context, gitExt);
bool wasFired = false;
target.ActiveRepositoriesChanged += () => wasFired = true;
var eventArgs = new PropertyChangedEventArgs(nameof(gitExt.ActiveRepositories));
gitExt.PropertyChanged += Raise.Event<PropertyChangedEventHandler>(gitExt, eventArgs);
target.JoinTillEmpty();
Assert.That(wasFired, Is.True);
}
[Test]
public void ExceptionReadingActiveRepositories_StillEmptySoNoEvent()
{
var context = CreateVSUIContext(true);
var gitExt = CreateGitExt(new[] { "repoPath" });
gitExt.ActiveRepositories.Returns(x => { throw new Exception("Boom!"); });
var target = CreateVSGitExt(context, gitExt);
bool wasFired = false;
target.ActiveRepositoriesChanged += () => wasFired = true;
var eventArgs = new PropertyChangedEventArgs(nameof(gitExt.ActiveRepositories));
gitExt.PropertyChanged += Raise.Event<PropertyChangedEventHandler>(gitExt, eventArgs);
Assert.That(target.ActiveRepositories, Is.Empty);
Assert.That(wasFired, Is.False);
}
[Test]
public void WhenUIContextChanged_ActiveRepositoriesChangedIsFired()
{
var context = CreateVSUIContext(false);
var gitExt = CreateGitExt();
var target = CreateVSGitExt(context, gitExt);
bool wasFired = false;
target.ActiveRepositoriesChanged += () => wasFired = true;
context.IsActive = true;
target.JoinTillEmpty();
Assert.That(wasFired, Is.True);
}
[Test]
public void WhenUIContextChanged_FiredUsingThreadPoolThread()
{
var context = CreateVSUIContext(false);
var gitExt = CreateGitExt();
var target = CreateVSGitExt(context, gitExt);
bool? threadPool = null;
target.ActiveRepositoriesChanged += () => threadPool = Thread.CurrentThread.IsThreadPoolThread;
context.IsActive = true;
target.JoinTillEmpty();
Assert.That(threadPool, Is.True);
}
}
public class TheActiveRepositoriesProperty : TestBaseClass
{
[Test]
public void RepositoryOpenContextNotActive_IsEmpty()
{
var context = CreateVSUIContext(false);
var target = CreateVSGitExt(context);
Assert.That(target.ActiveRepositories, Is.Empty);
}
[Test]
public void RepositoryOpenIsActive_InitializeWithActiveRepositories()
{
var repoPath = "repoPath";
var repoFactory = Substitute.For<ILocalRepositoryModelFactory>();
var context = CreateVSUIContext(true);
var gitExt = CreateGitExt(new[] { repoPath });
var target = CreateVSGitExt(context, gitExt, repoFactory: repoFactory);
target.JoinTillEmpty();
var activeRepositories = target.ActiveRepositories;
Assert.That(activeRepositories.Count, Is.EqualTo(1));
repoFactory.Received(1).Create(repoPath);
}
[Test]
public void ExceptionRefreshingRepositories_ReturnsEmptyList()
{
var repoPath = "repoPath";
var repoFactory = Substitute.For<ILocalRepositoryModelFactory>();
repoFactory.Create(repoPath).ReturnsForAnyArgs(x => { throw new Exception("Boom!"); });
var context = CreateVSUIContext(true);
var gitExt = CreateGitExt(new[] { repoPath });
var target = CreateVSGitExt(context, gitExt, repoFactory: repoFactory);
target.JoinTillEmpty();
var activeRepositories = target.ActiveRepositories;
repoFactory.Received(1).Create(repoPath);
Assert.That(activeRepositories.Count, Is.EqualTo(0));
}
[Test]
public async Task ActiveRepositoriesChangedOrderingShouldBeCorrectAcrossThreads()
{
var gitExt = new MockGitExt();
var repoFactory = new MockRepositoryFactory();
var target = CreateVSGitExt(gitExt: gitExt, repoFactory: repoFactory);
var activeRepositories1 = CreateActiveRepositories("repo1");
var activeRepositories2 = CreateActiveRepositories("repo2");
var task1 = Task.Run(() => gitExt.ActiveRepositories = activeRepositories1);
await Task.Delay(1);
var task2 = Task.Run(() => gitExt.ActiveRepositories = activeRepositories2);
await Task.WhenAll(task1, task2);
target.JoinTillEmpty();
Assert.That(target.ActiveRepositories.Single().LocalPath, Is.EqualTo("repo2"));
}
}
static IReadOnlyList<IGitRepositoryInfo> CreateActiveRepositories(params string[] repositoryPaths)
{
var repositories = new List<IGitRepositoryInfo>();
foreach (var repositoryPath in repositoryPaths)
{
var repoInfo = Substitute.For<IGitRepositoryInfo>();
repoInfo.RepositoryPath.Returns(repositoryPath);
repositories.Add(repoInfo);
}
return repositories.AsReadOnly();
}
static VSGitExt CreateVSGitExt(IVSUIContext context = null, IGitExt gitExt = null, IAsyncServiceProvider sp = null,
ILocalRepositoryModelFactory repoFactory = null, JoinableTaskContext joinableTaskContext = null, string contextGuidString = null)
{
context = context ?? CreateVSUIContext(true);
gitExt = gitExt ?? CreateGitExt();
var contextGuid = new Guid(contextGuidString ?? Guids.GitSccProviderId);
sp = sp ?? Substitute.For<IAsyncServiceProvider>();
repoFactory = repoFactory ?? Substitute.For<ILocalRepositoryModelFactory>();
joinableTaskContext = joinableTaskContext ?? new JoinableTaskContext();
var factory = Substitute.For<IVSUIContextFactory>();
factory.GetUIContext(contextGuid).Returns(context);
sp.GetServiceAsync(typeof(IGitExt)).Returns(gitExt);
var vsGitExt = new VSGitExt(sp, factory, repoFactory, joinableTaskContext);
vsGitExt.JoinTillEmpty();
return vsGitExt;
}
static IGitExt CreateGitExt(params string[] repositoryPaths)
{
var gitExt = Substitute.For<IGitExt>();
var repoList = CreateActiveRepositories(repositoryPaths);
gitExt.ActiveRepositories.Returns(repoList);
return gitExt;
}
static MockVSUIContext CreateVSUIContext(bool isActive)
{
return new MockVSUIContext { IsActive = isActive };
}
class MockVSUIContext : IVSUIContext
{
bool isActive;
Action action;
public bool IsActive
{
get { return isActive; }
set
{
isActive = value;
if (isActive && action != null)
{
action.Invoke();
action = null;
}
}
}
public void WhenActivated(Action action)
{
if (isActive)
{
action.Invoke();
return;
}
this.action = action;
}
}
class MockGitExt : IGitExt
{
IReadOnlyList<IGitRepositoryInfo> activeRepositories = new IGitRepositoryInfo[0];
public IReadOnlyList<IGitRepositoryInfo> ActiveRepositories
{
get { return activeRepositories; }
set
{
if (activeRepositories != value)
{
activeRepositories = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ActiveRepositories)));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
class MockRepositoryFactory : ILocalRepositoryModelFactory
{
public ILocalRepositoryModel Create(string localPath)
{
var result = Substitute.For<ILocalRepositoryModel>();
result.LocalPath.Returns(localPath);
if (localPath == "repo1")
{
// Trying to force #1493 here by introducing a a delay on the first
// ActiveRepositories changed notification so that the second completes
// first.
Thread.Sleep(10);
}
return result;
}
}
}