Skip to content

No more check function #968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 7 additions & 177 deletions LibGit2Sharp.Tests/FilterFixture.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

Expand All @@ -11,12 +10,10 @@ public class FilterFixture : BaseFixture
{
private const int GitPassThrough = -30;

readonly Func<FilterSource, IEnumerable<string>, int> checkPassThrough = (source, attr) => GitPassThrough;
readonly Func<Stream, Stream, int> successCallback = (reader, writer) => 0;
readonly Func<FilterSource, IEnumerable<string>, int> checkSuccess = (source, attr) => 0;

private const string FilterName = "the-filter";
readonly List<string> attributes = new List<string>{"test"};
readonly List<FilterAttribute> attributes = new List<FilterAttribute> { new FilterAttribute("test") };

[Fact]
public void CanRegisterFilterWithSingleAttribute()
Expand Down Expand Up @@ -55,101 +52,6 @@ public void SameFilterIsEqual()
Assert.Equal(filter, filter);
}

[Fact]
public void CheckCallbackNotMadeWhenFileStagedAndFilterNotRegistered()
{
bool called = false;
Func<FilterSource, IEnumerable<string>, int> callback = (source, attr) =>
{
called = true;
return GitPassThrough;
};

string repoPath = InitNewRepository();

new FakeFilter(FilterName + 4, attributes, callback);

using (var repo = CreateTestRepository(repoPath))
{
StageNewFile(repo);
}

Assert.False(called);
}

[Fact]
public void CheckCallbackMadeWhenFileStaged()
{
bool called = false;
Func<FilterSource, IEnumerable<string>, int> checkCallBack = (source, attr) =>
{
called = true;
return GitPassThrough;
};
string repoPath = InitNewRepository();

var filter = new FakeFilter(FilterName + 5, attributes, checkCallBack);

var filterRegistration = GlobalSettings.RegisterFilter(filter);
using (var repo = CreateTestRepository(repoPath))
{
StageNewFile(repo);
Assert.True(called);
}

GlobalSettings.DeregisterFilter(filterRegistration);
}

[Fact]
public void ApplyCallbackMadeWhenCheckCallbackReturnsZero()
{
bool called = false;

Func<Stream, Stream, int> applyCallback = (reader, writer) =>
{
called = true;
return 0; //successCallback
};

string repoPath = InitNewRepository();
var filter = new FakeFilter(FilterName + 6, attributes, checkSuccess, applyCallback);

var filterRegistration = GlobalSettings.RegisterFilter(filter);
using (var repo = CreateTestRepository(repoPath))
{
StageNewFile(repo);
}

GlobalSettings.DeregisterFilter(filterRegistration);

Assert.True(called);
}

[Fact]
public void ApplyCallbackNotMadeWhenCheckCallbackReturnsPassThrough()
{
bool called = false;

Func<Stream, Stream, int> applyCallback = (reader, writer) =>
{
called = true;
return 0;
};

string repoPath = InitNewRepository();
var filter = new FakeFilter(FilterName + 7, attributes, checkPassThrough, applyCallback);

var filterRegistration = GlobalSettings.RegisterFilter(filter);
using (var repo = CreateTestRepository(repoPath))
{
StageNewFile(repo);
}

GlobalSettings.DeregisterFilter(filterRegistration);

Assert.False(called);
}

[Fact]
public void InitCallbackNotMadeWhenFilterNeverUsed()
{
Expand All @@ -161,7 +63,6 @@ public void InitCallbackNotMadeWhenFilterNeverUsed()
};

var filter = new FakeFilter(FilterName + 11, attributes,
checkSuccess,
successCallback,
successCallback,
initializeCallback);
Expand All @@ -184,7 +85,6 @@ public void InitCallbackMadeWhenUsingTheFilter()
};

var filter = new FakeFilter(FilterName + 12, attributes,
checkSuccess,
successCallback,
successCallback,
initializeCallback);
Expand All @@ -202,68 +102,6 @@ public void InitCallbackMadeWhenUsingTheFilter()
GlobalSettings.DeregisterFilter(filterRegistration);
}

[Fact]
public void WhenStagingFileCheckIsCalledWithCleanForCorrectPath()
{
string repoPath = InitNewRepository();

var calledWithMode = FilterMode.Smudge;
string actualPath = string.Empty;
IEnumerable<string> actualAttributes = Enumerable.Empty<string>();
Func<FilterSource, IEnumerable<string>, int> callback = (source, attr) =>
{
calledWithMode = source.SourceMode;
actualPath = source.Path;
actualAttributes = attr;
return GitPassThrough;
};

var filter = new FakeFilter(FilterName + 13, attributes, callback);

var filterRegistration = GlobalSettings.RegisterFilter(filter);

using (var repo = CreateTestRepository(repoPath))
{
FileInfo expectedFile = StageNewFile(repo);

Assert.Equal(FilterMode.Clean, calledWithMode);
Assert.Equal(expectedFile.Name, actualPath);
Assert.Equal(attributes, actualAttributes);
}

GlobalSettings.DeregisterFilter(filterRegistration);
}


[Fact]
public void WhenCheckingOutAFileFileCheckIsCalledWithSmudgeForCorrectPath()
{
const string branchName = "branch";
string repoPath = InitNewRepository();

var calledWithMode = FilterMode.Clean;
string actualPath = string.Empty;
IEnumerable<string> actualAttributes = Enumerable.Empty<string>();
Func<FilterSource, IEnumerable<string>, int> callback = (source, attr) =>
{
calledWithMode = source.SourceMode;
actualPath = source.Path;
actualAttributes = attr;
return GitPassThrough;
};

var filter = new FakeFilter(FilterName + 14, attributes, callback);

var filterRegistration = GlobalSettings.RegisterFilter(filter);

FileInfo expectedFile = CheckoutFileForSmudge(repoPath, branchName, "hello");
Assert.Equal(FilterMode.Smudge, calledWithMode);
Assert.Equal(expectedFile.FullName, actualPath);
Assert.Equal(attributes, actualAttributes);

GlobalSettings.DeregisterFilter(filterRegistration);
}

[Fact]
public void WhenStagingFileApplyIsCalledWithCleanForCorrectPath()
{
Expand All @@ -275,7 +113,7 @@ public void WhenStagingFileApplyIsCalledWithCleanForCorrectPath()
called = true;
return GitPassThrough;
};
var filter = new FakeFilter(FilterName + 15, attributes, checkSuccess, clean);
var filter = new FakeFilter(FilterName + 15, attributes, clean);

var filterRegistration = GlobalSettings.RegisterFilter(filter);

Expand All @@ -298,7 +136,7 @@ public void CleanFilterWritesOutputToObjectTree()

Func<Stream, Stream, int> cleanCallback = SubstitutionCipherFilter.RotateByThirteenPlaces;

var filter = new FakeFilter(FilterName + 16, attributes, checkSuccess, cleanCallback);
var filter = new FakeFilter(FilterName + 16, attributes, cleanCallback);

var filterRegistration = GlobalSettings.RegisterFilter(filter);

Expand Down Expand Up @@ -328,7 +166,7 @@ public void WhenCheckingOutAFileFileSmudgeWritesCorrectFileToWorkingDirectory()

Func<Stream, Stream, int> smudgeCallback = SubstitutionCipherFilter.RotateByThirteenPlaces;

var filter = new FakeFilter(FilterName + 17, attributes, checkSuccess, null, smudgeCallback);
var filter = new FakeFilter(FilterName + 17, attributes, null, smudgeCallback);
var filterRegistration = GlobalSettings.RegisterFilter(filter);

FileInfo expectedFile = CheckoutFileForSmudge(repoPath, branchName, encodedInput);
Expand Down Expand Up @@ -361,7 +199,7 @@ public void FilterStreamsAreCoherent()
return GitPassThrough;
};

var filter = new FakeFilter(FilterName + 18, attributes, checkSuccess, assertor, assertor);
var filter = new FakeFilter(FilterName + 18, attributes, assertor, assertor);

var filterRegistration = GlobalSettings.RegisterFilter(filter);

Expand Down Expand Up @@ -433,36 +271,28 @@ private Repository CreateTestRepository(string path)

class EmptyFilter : Filter
{
public EmptyFilter(string name, IEnumerable<string> attributes)
public EmptyFilter(string name, IEnumerable<FilterAttribute> attributes)
: base(name, attributes)
{ }
}

class FakeFilter : Filter
{
private readonly Func<FilterSource, IEnumerable<string>, int> checkCallBack;
private readonly Func<Stream, Stream, int> cleanCallback;
private readonly Func<Stream, Stream, int> smudgeCallback;
private readonly Func<int> initCallback;

public FakeFilter(string name, IEnumerable<string> attributes,
Func<FilterSource, IEnumerable<string>, int> checkCallBack = null,
public FakeFilter(string name, IEnumerable<FilterAttribute> attributes,
Func<Stream, Stream, int> cleanCallback = null,
Func<Stream, Stream, int> smudgeCallback = null,
Func<int> initCallback = null)
: base(name, attributes)
{
this.checkCallBack = checkCallBack;
this.cleanCallback = cleanCallback;
this.smudgeCallback = smudgeCallback;
this.initCallback = initCallback;
}

protected override int Check(IEnumerable<string> attributes, FilterSource filterSource)
{
return checkCallBack != null ? checkCallBack(filterSource, attributes) : base.Check(attributes, filterSource);
}

protected override int Clean(string path, Stream input, Stream output)
{
return cleanCallback != null ? cleanCallback(input, output) : base.Clean(path, input, output);
Expand Down
Loading