refactor: Add JsonUtils class

pull/139/head
Robert Dailey 2 years ago
parent dcfbccae2f
commit 88bdc5a92e

@ -0,0 +1,94 @@
using System.IO.Abstractions;
using System.IO.Abstractions.Extensions;
using System.IO.Abstractions.TestingHelpers;
using FluentAssertions;
using NUnit.Framework;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.TestCorrelator;
namespace Common.Tests;
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class JsonUtilsTest
{
[Test]
public void Log_files_that_do_not_exist()
{
var fs = new MockFileSystem();
var log = new LoggerConfiguration()
.MinimumLevel.Is(LogEventLevel.Debug)
.WriteTo.TestCorrelator()
.CreateLogger();
using var context = TestCorrelator.CreateContext();
var path = fs.CurrentDirectory().SubDirectory("doesnt_exist");
var result = JsonUtils.GetJsonFilesInDirectories(new[] {path}, log);
result.Should().BeEmpty();
TestCorrelator.GetLogEventsFromCurrentContext()
.Should().ContainSingle()
.Which.RenderMessage()
.Should().Match("*doesnt_exist*");
}
[Test]
public void Log_files_that_only_exist()
{
var fs = new MockFileSystem();
var log = new LoggerConfiguration()
.MinimumLevel.Is(LogEventLevel.Debug)
.WriteTo.TestCorrelator()
.CreateLogger();
using var context = TestCorrelator.CreateContext();
var path = fs.CurrentDirectory().SubDirectory("exists").File("test.json");
fs.AddFile(path.FullName, new MockFileData(""));
var result = JsonUtils.GetJsonFilesInDirectories(new[] {path.Directory}, log);
result.Should().ContainSingle()
.Which.FullName
.Should().Be(path.FullName);
TestCorrelator.GetLogEventsFromCurrentContext().Should().BeEmpty();
}
[Test]
public void Log_files_that_both_exist_and_do_not_exist()
{
var fs = new MockFileSystem();
var log = new LoggerConfiguration()
.MinimumLevel.Is(LogEventLevel.Debug)
.WriteTo.TestCorrelator()
.CreateLogger();
using var context = TestCorrelator.CreateContext();
var paths = new[]
{
fs.CurrentDirectory().SubDirectory("does_not_exist"),
fs.CurrentDirectory().SubDirectory("exists")
};
var existingFile = paths[1].File("test.json").FullName;
fs.AddFile(existingFile, new MockFileData(""));
paths[1].Refresh();
var result = JsonUtils.GetJsonFilesInDirectories(paths, log);
result.Should().ContainSingle()
.Which.FullName
.Should().Be(existingFile);
TestCorrelator.GetLogEventsFromCurrentContext()
.Should().ContainSingle()
.Which.RenderMessage()
.Should().Match("*does_not_exist*");
}
}

@ -0,0 +1,19 @@
using System.IO.Abstractions;
using Serilog;
namespace Common;
public static class JsonUtils
{
public static IEnumerable<IFileInfo> GetJsonFilesInDirectories(IEnumerable<IDirectoryInfo> dirs, ILogger log)
{
var dirsThatExist = dirs.ToLookup(x => x.Exists);
foreach (var dir in dirsThatExist[false])
{
log.Debug("Specified metadata path does not exist: {Path}", dir);
}
return dirsThatExist[true].SelectMany(x => x.GetFiles("*.json"));
}
}
Loading…
Cancel
Save