Skip to content

Commit

Permalink
changed exception type in case of invalid characters in path
Browse files Browse the repository at this point in the history
  • Loading branch information
manne committed Jun 19, 2016
1 parent ac7bcb7 commit 850655d
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 20 deletions.
40 changes: 40 additions & 0 deletions TestHelpers.Tests/MockDirectoryArgumentPathTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Security.AccessControl;
using NUnit.Framework;

namespace System.IO.Abstractions.TestingHelpers.Tests
{
public class MockDirectoryArgumentPathTests
{
private static IEnumerable<Action<DirectoryBase>> GetFileSystemActionsForArgumentNullException()
{
yield return ds => ds.Delete(null);
yield return ds => ds.Delete(null, true);
yield return ds => ds.CreateDirectory(null);
yield return ds => ds.CreateDirectory(null, new DirectorySecurity());
yield return ds => ds.SetCreationTime(null, DateTime.Now);
yield return ds => ds.SetCreationTimeUtc(null, DateTime.Now);
yield return ds => ds.SetLastAccessTime(null, DateTime.Now);
yield return ds => ds.SetLastAccessTimeUtc(null, DateTime.Now);
yield return ds => ds.SetLastWriteTime(null, DateTime.Now);
yield return ds => ds.SetLastWriteTimeUtc(null, DateTime.Now);
yield return ds => ds.EnumerateDirectories(null);
yield return ds => ds.EnumerateDirectories(null, "foo");
yield return ds => ds.EnumerateDirectories(null, "foo", SearchOption.AllDirectories);
}

[TestCaseSource("GetFileSystemActionsForArgumentNullException")]
public void Operations_ShouldThrowArgumentNullExceptionIfPathIsNull(Action<DirectoryBase> action)
{
// Arrange
var fileSystem = new MockFileSystem();

// Act
TestDelegate wrapped = () => action(fileSystem.Directory);

// Assert
var exception = Assert.Throws<ArgumentNullException>(wrapped);
Assert.AreEqual("path", exception.ParamName);
}
}
}
2 changes: 1 addition & 1 deletion TestHelpers.Tests/MockFileAppendAllLinesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void MockFile_AppendAllLines_ShouldThrowArgumentExceptionIfPathContainsIn
TestDelegate action = () => fileSystem.File.AppendAllLines(path, new[] { "does not matter" });

// Assert
Assert.Throws<NotSupportedException>(action);
Assert.Throws<ArgumentException>(action);
}

[Test]
Expand Down
8 changes: 4 additions & 4 deletions TestHelpers.Tests/MockFileCopyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public void MockFile_Copy_ShouldThrowNotSupportedExceptionWhenSourceFileNameCont
var sourceFilePath = XFS.Path(@"c:\something\demo.txt") + invalidChar;

var exception =
Assert.Throws<NotSupportedException>(() => fileSystem.File.Copy(sourceFilePath, destFilePath));
Assert.Throws<ArgumentException>(() => fileSystem.File.Copy(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.EqualTo("The given path's format is not supported."),
Assert.That(exception.Message, Is.EqualTo("Illegal characters in path."),
string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
}
}
Expand Down Expand Up @@ -186,9 +186,9 @@ public void MockFile_Copy_ShouldThrowNotSupportedExceptionWhenTargetFileNameCont
var destFilePath = XFS.Path(@"c:\something\demo.txt") + invalidChar;

var exception =
Assert.Throws<NotSupportedException>(() => fileSystem.File.Copy(sourceFilePath, destFilePath));
Assert.Throws<ArgumentException>(() => fileSystem.File.Copy(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.EqualTo("The given path's format is not supported."),
Assert.That(exception.Message, Is.EqualTo("Illegal characters in path."),
string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
}
}
Expand Down
18 changes: 16 additions & 2 deletions TestHelpers.Tests/MockFileCreateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void Mockfile_Create_ShouldThrowArgumentExceptionIfPathIsZeroLength()
[TestCase("<")]
[TestCase(">")]
[TestCase("|")]
public void MockFile_Create_ShouldThrowArgumentNullExceptionIfPathIsNull(string path)
public void MockFile_Create_ShouldThrowArgumentNullExceptionIfPathIsNull1(string path)
{
// Arrange
var fileSystem = new MockFileSystem();
Expand All @@ -115,7 +115,7 @@ public void MockFile_Create_ShouldThrowArgumentNullExceptionIfPathIsNull(string
TestDelegate action = () => fileSystem.File.Create(path);

// Assert
Assert.Throws<NotSupportedException>(action);
Assert.Throws<ArgumentException>(action);
}

[TestCase(" ")]
Expand All @@ -131,5 +131,19 @@ public void MockFile_Create_ShouldThrowArgumentExceptionIfPathContainsOnlyWhites
// Assert
Assert.Throws<ArgumentException>(action);
}

[Test]
public void MockFile_Create_ShouldThrowArgumentNullExceptionIfPathIsNull()
{
// Arrange
var fileSystem = new MockFileSystem();

// Act
TestDelegate action = () => fileSystem.File.Create(null);

// Assert
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.That(exception.Message, Is.StringStarting("Path cannot be null."));
}
}
}
8 changes: 4 additions & 4 deletions TestHelpers.Tests/MockFileMoveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public void MockFile_Move_ShouldThrowNotSupportedExceptionWhenSourceFileNameCont
var sourceFilePath = XFS.Path(@"c:\something\demo.txt") + invalidChar;

var exception =
Assert.Throws<NotSupportedException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));
Assert.Throws<ArgumentException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.EqualTo("The given path's format is not supported."),
Assert.That(exception.Message, Is.EqualTo("Illegal characters in path."),
string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
}
}
Expand Down Expand Up @@ -156,9 +156,9 @@ public void MockFile_Move_ShouldThrowNotSupportedExceptionWhenTargetFileNameCont
var destFilePath = XFS.Path(@"c:\something\demo.txt") + invalidChar;

var exception =
Assert.Throws<NotSupportedException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));
Assert.Throws<ArgumentException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));

Assert.That(exception.Message, Is.EqualTo("The given path's format is not supported."),
Assert.That(exception.Message, Is.EqualTo("Illegal characters in path."),
string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
}
}
Expand Down
8 changes: 4 additions & 4 deletions TestHelpers.Tests/MockFileWriteAllLinesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ private static IEnumerable GetCasesForArgumentChecking(string path)

// IEnumerable
yield return new TestCaseData(writeEnumberable)
.SetName("WriteAllLines(string, IEnumerable<string>)");
.SetName("WriteAllLines(string, IEnumerable<string>) input: " + path);
yield return new TestCaseData(writeEnumberableUtf32)
.SetName("WriteAllLines(string, IEnumerable<string>, Encoding.UTF32)");
.SetName("WriteAllLines(string, IEnumerable<string>, Encoding.UTF32) input: " + path);

// string[]
yield return new TestCaseData(writeArray)
.SetName("WriteAllLines(string, string[])");
.SetName("WriteAllLines(string, string[]) input: " + path);
yield return new TestCaseData(writeArrayUtf32)
.SetName("WriteAllLines(string, string[], Encoding.UTF32)");
.SetName("WriteAllLines(string, string[], Encoding.UTF32) input: " + path);
}

private static IEnumerable ForNullEncoding
Expand Down
1 change: 1 addition & 0 deletions TestHelpers.Tests/TestHelpers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<ItemGroup>
<Compile Include="MockDriveInfoFactoryTests.cs" />
<Compile Include="MockDriveInfoTests.cs" />
<Compile Include="MockDirectoryArgumentPathTests.cs" />
<Compile Include="MockFileArgumentPathTests.cs" />
<Compile Include="MockFileDeleteTests.cs" />
<Compile Include="FileSystemTests.cs" />
Expand Down
6 changes: 6 additions & 0 deletions TestingHelpers/MockDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,22 @@ public override void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)

public override IEnumerable<string> EnumerateDirectories(string path)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

return EnumerateDirectories(path, "*");
}

public override IEnumerable<string> EnumerateDirectories(string path, string searchPattern)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

return EnumerateDirectories(path, searchPattern, SearchOption.TopDirectoryOnly);
}

public override IEnumerable<string> EnumerateDirectories(string path, string searchPattern, SearchOption searchOption)
{
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

path = EnsurePathEndsWithDirectorySeparator(path);

if (!Exists(path))
Expand Down
24 changes: 24 additions & 0 deletions TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public override void Copy(string sourceFileName, string destFileName)

public override void Copy(string sourceFileName, string destFileName, bool overwrite)
{
if (sourceFileName == null)
{
throw new ArgumentNullException("sourceFileName", "File name cannot be null.");
}

if (destFileName == null)
{
throw new ArgumentNullException("destFileName", "File name cannot be null.");
}

mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, "sourceFileName");
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName");

Expand All @@ -125,6 +135,10 @@ public override void Copy(string sourceFileName, string destFileName, bool overw

public override Stream Create(string path)
{
if (path == null)
{
throw new ArgumentNullException("path", "Path cannot be null.");
}
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

mockFileDataAccessor.AddFile(path, new MockFileData(new byte[0]));
Expand Down Expand Up @@ -300,6 +314,16 @@ private DateTime GetTimeFromFile(string path, Func<MockFileData, DateTime> exist

public override void Move(string sourceFileName, string destFileName)
{
if (sourceFileName == null)
{
throw new ArgumentNullException("sourceFileName", "File name cannot be null.");
}

if (destFileName == null)
{
throw new ArgumentNullException("destFileName", "File name cannot be null.");
}

mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, "sourceFileName");
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName");

Expand Down
9 changes: 4 additions & 5 deletions TestingHelpers/PathVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

using System.Linq;
using System.Linq;

namespace System.IO.Abstractions.TestingHelpers
{
Expand All @@ -21,7 +20,7 @@ public void IsLegalAbsoluteOrRelative(string path, string paramName)
{
if (path == null)
{
throw new ArgumentNullException(paramName, "File name cannot be null.");
throw new ArgumentNullException(paramName, Properties.Resources.VALUE_CANNOT_BE_NULL);
}

if (path == string.Empty)
Expand All @@ -31,12 +30,12 @@ public void IsLegalAbsoluteOrRelative(string path, string paramName)

if (path.Trim() == string.Empty)
{
throw new ArgumentException("The path is not of a legal form.", paramName);
throw new ArgumentException(Properties.Resources.THE_PATH_IS_NOT_OF_A_LEGAL_FORM, paramName);
}

if (ExtractFileName(path).IndexOfAny(_mockFileDataAccessor.Path.GetInvalidFileNameChars()) > -1)
{
throw new NotSupportedException("The given path's format is not supported.");
throw new ArgumentException(Properties.Resources.ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION);
}

var filePath = ExtractFilePath(path);
Expand Down

0 comments on commit 850655d

Please sign in to comment.