Skip to content

Commit

Permalink
Implemented clear command for removing all cached chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Dunham committed Jan 5, 2012
1 parent 98ba9df commit 22df260
Show file tree
Hide file tree
Showing 14 changed files with 275 additions and 114 deletions.
30 changes: 21 additions & 9 deletions git-bin.Tests/CommandFactoryTest.cs
Expand Up @@ -22,7 +22,7 @@ public void GetShowUsageCommand_InvokesShowUsageFactory()
return null;
};

var target = new CommandFactory(factory, null, null, null, null, null);
var target = new CommandFactory(factory, null, null, null, null, null, null, null);
target.GetShowUsageCommand();

Assert.IsTrue(wasShowUsageCommandFactoryCalled);
Expand All @@ -34,7 +34,7 @@ public void GetCommand_InvalidCommandArgument_ReturnsShowUsageCommand()
var showUsageCommand = new ShowUsageCommand();
Func<ShowUsageCommand> factory = () => showUsageCommand;

var target = new CommandFactory(factory, null, null, null, null, null);
var target = new CommandFactory(factory, null, null, null, null, null, null, null);
var command = target.GetCommand(new []{"invalid"});

Assert.AreSame(showUsageCommand, command);
Expand All @@ -46,7 +46,7 @@ public void GetCommand_NoCommandArgument_ReturnsShowUsageCommand()
var showUsageCommand = new ShowUsageCommand();
Func<ShowUsageCommand> factory = () => showUsageCommand;

var target = new CommandFactory(factory, null, null, null, null, null);
var target = new CommandFactory(factory, null, null, null, null, null, null, null);
var command = target.GetCommand(new string[0]);

Assert.AreSame(showUsageCommand, command);
Expand All @@ -58,7 +58,7 @@ public void GetCommand_CleanCommandArgument_ReturnsCleanCommand()
var cleanCommand = new CleanCommand(null, null, new []{"abc"});
Func<string[], CleanCommand> factory = x => cleanCommand;

var target = new CommandFactory(null, null, factory, null, null, null);
var target = new CommandFactory(null, null, null, factory, null, null, null, null);
var command = target.GetCommand(new[] { CommandFactory.CleanArgument });

Assert.AreSame(cleanCommand, command);
Expand All @@ -78,7 +78,7 @@ public void GetCommand_CleanCommandArgument_PassesTailOfArgumentArray()
return null;
};

var target = new CommandFactory(null, null, factory, null, null, null);
var target = new CommandFactory(null, null, null, factory, null, null, null, null);
target.GetCommand(inputArguments);

Assert.IsTrue(wasInvoked);
Expand All @@ -90,7 +90,7 @@ public void GetCommand_SmudgeCommandArgument_ReturnsSmudgeCommand()
var smudgeCommand = new SmudgeCommand(null, null, new string[0]);
Func<string[], SmudgeCommand> factory = x => smudgeCommand;

var target = new CommandFactory(null, null, null, factory, null, null);
var target = new CommandFactory(null, null, null, null, factory, null, null, null);
var command = target.GetCommand(new[] { CommandFactory.SmudgeArgument });

Assert.AreSame(smudgeCommand, command);
Expand All @@ -99,10 +99,10 @@ public void GetCommand_SmudgeCommandArgument_ReturnsSmudgeCommand()
[Test]
public void GetCommand_PushCommandArgument_ReturnsPushCommand()
{
var pushCommand = new PushCommand(null, null, null);
var pushCommand = new PushCommand(null, null, new string[0]);
Func<string[], PushCommand> factory = x => pushCommand;

var target = new CommandFactory(null, null, null, null, factory, null);
var target = new CommandFactory(null, null, null, null, null, factory, null, null);
var command = target.GetCommand(new[] { CommandFactory.PushArgument });

Assert.AreSame(pushCommand, command);
Expand All @@ -122,10 +122,22 @@ public void GetCommand_PushCommandArgument_PassesTailOfArgumentArray()
return null;
};

var target = new CommandFactory(null, null, null, null, factory, null);
var target = new CommandFactory(null, null, null, null, null, factory, null, null);
target.GetCommand(inputArguments);

Assert.IsTrue(wasInvoked);
}

[Test]
public void GetCommand_ClearCommandArgument_ReturnsClearCommand()
{
var clearCommand = new ClearCommand(null, new []{"-n"});
Func<string[], ClearCommand> factory = x => clearCommand;

var target = new CommandFactory(null, null, null, null, null, null, null, factory);
var command = target.GetCommand(new[] { CommandFactory.ClearArgument });

Assert.AreSame(clearCommand, command);
}
}
}
42 changes: 42 additions & 0 deletions git-bin.Tests/Commands/ClearCommandTest.cs
@@ -0,0 +1,42 @@
using GitBin;
using GitBin.Commands;
using Moq;
using NUnit.Framework;

namespace git_bin.Tests.Commands
{
[TestFixture]
public class ClearCommandTest
{
private Mock<ICacheManager> _cacheManager;

[SetUp]
public void SetUp()
{
_cacheManager = new Mock<ICacheManager>(MockBehavior.Strict);
}

[Test]
public void Execute_Force_CallsClearCache()
{
_cacheManager.Setup(x => x.ClearCache()).Verifiable();

var target = new ClearCommand(_cacheManager.Object, new[] {"-f"});
target.Execute();

_cacheManager.Verify();
}

[Test]
public void Execute_DryRun_DoesNotClearCache()
{
// mock is strict, so calling ClearCache will throw
_cacheManager.Setup(x => x.ListFiles()).Returns(new GitBinFileInfo[0]);

var target = new ClearCommand(_cacheManager.Object, new[] { "-n" });
target.Execute();

Assert.Pass();
}
}
}
54 changes: 0 additions & 54 deletions git-bin.Tests/Commands/StatusCommandTest.cs

This file was deleted.

44 changes: 44 additions & 0 deletions git-bin.Tests/GitBinFileInfoUtilsTest.cs
@@ -0,0 +1,44 @@
using GitBin;
using GitBin.Commands;
using GitBin.Remotes;
using Moq;
using NUnit.Framework;

namespace git_bin.Tests
{
[TestFixture]
public class GitBinFileInfoUtilsTest
{
[Test]
public void GetHumanReadableSize_LessThanOrEqualTo1K_StringIsBytes()
{
var zero = GitBinFileInfoUtils.GetHumanReadableSize(0);
var fifty = GitBinFileInfoUtils.GetHumanReadableSize(50);
var kilobyte = GitBinFileInfoUtils.GetHumanReadableSize(1024);

Assert.AreEqual("0B", zero);
Assert.AreEqual("50B", fifty);
Assert.AreEqual("1k", kilobyte);
}

[Test]
public void GetHumanReadableSize_Between1KAnd1MInclusive_StringIsKB()
{
var ninePointEight = GitBinFileInfoUtils.GetHumanReadableSize(10035);
var megabyte = GitBinFileInfoUtils.GetHumanReadableSize(1024 * 1024);

Assert.AreEqual("9.8k", ninePointEight);
Assert.AreEqual("1M", megabyte);
}

[Test]
public void GetHumanReadableSize_Between1Mand1GInclusive_StringIsMB()
{
var fourPointTwo = GitBinFileInfoUtils.GetHumanReadableSize(4404019);
var gigabyte = GitBinFileInfoUtils.GetHumanReadableSize(1024 * 1024 * 1024);

Assert.AreEqual("4.2M", fourPointTwo);
Assert.AreEqual("1G", gigabyte);
}
}
}
3 changes: 2 additions & 1 deletion git-bin.Tests/git-bin.Tests.csproj
Expand Up @@ -56,9 +56,10 @@
<ItemGroup>
<Compile Include="CommandFactoryTest.cs" />
<Compile Include="Commands\CleanCommandTest.cs" />
<Compile Include="Commands\ClearCommandTest.cs" />
<Compile Include="Commands\PushCommandTest.cs" />
<Compile Include="Commands\SmudgeCommandTest.cs" />
<Compile Include="Commands\StatusCommandTest.cs" />
<Compile Include="GitBinFileInfoUtilsTest.cs" />
<Compile Include="ConfigurationProviderTest.cs" />
<Compile Include="GitBinDocumentTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
2 changes: 2 additions & 0 deletions git-bin/ApplicationRegistrations.cs
Expand Up @@ -22,7 +22,9 @@ public static void Register(IBuilder builder)

builder.RegisterFactory<ShowUsageCommand>();
builder.RegisterFactory<VersionCommand>();
builder.RegisterFactory<string, PrintCommand>();
builder.RegisterFactory<string[], CleanCommand>();
builder.RegisterFactory<string[], ClearCommand>();
builder.RegisterFactory<string[], SmudgeCommand>();
builder.RegisterFactory<string[], PushCommand>();
builder.RegisterFactory<string[], StatusCommand>();
Expand Down
11 changes: 10 additions & 1 deletion git-bin/CacheManager.cs
Expand Up @@ -10,8 +10,9 @@ public interface ICacheManager
byte[] ReadFileFromCache(string filename);
void WriteFileToCache(string filename, byte[] contents, int contentLength);
void WriteFileToCache(string filename, Stream stream);
string[] GetFilenamesNotInCache(IEnumerable<string> filenamesToCheck);
GitBinFileInfo[] ListFiles();
void ClearCache();
string[] GetFilenamesNotInCache(IEnumerable<string> filenamesToCheck);
string GetPathForFile(string filename);
}

Expand Down Expand Up @@ -75,6 +76,14 @@ public GitBinFileInfo[] ListFiles()
return gitBinFileInfos.ToArray();
}

public void ClearCache()
{
foreach (var file in ListFiles())
{
File.Delete(GetPathForFile(file.Name));
}
}

public string[] GetFilenamesNotInCache(IEnumerable<string> filenamesToCheck)
{
var filenamesInCache = ListFiles().Select(fi => fi.Name);
Expand Down
40 changes: 28 additions & 12 deletions git-bin/CommandFactory.cs
Expand Up @@ -14,31 +14,38 @@ public class CommandFactory : ICommandFactory
{
public const string VersionArgument = "--version";
public const string CleanArgument = "clean";
public const string ClearArgument = "clear";
public const string SmudgeArgument = "smudge";
public const string PushArgument = "push";
public const string StatusArgument = "status";

private readonly Func<ShowUsageCommand> _showUsageCommandFactory;
private readonly Func<VersionCommand> _versionCommandFactory;
private readonly Func<string, PrintCommand> _printCommandFactory;
private readonly Func<string[], CleanCommand> _cleanCommandFactory;
private readonly Func<string[], SmudgeCommand> _smudgeCommandFactory;
private readonly Func<string[], PushCommand> _pushCommandFactory;
private readonly Func<string[], StatusCommand> _statusCommandFactory;
private readonly Func<string[], ClearCommand> _clearCommandFactory;

public CommandFactory(
Func<ShowUsageCommand> showUsageCommandFactory,
Func<VersionCommand> versionCommandFactory,
Func<string, PrintCommand> printCommandFactory,
Func<string[], CleanCommand> cleanCommandFactory,
Func<string[], SmudgeCommand> smudgeCommandFactory,
Func<string[], PushCommand> pushCommandFactory,
Func<string[], StatusCommand> statusCommandFactory)
Func<string[], StatusCommand> statusCommandFactory,
Func<string[], ClearCommand> clearCommandFactory)
{
_showUsageCommandFactory = showUsageCommandFactory;
_versionCommandFactory = versionCommandFactory;
_printCommandFactory = printCommandFactory;
_cleanCommandFactory = cleanCommandFactory;
_smudgeCommandFactory = smudgeCommandFactory;
_pushCommandFactory = pushCommandFactory;
_statusCommandFactory = statusCommandFactory;
_clearCommandFactory = clearCommandFactory;
}

public ICommand GetCommand(string[] args)
Expand All @@ -51,23 +58,32 @@ public ICommand GetCommand(string[] args)

try
{
if (commandArgument == VersionArgument)
return _versionCommandFactory();
switch (commandArgument)
{
case VersionArgument:
return _versionCommandFactory();

if (commandArgument == CleanArgument)
return _cleanCommandFactory(argsTail);
case CleanArgument:
return _cleanCommandFactory(argsTail);

if (commandArgument == SmudgeArgument)
return _smudgeCommandFactory(argsTail);
case ClearArgument:
return _clearCommandFactory(argsTail);

if (commandArgument == PushArgument)
return _pushCommandFactory(argsTail);
case SmudgeArgument:
return _smudgeCommandFactory(argsTail);

if (commandArgument == StatusArgument)
return _statusCommandFactory(argsTail);
case PushArgument:
return _pushCommandFactory(argsTail);

case StatusArgument:
return _statusCommandFactory(argsTail);
}
}
catch (ArgumentException)
catch (ArgumentException e)
{
if (!string.IsNullOrEmpty(e.Message))
return _printCommandFactory(e.Message);

return GetShowUsageCommand();
}

Expand Down

0 comments on commit 22df260

Please sign in to comment.