Skip to content

Commit

Permalink
fix: null-handling in Wrap-Method in factories for IDriveInfo or `I…
Browse files Browse the repository at this point in the history
…FileSystemWatcher` (#977)

Building on #976 also fix null-handling for DriveInfo and FileSystemWatcher factories:
When providing null as parameter to the implementations of IDriveInfoFactory or IFileSystemWatcherFactory return null instead of throwing an exception.

This extends the issue reported in #975 to other factories used in System.IO.Abstractions.
  • Loading branch information
vbreuss authored Apr 24, 2023
1 parent 1fcc5cd commit 554e97c
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public IDriveInfo New(string driveName)
/// <inheritdoc />
public IDriveInfo Wrap(DriveInfo driveInfo)
{
if (driveInfo == null)
{
return null;
}

return New(driveInfo.Name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public IFileSystemWatcher New(string path, string filter)

/// <inheritdoc />
public IFileSystemWatcher Wrap(FileSystemWatcher fileSystemWatcher)
=> throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
{
if (fileSystemWatcher == null)
{
return null;
}

throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public IDriveInfo New(string driveName)
/// <inheritdoc />
public IDriveInfo Wrap(DriveInfo driveInfo)
{
if (driveInfo == null)
{
return null;
}

return new DriveInfoWrapper(fileSystem, driveInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public IFileSystemWatcher New(string path, string filter)

/// <inheritdoc />
public IFileSystemWatcher Wrap(FileSystemWatcher fileSystemWatcher)
=> new FileSystemWatcherWrapper(FileSystem, fileSystemWatcher);
{
if (fileSystemWatcher == null)
{
return null;
}

return new FileSystemWatcherWrapper(FileSystem, fileSystemWatcher);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,15 @@ public void MockDriveInfoFactory_New_WithPathShouldReturnDrive()
// Assert
Assert.That(actualResult.Name, Is.EquivalentTo(@"Z:\"));
}

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

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

Assert.IsNull(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,15 @@ public void MockFileSystemWatcherFactory_FromPath_ShouldThrowNotImplementedExcep
var factory = new MockFileSystemWatcherFactory(new MockFileSystem());
Assert.Throws<NotImplementedException>(() => factory.New(path));
}

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

var result = fileSystem.FileSystemWatcher.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 DriveInfoFactoryTests
{
[Test]
public void Wrap_WithNull_ShouldReturnNull()
{
var fileSystem = new FileSystem();

var result = fileSystem.DriveInfo.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 FileSystemWatcherFactoryTests
{
[Test]
public void Wrap_WithNull_ShouldReturnNull()
{
var fileSystem = new FileSystem();

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

Assert.IsNull(result);
}
}
}

0 comments on commit 554e97c

Please sign in to comment.