提交 b8196926 编写于 作者: N nulltoken

Added some infrastructure for running isolated tests which modify the content of a git repository.

上级 88bfc327
......@@ -5,10 +5,8 @@
namespace libgit2sharp.Tests
{
[TestFixture]
public class InstantiatingARepository
public class InstantiatingARepository : ReadOnlyRepositoryFixtureBase
{
const string PathToRepository = "../../Resources/testrepo.git";
[Test]
public void ShouldThrowIfPassedANonValidGitDirectory()
{
......
namespace libgit2sharp.Tests
{
public class ReadOnlyRepositoryFixtureBase
{
protected virtual string PathToRepository { get { return "../../Resources/testrepo.git"; } }
}
}
\ No newline at end of file
using System;
using System.IO;
using System.Reflection;
using NUnit.Framework;
namespace libgit2sharp.Tests
{
public class ReadWriteRepositoryFixtureBase : ReadOnlyRepositoryFixtureBase
{
private const string TestRepositoriesDirectoryName = "TestRepos";
private static readonly string _testRepositoriesDirectoryPath = RetrieveTestRepositoriesDirectory();
private string _pathToRepository;
protected override string PathToRepository { get { return _pathToRepository; } }
[SetUp]
public void Setup()
{
// Create temporary working directory
string workDirpath = Path.Combine(_testRepositoriesDirectoryPath, this.GetType().Name, Guid.NewGuid().ToString().Substring(0, 8));
var source = new DirectoryInfo(base.PathToRepository);
var tempRepository = new DirectoryInfo(Path.Combine(workDirpath, source.Name));
CopyFilesRecursively(source, tempRepository);
_pathToRepository = tempRepository.FullName;
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
Directory.Delete(_testRepositoriesDirectoryPath, true);
}
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
// From http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c/58779#58779
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
}
static private string RetrieveTestRepositoriesDirectory()
{
return Path.Combine(RetrieveAssemblyDirectory(), TestRepositoriesDirectoryName);
}
static private string RetrieveAssemblyDirectory()
{
// From http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in/283917#283917
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
}
\ No newline at end of file
......@@ -5,10 +5,8 @@
namespace libgit2sharp.Tests
{
[TestFixture]
public class RepositoryFixtures
public class RepositoryFixtures : ReadOnlyRepositoryFixtureBase
{
const string PathToRepository = "../../Resources/testrepo.git";
[Test]
public void Ctor_RetrieveDetails()
{
......
......@@ -45,6 +45,8 @@
<ItemGroup>
<Compile Include="GitDateFixture.cs" />
<Compile Include="ObjectIdFixture.cs" />
<Compile Include="ReadOnlyRepositoryFixtureBase.cs" />
<Compile Include="ReadWriteRepositoryFixtureBase.cs" />
<Compile Include="RepositoryFixtures.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="InstantiatingARepository.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册