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

Commit cfadc62

Browse files
committed
Add unit tests for ValidatePathWarning
PathWarning_Is_Set_For_Existing_Clone_At_Destination PathWarning_Is_Set_For_Repository_With_No_Origin PathWarning_Is_Set_For_Directory_With_No_Repository PathWarning_Is_Set_For_Existing_Repository_At_Destination_With_Different_Remote
1 parent 5f2012a commit cfadc62

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

test/GitHub.App.UnitTests/ViewModels/Dialog/Clone/RepositoryCloneViewModelTests.cs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.ComponentModel;
3+
using System.Globalization;
34
using System.IO;
45
using System.Linq.Expressions;
56
using System.Reactive.Linq;
@@ -9,6 +10,7 @@
910
using GitHub.Primitives;
1011
using GitHub.Services;
1112
using GitHub.ViewModels.Dialog.Clone;
13+
using LibGit2Sharp;
1214
using NSubstitute;
1315
using NUnit.Framework;
1416
using Rothko;
@@ -186,6 +188,63 @@ public async Task PathWarning_Is_Set_For_Existing_File_At_Destination()
186188
Assert.That(target.PathWarning, Is.EqualTo(Resources.DestinationAlreadyExists));
187189
}
188190

191+
[Test]
192+
public async Task PathWarning_Is_Set_For_Existing_Clone_At_Destination()
193+
{
194+
var owner = "owner";
195+
var repo = "repo";
196+
var remoteUrl = CreateGitHubUrl("owner", "repo");
197+
var gitService = CreateGitService(true, remoteUrl);
198+
var target = CreateTarget(gitService: gitService);
199+
SetRepository(target.GitHubTab, CreateRepositoryModel(owner, repo));
200+
target.Path = directoryExists;
201+
202+
Assert.That(target.PathWarning, Is.EqualTo(Resources.YouHaveAlreadyClonedToThisLocation));
203+
}
204+
205+
[Test]
206+
public async Task PathWarning_Is_Set_For_Repository_With_No_Origin()
207+
{
208+
var owner = "owner";
209+
var repo = "repo";
210+
var gitService = CreateGitService(true, null);
211+
var target = CreateTarget(gitService: gitService);
212+
SetRepository(target.GitHubTab, CreateRepositoryModel(owner, repo));
213+
target.Path = directoryExists;
214+
215+
Assert.That(target.PathWarning, Is.EqualTo(Resources.LocalRepositoryDoesntHaveARemoteOrigin));
216+
}
217+
218+
[Test]
219+
public async Task PathWarning_Is_Set_For_Directory_With_No_Repository()
220+
{
221+
var owner = "owner";
222+
var repo = "repo";
223+
var gitService = CreateGitService(false, null);
224+
var target = CreateTarget(gitService: gitService);
225+
SetRepository(target.GitHubTab, CreateRepositoryModel(owner, repo));
226+
target.Path = directoryExists;
227+
228+
Assert.That(target.PathWarning, Is.EqualTo(Resources.CantFindARepositoryAtLocalPath));
229+
}
230+
231+
[Test]
232+
public async Task PathWarning_Is_Set_For_Existing_Repository_At_Destination_With_Different_Remote()
233+
{
234+
var originalOwner = "original_Owner";
235+
var forkedOwner = "forked_owner";
236+
var repo = "repo";
237+
var forkedUrl = CreateGitHubUrl(forkedOwner, repo);
238+
var expectMessage = string.Format(CultureInfo.CurrentCulture, Resources.LocalRepositoryHasARemoteOf, forkedUrl);
239+
var gitService = CreateGitService(true, CreateGitHubUrl(forkedOwner, repo));
240+
var target = CreateTarget(gitService: gitService);
241+
SetRepository(target.GitHubTab, CreateRepositoryModel(originalOwner, repo));
242+
243+
target.Path = directoryExists;
244+
245+
Assert.That(target.PathWarning, Is.EqualTo(expectMessage));
246+
}
247+
189248
[Test]
190249
public async Task Repository_Name_Replaces_Last_Part_Of_Non_Base_Path()
191250
{
@@ -352,7 +411,7 @@ static RepositoryCloneViewModel CreateTarget(
352411
usageTracker = usageTracker ?? Substitute.For<IUsageTracker>();
353412
gitHubTab = gitHubTab ?? CreateSelectViewModel();
354413
enterpriseTab = enterpriseTab ?? CreateSelectViewModel();
355-
gitService = gitService ?? Substitute.For<IGitService>();
414+
gitService = gitService ?? CreateGitService(true, "https://github.com/owner/repo");
356415
urlTab = urlTab ?? CreateRepositoryUrlViewModel();
357416

358417
return new RepositoryCloneViewModel(
@@ -367,6 +426,21 @@ static RepositoryCloneViewModel CreateTarget(
367426
urlTab);
368427
}
369428

429+
private static IGitService CreateGitService(bool repositoryExists, UriString remoteUrl)
430+
{
431+
var gitService = Substitute.For<IGitService>();
432+
433+
IRepository repository = null;
434+
if (repositoryExists)
435+
{
436+
repository = Substitute.For<IRepository>();
437+
gitService.GetRemoteUri(repository).Returns(remoteUrl);
438+
}
439+
440+
gitService.GetRepository(directoryExists).Returns(repository);
441+
return gitService;
442+
}
443+
370444
static IUsageService CreateUsageService(bool isGroupA = false)
371445
{
372446
var usageService = Substitute.For<IUsageService>();
@@ -389,9 +463,15 @@ static IRepositoryModel CreateRepositoryModel(string owner, string name)
389463
var repository = Substitute.For<IRepositoryModel>();
390464
repository.Owner.Returns(owner);
391465
repository.Name.Returns(name);
466+
repository.CloneUrl.Returns(CreateGitHubUrl(owner, name));
392467
return repository;
393468
}
394469

470+
static UriString CreateGitHubUrl(string owner, string repo)
471+
{
472+
return new UriString($"https://github.com/{owner}/{repo}");
473+
}
474+
395475
static IRepositoryUrlViewModel CreateRepositoryUrlViewModel()
396476
{
397477
var repositoryUrlViewModel = Substitute.For<IRepositoryUrlViewModel>();

0 commit comments

Comments
 (0)