Skip to content

Commit 1fcc5cd

Browse files
authored
fix: null-handling in Wrap-Method in factories for IDirectoryInfo or IFileInfo (#976)
Fixes #975: When providing `null` as parameter to the implementations of `IFileInfoFactory` or `IDirectoryInfoFactory` return `null` instead of throwing an exception.
1 parent e84ace2 commit 1fcc5cd

File tree

8 files changed

+86
-2
lines changed

8 files changed

+86
-2
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectoryInfoFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public IDirectoryInfo New(string path)
3333
/// <inheritdoc />
3434
public IDirectoryInfo Wrap(DirectoryInfo directoryInfo)
3535
{
36+
if (directoryInfo == null)
37+
{
38+
return null;
39+
}
40+
3641
return new MockDirectoryInfo(mockFileSystem, directoryInfo.Name);
3742
}
3843
}

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileInfoFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public IFileInfo New(string fileName)
3232
/// <inheritdoc />
3333
public IFileInfo Wrap(FileInfo fileInfo)
3434
{
35+
if (fileInfo == null)
36+
{
37+
return null;
38+
}
39+
3540
return new MockFileInfo(mockFileSystem, fileInfo.Name);
3641
}
3742
}

src/TestableIO.System.IO.Abstractions.Wrappers/DirectoryInfoFactory.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ public IDirectoryInfo New(string path)
2828
var realDirectoryInfo = new DirectoryInfo(path);
2929
return new DirectoryInfoWrapper(fileSystem, realDirectoryInfo);
3030
}
31-
31+
3232
/// <inheritdoc />
3333
public IDirectoryInfo Wrap(DirectoryInfo directoryInfo)
3434
{
35+
if (directoryInfo == null)
36+
{
37+
return null;
38+
}
39+
3540
return new DirectoryInfoWrapper(fileSystem, directoryInfo);
3641
}
3742
}

src/TestableIO.System.IO.Abstractions.Wrappers/FileInfoFactory.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ public IFileInfo New(string fileName)
2828
var realFileInfo = new FileInfo(fileName);
2929
return new FileInfoWrapper(fileSystem, realFileInfo);
3030
}
31-
31+
3232
/// <inheritdoc />
3333
public IFileInfo Wrap(FileInfo fileInfo)
3434
{
35+
if (fileInfo == null)
36+
{
37+
return null;
38+
}
39+
3540
return new FileInfoWrapper(fileSystem, fileInfo);
3641
}
3742
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using NUnit.Framework;
2+
3+
namespace System.IO.Abstractions.TestingHelpers.Tests
4+
{
5+
[TestFixture]
6+
public class MockDirectoryInfoFactoryTests
7+
{
8+
[Test]
9+
public void MockDirectoryInfoFactory_Wrap_WithNull_ShouldReturnNull()
10+
{
11+
var fileSystem = new MockFileSystem();
12+
13+
var result = fileSystem.DirectoryInfo.Wrap(null);
14+
15+
Assert.IsNull(result);
16+
}
17+
}
18+
}

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoFactoryTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,15 @@ public void MockFileInfoFactory_New_ShouldReturnFileInfoForNonExistentFile()
4141
// Assert
4242
Assert.IsNotNull(result);
4343
}
44+
45+
[Test]
46+
public void MockFileInfoFactory_Wrap_WithNull_ShouldReturnNull()
47+
{
48+
var fileSystem = new MockFileSystem();
49+
50+
var result = fileSystem.FileInfo.Wrap(null);
51+
52+
Assert.IsNull(result);
53+
}
4454
}
4555
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using NUnit.Framework;
2+
3+
namespace System.IO.Abstractions.Tests
4+
{
5+
[TestFixture]
6+
public class DirectoryInfoFactoryTests
7+
{
8+
[Test]
9+
public void Wrap_WithNull_ShouldReturnNull()
10+
{
11+
var fileSystem = new FileSystem();
12+
13+
var result = fileSystem.DirectoryInfo.Wrap(null);
14+
15+
Assert.IsNull(result);
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using NUnit.Framework;
2+
3+
namespace System.IO.Abstractions.Tests
4+
{
5+
[TestFixture]
6+
public class FileInfoFactoryTests
7+
{
8+
[Test]
9+
public void Wrap_WithNull_ShouldReturnNull()
10+
{
11+
var fileSystem = new FileSystem();
12+
13+
var result = fileSystem.FileInfo.Wrap(null);
14+
15+
Assert.IsNull(result);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)