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

Commit 9a2d01a

Browse files
committed
Lock dynamic repositories so they are safe for multiple build agents on same VM
1 parent a1c9cdc commit 9a2d01a

File tree

8 files changed

+260
-104
lines changed

8 files changed

+260
-104
lines changed

src/GitTools.Core.Tests/Git/GitRepositoryFactoryTests.cs renamed to src/GitTools.Core.Tests/Git/DynamicRepositoriesTests.cs

Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using Testing;
1212

1313
[TestFixture]
14-
public class GitRepositoryFactoryTests
14+
public class DynamicRepositoriesTests
1515
{
1616
const string DefaultBranchName = "master";
1717
const string SpecificBranchName = "feature/foo";
@@ -47,12 +47,12 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
4747
Url = fixture.RepositoryPath
4848
};
4949

50-
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, branchName, branch.Tip.Sha))
50+
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, branchName, branch.Tip.Sha))
5151
{
52-
dynamicRepositoryPath = gitRepository.Info.Path;
53-
gitRepository.Info.Path.ShouldBe(Path.Combine(expectedDynamicRepoLocation, ".git"));
52+
dynamicRepositoryPath = dynamicRepository.Repository.Info.Path;
53+
dynamicRepository.Repository.Info.Path.ShouldBe(Path.Combine(expectedDynamicRepoLocation, ".git\\"));
5454

55-
var currentBranch = gitRepository.Head.CanonicalName;
55+
var currentBranch = dynamicRepository.Repository.Head.CanonicalName;
5656

5757
currentBranch.ShouldEndWith(expectedBranchName);
5858
}
@@ -89,17 +89,17 @@ public void UpdatesExistingDynamicRepository()
8989
Url = mainRepositoryFixture.RepositoryPath
9090
};
9191

92-
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", commit.Sha))
92+
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", commit.Sha))
9393
{
94-
dynamicRepositoryPath = gitRepository.Info.Path;
94+
dynamicRepositoryPath = dynamicRepository.Repository.Info.Path;
9595
}
9696

9797
var newCommit = mainRepositoryFixture.Repository.MakeACommit();
9898

99-
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", commit.Sha))
99+
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", newCommit.Sha))
100100
{
101-
gitRepository.Info.Path.ShouldBe(dynamicRepositoryPath);
102-
gitRepository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
101+
dynamicRepository.Repository.Info.Path.ShouldBe(dynamicRepositoryPath);
102+
dynamicRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
103103
}
104104
}
105105
}
@@ -128,21 +128,19 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
128128
{
129129
using (var fixture = new EmptyRepositoryFixture())
130130
{
131-
fixture.Repository.CreateFileAndCommit("TestFile.txt");
131+
var head = fixture.Repository.CreateFileAndCommit("TestFile.txt");
132132
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
133133
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split(Path.DirectorySeparatorChar).Last());
134134
Directory.CreateDirectory(expectedDynamicRepoLocation);
135135

136136
var repositoryInfo = new RepositoryInfo
137137
{
138-
Url = fixture.RepositoryPath,
139-
TargetBranch = "master"
138+
Url = fixture.RepositoryPath
140139
};
141140

142-
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo))
141+
using (var dynamicRepository = DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", head.Sha))
143142
{
144-
gitRepository.IsDynamic.ShouldBe(true);
145-
gitRepository.DotGitDirectory.ShouldBe(Path.Combine(expectedDynamicRepoLocation + "_1", ".git"));
143+
dynamicRepository.Repository.Info.Path.ShouldBe(Path.Combine(expectedDynamicRepoLocation + "_1", ".git\\"));
146144
}
147145
}
148146
}
@@ -161,18 +159,58 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
161159
}
162160
}
163161

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+
164203
[Test]
165204
public void ThrowsExceptionWhenNotEnoughInfo()
166205
{
167206
var tempDir = Path.GetTempPath();
168207

169208
var repositoryInfo = new RepositoryInfo
170209
{
171-
Url = tempDir,
172-
TargetBranch = "master"
210+
Url = tempDir
173211
};
174212

175-
Should.Throw<Exception>(() => DynamicRepositories.CreateOrOpen(repositoryInfo));
213+
Should.Throw<Exception>(() => DynamicRepositories.CreateOrOpen(repositoryInfo, tempDir, null, null));
176214
}
177215

178216
[Test]
@@ -187,21 +225,19 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
187225
{
188226
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
189227
{
190-
mainRepositoryFixture.Repository.MakeACommit();
228+
var commit = mainRepositoryFixture.Repository.MakeACommit();
191229

192230
var repositoryInfo = new RepositoryInfo
193231
{
194-
Url = mainRepositoryFixture.RepositoryPath,
195-
TargetBranch = "feature1"
232+
Url = mainRepositoryFixture.RepositoryPath
196233
};
197234

198235
mainRepositoryFixture.Repository.Checkout(mainRepositoryFixture.Repository.CreateBranch("feature1"));
199236

200237
Should.NotThrow(() =>
201238
{
202-
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo))
239+
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "feature1", commit.Sha))
203240
{
204-
// this code shouldn't throw
205241
}
206242
});
207243
}
@@ -215,35 +251,46 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
215251
[Test]
216252
public void UsingDynamicRepositoryWithoutTargetBranchFails()
217253
{
218-
var repoName = Guid.NewGuid().ToString();
219254
var tempPath = Path.GetTempPath();
220-
var tempDir = Path.Combine(tempPath, repoName);
221-
Directory.CreateDirectory(tempDir);
222255

223-
try
256+
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
224257
{
225-
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
226-
{
227-
mainRepositoryFixture.Repository.MakeACommit();
258+
mainRepositoryFixture.Repository.MakeACommit();
228259

229-
var repositoryInfo = new RepositoryInfo
230-
{
231-
Url = mainRepositoryFixture.RepositoryPath,
232-
TargetBranch = null
233-
};
260+
var repositoryInfo = new RepositoryInfo
261+
{
262+
Url = mainRepositoryFixture.RepositoryPath
263+
};
234264

235-
Should.Throw<Exception>(() =>
265+
Should.Throw<GitToolsException>(() =>
266+
{
267+
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, null, null))
236268
{
237-
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo))
238-
{
239-
// this code shouldn't throw
240-
}
241-
});
242-
}
269+
}
270+
});
243271
}
244-
finally
272+
}
273+
274+
[Test]
275+
public void UsingDynamicRepositoryWithoutTargetBranchCommitFails()
276+
{
277+
var tempPath = Path.GetTempPath();
278+
279+
using (var mainRepositoryFixture = new EmptyRepositoryFixture())
245280
{
246-
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+
});
247294
}
248295
}
249296

@@ -259,15 +306,13 @@ public void TestErrorThrownForInvalidRepository()
259306
{
260307
var repositoryInfo = new RepositoryInfo
261308
{
262-
Url = "http://127.0.0.1/testrepo.git",
263-
TargetBranch = "master"
309+
Url = "http://127.0.0.1/testrepo.git"
264310
};
265311

266312
Should.Throw<Exception>(() =>
267313
{
268-
using (var gitRepository = DynamicRepositories.CreateOrOpen(repositoryInfo))
314+
using (DynamicRepositories.CreateOrOpen(repositoryInfo, tempPath, "master", "sha"))
269315
{
270-
// this code shouldn't throw
271316
}
272317
});
273318
}

src/GitTools.Core.Tests/GitTools.Core.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<Compile Include="Git\Extensions\AuthenticationInfoExtensionsTests.cs" />
7575
<Compile Include="Git\GitDirFinderTests.cs" />
7676
<Compile Include="Git\GitRepositoryHelperTests.cs" />
77-
<Compile Include="Git\GitRepositoryFactoryTests.cs" />
77+
<Compile Include="Git\DynamicRepositoriesTests.cs" />
7878
<Compile Include="GlobalInitialization.cs" />
7979
<Compile Include="Properties\AssemblyInfo.cs" />
8080
</ItemGroup>

src/GitTools.Core/GitTools.Core.Shared/Exceptions/GitToolsException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ public GitToolsException(string messageFormat, params object[] args)
88
: base(string.Format(messageFormat, args))
99
{
1010
}
11+
public GitToolsException(string message, Exception innerException)
12+
: base(message, innerException)
13+
{
14+
}
1115
}
1216
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
namespace GitTools.Git
22
{
3+
using LibGit2Sharp;
4+
35
public class AuthenticationInfo
46
{
57
public string Username { get; set; }
68
public string Password { get; set; }
79
public string Token { get; set; }
10+
11+
public FetchOptions ToFetchOptions()
12+
{
13+
var fetchOptions = new FetchOptions();
14+
15+
if (!string.IsNullOrEmpty(Username))
16+
{
17+
fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials
18+
{
19+
Username = Username,
20+
Password = Password
21+
};
22+
}
23+
24+
return fetchOptions;
25+
}
826
}
927
}

0 commit comments

Comments
 (0)