Skip to content

Commit

Permalink
Test that configured directory extists.
Browse files Browse the repository at this point in the history
  • Loading branch information
atmoos committed May 20, 2024
1 parent 3d84e95 commit bad339d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
25 changes: 22 additions & 3 deletions source/GlassView.Export.Test/GlassViewTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,37 @@ 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<SummaryExporter>(publicExporter);
var directoryExporter = Assert.Single(exporter);
Assert.IsType<DirectoryExport>(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)))
Expand Down
10 changes: 4 additions & 6 deletions source/GlassView.Export/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -26,11 +23,12 @@ private static JsonSerializerOptions Options(JsonSerializerOptions options)
}

/// <summary>
/// Find the leaf directory in the current directory structure.
/// Recursively looks upward toward parent directories for the leaf directory <paramref name="leafDirectoryName"/>
/// in the current directory structure staring at the current working directory.
/// </summary>
/// <param name="leafDirectoryName">The leaf directories name.</param>
/// <returns>If found returns that directory.
/// When not found returns the leaf directory as subdirectory of the current directory.</returns>
/// <returns>If found, returns that directory.
/// When not found, returns the leaf directory as subdirectory of the current directory.</returns>
internal static DirectoryInfo FindLeaf(String leafDirectoryName)
{
DirectoryInfo? result;
Expand Down
2 changes: 1 addition & 1 deletion source/GlassView.Export/GlassView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit bad339d

Please sign in to comment.