@@ -95,6 +95,25 @@ public async Task Indent_Heuristic_Is_Enabled(string content1, string content2,
9595 Assert . That ( changes . Patch . Replace ( '\n ' , '.' ) , Contains . Substring ( expectPatch ) ) ;
9696 }
9797 }
98+
99+ [ TestCase ( "foo.txt" , "a.b." , "bar.txt" , "a.b.c.d." , 2 ) ]
100+ [ TestCase ( @"dir\foo.txt" , "a.b." , @"dir\bar.txt" , "a.b.c.d." , 2 ) ]
101+ [ TestCase ( @"dir\foo.txt" , "a.b." , @"dir\foo.txt" , "a.b.c.d." , 2 ) ]
102+ [ TestCase ( @"dir\unrelated.txt" , "x.x.x.x." , @"dir\foo.txt" , "a.b.c.d." , 4 ) ]
103+ public async Task Rename ( string oldPath , string oldContent , string newPath , string newContent , int expectLinesAdded )
104+ {
105+ using ( var temp = new TempRepository ( ) )
106+ {
107+ var commit1 = AddCommit ( temp . Repository , oldPath , oldContent . Replace ( '.' , '\n ' ) ) ;
108+ var commit2 = AddCommit ( temp . Repository , newPath , newContent . Replace ( '.' , '\n ' ) ) ;
109+ var contentBytes = new UTF8Encoding ( false ) . GetBytes ( newContent . Replace ( '.' , '\n ' ) ) ;
110+ var target = new GitService ( new RepositoryFacade ( ) ) ;
111+
112+ var changes = await target . CompareWith ( temp . Repository , commit1 . Sha , commit2 . Sha , newPath , contentBytes ) ;
113+
114+ Assert . That ( changes ? . LinesAdded , Is . EqualTo ( expectLinesAdded ) ) ;
115+ }
116+ }
98117 }
99118
100119 public class TheCreateLocalRepositoryModelMethod
@@ -406,14 +425,25 @@ static Commit AddCommit(Repository repo, string path = "file.txt", string conten
406425 content = content ?? Guid . NewGuid ( ) . ToString ( ) ;
407426
408427 var dir = repo . Info . WorkingDirectory ;
428+ DeleteFilesNotInGit ( dir ) ;
409429 var file = Path . Combine ( dir , path ) ;
430+ Directory . CreateDirectory ( Path . GetDirectoryName ( file ) ) ;
410431 File . WriteAllText ( file , content ) ;
411- Commands . Stage ( repo , path ) ;
432+ Commands . Stage ( repo , "*" ) ;
412433 var signature = new Signature ( "foobar" , "[email protected] " , DateTime . Now ) ; 413434 var commit = repo . Commit ( "message" , signature , signature ) ;
414435 return commit ;
415436 }
416437
438+ static void DeleteFilesNotInGit ( string dir )
439+ {
440+ var gitDir = Path . Combine ( dir , @".git\" ) ;
441+ Directory . GetFiles ( dir , "*" , SearchOption . AllDirectories )
442+ . Where ( f => ! f . StartsWith ( gitDir , StringComparison . OrdinalIgnoreCase ) )
443+ . ToList ( )
444+ . ForEach ( File . Delete ) ;
445+ }
446+
417447 protected class TempRepository : TempDirectory
418448 {
419449 public TempRepository ( )
0 commit comments