-
Notifications
You must be signed in to change notification settings - Fork 3
Small enhancements for IO file sys operations #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7591bbc
Add support for directory and file path validation utilities and regi…
darthsharp 5ae72d8
Add unit tests for `DirectoryExtensions` to validate directory creati…
darthsharp 5752e58
Suppress nullable warnings in `EnumerableStringExtensions` and adjust…
darthsharp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
source/Core/CreativeCoders.Core/IO/IOServiceCollectionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
tests/CreativeCoders.Core.UnitTests/IO/DirectoryExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.