Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/HostedServices/TempFileCleanupTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

63 lines
2.2 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Runtime;
using Umbraco.Cms.Infrastructure.HostedServices;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.HostedServices
{
[TestFixture]
public class TempFileCleanupTests
{
private Mock<IIOHelper> _mockIOHelper;
private readonly string _testPath = Path.Combine(TestContext.CurrentContext.TestDirectory.Split("bin")[0], "App_Data", "TEMP");
[Test]
public async Task Does_Not_Execute_When_Not_Main_Dom()
{
TempFileCleanup sut = CreateTempFileCleanup(isMainDom: false);
await sut.PerformExecuteAsync(null);
VerifyFilesNotCleaned();
}
[Test]
public async Task Executes_And_Cleans_Files()
{
TempFileCleanup sut = CreateTempFileCleanup();
await sut.PerformExecuteAsync(null);
VerifyFilesCleaned();
}
private TempFileCleanup CreateTempFileCleanup(bool isMainDom = true)
{
var mockMainDom = new Mock<IMainDom>();
mockMainDom.SetupGet(x => x.IsMainDom).Returns(isMainDom);
_mockIOHelper = new Mock<IIOHelper>();
_mockIOHelper.Setup(x => x.GetTempFolders())
.Returns(new DirectoryInfo[] { new DirectoryInfo(_testPath) });
_mockIOHelper.Setup(x => x.CleanFolder(It.IsAny<DirectoryInfo>(), It.IsAny<TimeSpan>()))
.Returns(CleanFolderResult.Success());
var mockLogger = new Mock<ILogger<TempFileCleanup>>();
var mockProfilingLogger = new Mock<IProfilingLogger>();
return new TempFileCleanup(_mockIOHelper.Object, mockMainDom.Object, mockLogger.Object);
}
private void VerifyFilesNotCleaned() => VerifyFilesCleaned(Times.Never());
private void VerifyFilesCleaned() => VerifyFilesCleaned(Times.Once());
private void VerifyFilesCleaned(Times times) => _mockIOHelper.Verify(x => x.CleanFolder(It.Is<DirectoryInfo>(y => y.FullName == _testPath), It.IsAny<TimeSpan>()), times);
}
}