Skip to content

Commit

Permalink
fix: null-handling in Wrap-Method in factories for IDirectoryInfo o…
Browse files Browse the repository at this point in the history
…r `IFileInfo` (#976)

Fixes #975:
When providing `null` as parameter to the implementations of `IFileInfoFactory` or `IDirectoryInfoFactory` return `null` instead of throwing an exception.
  • Loading branch information
vbreuss authored Apr 23, 2023
1 parent e84ace2 commit 1fcc5cd
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public IDirectoryInfo New(string path)
/// <inheritdoc />
public IDirectoryInfo Wrap(DirectoryInfo directoryInfo)
{
if (directoryInfo == null)
{
return null;
}

return new MockDirectoryInfo(mockFileSystem, directoryInfo.Name);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public IFileInfo New(string fileName)
/// <inheritdoc />
public IFileInfo Wrap(FileInfo fileInfo)
{
if (fileInfo == null)
{
return null;
}

return new MockFileInfo(mockFileSystem, fileInfo.Name);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ public IDirectoryInfo New(string path)
var realDirectoryInfo = new DirectoryInfo(path);
return new DirectoryInfoWrapper(fileSystem, realDirectoryInfo);
}

/// <inheritdoc />
public IDirectoryInfo Wrap(DirectoryInfo directoryInfo)
{
if (directoryInfo == null)
{
return null;
}

return new DirectoryInfoWrapper(fileSystem, directoryInfo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ public IFileInfo New(string fileName)
var realFileInfo = new FileInfo(fileName);
return new FileInfoWrapper(fileSystem, realFileInfo);
}

/// <inheritdoc />
public IFileInfo Wrap(FileInfo fileInfo)
{
if (fileInfo == null)
{
return null;
}

return new FileInfoWrapper(fileSystem, fileInfo);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NUnit.Framework;

namespace System.IO.Abstractions.TestingHelpers.Tests
{
[TestFixture]
public class MockDirectoryInfoFactoryTests
{
[Test]
public void MockDirectoryInfoFactory_Wrap_WithNull_ShouldReturnNull()
{
var fileSystem = new MockFileSystem();

var result = fileSystem.DirectoryInfo.Wrap(null);

Assert.IsNull(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,15 @@ public void MockFileInfoFactory_New_ShouldReturnFileInfoForNonExistentFile()
// Assert
Assert.IsNotNull(result);
}

[Test]
public void MockFileInfoFactory_Wrap_WithNull_ShouldReturnNull()
{
var fileSystem = new MockFileSystem();

var result = fileSystem.FileInfo.Wrap(null);

Assert.IsNull(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NUnit.Framework;

namespace System.IO.Abstractions.Tests
{
[TestFixture]
public class DirectoryInfoFactoryTests
{
[Test]
public void Wrap_WithNull_ShouldReturnNull()
{
var fileSystem = new FileSystem();

var result = fileSystem.DirectoryInfo.Wrap(null);

Assert.IsNull(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NUnit.Framework;

namespace System.IO.Abstractions.Tests
{
[TestFixture]
public class FileInfoFactoryTests
{
[Test]
public void Wrap_WithNull_ShouldReturnNull()
{
var fileSystem = new FileSystem();

var result = fileSystem.FileInfo.Wrap(null);

Assert.IsNull(result);
}
}
}

0 comments on commit 1fcc5cd

Please sign in to comment.