From bad339d21546b0655547ab3e3f2b49edcb1a4aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=A4gi?= Date: Mon, 20 May 2024 12:45:14 +0200 Subject: [PATCH] Test that configured directory extists. --- source/GlassView.Export.Test/GlassViewTest.cs | 25 ++++++++++++++++--- source/GlassView.Export/Extensions.cs | 10 +++----- source/GlassView.Export/GlassView.cs | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/source/GlassView.Export.Test/GlassViewTest.cs b/source/GlassView.Export.Test/GlassViewTest.cs index 6f9bb54..746e617 100644 --- a/source/GlassView.Export.Test/GlassViewTest.cs +++ b/source/GlassView.Export.Test/GlassViewTest.cs @@ -7,10 +7,10 @@ public class GlassViewTest { [Fact] - public void ConfigureWithDirectory_ReturnsDirectoryExporter() + public void ConfigureWithDirectoryReturnsDirectoryExporter() { String path = Path.Combine("some", "export", "path"); - IConfiguration configuration = ReadFromJson(DirectoryExportJson(path)); + IConfiguration configuration = ConfigFromJson(DirectoryExportJson(path)); IGlassView publicExporter = GlassView.Configure(configuration); var exporter = Assert.IsType(publicExporter); @@ -18,7 +18,26 @@ public void ConfigureWithDirectory_ReturnsDirectoryExporter() Assert.IsType(directoryExporter); } - private static IConfiguration ReadFromJson(String json) + [Fact] + public void ConfigureWithDirectoryCreatesTheExportPathAsNeeded() + { + const String topPath = "top"; + var path = Path.Combine(topPath, "this", "path", "does", "not", "exist", "yet"); + IConfiguration configuration = ConfigFromJson(DirectoryExportJson(path)); + + Assert.False(Directory.Exists(path), "Precondition failed: directory already exists."); + + try { + var exporter = GlassView.Configure(configuration); + Assert.True(Directory.Exists(path), "The directory was not created."); + Assert.NotNull(exporter); // Avoid warnings & the compiler optimising away the configure step. + } + finally { + Directory.Delete(topPath, recursive: true); + } + } + + private static IConfiguration ConfigFromJson(String json) { return new ConfigurationBuilder() .AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))) diff --git a/source/GlassView.Export/Extensions.cs b/source/GlassView.Export/Extensions.cs index 0f0587a..1ab3dba 100644 --- a/source/GlassView.Export/Extensions.cs +++ b/source/GlassView.Export/Extensions.cs @@ -1,8 +1,5 @@ using System.Text.Json; -using BenchmarkDotNet.Reports; using Atmoos.GlassView.Core; -using Microsoft.Extensions.Configuration; -using static System.Threading.Tasks.ConfigureAwaitOptions; namespace Atmoos.GlassView.Export; @@ -26,11 +23,12 @@ private static JsonSerializerOptions Options(JsonSerializerOptions options) } /// - /// Find the leaf directory in the current directory structure. + /// Recursively looks upward toward parent directories for the leaf directory + /// in the current directory structure staring at the current working directory. /// /// The leaf directories name. - /// If found returns that directory. - /// When not found returns the leaf directory as subdirectory of the current directory. + /// If found, returns that directory. + /// When not found, returns the leaf directory as subdirectory of the current directory. internal static DirectoryInfo FindLeaf(String leafDirectoryName) { DirectoryInfo? result; diff --git a/source/GlassView.Export/GlassView.cs b/source/GlassView.Export/GlassView.cs index 7f5b88c..e7c7195 100644 --- a/source/GlassView.Export/GlassView.cs +++ b/source/GlassView.Export/GlassView.cs @@ -42,7 +42,7 @@ public static IGlassView Configure(IConfiguration config, ILogger logger) return new SummaryExporter(exporters, logger); } - private static DirectoryInfo GlassViewArtifactsDirectory() => Extensions.FindLeaf("BenchmarkDotNet.Artifacts").CreateSubdirectory(nameof(GlassView)); + private static DirectoryInfo GlassViewArtifactsDirectory() => FindLeaf("BenchmarkDotNet.Artifacts").CreateSubdirectory(nameof(GlassView)); private static JsonSerializerOptions SerializationOptions() => new JsonSerializerOptions().EnableGlassView(); }