parent
2a222ac6e5
commit
4d99530e48
@ -0,0 +1,41 @@
|
||||
using System.IO.Abstractions;
|
||||
using NSubstitute;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Trash.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
[Parallelizable(ParallelScope.All)]
|
||||
public class LogJanitorTest
|
||||
{
|
||||
[Test]
|
||||
public void Keep_correct_number_of_newest_log_files()
|
||||
{
|
||||
var fs = Substitute.For<IFileSystem>();
|
||||
var janitor = new LogJanitor(fs);
|
||||
|
||||
var testFileInfoList = new[]
|
||||
{
|
||||
Substitute.For<IFileInfo>(),
|
||||
Substitute.For<IFileInfo>(),
|
||||
Substitute.For<IFileInfo>(),
|
||||
Substitute.For<IFileInfo>()
|
||||
};
|
||||
|
||||
testFileInfoList[0].Name.Returns("trash_2021-05-15_19-00-00");
|
||||
testFileInfoList[1].Name.Returns("trash_2021-05-15_20-00-00");
|
||||
testFileInfoList[2].Name.Returns("trash_2021-05-15_21-00-00");
|
||||
testFileInfoList[3].Name.Returns("trash_2021-05-15_22-00-00");
|
||||
|
||||
fs.DirectoryInfo.FromDirectoryName(Arg.Any<string>()).GetFiles()
|
||||
.Returns(testFileInfoList);
|
||||
|
||||
janitor.DeleteOldestLogFiles(2);
|
||||
|
||||
testFileInfoList[0].Received().Delete();
|
||||
testFileInfoList[1].Received().Delete();
|
||||
testFileInfoList[2].DidNotReceive().Delete();
|
||||
testFileInfoList[3].DidNotReceive().Delete();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace Trash
|
||||
{
|
||||
public interface ILogJanitor
|
||||
{
|
||||
void DeleteOldestLogFiles(int numberOfNewestToKeep);
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
|
||||
namespace Trash
|
||||
{
|
||||
public class LogJanitor : ILogJanitor
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
|
||||
public LogJanitor(IFileSystem fileSystem)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
}
|
||||
|
||||
public void DeleteOldestLogFiles(int numberOfNewestToKeep)
|
||||
{
|
||||
foreach (var file in _fileSystem.DirectoryInfo.FromDirectoryName(AppPaths.LogDirectory).GetFiles()
|
||||
.OrderByDescending(f => f.Name)
|
||||
.Skip(numberOfNewestToKeep))
|
||||
{
|
||||
file.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue