Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package for shared testing tools #42

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Sources/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@

<ItemGroup>
<PackageReference Include="Kysect.Editorconfig" />
<None Include="$(SolutionDir)..\Images\Kysect-logo.png" Pack="true" PackagePath="" />
<None Include="$(SolutionDir)..\README.md" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup Condition="$(Configuration) == 'Release'">
<None Include="$(SolutionDir)..\Images\Kysect-logo.png" Pack="true" PackagePath="/" />
<None Include="$(SolutionDir)..\README.md" Pack="true" PackagePath="/" />
</ItemGroup>
</Project>
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 @@ -3,7 +3,6 @@
<Title>Kysect.CommonLib.DependencyInjection</Title>
<Description>Common lib for Kysect projects.</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IsPackable>true</IsPackable>
</PropertyGroup>

Expand Down
17 changes: 17 additions & 0 deletions Sources/Kysect.CommonLib.Testing/Kysect.CommonLib.Testing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<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();
}
}
6 changes: 6 additions & 0 deletions Sources/Kysect.CommonLib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion Sources/Kysect.CommonLib/Kysect.CommonLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<Title>Kysect.CommonLib</Title>
<Description>Common lib for Kysect projects.</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IsPackable>true</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Expand Down
Loading