Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions source/Core/CreativeCoders.Core/IO/DirectoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.IO.Abstractions;

#nullable enable

namespace CreativeCoders.Core.IO;

public static class DirectoryExtensions
{
public static void EnsureDirectoryExists(this IDirectory directory, string? directoryPath)
{
if (string.IsNullOrWhiteSpace(directoryPath))
{
return;
}

directory.CreateDirectory(directoryPath);
}

public static void EnsureDirectoryForFileNameExists(this IDirectory directory, string? filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
return;
}

var directoryPath = directory.FileSystem.Path.GetDirectoryName(filePath);
if (string.IsNullOrWhiteSpace(directoryPath))
{
return;
}

directory.CreateDirectory(directoryPath);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace CreativeCoders.Core.IO;

[ExcludeFromCodeCoverage]
public static class IOServiceCollectionExtensions
{
public static IServiceCollection AddFileSystem(this IServiceCollection services)
{
services.TryAddSingleton<IFileSystem, FileSystemEx>();
services.TryAddSingleton<IFileSystemEx, FileSystemEx>();

return services;
}
Comment thread
darthsharp marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace CreativeCoders.Core.Text;

public static class EnumerableStringExtensions
{
[SuppressMessage("ReSharper", "NullableWarningSuppressionIsUsed")]
public static Dictionary<string, string> ToDictionary(this IEnumerable<string> items, string separator,
bool ignoreInvalidEntries = true)
{
Expand All @@ -27,7 +28,7 @@ public static Dictionary<string, string> ToDictionary(this IEnumerable<string> i

return x != null;
})
.ToDictionary(x => x.Key, x => x.Value);
.ToDictionary(x => x!.Key, x => x!.Value);
}

[ExcludeFromCodeCoverage]
Expand Down
107 changes: 107 additions & 0 deletions tests/CreativeCoders.Core.UnitTests/IO/DirectoryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System.IO.Abstractions;
using CreativeCoders.Core.IO;
using FakeItEasy;
using AwesomeAssertions;
using Xunit;

#nullable enable
namespace CreativeCoders.Core.UnitTests.IO;

public class DirectoryExtensionsTests
{
[Fact]
public void EnsureDirectoryExists_ValidPath_CreatesDirectory()
{
// Arrange
var directory = A.Fake<IDirectory>();
const string path = "/test/path";

// Act
directory.EnsureDirectoryExists(path);

// Assert
A.CallTo(() => directory.CreateDirectory(path))
.MustHaveHappenedOnceExactly();
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void EnsureDirectoryExists_NullOrEmptyPath_DoesNotCreateDirectory(string? path)
{
// Arrange
var directory = A.Fake<IDirectory>();

// Act
directory.EnsureDirectoryExists(path);

// Assert
A.CallTo(() => directory.CreateDirectory(A<string>._))
.MustNotHaveHappened();
}

[Fact]
public void EnsureDirectoryForFileNameExists_ValidFilePath_CreatesDirectory()
{
// Arrange
var directory = A.Fake<IDirectory>();
var fileSystem = A.Fake<IFileSystem>();
var pathMock = A.Fake<IPath>();

A.CallTo(() => directory.FileSystem).Returns(fileSystem);
A.CallTo(() => fileSystem.Path).Returns(pathMock);

const string filePath = "/test/path/file.txt";
const string directoryPath = "/test/path";

A.CallTo(() => pathMock.GetDirectoryName(filePath)).Returns(directoryPath);

// Act
directory.EnsureDirectoryForFileNameExists(filePath);

// Assert
A.CallTo(() => directory.CreateDirectory(directoryPath))
.MustHaveHappenedOnceExactly();
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void EnsureDirectoryForFileNameExists_NullOrEmptyFilePath_DoesNotCreateDirectory(string? filePath)
{
// Arrange
var directory = A.Fake<IDirectory>();

// Act
directory.EnsureDirectoryForFileNameExists(filePath);

// Assert
A.CallTo(() => directory.CreateDirectory(A<string>._))
.MustNotHaveHappened();
}

[Fact]
public void EnsureDirectoryForFileNameExists_GetDirectoryNameReturnsNull_DoesNotCreateDirectory()
{
// Arrange
var directory = A.Fake<IDirectory>();
var fileSystem = A.Fake<IFileSystem>();
var pathMock = A.Fake<IPath>();

A.CallTo(() => directory.FileSystem).Returns(fileSystem);
A.CallTo(() => fileSystem.Path).Returns(pathMock);

const string filePath = "file.txt";

A.CallTo(() => pathMock.GetDirectoryName(filePath)).Returns(null);

// Act
directory.EnsureDirectoryForFileNameExists(filePath);

// Assert
A.CallTo(() => directory.CreateDirectory(A<string>._))
.MustNotHaveHappened();
}
}
Loading