11using System ;
22using System . ComponentModel ;
3+ using System . Globalization ;
34using System . IO ;
45using System . Linq . Expressions ;
56using System . Reactive . Linq ;
910using GitHub . Primitives ;
1011using GitHub . Services ;
1112using GitHub . ViewModels . Dialog . Clone ;
13+ using LibGit2Sharp ;
1214using NSubstitute ;
1315using NUnit . Framework ;
1416using 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