diff --git a/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/PathHelpers.cs b/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/PathHelpers.cs index 5c831af..742fe8f 100644 --- a/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/PathHelpers.cs +++ b/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/PathHelpers.cs @@ -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; diff --git a/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/ProjectBuilder.cs b/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/ProjectBuilder.cs index a4f95e2..790dfcc 100644 --- a/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/ProjectBuilder.cs +++ b/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/ProjectBuilder.cs @@ -1,4 +1,3 @@ -using Meziantou.Framework; using System.Xml.Linq; using Xunit.Abstractions; using System.Text.Json; @@ -6,7 +5,7 @@ namespace Workleap.DotNet.CodingStandards.Tests.Helpers; -internal sealed class ProjectBuilder : IAsyncDisposable +internal sealed class ProjectBuilder : IDisposable { private const string SarifFileName = "BuildOutput.sarif"; @@ -39,12 +38,12 @@ public ProjectBuilder(PackageFixture fixture, ITestOutputHelper testOutputHelper """); - 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; } @@ -76,7 +75,7 @@ public ProjectBuilder AddCsprojFile(Dictionary properties = null """; - File.WriteAllText(_directory.FullPath / "test.csproj", content); + File.WriteAllText(_directory.GetPath("test.csproj"), content); return this; } @@ -93,11 +92,11 @@ public async Task 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(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(); } diff --git a/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/TemporaryDirectory.cs b/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/TemporaryDirectory.cs new file mode 100644 index 0000000..c6ce75a --- /dev/null +++ b/tests/Workleap.DotNet.CodingStandards.Tests/Helpers/TemporaryDirectory.cs @@ -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 + } + } +} diff --git a/tests/Workleap.DotNet.CodingStandards.Tests/PackageFixture.cs b/tests/Workleap.DotNet.CodingStandards.Tests/PackageFixture.cs index c475fcd..511adcf 100644 --- a/tests/Workleap.DotNet.CodingStandards.Tests/PackageFixture.cs +++ b/tests/Workleap.DotNet.CodingStandards.Tests/PackageFixture.cs @@ -1,6 +1,5 @@ using System.Diagnostics; using CliWrap; -using Meziantou.Framework; using Workleap.DotNet.CodingStandards.Tests.Helpers; namespace Workleap.DotNet.CodingStandards.Tests; @@ -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) @@ -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); diff --git a/tests/Workleap.DotNet.CodingStandards.Tests/UnitTest1.cs b/tests/Workleap.DotNet.CodingStandards.Tests/UnitTest1.cs index 3cf5d63..9aeb855 100644 --- a/tests/Workleap.DotNet.CodingStandards.Tests/UnitTest1.cs +++ b/tests/Workleap.DotNet.CodingStandards.Tests/UnitTest1.cs @@ -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(); @@ -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"]); @@ -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", """ _ = ""; @@ -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", """ _ = ""; diff --git a/tests/Workleap.DotNet.CodingStandards.Tests/Workleap.DotNet.CodingStandards.Tests.csproj b/tests/Workleap.DotNet.CodingStandards.Tests/Workleap.DotNet.CodingStandards.Tests.csproj index 4732c62..e51554f 100644 --- a/tests/Workleap.DotNet.CodingStandards.Tests/Workleap.DotNet.CodingStandards.Tests.csproj +++ b/tests/Workleap.DotNet.CodingStandards.Tests/Workleap.DotNet.CodingStandards.Tests.csproj @@ -16,7 +16,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive -