1+ namespace GitTools . Tests . Git
2+ {
3+ using System ;
4+ using System . IO ;
5+ using System . Linq ;
6+ using GitTools . Git ;
7+ using IO ;
8+ using LibGit2Sharp ;
9+ using NUnit . Framework ;
10+ using Shouldly ;
11+ using Testing ;
12+
13+ [ TestFixture ]
14+ public class DynamicRepositoriesTests
15+ {
16+ const string DefaultBranchName = "master" ;
17+ const string SpecificBranchName = "feature/foo" ;
18+
19+ [ Test ]
20+ [ TestCase ( DefaultBranchName , DefaultBranchName ) ]
21+ [ TestCase ( SpecificBranchName , SpecificBranchName ) ]
22+ [ Category ( "NoMono" ) ]
23+ public void WorksCorrectlyWithRemoteRepository ( string branchName , string expectedBranchName )
24+ {
25+ var repoName = Guid . NewGuid ( ) . ToString ( ) ;
26+ var tempPath = Path . GetTempPath ( ) ;
27+ var tempDir = Path . Combine ( tempPath , repoName ) ;
28+ Directory . CreateDirectory ( tempDir ) ;
29+ string dynamicRepositoryPath = null ;
30+
31+ try
32+ {
33+ using ( var fixture = new EmptyRepositoryFixture ( ) )
34+ {
35+ var expectedDynamicRepoLocation = Path . Combine ( tempPath , fixture . RepositoryPath . Split ( Path . DirectorySeparatorChar ) . Last ( ) ) ;
36+
37+ fixture . Repository . MakeCommits ( 5 ) ;
38+ fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
39+
40+ var branch = fixture . Repository . CreateBranch ( SpecificBranchName ) ;
41+
42+ // Copy contents into working directory
43+ File . Copy ( Path . Combine ( fixture . RepositoryPath , "TestFile.txt" ) , Path . Combine ( tempDir , "TestFile.txt" ) ) ;
44+
45+ var repositoryInfo = new RepositoryInfo
46+ {
47+ Url = fixture . RepositoryPath
48+ } ;
49+
50+ using ( var dynamicRepository = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , branchName , branch . Tip . Sha ) )
51+ {
52+ dynamicRepositoryPath = dynamicRepository . Repository . Info . Path ;
53+ dynamicRepository . Repository . Info . Path . ShouldBe ( Path . Combine ( expectedDynamicRepoLocation , ".git\\ " ) ) ;
54+
55+ var currentBranch = dynamicRepository . Repository . Head . CanonicalName ;
56+
57+ currentBranch . ShouldEndWith ( expectedBranchName ) ;
58+ }
59+ }
60+ }
61+ finally
62+ {
63+ Directory . Delete ( tempDir , true ) ;
64+
65+ if ( dynamicRepositoryPath != null )
66+ {
67+ DeleteHelper . DeleteGitRepository ( dynamicRepositoryPath ) ;
68+ }
69+ }
70+ }
71+
72+ [ Test ]
73+ public void UpdatesExistingDynamicRepository ( )
74+ {
75+ var repoName = Guid . NewGuid ( ) . ToString ( ) ;
76+ var tempPath = Path . GetTempPath ( ) ;
77+ var tempDir = Path . Combine ( tempPath , repoName ) ;
78+ Directory . CreateDirectory ( tempDir ) ;
79+ string dynamicRepositoryPath = null ;
80+
81+ try
82+ {
83+ using ( var mainRepositoryFixture = new EmptyRepositoryFixture ( ) )
84+ {
85+ var commit = mainRepositoryFixture . Repository . MakeACommit ( ) ;
86+
87+ var repositoryInfo = new RepositoryInfo
88+ {
89+ Url = mainRepositoryFixture . RepositoryPath
90+ } ;
91+
92+ using ( var dynamicRepository = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , commit . Sha ) )
93+ {
94+ dynamicRepositoryPath = dynamicRepository . Repository . Info . Path ;
95+ }
96+
97+ var newCommit = mainRepositoryFixture . Repository . MakeACommit ( ) ;
98+
99+ using ( var dynamicRepository = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , newCommit . Sha ) )
100+ {
101+ dynamicRepository . Repository . Info . Path . ShouldBe ( dynamicRepositoryPath ) ;
102+ dynamicRepository . Repository . Commits . ShouldContain ( c => c . Sha == newCommit . Sha ) ;
103+ }
104+ }
105+ }
106+ finally
107+ {
108+ Directory . Delete ( tempDir , true ) ;
109+
110+ if ( dynamicRepositoryPath != null )
111+ {
112+ DeleteHelper . DeleteGitRepository ( dynamicRepositoryPath ) ;
113+ }
114+ }
115+ }
116+
117+ [ Test ]
118+ [ Category ( "NoMono" ) ]
119+ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken ( )
120+ {
121+ var repoName = Guid . NewGuid ( ) . ToString ( ) ;
122+ var tempPath = Path . GetTempPath ( ) ;
123+ var tempDir = Path . Combine ( tempPath , repoName ) ;
124+ Directory . CreateDirectory ( tempDir ) ;
125+ string expectedDynamicRepoLocation = null ;
126+
127+ try
128+ {
129+ using ( var fixture = new EmptyRepositoryFixture ( ) )
130+ {
131+ var head = fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
132+ File . Copy ( Path . Combine ( fixture . RepositoryPath , "TestFile.txt" ) , Path . Combine ( tempDir , "TestFile.txt" ) ) ;
133+ expectedDynamicRepoLocation = Path . Combine ( tempPath , fixture . RepositoryPath . Split ( Path . DirectorySeparatorChar ) . Last ( ) ) ;
134+ Directory . CreateDirectory ( expectedDynamicRepoLocation ) ;
135+
136+ var repositoryInfo = new RepositoryInfo
137+ {
138+ Url = fixture . RepositoryPath
139+ } ;
140+
141+ using ( var dynamicRepository = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , head . Sha ) )
142+ {
143+ dynamicRepository . Repository . Info . Path . ShouldBe ( Path . Combine ( expectedDynamicRepoLocation + "_1" , ".git\\ " ) ) ;
144+ }
145+ }
146+ }
147+ finally
148+ {
149+ DeleteHelper . DeleteDirectory ( tempDir , true ) ;
150+ if ( expectedDynamicRepoLocation != null )
151+ {
152+ DeleteHelper . DeleteDirectory ( expectedDynamicRepoLocation , true ) ;
153+ }
154+
155+ if ( expectedDynamicRepoLocation != null )
156+ {
157+ DeleteHelper . DeleteGitRepository ( expectedDynamicRepoLocation + "_1" ) ;
158+ }
159+ }
160+ }
161+
162+ [ Test ]
163+ [ Category ( "NoMono" ) ]
164+ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderIsInUse ( )
165+ {
166+ var tempPath = Path . GetTempPath ( ) ;
167+ var expectedDynamicRepoLocation = default ( string ) ;
168+ var expectedDynamicRepo2Location = default ( string ) ;
169+
170+ try
171+ {
172+ using ( var fixture = new EmptyRepositoryFixture ( ) )
173+ {
174+ var head = fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
175+ var repositoryInfo = new RepositoryInfo
176+ {
177+ Url = fixture . RepositoryPath
178+ } ;
179+
180+ using ( var dynamicRepository = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , head . Sha ) )
181+ using ( var dynamicRepository2 = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , head . Sha ) )
182+ {
183+ expectedDynamicRepoLocation = dynamicRepository . Repository . Info . Path ;
184+ expectedDynamicRepo2Location = dynamicRepository2 . Repository . Info . Path ;
185+ dynamicRepository . Repository . Info . Path . ShouldNotBe ( dynamicRepository2 . Repository . Info . Path ) ;
186+ }
187+ }
188+ }
189+ finally
190+ {
191+ if ( expectedDynamicRepoLocation != null )
192+ {
193+ DeleteHelper . DeleteDirectory ( expectedDynamicRepoLocation , true ) ;
194+ }
195+
196+ if ( expectedDynamicRepo2Location != null )
197+ {
198+ DeleteHelper . DeleteGitRepository ( expectedDynamicRepo2Location ) ;
199+ }
200+ }
201+ }
202+
203+ [ Test ]
204+ public void ThrowsExceptionWhenNotEnoughInfo ( )
205+ {
206+ var tempDir = Path . GetTempPath ( ) ;
207+
208+ var repositoryInfo = new RepositoryInfo
209+ {
210+ Url = tempDir
211+ } ;
212+
213+ Should . Throw < Exception > ( ( ) => DynamicRepositories . CreateOrOpen ( repositoryInfo , tempDir , null , null ) ) ;
214+ }
215+
216+ [ Test ]
217+ public void UsingDynamicRepositoryWithFeatureBranchWorks ( )
218+ {
219+ var repoName = Guid . NewGuid ( ) . ToString ( ) ;
220+ var tempPath = Path . GetTempPath ( ) ;
221+ var tempDir = Path . Combine ( tempPath , repoName ) ;
222+ Directory . CreateDirectory ( tempDir ) ;
223+
224+ try
225+ {
226+ using ( var mainRepositoryFixture = new EmptyRepositoryFixture ( ) )
227+ {
228+ var commit = mainRepositoryFixture . Repository . MakeACommit ( ) ;
229+
230+ var repositoryInfo = new RepositoryInfo
231+ {
232+ Url = mainRepositoryFixture . RepositoryPath
233+ } ;
234+
235+ Commands . Checkout ( mainRepositoryFixture . Repository , mainRepositoryFixture . Repository . CreateBranch ( "feature1" ) ) ;
236+
237+ Should . NotThrow ( ( ) =>
238+ {
239+ using ( DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "feature1" , commit . Sha ) )
240+ {
241+ }
242+ } ) ;
243+ }
244+ }
245+ finally
246+ {
247+ Directory . Delete ( tempDir , true ) ;
248+ }
249+ }
250+
251+ [ Test ]
252+ public void UsingDynamicRepositoryWithoutTargetBranchFails ( )
253+ {
254+ var tempPath = Path . GetTempPath ( ) ;
255+
256+ using ( var mainRepositoryFixture = new EmptyRepositoryFixture ( ) )
257+ {
258+ mainRepositoryFixture . Repository . MakeACommit ( ) ;
259+
260+ var repositoryInfo = new RepositoryInfo
261+ {
262+ Url = mainRepositoryFixture . RepositoryPath
263+ } ;
264+
265+ Should . Throw < GitToolsException > ( ( ) =>
266+ {
267+ using ( DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , null , null ) )
268+ {
269+ }
270+ } ) ;
271+ }
272+ }
273+
274+ [ Test ]
275+ public void UsingDynamicRepositoryWithoutTargetBranchCommitFails ( )
276+ {
277+ var tempPath = Path . GetTempPath ( ) ;
278+
279+ using ( var mainRepositoryFixture = new EmptyRepositoryFixture ( ) )
280+ {
281+ mainRepositoryFixture . Repository . MakeACommit ( ) ;
282+
283+ var repositoryInfo = new RepositoryInfo
284+ {
285+ Url = mainRepositoryFixture . RepositoryPath
286+ } ;
287+
288+ Should . Throw < GitToolsException > ( ( ) =>
289+ {
290+ using ( DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , null ) )
291+ {
292+ }
293+ } ) ;
294+ }
295+ }
296+
297+ [ Test ]
298+ public void TestErrorThrownForInvalidRepository ( )
299+ {
300+ var repoName = Guid . NewGuid ( ) . ToString ( ) ;
301+ var tempPath = Path . GetTempPath ( ) ;
302+ var tempDir = Path . Combine ( tempPath , repoName ) ;
303+ Directory . CreateDirectory ( tempDir ) ;
304+
305+ try
306+ {
307+ var repositoryInfo = new RepositoryInfo
308+ {
309+ Url = "http://127.0.0.1/testrepo.git"
310+ } ;
311+
312+ Should . Throw < Exception > ( ( ) =>
313+ {
314+ using ( DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , "sha" ) )
315+ {
316+ }
317+ } ) ;
318+ }
319+ finally
320+ {
321+ Directory . Delete ( tempDir , true ) ;
322+ }
323+ }
324+ }
325+ }
0 commit comments