Skip to content

Commit 6282495

Browse files
authored
Add temp path overload for disposable extensions (#63)
1 parent 2891530 commit 6282495

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/System.IO.Abstractions.Extensions/IFileSystemExtensions.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ public static IDisposable CreateDisposableDirectory(this IFileSystem fileSystem,
5555
return fileSystem.CreateDisposableDirectory(path, dir => new DisposableDirectory(dir), out directoryInfo);
5656
}
5757

58+
/// <inheritdoc cref="CreateDisposableDirectory(IFileSystem, out IDirectoryInfo)"/>
59+
/// <summary>
60+
/// Creates a new <see cref="IDirectoryInfo"/> using a random name from the temp path and returns an
61+
/// <see cref="IDisposable"/> created by <paramref name="disposableFactory"/>, that should delete the directory when disposed.
62+
/// </summary>
63+
/// <param name="disposableFactory">
64+
/// A <see cref="Func{T, TResult}"/> that acts as a factory method. Given the <see cref="IDirectoryInfo"/>, create the
65+
/// <see cref="IDisposable"/> that will manage the its lifetime.
66+
/// </param>
67+
public static T CreateDisposableDirectory<T>(
68+
this IFileSystem fileSystem,
69+
Func<IDirectoryInfo, T> disposableFactory,
70+
out IDirectoryInfo directoryInfo) where T : IDisposable
71+
{
72+
return fileSystem.CreateDisposableDirectory(fileSystem.Path.GetRandomTempPath(), disposableFactory, out directoryInfo);
73+
}
74+
5875
/// <inheritdoc cref="CreateDisposableDirectory(IFileSystem, string, out IDirectoryInfo)"/>
5976
/// <summary>
6077
/// Creates a new <see cref="IDirectoryInfo"/> using a path provided by <paramref name="path"/>, and returns an
@@ -116,6 +133,23 @@ public static IDisposable CreateDisposableFile(this IFileSystem fileSystem, stri
116133
return fileSystem.CreateDisposableFile(path, file => new DisposableFile(file), out fileInfo);
117134
}
118135

136+
/// <inheritdoc cref="CreateDisposableFile(IFileSystem, out IFileInfo)"/>
137+
/// <summary>
138+
/// Creates a new <see cref="IFileInfo"/> using a random name from the temp path and returns an
139+
/// <see cref="IDisposable"/> created by <paramref name="disposableFactory"/>, that should delete the file when disposed.
140+
/// </summary>
141+
/// <param name="disposableFactory">
142+
/// A <see cref="Func{T, TResult}"/> that acts as a factory method. Given the <see cref="IFileInfo"/>, create the
143+
/// <see cref="IDisposable"/> that will manage the its lifetime.
144+
/// </param>
145+
public static T CreateDisposableFile<T>(
146+
this IFileSystem fileSystem,
147+
Func<IFileInfo, T> disposableFactory,
148+
out IFileInfo fileInfo) where T : IDisposable
149+
{
150+
return fileSystem.CreateDisposableFile(fileSystem.Path.GetRandomTempPath(), disposableFactory, out fileInfo);
151+
}
152+
119153
/// <inheritdoc cref="CreateDisposableFile(IFileSystem, string, out IFileInfo)"/>
120154
/// <summary>
121155
/// Creates a new <see cref="IFileInfo"/> using a path provided by <paramref name="path"/>, and returns an

tests/System.IO.Abstractions.Extensions.Tests/FileSystemExtensionsTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public void CreateDisposableDirectory_Custom_IDisposable_Test()
8989
{
9090
// Arrange
9191
var fs = new FileSystem();
92-
var path = fs.Path.Combine(fs.Path.GetTempPath(), fs.Path.GetRandomFileName());
92+
string path = null;
9393

9494
// Act
9595
CustomDisposableDirectory customDisposable;
96-
using (customDisposable = fs.CreateDisposableDirectory(path, dir => new CustomDisposableDirectory(dir), out var dirInfo))
96+
using (customDisposable = fs.CreateDisposableDirectory(dir => new CustomDisposableDirectory(dir), out var dirInfo))
9797
{
9898
path = dirInfo.FullName;
9999

@@ -102,6 +102,7 @@ public void CreateDisposableDirectory_Custom_IDisposable_Test()
102102
}
103103

104104
// Assert directory is deleted
105+
Assert.IsNotNull(path);
105106
Assert.IsFalse(fs.Directory.Exists(path), "Directory should not exist");
106107
Assert.IsTrue(customDisposable.DeleteFileSystemInfoWasCalled, "Custom disposable delete should have been called");
107108
}
@@ -150,11 +151,11 @@ public void CreateDisposableFile_Custom_IDisposable_Test()
150151
{
151152
// Arrange
152153
var fs = new FileSystem();
153-
var path = fs.Path.Combine(fs.Path.GetTempPath(), fs.Path.GetRandomFileName());
154+
string path = null;
154155

155156
// Act
156157
CustomDisposableFile customDisposable;
157-
using (customDisposable = fs.CreateDisposableFile(path, dir => new CustomDisposableFile(dir), out var fileInfo))
158+
using (customDisposable = fs.CreateDisposableFile(dir => new CustomDisposableFile(dir), out var fileInfo))
158159
{
159160
path = fileInfo.FullName;
160161

@@ -163,6 +164,7 @@ public void CreateDisposableFile_Custom_IDisposable_Test()
163164
}
164165

165166
// Assert file is deleted
167+
Assert.IsNotNull(path);
166168
Assert.IsFalse(fs.File.Exists(path), "File should not exist");
167169
Assert.IsTrue(customDisposable.DeleteFileSystemInfoWasCalled, "Custom disposable delete should have been called");
168170
}

0 commit comments

Comments
 (0)