From e33b6f1d952d198f2bb77fb6959650207c721b82 Mon Sep 17 00:00:00 2001 From: Fredi Kats Date: Sat, 27 Apr 2024 12:09:39 +0200 Subject: [PATCH 1/2] Add project for Kysect.CommonLib.Testing --- Sources/Directory.Build.props | 7 +++++-- .../Kysect.CommonLib.DependencyInjection.csproj | 1 - .../Kysect.CommonLib.Testing.csproj | 8 ++++++++ Sources/Kysect.CommonLib.sln | 6 ++++++ Sources/Kysect.CommonLib/Kysect.CommonLib.csproj | 1 - 5 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 Sources/Kysect.CommonLib.Testing/Kysect.CommonLib.Testing.csproj diff --git a/Sources/Directory.Build.props b/Sources/Directory.Build.props index a15de8c..1008e2c 100644 --- a/Sources/Directory.Build.props +++ b/Sources/Directory.Build.props @@ -29,7 +29,10 @@ - - + + + + + \ No newline at end of file diff --git a/Sources/Kysect.CommonLib.DependencyInjection/Kysect.CommonLib.DependencyInjection.csproj b/Sources/Kysect.CommonLib.DependencyInjection/Kysect.CommonLib.DependencyInjection.csproj index f96db51..5464c6b 100644 --- a/Sources/Kysect.CommonLib.DependencyInjection/Kysect.CommonLib.DependencyInjection.csproj +++ b/Sources/Kysect.CommonLib.DependencyInjection/Kysect.CommonLib.DependencyInjection.csproj @@ -3,7 +3,6 @@ Kysect.CommonLib.DependencyInjection Common lib for Kysect projects. netstandard2.0 - True true diff --git a/Sources/Kysect.CommonLib.Testing/Kysect.CommonLib.Testing.csproj b/Sources/Kysect.CommonLib.Testing/Kysect.CommonLib.Testing.csproj new file mode 100644 index 0000000..ba9c2ef --- /dev/null +++ b/Sources/Kysect.CommonLib.Testing/Kysect.CommonLib.Testing.csproj @@ -0,0 +1,8 @@ + + + + netstandard2.0 + true + + + diff --git a/Sources/Kysect.CommonLib.sln b/Sources/Kysect.CommonLib.sln index 8fb6e01..d1c96dd 100644 --- a/Sources/Kysect.CommonLib.sln +++ b/Sources/Kysect.CommonLib.sln @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Directory.Packages.props = Directory.Packages.props EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kysect.CommonLib.Testing", "Kysect.CommonLib.Testing\Kysect.CommonLib.Testing.csproj", "{3FF6B2B6-9FE7-4401-8478-1A5E1121EAD4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {AA74524A-D403-4EC2-849B-92A2EF25A570}.Debug|Any CPU.Build.0 = Debug|Any CPU {AA74524A-D403-4EC2-849B-92A2EF25A570}.Release|Any CPU.ActiveCfg = Release|Any CPU {AA74524A-D403-4EC2-849B-92A2EF25A570}.Release|Any CPU.Build.0 = Release|Any CPU + {3FF6B2B6-9FE7-4401-8478-1A5E1121EAD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3FF6B2B6-9FE7-4401-8478-1A5E1121EAD4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3FF6B2B6-9FE7-4401-8478-1A5E1121EAD4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3FF6B2B6-9FE7-4401-8478-1A5E1121EAD4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Sources/Kysect.CommonLib/Kysect.CommonLib.csproj b/Sources/Kysect.CommonLib/Kysect.CommonLib.csproj index a0acc6a..c326a89 100644 --- a/Sources/Kysect.CommonLib/Kysect.CommonLib.csproj +++ b/Sources/Kysect.CommonLib/Kysect.CommonLib.csproj @@ -3,7 +3,6 @@ Kysect.CommonLib Common lib for Kysect projects. netstandard2.0 - True true true From f31fc0ae1adb2872bbc42c5deceaaa70e63b349d Mon Sep 17 00:00:00 2001 From: Fredi Kats Date: Sat, 27 Apr 2024 12:33:08 +0200 Subject: [PATCH 2/2] Add TestTemporaryDirectory --- Sources/Directory.Packages.props | 2 +- .../Kysect.CommonLib.Testing.csproj | 9 +++ .../TestLoggerProvider.cs | 21 ++++++ .../TestTemporaryDirectory.cs | 67 +++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 Sources/Kysect.CommonLib.Testing/TestLoggerProvider.cs create mode 100644 Sources/Kysect.CommonLib.Testing/TestTemporaryDirectory.cs diff --git a/Sources/Directory.Packages.props b/Sources/Directory.Packages.props index 401fb9e..bfdbad4 100644 --- a/Sources/Directory.Packages.props +++ b/Sources/Directory.Packages.props @@ -32,7 +32,7 @@ - + diff --git a/Sources/Kysect.CommonLib.Testing/Kysect.CommonLib.Testing.csproj b/Sources/Kysect.CommonLib.Testing/Kysect.CommonLib.Testing.csproj index ba9c2ef..229941a 100644 --- a/Sources/Kysect.CommonLib.Testing/Kysect.CommonLib.Testing.csproj +++ b/Sources/Kysect.CommonLib.Testing/Kysect.CommonLib.Testing.csproj @@ -5,4 +5,13 @@ true + + + + + + + + + diff --git a/Sources/Kysect.CommonLib.Testing/TestLoggerProvider.cs b/Sources/Kysect.CommonLib.Testing/TestLoggerProvider.cs new file mode 100644 index 0000000..81ab20d --- /dev/null +++ b/Sources/Kysect.CommonLib.Testing/TestLoggerProvider.cs @@ -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 + } +} \ No newline at end of file diff --git a/Sources/Kysect.CommonLib.Testing/TestTemporaryDirectory.cs b/Sources/Kysect.CommonLib.Testing/TestTemporaryDirectory.cs new file mode 100644 index 0000000..04ce132 --- /dev/null +++ b/Sources/Kysect.CommonLib.Testing/TestTemporaryDirectory.cs @@ -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(); + } +} \ No newline at end of file