Skip to content

Commit

Permalink
Remove dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Feb 23, 2024
1 parent 46b9a8e commit 34dd937
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using Meziantou.Framework;

namespace Workleap.DotNet.CodingStandards.Tests.Helpers;

internal static class PathHelpers
{
public static FullPath GetRootDirectory()
public static string GetRootDirectory()
{
var directory = FullPath.CurrentDirectory();
while (!Directory.Exists(directory / ".git"))
var directory = Environment.CurrentDirectory;
while (!Directory.Exists(Path.Combine(directory, ".git")))
{
directory = directory.Parent;
directory = Path.GetDirectoryName(directory);
}

return directory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Meziantou.Framework;
using System.Xml.Linq;
using Xunit.Abstractions;
using System.Text.Json;
using CliWrap;

namespace Workleap.DotNet.CodingStandards.Tests.Helpers;

internal sealed class ProjectBuilder : IAsyncDisposable
internal sealed class ProjectBuilder : IDisposable
{
private const string SarifFileName = "BuildOutput.sarif";

Expand Down Expand Up @@ -39,12 +38,12 @@ public ProjectBuilder(PackageFixture fixture, ITestOutputHelper testOutputHelper
</configuration>
""");

File.Copy(PathHelpers.GetRootDirectory() / "global.json", _directory.FullPath / "global.json");
File.Copy(Path.Combine(PathHelpers.GetRootDirectory(), "global.json"), _directory.GetPath("global.json"));
}

public ProjectBuilder AddFile(string relativePath, string content)
{
File.WriteAllText(_directory.FullPath / relativePath, content);
File.WriteAllText(_directory.GetPath(relativePath), content);
return this;
}

Expand Down Expand Up @@ -76,7 +75,7 @@ public ProjectBuilder AddCsprojFile(Dictionary<string, string> properties = null
</Project>
""";

File.WriteAllText(_directory.FullPath / "test.csproj", content);
File.WriteAllText(_directory.GetPath("test.csproj"), content);
return this;
}

Expand All @@ -93,11 +92,11 @@ public async Task<SarifFile> BuildAndGetOutput(string[] buildArguments = null)

_testOutputHelper.WriteLine("Process exit code: " + result.ExitCode);

var bytes = File.ReadAllBytes(_directory.FullPath / SarifFileName);
var bytes = File.ReadAllBytes(_directory.GetPath(SarifFileName));
var sarif = JsonSerializer.Deserialize<SarifFile>(bytes);
_testOutputHelper.WriteLine("Sarif result:\n" + string.Join("\n", sarif.AllResults().Select(r => r.ToString())));
return sarif;
}

public ValueTask DisposeAsync() => _directory.DisposeAsync();
public void Dispose() => _directory.Dispose();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Workleap.DotNet.CodingStandards.Tests.Helpers;

internal sealed class TemporaryDirectory : IDisposable
{
private TemporaryDirectory(string fullPath) => FullPath = fullPath;

public string FullPath { get; }

public static TemporaryDirectory Create()
{
var path = Path.GetFullPath(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")));
Directory.CreateDirectory(path);
return new TemporaryDirectory(path);
}

public string GetPath(string relativePath)
{
return Path.Combine(FullPath, relativePath);
}

public void CreateTextFile(string relativePath, string content)
{
var path = GetPath(relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllText(path, content);
}

public void Dispose()
{
try
{
Directory.Delete(FullPath, recursive: true);
}
catch
{
// We use this code in tests, so it's not important if a folder cannot be deleted
}
}
}
16 changes: 8 additions & 8 deletions tests/Workleap.DotNet.CodingStandards.Tests/PackageFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Diagnostics;
using CliWrap;
using Meziantou.Framework;
using Workleap.DotNet.CodingStandards.Tests.Helpers;

namespace Workleap.DotNet.CodingStandards.Tests;
Expand All @@ -9,16 +8,16 @@ public sealed class PackageFixture : IAsyncLifetime
{
private readonly TemporaryDirectory _packageDirectory = TemporaryDirectory.Create();

public FullPath PackageDirectory => _packageDirectory.FullPath;
public string PackageDirectory => _packageDirectory.FullPath;

public async Task InitializeAsync()
{
var nuspecPath = PathHelpers.GetRootDirectory() / "Workleap.DotNet.CodingStandards.nuspec";
var nuspecPath = Path.Combine(PathHelpers.GetRootDirectory(), "Workleap.DotNet.CodingStandards.nuspec");
string[] args = ["pack", nuspecPath, "-ForceEnglishOutput", "-Version", "999.9.9", "-OutputDirectory", _packageDirectory.FullPath];

if (OperatingSystem.IsWindows())
{
var exe = FullPath.GetTempPath() / $"nuget-{Guid.NewGuid()}.exe";
var exe = Path.Combine(Path.GetTempPath(), $"nuget-{Guid.NewGuid()}.exe");
await DownloadFileAsync("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", exe);

await Cli.Wrap(exe)
Expand All @@ -41,14 +40,15 @@ await Cli.Wrap(exe)
}
}

public async Task DisposeAsync()
public Task DisposeAsync()
{
await _packageDirectory.DisposeAsync();
_packageDirectory.Dispose();
return Task.CompletedTask;
}

private static async Task DownloadFileAsync(string url, FullPath path)
private static async Task DownloadFileAsync(string url, string path)
{
path.CreateParentDirectory();
Directory.CreateDirectory(Path.GetDirectoryName(path));
await using var nugetStream = await SharedHttpClient.Instance.GetStreamAsync(url);
await using var fileStream = File.Create(path);
await nugetStream.CopyToAsync(fileStream);
Expand Down
8 changes: 4 additions & 4 deletions tests/Workleap.DotNet.CodingStandards.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class UnitTest1(PackageFixture fixture, ITestOutputHelper testOutputHelpe
[Fact]
public async Task BannedSymbolsAreReported()
{
await using var project = new ProjectBuilder(fixture, testOutputHelper);
using var project = new ProjectBuilder(fixture, testOutputHelper);
project.AddCsprojFile();
project.AddFile("sample.cs", "_ = System.DateTime.Now;");
var data = await project.BuildAndGetOutput();
Expand All @@ -18,7 +18,7 @@ public async Task BannedSymbolsAreReported()
[Fact]
public async Task WarningsAsErrorOnGitHubActions()
{
await using var project = new ProjectBuilder(fixture, testOutputHelper);
using var project = new ProjectBuilder(fixture, testOutputHelper);
project.AddCsprojFile();
project.AddFile("sample.cs", "_ = System.DateTime.Now;");
var data = await project.BuildAndGetOutput(["--configuration", "Release", "/p:GITHUB_ACTIONS=true"]);
Expand All @@ -28,7 +28,7 @@ public async Task WarningsAsErrorOnGitHubActions()
[Fact]
public async Task NamingConvention_Invalid()
{
await using var project = new ProjectBuilder(fixture, testOutputHelper);
using var project = new ProjectBuilder(fixture, testOutputHelper);
project.AddCsprojFile();
project.AddFile("sample.cs", """
_ = "";
Expand All @@ -49,7 +49,7 @@ class Sample
[Fact]
public async Task NamingConvention_Valid()
{
await using var project = new ProjectBuilder(fixture, testOutputHelper);
using var project = new ProjectBuilder(fixture, testOutputHelper);
project.AddCsprojFile();
project.AddFile("sample.cs", """
_ = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Framework.TemporaryDirectory" Version="1.0.9" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 34dd937

Please sign in to comment.