Skip to content

Commit

Permalink
Add TestTemporaryDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed Apr 27, 2024
1 parent ce16141 commit 4f33a3d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<PackageVersion Include="Nullable" Version="1.3.1" />
<PackageVersion Include="TestableIO.System.IO.Abstractions" Version="20.0.4" />
<PackageVersion Include="TestableIO.System.IO.Abstractions.Analyzers" Version="2022.0.0" />
<PackageVersion Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="20.0.4" />
<PackageVersion Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="21.0.2" />
<PackageVersion Include="xunit" Version="2.7.1" />
<PackageVersion Include="xunit.analyzers" Version="1.12.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.8" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kysect.CommonLib.DependencyInjection\Kysect.CommonLib.DependencyInjection.csproj" />
<ProjectReference Include="..\Kysect.CommonLib\Kysect.CommonLib.csproj" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions Sources/Kysect.CommonLib.Testing/TestLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.Logging;

#if DEBUG
using Kysect.CommonLib.DependencyInjection.Logging;
#else
using Microsoft.Extensions.Logging.Abstractions;
#endif

namespace Kysect.CommonLib.Testing;

public static class TestLoggerProvider
{
public static ILogger Provide()
{
#if DEBUG
return DefaultLoggerConfiguration.CreateConsoleLogger();
#else
return NullLogger.Instance;
#endif
}
}
67 changes: 67 additions & 0 deletions Sources/Kysect.CommonLib.Testing/TestTemporaryDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Kysect.CommonLib.BaseTypes.Extensions;
using Kysect.CommonLib.FileSystem;
using System.IO.Abstractions;

namespace Kysect.CommonLib.Testing;

public class TestTemporaryDirectory : IDisposable
{
private readonly System.IO.Abstractions.FileSystem _fileSystem;
private readonly IDirectoryInfo _directoryInfo;

public TestTemporaryDirectory(System.IO.Abstractions.FileSystem fileSystem, string rootPath = ".")
{
_fileSystem = fileSystem.ThrowIfNull();
rootPath.ThrowIfNull();

string path = _fileSystem.Path.Combine(rootPath, "TempDirectory", Guid.NewGuid().ToString());

if (_fileSystem.Directory.Exists(path))
{
IDirectoryInfo directoryInfo = _fileSystem.DirectoryInfo.New(path);
DeleteRecursive(directoryInfo);
}

_directoryInfo = _fileSystem.Directory.CreateDirectory(path);
fileSystem.EnsureDirectoryExists(path);
}

public string GetTemporaryDirectory()
{
return _fileSystem.Path.Combine(_directoryInfo.FullName, Guid.NewGuid().ToString());
}

public void Dispose()
{
DeleteRecursive(_directoryInfo);
}

// KB: https://github.com/libgit2/libgit2sharp/issues/1354
public static void DeleteRecursive(IDirectoryInfo target)
{
target.ThrowIfNull();

if (!target.Exists)
{
return;
}

foreach (var file in target.EnumerateFiles())
{
if (file.IsReadOnly)
{
file.IsReadOnly = false;
}

file.Delete();
}

foreach (IDirectoryInfo dir in target.EnumerateDirectories())
{
DeleteRecursive(dir);
}


target.Delete();
}
}

0 comments on commit 4f33a3d

Please sign in to comment.