-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
Sources/Kysect.CommonLib.Testing/TestTemporaryDirectory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |