fix: fixed the Git root searching plus added tests

This commit is contained in:
Dylan R. E. Moonfire 2021-12-06 23:37:04 -06:00
parent 2d8829b8e9
commit 37f5e3f4cb
8 changed files with 139 additions and 8 deletions

View file

@ -1,7 +1,5 @@
#dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=../artifacts/{assembly}-test-result.xml;MethodFormat=Default;FailureBodyFormat=Verbose" --collect:"XPlat Code Coverage" dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=../artifacts/{assembly}-test-result.xml;MethodFormat=Default;FailureBodyFormat=Verbose" --collect:"XPlat Code Coverage"
#dotnet new tool-manifest dotnet new tool-manifest
#dotnet tool install dotnet-reportgenerator-globaltool dotnet tool install dotnet-reportgenerator-globaltool
#dotnet tool run reportgenerator -reports:tests/*/TestResults/*/coverage.cobertura.xml -targetdir:./coverage "-reporttypes:Cobertura;TextSummary" dotnet tool run reportgenerator -reports:tests/*/TestResults/*/coverage.cobertura.xml -targetdir:./coverage "-reporttypes:Cobertura;TextSummary"
#grep "Line coverage" coverage/Summary.txt grep "Line coverage" coverage/Summary.txt
echo "Line coverage 0.00%"

View file

@ -7,6 +7,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{1C957FCA-B9A
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MfGames.IO", "src\MfGames.IO\MfGames.IO.csproj", "{D4386FAC-E9E0-4FBF-9423-5F3699F19920}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MfGames.IO", "src\MfGames.IO\MfGames.IO.csproj", "{D4386FAC-E9E0-4FBF-9423-5F3699F19920}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{6F7CE793-DD94-4AD7-B3CB-94ECF2BA77A6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MfGames.IO.Tests", "tests\MfGames.IO.Tests\MfGames.IO.Tests.csproj", "{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -32,8 +36,21 @@ Global
{D4386FAC-E9E0-4FBF-9423-5F3699F19920}.Release|x64.Build.0 = Release|Any CPU {D4386FAC-E9E0-4FBF-9423-5F3699F19920}.Release|x64.Build.0 = Release|Any CPU
{D4386FAC-E9E0-4FBF-9423-5F3699F19920}.Release|x86.ActiveCfg = Release|Any CPU {D4386FAC-E9E0-4FBF-9423-5F3699F19920}.Release|x86.ActiveCfg = Release|Any CPU
{D4386FAC-E9E0-4FBF-9423-5F3699F19920}.Release|x86.Build.0 = Release|Any CPU {D4386FAC-E9E0-4FBF-9423-5F3699F19920}.Release|x86.Build.0 = Release|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Debug|x64.ActiveCfg = Debug|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Debug|x64.Build.0 = Debug|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Debug|x86.ActiveCfg = Debug|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Debug|x86.Build.0 = Debug|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Release|Any CPU.Build.0 = Release|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Release|x64.ActiveCfg = Release|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Release|x64.Build.0 = Release|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Release|x86.ActiveCfg = Release|Any CPU
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{D4386FAC-E9E0-4FBF-9423-5F3699F19920} = {1C957FCA-B9AA-4A64-BF20-E215EAE5C4E4} {D4386FAC-E9E0-4FBF-9423-5F3699F19920} = {1C957FCA-B9AA-4A64-BF20-E215EAE5C4E4}
{4E3B0E55-A1A0-4007-8CF2-C02396FBBB22} = {6F7CE793-DD94-4AD7-B3CB-94ECF2BA77A6}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View file

@ -90,3 +90,13 @@ The same as `File.WriteAllText(file.GetFullPath, text)`.
#### `void WriteAllText(this FileInfo file, string text, Encoding encoding)` #### `void WriteAllText(this FileInfo file, string text, Encoding encoding)`
The same as `File.WriteAllText(file.GetFullPath, text, encoding)`. The same as `File.WriteAllText(file.GetFullPath, text, encoding)`.
### Type Extensions
#### `DirectoryInfo? GetDirectory(this Type? type)`
Gets a directory containing the type's assembly directory.
#### `FileInfo? GetFile(this Type? type)`
Gets a file representing the type's assembly's `Location` property or null.

View file

@ -86,7 +86,7 @@ namespace MfGames.IO.Extensions
// If the directory is null, just return null. Same with // If the directory is null, just return null. Same with
// non-existing directories. We don't use directory.Exists here // non-existing directories. We don't use directory.Exists here
// because it seems to be cached and we get incorrect data. // because it seems to be cached and we get incorrect data.
if (directory == null || !File.Exists(directory.FullName)) if (directory == null || !Directory.Exists(directory.FullName))
{ {
return null; return null;
} }

View file

@ -0,0 +1,31 @@
using System;
using System.IO;
namespace MfGames.IO.Extensions
{
/// <summary>
/// Additional methods for types.
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Gets the directory for a given type.
/// </summary>
/// <param name="type">The type assembly to get the directory.</param>
/// <returns>The directory or null if the assembly or location is null.</returns>
public static DirectoryInfo? GetDirectory(this Type? type)
{
return type?.Assembly.GetDirectory();
}
/// <summary>
/// Gets the file for a given type's assembly.
/// </summary>
/// <param name="type">The type assembly to get the directory.</param>
/// <returns>The directory or null if the assembly or location is null.</returns>
public static FileInfo? GetFile(this Type? type)
{
return type?.Assembly.GetFile();
}
}
}

View file

@ -0,0 +1,29 @@
using System.IO;
using MfGames.IO.Extensions;
using Xunit;
namespace MfGames.IO.Tests
{
public class AssemblyExtensionsTests
{
[Fact]
public void DirectoryExists()
{
DirectoryInfo? dir = this.GetType().Assembly.GetDirectory();
Assert.NotNull(dir);
Assert.True(dir!.Exists);
}
[Fact]
public void FileExists()
{
FileInfo? file = this.GetType().Assembly.GetFile();
Assert.NotNull(file);
Assert.True(file!.Exists);
}
}
}

View file

@ -0,0 +1,21 @@
using System.IO;
using MfGames.IO.Extensions;
using Xunit;
namespace MfGames.IO.Tests
{
public class GitRootTests
{
[Fact]
public void FindGitRoot()
{
DirectoryInfo? dir = this.GetType().GetDirectory();
DirectoryInfo? git = dir.FindGitRoot();
Assert.NotNull(git);
Assert.True(git!.GetDirectories(".git").Length > 0);
}
}
}

View file

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\MfGames.IO\MfGames.IO.csproj" />
</ItemGroup>
</Project>