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 src/System.IO.Abstractions.Extensions/IFileSystemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ public static IDisposable CreateDisposableDirectory(this IFileSystem fileSystem,
return fileSystem.CreateDisposableDirectory(path, dir => new DisposableDirectory(dir), out directoryInfo);
}

/// <inheritdoc cref="CreateDisposableDirectory(IFileSystem, out IDirectoryInfo)"/>
/// <summary>
/// Creates a new <see cref="IDirectoryInfo"/> using a random name from the temp path and returns an
/// <see cref="IDisposable"/> created by <paramref name="disposableFactory"/>, that should delete the directory when disposed.
/// </summary>
/// <param name="disposableFactory">
/// A <see cref="Func{T, TResult}"/> that acts as a factory method. Given the <see cref="IDirectoryInfo"/>, create the
/// <see cref="IDisposable"/> that will manage the its lifetime.
/// </param>
public static T CreateDisposableDirectory<T>(
this IFileSystem fileSystem,
Func<IDirectoryInfo, T> disposableFactory,
out IDirectoryInfo directoryInfo) where T : IDisposable
{
return fileSystem.CreateDisposableDirectory(fileSystem.Path.GetRandomTempPath(), disposableFactory, out directoryInfo);
}

/// <inheritdoc cref="CreateDisposableDirectory(IFileSystem, string, out IDirectoryInfo)"/>
/// <summary>
/// Creates a new <see cref="IDirectoryInfo"/> using a path provided by <paramref name="path"/>, and returns an
Expand Down Expand Up @@ -116,6 +133,23 @@ public static IDisposable CreateDisposableFile(this IFileSystem fileSystem, stri
return fileSystem.CreateDisposableFile(path, file => new DisposableFile(file), out fileInfo);
}

/// <inheritdoc cref="CreateDisposableFile(IFileSystem, out IFileInfo)"/>
/// <summary>
/// Creates a new <see cref="IFileInfo"/> using a random name from the temp path and returns an
/// <see cref="IDisposable"/> created by <paramref name="disposableFactory"/>, that should delete the file when disposed.
/// </summary>
/// <param name="disposableFactory">
/// A <see cref="Func{T, TResult}"/> that acts as a factory method. Given the <see cref="IFileInfo"/>, create the
/// <see cref="IDisposable"/> that will manage the its lifetime.
/// </param>
public static T CreateDisposableFile<T>(
this IFileSystem fileSystem,
Func<IFileInfo, T> disposableFactory,
out IFileInfo fileInfo) where T : IDisposable
{
return fileSystem.CreateDisposableFile(fileSystem.Path.GetRandomTempPath(), disposableFactory, out fileInfo);
}

/// <inheritdoc cref="CreateDisposableFile(IFileSystem, string, out IFileInfo)"/>
/// <summary>
/// Creates a new <see cref="IFileInfo"/> using a path provided by <paramref name="path"/>, and returns an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public void CreateDisposableDirectory_Custom_IDisposable_Test()
{
// Arrange
var fs = new FileSystem();
var path = fs.Path.Combine(fs.Path.GetTempPath(), fs.Path.GetRandomFileName());
string path = null;

// Act
CustomDisposableDirectory customDisposable;
using (customDisposable = fs.CreateDisposableDirectory(path, dir => new CustomDisposableDirectory(dir), out var dirInfo))
using (customDisposable = fs.CreateDisposableDirectory(dir => new CustomDisposableDirectory(dir), out var dirInfo))
{
path = dirInfo.FullName;

Expand All @@ -102,6 +102,7 @@ public void CreateDisposableDirectory_Custom_IDisposable_Test()
}

// Assert directory is deleted
Assert.IsNotNull(path);
Assert.IsFalse(fs.Directory.Exists(path), "Directory should not exist");
Assert.IsTrue(customDisposable.DeleteFileSystemInfoWasCalled, "Custom disposable delete should have been called");
}
Expand Down Expand Up @@ -150,11 +151,11 @@ public void CreateDisposableFile_Custom_IDisposable_Test()
{
// Arrange
var fs = new FileSystem();
var path = fs.Path.Combine(fs.Path.GetTempPath(), fs.Path.GetRandomFileName());
string path = null;

// Act
CustomDisposableFile customDisposable;
using (customDisposable = fs.CreateDisposableFile(path, dir => new CustomDisposableFile(dir), out var fileInfo))
using (customDisposable = fs.CreateDisposableFile(dir => new CustomDisposableFile(dir), out var fileInfo))
{
path = fileInfo.FullName;

Expand All @@ -163,6 +164,7 @@ public void CreateDisposableFile_Custom_IDisposable_Test()
}

// Assert file is deleted
Assert.IsNotNull(path);
Assert.IsFalse(fs.File.Exists(path), "File should not exist");
Assert.IsTrue(customDisposable.DeleteFileSystemInfoWasCalled, "Custom disposable delete should have been called");
}
Expand Down