Skip to content

Commit 554e97c

Browse files
authored
fix: null-handling in Wrap-Method in factories for IDriveInfo or IFileSystemWatcher (#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.
1 parent 1fcc5cd commit 554e97c

File tree

8 files changed

+82
-2
lines changed

8 files changed

+82
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public IDriveInfo New(string driveName)
5656
/// <inheritdoc />
5757
public IDriveInfo Wrap(DriveInfo driveInfo)
5858
{
59+
if (driveInfo == null)
60+
{
61+
return null;
62+
}
63+
5964
return New(driveInfo.Name);
6065
}
6166

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public IFileSystemWatcher New(string path, string filter)
4343

4444
/// <inheritdoc />
4545
public IFileSystemWatcher Wrap(FileSystemWatcher fileSystemWatcher)
46-
=> throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
46+
{
47+
if (fileSystemWatcher == null)
48+
{
49+
return null;
50+
}
51+
52+
throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
53+
}
4754
}
4855
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public IDriveInfo New(string driveName)
3838
/// <inheritdoc />
3939
public IDriveInfo Wrap(DriveInfo driveInfo)
4040
{
41+
if (driveInfo == null)
42+
{
43+
return null;
44+
}
45+
4146
return new DriveInfoWrapper(fileSystem, driveInfo);
4247
}
4348

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public IFileSystemWatcher New(string path, string filter)
4242

4343
/// <inheritdoc />
4444
public IFileSystemWatcher Wrap(FileSystemWatcher fileSystemWatcher)
45-
=> new FileSystemWatcherWrapper(FileSystem, fileSystemWatcher);
45+
{
46+
if (fileSystemWatcher == null)
47+
{
48+
return null;
49+
}
50+
51+
return new FileSystemWatcherWrapper(FileSystem, fileSystemWatcher);
52+
}
4653
}
4754
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,15 @@ public void MockDriveInfoFactory_New_WithPathShouldReturnDrive()
9797
// Assert
9898
Assert.That(actualResult.Name, Is.EquivalentTo(@"Z:\"));
9999
}
100+
101+
[Test]
102+
public void MockDriveInfoFactory_Wrap_WithNull_ShouldReturnNull()
103+
{
104+
var fileSystem = new MockFileSystem();
105+
106+
var result = fileSystem.DriveInfo.Wrap(null);
107+
108+
Assert.IsNull(result);
109+
}
100110
}
101111
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,15 @@ public void MockFileSystemWatcherFactory_FromPath_ShouldThrowNotImplementedExcep
3737
var factory = new MockFileSystemWatcherFactory(new MockFileSystem());
3838
Assert.Throws<NotImplementedException>(() => factory.New(path));
3939
}
40+
41+
[Test]
42+
public void MockFileSystemWatcherFactory_Wrap_WithNull_ShouldReturnNull()
43+
{
44+
var fileSystem = new MockFileSystem();
45+
46+
var result = fileSystem.FileSystemWatcher.Wrap(null);
47+
48+
Assert.IsNull(result);
49+
}
4050
}
4151
}
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 DriveInfoFactoryTests
7+
{
8+
[Test]
9+
public void Wrap_WithNull_ShouldReturnNull()
10+
{
11+
var fileSystem = new FileSystem();
12+
13+
var result = fileSystem.DriveInfo.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 FileSystemWatcherFactoryTests
7+
{
8+
[Test]
9+
public void Wrap_WithNull_ShouldReturnNull()
10+
{
11+
var fileSystem = new FileSystem();
12+
13+
var result = fileSystem.FileSystemWatcher.Wrap(null);
14+
15+
Assert.IsNull(result);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)