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 pathRepositoryCloneViewModelTests.cs
More file actions
486 lines (390 loc) · 19 KB
/
Copy pathRepositoryCloneViewModelTests.cs
File metadata and controls
486 lines (390 loc) · 19 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using GitHub.ViewModels.Dialog.Clone;
using LibGit2Sharp;
using NSubstitute;
using NUnit.Framework;
using Rothko;
namespace GitHub.App.UnitTests.ViewModels.Dialog.Clone
{
public class RepositoryCloneViewModelTests
{
const string directoryEmpty = "d:\\empty\\directory";
const string directoryExists = "d:\\exists\\directory";
const string fileExists = "d:\\exists\\file";
const string defaultPath = "d:\\default\\path";
public class TheUrlProperty
{
[TestCase("https://github.com;https://enterprise.com", null, 0)]
[TestCase("https://github.com;https://enterprise.com", "https://github.com/foo/bar", 0)]
[TestCase("https://github.com;https://enterprise.com", "https://enterprise.com/foo/bar", 1)]
[TestCase("https://github.com;https://enterprise.com", "HTTPS://ENTERPRISE.COM/FOO/BAR", 1)]
[TestCase("https://github.com;https://enterprise.com", "https://unknown.com/foo/bar", 0)]
public async Task Select_Tab_For_Url(string addresses, string url, int expectTabIndex)
{
var cm = CreateConnectionManager(addresses.Split(';'));
var target = CreateTarget(connectionManager: cm);
target.Url = url;
await target.InitializeAsync(null);
Assert.That(target.SelectedTabIndex, Is.EqualTo(expectTabIndex));
}
[TestCase("https://github.com;https://enterprise.com", null, "", "")]
[TestCase("https://github.com;https://enterprise.com", "https://github.com/foo/bar", "https://github.com/foo/bar", "")]
[TestCase("https://github.com;https://enterprise.com", "https://enterprise.com/foo/bar", "", "https://enterprise.com/foo/bar")]
[TestCase("https://github.com;https://enterprise.com", "HTTPS://GITHUB.COM/FOO/BAR", "HTTPS://GITHUB.COM/FOO/BAR", "")]
[TestCase("https://github.com;https://enterprise.com", "HTTPS://ENTERPRISE.COM/FOO/BAR", "", "HTTPS://ENTERPRISE.COM/FOO/BAR")]
[TestCase("https://github.com;https://enterprise.com", "https://unknown.com/foo/bar", "", "")]
public async Task Set_Filter_For_Url(string addresses, string url, string expectGitHubFilter, string expectEnterpriseFilter)
{
var cm = CreateConnectionManager(addresses.Split(';'));
var target = CreateTarget(connectionManager: cm);
target.Url = url;
await target.InitializeAsync(null);
Assert.That(target.GitHubTab.Filter, Is.EqualTo(expectGitHubFilter));
Assert.That(target.EnterpriseTab.Filter, Is.EqualTo(expectEnterpriseFilter));
}
}
[Test]
public async Task GitHubPage_Is_Initialized()
{
var cm = CreateConnectionManager("https://github.com");
var target = CreateTarget(connectionManager: cm);
await target.InitializeAsync(null);
target.GitHubTab.Received(1).Initialize(cm.Connections[0]);
target.EnterpriseTab.DidNotReceiveWithAnyArgs().Initialize(null);
}
[Test]
public async Task EnterprisePage_Is_Initialized()
{
var cm = CreateConnectionManager("https://enterprise.com");
var target = CreateTarget(connectionManager: cm);
await target.InitializeAsync(null);
target.GitHubTab.DidNotReceiveWithAnyArgs().Initialize(null);
target.EnterpriseTab.Received(1).Initialize(cm.Connections[0]);
}
[Test]
public async Task GitHub_And_Enterprise_Pages_Are_Initialized()
{
var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
var target = CreateTarget(connectionManager: cm);
await target.InitializeAsync(null);
target.GitHubTab.Received(1).Initialize(cm.Connections[0]);
target.EnterpriseTab.Received(1).Initialize(cm.Connections[1]);
}
[Test]
public async Task GitHubPage_Is_Loaded()
{
var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
var target = CreateTarget(connectionManager: cm);
await target.InitializeAsync(cm.Connections[0]);
await target.GitHubTab.Received(1).Activate();
await target.EnterpriseTab.DidNotReceive().Activate();
}
[Test]
public async Task Enterprise_Is_Loaded()
{
var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
var target = CreateTarget(connectionManager: cm);
await target.InitializeAsync(cm.Connections[1]);
await target.GitHubTab.DidNotReceive().Activate();
await target.EnterpriseTab.Received(1).Activate();
}
[Test]
public async Task Switching_To_GitHubPage_Loads_It()
{
var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
var target = CreateTarget(connectionManager: cm);
await target.InitializeAsync(cm.Connections[1]);
await target.GitHubTab.DidNotReceive().Activate();
target.SelectedTabIndex = 0;
await target.GitHubTab.Received(1).Activate();
}
[Test]
public async Task Switching_To_EnterprisePage_Loads_It()
{
var cm = CreateConnectionManager("https://github.com", "https://enterprise.com");
var target = CreateTarget(connectionManager: cm);
await target.InitializeAsync(cm.Connections[0]);
await target.EnterpriseTab.DidNotReceive().Activate();
target.SelectedTabIndex = 1;
await target.EnterpriseTab.Received(1).Activate();
}
[Test]
public void Path_Is_Initialized()
{
var target = CreateTarget();
Assert.That(target.Path, Is.EqualTo(defaultPath));
}
[Test]
public void Owner_And_Repository_Name_Is_Appended_To_Base_Path()
{
var owner = "owner";
var repo = "repo";
var target = CreateTarget();
var expectPath = Path.Combine(defaultPath, owner, repo);
SetRepository(target.GitHubTab, CreateRepositoryModel(owner, repo));
Assert.That(target.Path, Is.EqualTo(expectPath));
}
[Test]
public void PathWarning_Is_Not_Set_When_No_Repository_Selected()
{
var target = CreateTarget();
target.Path = directoryExists;
Assert.That(target.PathWarning, Is.Null);
}
[Test]
public void PathWarning_Is_Set_For_Existing_File_At_Destination()
{
var target = CreateTarget();
SetRepository(target.GitHubTab, CreateRepositoryModel("owner", "repo"));
target.Path = fileExists;
Assert.That(target.PathWarning, Is.EqualTo(Resources.DestinationAlreadyExists));
}
[Test]
public void PathWarning_Is_Set_For_Existing_Clone_At_Destination()
{
var owner = "owner";
var repo = "repo";
var remoteUrl = CreateGitHubUrl("owner", "repo");
var gitService = CreateGitService(true, remoteUrl);
var target = CreateTarget(gitService: gitService);
SetRepository(target.GitHubTab, CreateRepositoryModel(owner, repo));
target.Path = directoryExists;
Assert.That(target.PathWarning, Is.EqualTo(Resources.YouHaveAlreadyClonedToThisLocation));
}
[Test]
public void PathWarning_Is_Set_For_Repository_With_No_Origin()
{
var owner = "owner";
var repo = "repo";
var gitService = CreateGitService(true, null);
var target = CreateTarget(gitService: gitService);
SetRepository(target.GitHubTab, CreateRepositoryModel(owner, repo));
target.Path = directoryExists;
Assert.That(target.PathWarning, Is.EqualTo(Resources.LocalRepositoryDoesntHaveARemoteOrigin));
}
[Test]
public void PathWarning_Is_Set_For_Directory_With_No_Repository()
{
var owner = "owner";
var repo = "repo";
var gitService = CreateGitService(false, null);
var target = CreateTarget(gitService: gitService);
SetRepository(target.GitHubTab, CreateRepositoryModel(owner, repo));
target.Path = directoryExists;
Assert.That(target.PathWarning, Is.EqualTo(Resources.DirectoryAtDestinationNotEmpty));
}
[Test]
public void PathWarning_Is_Set_For_Existing_Repository_At_Destination_With_Different_Remote()
{
var originalOwner = "original_Owner";
var forkedOwner = "forked_owner";
var repo = "repo";
var forkedUrl = CreateGitHubUrl(forkedOwner, repo);
var expectMessage = string.Format(CultureInfo.CurrentCulture, Resources.LocalRepositoryHasARemoteOf, forkedUrl);
var gitService = CreateGitService(true, CreateGitHubUrl(forkedOwner, repo));
var target = CreateTarget(gitService: gitService);
SetRepository(target.GitHubTab, CreateRepositoryModel(originalOwner, repo));
target.Path = directoryExists;
Assert.That(target.PathWarning, Is.EqualTo(expectMessage));
}
[Test]
public void PathWarning_Is_Not_Set_When_EmptyDirectoryExists_Selected()
{
var target = CreateTarget();
target.Path = directoryEmpty;
Assert.That(target.PathWarning, Is.Null);
}
[Test]
public void Repository_Name_Replaces_Last_Part_Of_Non_Base_Path()
{
var target = CreateTarget();
var owner = "owner";
target.Path = "d:\\efault";
SetRepository(target.GitHubTab, CreateRepositoryModel(owner, "name"));
target.Path = $"d:\\efault\\{owner}\\foo";
SetRepository(target.GitHubTab, CreateRepositoryModel(owner, "repo"));
Assert.That(target.Path, Is.EqualTo($"d:\\efault\\{owner}\\repo"));
}
[TestCase("c:\\base", "owner1/repo1", "c:\\base\\owner1\\repo1", "owner2/repo2", "c:\\base\\owner2\\repo2",
Description = "Path unchanged")]
[TestCase("c:\\base", "owner1/repo1", "c:\\base\\owner1\\changed", "owner2/repo2", "c:\\base\\owner2\\repo2",
Description = "Repo name changed")]
[TestCase("c:\\base", "owner1/repo1", "c:\\base\\owner1", "owner2/repo2", "c:\\base\\owner2\\repo2",
Description = "Repo name deleted")]
[TestCase("c:\\base", "owner1/repo1", "c:\\base", "owner2/repo2", "c:\\base\\owner2\\repo2",
Description = "Base path reverted")]
[TestCase("c:\\base", "owner1/repo1", "c:\\new\\base\\owner1\\changed", "owner2/repo2", "c:\\new\\base\\owner2\\repo2",
Description = "Base path and repo name changed")]
[TestCase("c:\\base", "owner1/repo1", "c:\\new\\base\\owner1", "owner2/repo2", "c:\\new\\base\\owner2\\repo2",
Description = "Base path changed and repo name deleted")]
[TestCase("c:\\base", "owner1/repo1", "c:\\new\\base", "owner2/repo2", "c:\\new\\base\\owner2\\repo2",
Description = "Base path changed and repo owner/name deleted")]
[TestCase("c:\\base", "owner1/repo1", "", "owner2/repo2", "c:\\base\\owner2\\repo2",
Description = "Base path cleared")]
[TestCase("c:\\base", "owner1/repo1", "c:\\base\\repo1", "owner2/repo2", "c:\\base\\owner2\\repo2",
Description = "Owner deleted")]
[TestCase("c:\\base", "same/same", "c:\\base\\same\\same", "owner2/repo2", "c:\\base\\owner2\\repo2",
Description = "Owner and repo have same name")]
public void User_Edits_Path(string defaultClonePath, string repo1, string userPath, string repo2, string expectPath)
{
var target = CreateTarget(defaultClonePath: defaultClonePath);
SetRepository(target.GitHubTab, CreateRepositoryModel(repo1));
target.Path = userPath;
SetRepository(target.GitHubTab, CreateRepositoryModel(repo2));
Assert.That(target.Path, Is.EqualTo(expectPath));
}
[Test]
public async Task Clone_Is_Initially_Disabled()
{
var target = CreateTarget();
await target.InitializeAsync(null);
Assert.That(target.Clone.CanExecute(null), Is.False);
}
[Test]
public async Task Clone_Is_Enabled_When_Repository_Selected()
{
var target = CreateTarget();
await target.InitializeAsync(null);
SetRepository(target.GitHubTab, CreateRepositoryModel());
Assert.That(target.Clone.CanExecute(null), Is.True);
}
[Test]
public async Task Clone_Is_Disabled_When_Path_DirectoryExists()
{
var target = CreateTarget();
await target.InitializeAsync(null);
SetRepository(target.GitHubTab, CreateRepositoryModel());
Assert.That(target.Clone.CanExecute(null), Is.True);
target.Path = directoryExists;
Assert.That(target.Clone.CanExecute(null), Is.False);
}
[Test]
public async Task Open_Is_Enabled_When_Path_DirectoryExists()
{
var target = CreateTarget();
await target.InitializeAsync(null);
Assert.That(target.Open.CanExecute(null), Is.False);
SetRepository(target.GitHubTab, CreateRepositoryModel());
target.Path = directoryExists;
Assert.That(target.Open.CanExecute(null), Is.True);
}
[Test]
public async Task Clone_Is_Enabled_When_Path_EmptyDirectoryExists()
{
var target = CreateTarget();
await target.InitializeAsync(null);
SetRepository(target.GitHubTab, CreateRepositoryModel());
Assert.That(target.Clone.CanExecute(null), Is.True);
target.Path = directoryEmpty;
Assert.That(target.Clone.CanExecute(null), Is.True);
}
static void SetRepository(IRepositoryCloneTabViewModel vm, RepositoryModel repository)
{
vm.Repository.Returns(repository);
vm.PropertyChanged += Raise.Event<PropertyChangedEventHandler>(
vm,
new PropertyChangedEventArgs(nameof(vm.Repository)));
}
static IConnectionManager CreateConnectionManager(params string[] addresses)
{
var result = Substitute.For<IConnectionManager>();
var connections = new ObservableCollectionEx<IConnection>();
result.Connections.Returns(connections);
result.GetLoadedConnections().Returns(connections);
result.GetConnection(null).ReturnsForAnyArgs(default(IConnection));
foreach (var address in addresses)
{
var connection = Substitute.For<IConnection>();
var hostAddress = HostAddress.Create(address);
connection.HostAddress.Returns(hostAddress);
connection.IsLoggedIn.Returns(true);
connections.Add(connection);
result.GetConnection(hostAddress).Returns(connection);
}
return result;
}
static IRepositorySelectViewModel CreateSelectViewModel()
{
var result = Substitute.For<IRepositorySelectViewModel>();
result.Repository.Returns((RepositoryModel)null);
result.WhenForAnyArgs(x => x.Initialize(null)).Do(_ => result.IsEnabled.Returns(true));
return result;
}
static IRepositoryCloneService CreateRepositoryCloneService(string defaultClonePath)
{
var result = Substitute.For<IRepositoryCloneService>();
result.DefaultClonePath.Returns(defaultClonePath);
result.DestinationDirectoryExists(directoryExists).Returns(true);
result.DestinationDirectoryExists(directoryEmpty).Returns(true);
result.DestinationDirectoryEmpty(directoryEmpty).Returns(true);
result.DestinationFileExists(directoryExists).Returns(false);
result.DestinationDirectoryExists(fileExists).Returns(false);
result.DestinationFileExists(fileExists).Returns(true);
return result;
}
static RepositoryCloneViewModel CreateTarget(
IOperatingSystem os = null,
IConnectionManager connectionManager = null,
IRepositoryCloneService service = null,
IUsageTracker usageTracker = null,
IDialogService dialogService = null,
IRepositorySelectViewModel gitHubTab = null,
IRepositorySelectViewModel enterpriseTab = null,
IGitService gitService = null,
string defaultClonePath = defaultPath)
{
os = os ?? Substitute.For<IOperatingSystem>();
connectionManager = connectionManager ?? CreateConnectionManager("https://github.com");
service = service ?? CreateRepositoryCloneService(defaultClonePath);
usageTracker = usageTracker ?? Substitute.For<IUsageTracker>();
dialogService = dialogService ?? Substitute.For<IDialogService>();
gitHubTab = gitHubTab ?? CreateSelectViewModel();
enterpriseTab = enterpriseTab ?? CreateSelectViewModel();
gitService = gitService ?? CreateGitService(true, "https://github.com/owner/repo");
return new RepositoryCloneViewModel(
os,
connectionManager,
service,
gitService,
usageTracker,
dialogService,
gitHubTab,
enterpriseTab);
}
private static IGitService CreateGitService(bool repositoryExists, UriString remoteUrl)
{
var gitService = Substitute.For<IGitService>();
IRepository repository = null;
if (repositoryExists)
{
repository = Substitute.For<IRepository>();
gitService.GetRemoteUri(repository).Returns(remoteUrl);
}
gitService.GetRepository(directoryExists).Returns(repository);
return gitService;
}
static RepositoryModel CreateRepositoryModel(string repo = "owner/repo")
{
var split = repo.Split('/');
var (owner, name) = (split[0], split[1]);
return CreateRepositoryModel(owner, name);
}
static RepositoryModel CreateRepositoryModel(string owner, string name)
{
var cloneUrl = CreateGitHubUrl(owner, name);
return new RepositoryModel(name, cloneUrl);
}
static UriString CreateGitHubUrl(string owner, string repo)
{
return new UriString($"https://github.com/{owner}/{repo}");
}
}
}