|
|
|
@ -1,7 +1,12 @@
|
|
|
|
|
using System.IO.Abstractions;
|
|
|
|
|
using System.IO.Abstractions.TestingHelpers;
|
|
|
|
|
using AutoFixture.NUnit3;
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using MoreLinq.Extensions;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Recyclarr.Logging;
|
|
|
|
|
using TestLibrary.AutoFixture;
|
|
|
|
|
using TrashLib;
|
|
|
|
|
|
|
|
|
|
namespace Recyclarr.Tests;
|
|
|
|
|
|
|
|
|
@ -9,33 +14,28 @@ namespace Recyclarr.Tests;
|
|
|
|
|
[Parallelizable(ParallelScope.All)]
|
|
|
|
|
public class LogJanitorTest
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public void Keep_correct_number_of_newest_log_files()
|
|
|
|
|
[Test, AutoMockData]
|
|
|
|
|
public void Keep_correct_number_of_newest_log_files(
|
|
|
|
|
[Frozen] IAppPaths paths,
|
|
|
|
|
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
|
|
|
|
|
LogJanitor janitor)
|
|
|
|
|
{
|
|
|
|
|
var fs = Substitute.For<IFileSystem>();
|
|
|
|
|
var janitor = new LogJanitor(fs);
|
|
|
|
|
const string logDir = "C:\\logs";
|
|
|
|
|
paths.LogDirectory.Returns(logDir);
|
|
|
|
|
|
|
|
|
|
var testFileInfoList = new[]
|
|
|
|
|
var testFiles = new[]
|
|
|
|
|
{
|
|
|
|
|
Substitute.For<IFileInfo>(),
|
|
|
|
|
Substitute.For<IFileInfo>(),
|
|
|
|
|
Substitute.For<IFileInfo>(),
|
|
|
|
|
Substitute.For<IFileInfo>()
|
|
|
|
|
$"{logDir}\\trash_2021-05-15_19-00-00",
|
|
|
|
|
$"{logDir}\\trash_2021-05-15_20-00-00",
|
|
|
|
|
$"{logDir}\\trash_2021-05-15_21-00-00",
|
|
|
|
|
$"{logDir}\\trash_2021-05-15_22-00-00"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
testFiles.ForEach(x => fs.AddFile(x, new MockFileData("")));
|
|
|
|
|
|
|
|
|
|
janitor.DeleteOldestLogFiles(2);
|
|
|
|
|
|
|
|
|
|
testFileInfoList[0].Received().Delete();
|
|
|
|
|
testFileInfoList[1].Received().Delete();
|
|
|
|
|
testFileInfoList[2].DidNotReceive().Delete();
|
|
|
|
|
testFileInfoList[3].DidNotReceive().Delete();
|
|
|
|
|
fs.FileExists(testFiles[2]).Should().BeTrue();
|
|
|
|
|
fs.FileExists(testFiles[3]).Should().BeTrue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|