Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/vNext' into error-handling-refactor
Browse files Browse the repository at this point in the history
Conflicts:
	LibGit2Sharp/LibGit2SharpException.cs
  • Loading branch information
tclem committed Jun 21, 2012
2 parents 08374d7 + f8f29a9 commit 55ee014
Show file tree
Hide file tree
Showing 68 changed files with 6,900 additions and 243 deletions.
Binary file added Lib/MoQ/Moq.dll
Binary file not shown.
5,768 changes: 5,768 additions & 0 deletions Lib/MoQ/Moq.xml

Large diffs are not rendered by default.

Binary file modified Lib/NativeBinaries/amd64/git2.dll
Binary file not shown.
Binary file modified Lib/NativeBinaries/amd64/git2.pdb
Binary file not shown.
Binary file modified Lib/NativeBinaries/x86/git2.dll
Binary file not shown.
Binary file modified Lib/NativeBinaries/x86/git2.pdb
Binary file not shown.
4 changes: 2 additions & 2 deletions LibGit2Sharp.Tests/AttributesFixture.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void StagingHonorsTheAttributesFiles()
} }
} }


private void AssertNormalization(Repository repo, string filename, bool shouldHaveBeenNormalized, string expectedSha) private static void AssertNormalization(Repository repo, string filename, bool shouldHaveBeenNormalized, string expectedSha)
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.Append("I'm going to be dynamically processed\r\n"); sb.Append("I'm going to be dynamically processed\r\n");
Expand All @@ -44,7 +44,7 @@ private void AssertNormalization(Repository repo, string filename, bool shouldHa
Assert.Equal(!shouldHaveBeenNormalized, blob.ContentAsUtf8().Contains("\r")); Assert.Equal(!shouldHaveBeenNormalized, blob.ContentAsUtf8().Contains("\r"));
} }


private void CreateAttributesFile(Repository repo) private static void CreateAttributesFile(Repository repo)
{ {
const string relativePath = ".gitattributes"; const string relativePath = ".gitattributes";
string fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath); string fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath);
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ private static void CreateAndStageANewFile(Repository repo)
repo.Index.Stage(relativeFilepath); repo.Index.Stage(relativeFilepath);
} }


private void AssertCommitHasBeenAmended(Repository repo, Commit amendedCommit, Commit originalCommit) private static void AssertCommitHasBeenAmended(Repository repo, Commit amendedCommit, Commit originalCommit)
{ {
Commit headCommit = repo.Head.Tip; Commit headCommit = repo.Head.Tip;
Assert.Equal(amendedCommit, headCommit); Assert.Equal(amendedCommit, headCommit);
Expand Down
140 changes: 140 additions & 0 deletions LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,6 +23,146 @@ private static void SetUpSimpleDiffContext(Repository repo)
File.AppendAllText(fullpath, "!!!\n"); File.AppendAllText(fullpath, "!!!\n");
} }


[Fact]
/*
* No direct git equivalent but should output
*
* diff --git a/file.txt b/file.txt
* index ce01362..4f125e3 100644
* --- a/file.txt
* +++ b/file.txt
* @@ -1 +1,3 @@
* hello
* +world
* +!!!
*/
public void CanCompareASimpleTreeAgainstTheWorkDir()
{
var scd = BuildSelfCleaningDirectory();

using (var repo = Repository.Init(scd.RootedDirectoryPath))
{
SetUpSimpleDiffContext(repo);

TreeChanges changes = repo.Diff.Compare(repo.Head.Tip.Tree, DiffTarget.WorkingDirectory);

var expected = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("index ce01362..4f125e3 100644\n")
.Append("--- a/file.txt\n")
.Append("+++ b/file.txt\n")
.Append("@@ -1 +1,3 @@\n")
.Append(" hello\n")
.Append("+world\n")
.Append("+!!!\n");

Assert.Equal(expected.ToString(), changes.Patch);
}
}

[Fact]
/*
* $ git diff HEAD
* diff --git a/file.txt b/file.txt
* index ce01362..4f125e3 100644
* --- a/file.txt
* +++ b/file.txt
* @@ -1 +1,3 @@
* hello
* +world
* +!!!
*/
public void CanCompareASimpleTreeAgainstTheWorkDirAndTheIndex()
{
var scd = BuildSelfCleaningDirectory();

using (var repo = Repository.Init(scd.RootedDirectoryPath))
{
SetUpSimpleDiffContext(repo);

TreeChanges changes = repo.Diff.Compare(repo.Head.Tip.Tree, DiffTarget.BothWorkingDirectoryAndIndex);

var expected = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("index ce01362..4f125e3 100644\n")
.Append("--- a/file.txt\n")
.Append("+++ b/file.txt\n")
.Append("@@ -1 +1,3 @@\n")
.Append(" hello\n")
.Append("+world\n")
.Append("+!!!\n");

Assert.Equal(expected.ToString(), changes.Patch);
}
}


[Fact]
/*
* $ git diff
*
* $ git diff HEAD
* diff --git a/file.txt b/file.txt
* deleted file mode 100644
* index ce01362..0000000
* --- a/file.txt
* +++ /dev/null
* @@ -1 +0,0 @@
* -hello
*
* $ git diff --cached
* diff --git a/file.txt b/file.txt
* deleted file mode 100644
* index ce01362..0000000
* --- a/file.txt
* +++ /dev/null
* @@ -1 +0,0 @@
* -hello
*/
public void ShowcaseTheDifferenceBetweenTheTwoKindOfComparison()
{
var scd = BuildSelfCleaningDirectory();

using (var repo = Repository.Init(scd.RootedDirectoryPath))
{
SetUpSimpleDiffContext(repo);

var fullpath = Path.Combine(repo.Info.WorkingDirectory, "file.txt");
File.Move(fullpath, fullpath + ".bak");
repo.Index.Stage(fullpath);
File.Move(fullpath + ".bak", fullpath);

FileStatus state = repo.Index.RetrieveStatus("file.txt");
Assert.Equal(FileStatus.Removed | FileStatus.Untracked, state);


TreeChanges wrkDirToIdxToTree = repo.Diff.Compare(repo.Head.Tip.Tree, DiffTarget.BothWorkingDirectoryAndIndex);
var expected = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("deleted file mode 100644\n")
.Append("index ce01362..0000000\n")
.Append("--- a/file.txt\n")
.Append("+++ /dev/null\n")
.Append("@@ -1 +0,0 @@\n")
.Append("-hello\n");

Assert.Equal(expected.ToString(), wrkDirToIdxToTree.Patch);

TreeChanges wrkDirToTree = repo.Diff.Compare(repo.Head.Tip.Tree, DiffTarget.WorkingDirectory);
expected = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("index ce01362..4f125e3 100644\n")
.Append("--- a/file.txt\n")
.Append("+++ b/file.txt\n")
.Append("@@ -1 +1,3 @@\n")
.Append(" hello\n")
.Append("+world\n")
.Append("+!!!\n");

Assert.Equal(expected.ToString(), wrkDirToTree.Patch);
}
}

[Fact] [Fact]
/* /*
* $ git diff --cached * $ git diff --cached
Expand Down
39 changes: 39 additions & 0 deletions LibGit2Sharp.Tests/DiffWorkdirToIndexFixture.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
{
public class DiffWorkdirToIndexFixture : BaseFixture
{
/*
* $ git diff
* diff --git a/deleted_unstaged_file.txt b/deleted_unstaged_file.txt
* deleted file mode 100644
* index f2e4113..0000000
* --- a/deleted_unstaged_file.txt
* +++ /dev/null
* @@ -1 +0,0 @@
* -stuff
* diff --git a/modified_unstaged_file.txt b/modified_unstaged_file.txt
* index 9217230..da6fd65 100644
* --- a/modified_unstaged_file.txt
* +++ b/modified_unstaged_file.txt
* @@ -1 +1,2 @@
* +some more text
* more files! more files!
*/
[Fact]
public void CanCompareTheWorkDirAgainstTheIndex()
{
using (var repo = new Repository(StandardTestRepoPath))
{
TreeChanges changes = repo.Diff.Compare();

Assert.Equal(2, changes.Count());
Assert.Equal("deleted_unstaged_file.txt", changes.Deleted.Single().Path);
Assert.Equal("modified_unstaged_file.txt", changes.Modified.Single().Path);
}
}
}
}
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/IndexFixture.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void ReadIndexWithBadParamsFails()


[Theory] [Theory]
[InlineData("1/branch_file.txt", FileStatus.Unaltered, true, FileStatus.Unaltered, true, 0)] [InlineData("1/branch_file.txt", FileStatus.Unaltered, true, FileStatus.Unaltered, true, 0)]
[InlineData("README", FileStatus.Unaltered, true, FileStatus.Unaltered, true, 0)]
[InlineData("deleted_unstaged_file.txt", FileStatus.Missing, true, FileStatus.Removed, false, -1)] [InlineData("deleted_unstaged_file.txt", FileStatus.Missing, true, FileStatus.Removed, false, -1)]
[InlineData("modified_unstaged_file.txt", FileStatus.Modified, true, FileStatus.Staged, true, 0)] [InlineData("modified_unstaged_file.txt", FileStatus.Modified, true, FileStatus.Staged, true, 0)]
[InlineData("new_untracked_file.txt", FileStatus.Untracked, false, FileStatus.Added, true, 1)] [InlineData("new_untracked_file.txt", FileStatus.Untracked, false, FileStatus.Added, true, 1)]
Expand Down
1 change: 0 additions & 1 deletion LibGit2Sharp.Tests/LazyFixture.cs
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,5 @@
using System; using System;
using LibGit2Sharp.Core.Compat; using LibGit2Sharp.Core.Compat;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit; using Xunit;


namespace LibGit2Sharp.Tests namespace LibGit2Sharp.Tests
Expand Down
7 changes: 7 additions & 0 deletions LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<DocumentationFile /> <DocumentationFile />
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\MoQ\Moq.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="xunit"> <Reference Include="xunit">
Expand All @@ -53,12 +57,15 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="MetaFixture.cs" />
<Compile Include="MockedRepositoryFixture.cs" />
<Compile Include="ConfigurationFixture.cs" /> <Compile Include="ConfigurationFixture.cs" />
<Compile Include="AttributesFixture.cs" /> <Compile Include="AttributesFixture.cs" />
<Compile Include="CommitAncestorFixture.cs" /> <Compile Include="CommitAncestorFixture.cs" />
<Compile Include="NoteFixture.cs" /> <Compile Include="NoteFixture.cs" />
<Compile Include="DiffBlobToBlobFixture.cs" /> <Compile Include="DiffBlobToBlobFixture.cs" />
<Compile Include="DiffTreeToTargetFixture.cs" /> <Compile Include="DiffTreeToTargetFixture.cs" />
<Compile Include="DiffWorkdirToIndexFixture.cs" />
<Compile Include="ObjectDatabaseFixture.cs" /> <Compile Include="ObjectDatabaseFixture.cs" />
<Compile Include="DiffTreeToTreeFixture.cs" /> <Compile Include="DiffTreeToTreeFixture.cs" />
<Compile Include="RepositoryOptionsFixture.cs" /> <Compile Include="RepositoryOptionsFixture.cs" />
Expand Down
89 changes: 89 additions & 0 deletions LibGit2Sharp.Tests/MetaFixture.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Text;
using Xunit;
using System.Reflection;
using System;
using System.Linq;
using System.Collections.Generic;

namespace LibGit2Sharp.Tests
{
public class MetaFixture
{
private static readonly Type[] excludedTypes = new[] { typeof(Repository) };

// Related to https://github.com/libgit2/libgit2sharp/pull/185
[Fact]
public void TypesInLibGit2SharpMustBeExtensibleInATestingContext()
{
var nonTestableTypes = new Dictionary<Type, IEnumerable<string>>();

IEnumerable<Type> libGit2SharpTypes = Assembly.GetAssembly(typeof(Repository)).GetExportedTypes().Where(t => !excludedTypes.Contains(t) && t.Namespace == typeof(Repository).Namespace);

foreach (Type type in libGit2SharpTypes)
{
if (type.IsInterface || type.IsEnum || IsStatic(type))
continue;

ConstructorInfo[] publicConstructor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
if (publicConstructor.Any())
{
continue;
}

var nonVirtualMethodNamesForType = GetNonVirtualPublicMethodsNames(type).ToList();
if (nonVirtualMethodNamesForType.Any())
{
nonTestableTypes.Add(type, nonVirtualMethodNamesForType);
continue;
}

if (!HasEmptyProtectedConstructor(type))
{
nonTestableTypes.Add(type, new List<string>());
}
}

if (nonTestableTypes.Any())
{
Assert.True(false, Environment.NewLine + BuildNonTestableTypesMessage(nonTestableTypes));
}
}

private static string BuildNonTestableTypesMessage(Dictionary<Type, IEnumerable<string>> nonTestableTypes)
{
var sb = new StringBuilder();

foreach (var kvp in nonTestableTypes)
{
sb.AppendFormat("'{0}' cannot be easily abstracted in a testing context. Please make sure it either has a public constructor, or an empty protected constructor.{1}",
kvp.Key, Environment.NewLine);

foreach (string methodName in kvp.Value)
{
sb.AppendFormat(" - Method '{0}' must be virtual{1}", methodName, Environment.NewLine);
}
}

return sb.ToString();
}

private static IEnumerable<string> GetNonVirtualPublicMethodsNames(Type type)
{
var publicMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

return from mi in publicMethods where !mi.IsVirtual && !mi.IsStatic select mi.ToString();
}

private static bool HasEmptyProtectedConstructor(Type type)
{
ConstructorInfo[] nonPublicConstructors = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);

return nonPublicConstructors.Any(ci => !ci.IsPrivate && !ci.IsAssembly && !ci.IsFinal && !ci.GetParameters().Any());
}

private static bool IsStatic(Type type)
{
return type.IsAbstract && type.IsSealed;
}
}
}
Loading

0 comments on commit 55ee014

Please sign in to comment.