11
11
using Testing ;
12
12
13
13
[ TestFixture ]
14
- public class GitRepositoryFactoryTests
14
+ public class DynamicRepositoriesTests
15
15
{
16
16
const string DefaultBranchName = "master" ;
17
17
const string SpecificBranchName = "feature/foo" ;
@@ -37,25 +37,22 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
37
37
fixture . Repository . MakeCommits ( 5 ) ;
38
38
fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
39
39
40
- fixture . Repository . CreateBranch ( SpecificBranchName ) ;
40
+ var branch = fixture . Repository . CreateBranch ( SpecificBranchName ) ;
41
41
42
42
// Copy contents into working directory
43
43
File . Copy ( Path . Combine ( fixture . RepositoryPath , "TestFile.txt" ) , Path . Combine ( tempDir , "TestFile.txt" ) ) ;
44
44
45
45
var repositoryInfo = new RepositoryInfo
46
46
{
47
- Url = fixture . RepositoryPath ,
48
- Branch = branchName
47
+ Url = fixture . RepositoryPath
49
48
} ;
50
49
51
- using ( var gitRepository = GitRepositoryFactory . CreateRepository ( repositoryInfo ) )
50
+ using ( var dynamicRepository = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , branchName , branch . Tip . Sha ) )
52
51
{
53
- dynamicRepositoryPath = gitRepository . DotGitDirectory ;
52
+ dynamicRepositoryPath = dynamicRepository . Repository . Info . Path ;
53
+ dynamicRepository . Repository . Info . Path . ShouldBe ( Path . Combine ( expectedDynamicRepoLocation , ".git\\ " ) ) ;
54
54
55
- gitRepository . IsDynamic . ShouldBe ( true ) ;
56
- gitRepository . DotGitDirectory . ShouldBe ( Path . Combine ( expectedDynamicRepoLocation , ".git" ) ) ;
57
-
58
- var currentBranch = gitRepository . Repository . Head . CanonicalName ;
55
+ var currentBranch = dynamicRepository . Repository . Head . CanonicalName ;
59
56
60
57
currentBranch . ShouldEndWith ( expectedBranchName ) ;
61
58
}
@@ -85,26 +82,24 @@ public void UpdatesExistingDynamicRepository()
85
82
{
86
83
using ( var mainRepositoryFixture = new EmptyRepositoryFixture ( ) )
87
84
{
88
- mainRepositoryFixture . Repository . MakeCommits ( 1 ) ;
85
+ var commit = mainRepositoryFixture . Repository . MakeACommit ( ) ;
89
86
90
87
var repositoryInfo = new RepositoryInfo
91
88
{
92
- Url = mainRepositoryFixture . RepositoryPath ,
93
- Branch = "master"
89
+ Url = mainRepositoryFixture . RepositoryPath
94
90
} ;
95
91
96
- using ( var gitRepository = GitRepositoryFactory . CreateRepository ( repositoryInfo ) )
92
+ using ( var dynamicRepository = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , commit . Sha ) )
97
93
{
98
- dynamicRepositoryPath = gitRepository . DotGitDirectory ;
94
+ dynamicRepositoryPath = dynamicRepository . Repository . Info . Path ;
99
95
}
100
96
101
97
var newCommit = mainRepositoryFixture . Repository . MakeACommit ( ) ;
102
98
103
- using ( var gitRepository = GitRepositoryFactory . CreateRepository ( repositoryInfo ) )
99
+ using ( var dynamicRepository = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , newCommit . Sha ) )
104
100
{
105
- mainRepositoryFixture . Repository . DumpGraph ( ) ;
106
- gitRepository . Repository . DumpGraph ( ) ;
107
- gitRepository . Repository . Commits . ShouldContain ( c => c . Sha == newCommit . Sha ) ;
101
+ dynamicRepository . Repository . Info . Path . ShouldBe ( dynamicRepositoryPath ) ;
102
+ dynamicRepository . Repository . Commits . ShouldContain ( c => c . Sha == newCommit . Sha ) ;
108
103
}
109
104
}
110
105
}
@@ -133,21 +128,19 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
133
128
{
134
129
using ( var fixture = new EmptyRepositoryFixture ( ) )
135
130
{
136
- fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
131
+ var head = fixture . Repository . CreateFileAndCommit ( "TestFile.txt" ) ;
137
132
File . Copy ( Path . Combine ( fixture . RepositoryPath , "TestFile.txt" ) , Path . Combine ( tempDir , "TestFile.txt" ) ) ;
138
133
expectedDynamicRepoLocation = Path . Combine ( tempPath , fixture . RepositoryPath . Split ( Path . DirectorySeparatorChar ) . Last ( ) ) ;
139
134
Directory . CreateDirectory ( expectedDynamicRepoLocation ) ;
140
135
141
136
var repositoryInfo = new RepositoryInfo
142
137
{
143
- Url = fixture . RepositoryPath ,
144
- Branch = "master"
138
+ Url = fixture . RepositoryPath
145
139
} ;
146
140
147
- using ( var gitRepository = GitRepositoryFactory . CreateRepository ( repositoryInfo ) )
141
+ using ( var dynamicRepository = DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , head . Sha ) )
148
142
{
149
- gitRepository . IsDynamic . ShouldBe ( true ) ;
150
- gitRepository . DotGitDirectory . ShouldBe ( Path . Combine ( expectedDynamicRepoLocation + "_1" , ".git" ) ) ;
143
+ dynamicRepository . Repository . Info . Path . ShouldBe ( Path . Combine ( expectedDynamicRepoLocation + "_1" , ".git\\ " ) ) ;
151
144
}
152
145
}
153
146
}
@@ -166,18 +159,58 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
166
159
}
167
160
}
168
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
+
169
203
[ Test ]
170
204
public void ThrowsExceptionWhenNotEnoughInfo ( )
171
205
{
172
206
var tempDir = Path . GetTempPath ( ) ;
173
207
174
208
var repositoryInfo = new RepositoryInfo
175
209
{
176
- Url = tempDir ,
177
- Branch = "master"
210
+ Url = tempDir
178
211
} ;
179
212
180
- Should . Throw < Exception > ( ( ) => GitRepositoryFactory . CreateRepository ( repositoryInfo ) ) ;
213
+ Should . Throw < Exception > ( ( ) => DynamicRepositories . CreateOrOpen ( repositoryInfo , tempDir , null , null ) ) ;
181
214
}
182
215
183
216
[ Test ]
@@ -192,21 +225,19 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
192
225
{
193
226
using ( var mainRepositoryFixture = new EmptyRepositoryFixture ( ) )
194
227
{
195
- mainRepositoryFixture . Repository . MakeACommit ( ) ;
228
+ var commit = mainRepositoryFixture . Repository . MakeACommit ( ) ;
196
229
197
230
var repositoryInfo = new RepositoryInfo
198
231
{
199
- Url = mainRepositoryFixture . RepositoryPath ,
200
- Branch = "feature1"
232
+ Url = mainRepositoryFixture . RepositoryPath
201
233
} ;
202
234
203
235
mainRepositoryFixture . Repository . Checkout ( mainRepositoryFixture . Repository . CreateBranch ( "feature1" ) ) ;
204
236
205
237
Should . NotThrow ( ( ) =>
206
238
{
207
- using ( var gitRepository = GitRepositoryFactory . CreateRepository ( repositoryInfo ) )
239
+ using ( DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "feature1" , commit . Sha ) )
208
240
{
209
- // this code shouldn't throw
210
241
}
211
242
} ) ;
212
243
}
@@ -220,35 +251,46 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
220
251
[ Test ]
221
252
public void UsingDynamicRepositoryWithoutTargetBranchFails ( )
222
253
{
223
- var repoName = Guid . NewGuid ( ) . ToString ( ) ;
224
254
var tempPath = Path . GetTempPath ( ) ;
225
- var tempDir = Path . Combine ( tempPath , repoName ) ;
226
- Directory . CreateDirectory ( tempDir ) ;
227
255
228
- try
256
+ using ( var mainRepositoryFixture = new EmptyRepositoryFixture ( ) )
229
257
{
230
- using ( var mainRepositoryFixture = new EmptyRepositoryFixture ( ) )
231
- {
232
- mainRepositoryFixture . Repository . MakeACommit ( ) ;
258
+ mainRepositoryFixture . Repository . MakeACommit ( ) ;
233
259
234
- var repositoryInfo = new RepositoryInfo
235
- {
236
- Url = mainRepositoryFixture . RepositoryPath ,
237
- Branch = null
238
- } ;
260
+ var repositoryInfo = new RepositoryInfo
261
+ {
262
+ Url = mainRepositoryFixture . RepositoryPath
263
+ } ;
239
264
240
- Should . Throw < Exception > ( ( ) =>
265
+ Should . Throw < GitToolsException > ( ( ) =>
266
+ {
267
+ using ( DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , null , null ) )
241
268
{
242
- using ( var gitRepository = GitRepositoryFactory . CreateRepository ( repositoryInfo ) )
243
- {
244
- // this code shouldn't throw
245
- }
246
- } ) ;
247
- }
269
+ }
270
+ } ) ;
248
271
}
249
- finally
272
+ }
273
+
274
+ [ Test ]
275
+ public void UsingDynamicRepositoryWithoutTargetBranchCommitFails ( )
276
+ {
277
+ var tempPath = Path . GetTempPath ( ) ;
278
+
279
+ using ( var mainRepositoryFixture = new EmptyRepositoryFixture ( ) )
250
280
{
251
- Directory . Delete ( tempDir , true ) ;
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
+ } ) ;
252
294
}
253
295
}
254
296
@@ -264,15 +306,13 @@ public void TestErrorThrownForInvalidRepository()
264
306
{
265
307
var repositoryInfo = new RepositoryInfo
266
308
{
267
- Url = "http://127.0.0.1/testrepo.git" ,
268
- Branch = "master"
309
+ Url = "http://127.0.0.1/testrepo.git"
269
310
} ;
270
311
271
312
Should . Throw < Exception > ( ( ) =>
272
313
{
273
- using ( var gitRepository = GitRepositoryFactory . CreateRepository ( repositoryInfo ) )
314
+ using ( DynamicRepositories . CreateOrOpen ( repositoryInfo , tempPath , "master" , "sha" ) )
274
315
{
275
- // this code shouldn't throw
276
316
}
277
317
} ) ;
278
318
}
0 commit comments