Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use CommonExceptions to create exceptions #1027

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace System.IO.Abstractions.TestingHelpers
internal static class CommonExceptions
{
private const int _fileLockHResult = unchecked((int)0x80070020);

public static FileNotFoundException FileNotFound(string path) =>
new FileNotFoundException(
string.Format(
Expand Down Expand Up @@ -34,10 +34,10 @@ public static UnauthorizedAccessException AccessDenied(string path) =>
)
);

public static Exception InvalidUseOfVolumeSeparator() =>
public static NotSupportedException InvalidUseOfVolumeSeparator() =>
new NotSupportedException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"));

public static Exception PathIsNotOfALegalForm(string paramName) =>
public static ArgumentException PathIsNotOfALegalForm(string paramName) =>
new ArgumentException(
StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"),
paramName
Expand All @@ -54,7 +54,7 @@ public static ArgumentException IllegalCharactersInPath(string paramName = null)
? new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"), paramName)
: new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"));

public static Exception InvalidUncPath(string paramName) =>
public static ArgumentException InvalidUncPath(string paramName) =>
new ArgumentException(@"The UNC path should be of the form \\server\share.", paramName);

public static IOException ProcessCannotAccessFileInUse(string paramName = null) =>
Expand All @@ -73,5 +73,32 @@ public static ArgumentException AppendAccessOnlyInWriteOnlyMode()

public static NotImplementedException NotImplemented() =>
new NotImplementedException(StringResources.Manager.GetString("NOT_IMPLEMENTED_EXCEPTION"));

public static IOException CannotCreateBecauseSameNameAlreadyExists(string path) =>
new IOException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("CANNOT_CREATE_BECAUSE_SAME_NAME_ALREADY_EXISTS"),
path
)
);

public static IOException NameCannotBeResolvedByTheSystem(string path) =>
new IOException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("NAME_CANNOT_BE_RESOLVED_BY_THE_SYSTEM"),
path
)
);

public static DirectoryNotFoundException PathDoesNotExistOrCouldNotBeFound(string path) =>
new DirectoryNotFoundException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("PATH_DOES_NOT_EXIST_OR_COULD_NOT_BE_FOUND"),
path
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public override void Delete(string path, bool recursive)

if (!affectedPaths.Any())
{
throw new DirectoryNotFoundException(path + " does not exist or could not be found.");
throw CommonExceptions.PathDoesNotExistOrCouldNotBeFound(path);
}

if (!recursive && affectedPaths.Count > 1)
Expand Down Expand Up @@ -526,18 +526,17 @@ public override void Move(string sourceDirName, string destDirName)

if (!mockFileDataAccessor.Directory.Exists(fullSourcePath))
{
throw new DirectoryNotFoundException($"Could not find a part of the path '{sourceDirName}'.");
throw CommonExceptions.CouldNotFindPartOfPath(sourceDirName);
}

if (!mockFileDataAccessor.Directory.GetParent(fullDestPath).Exists)
{
throw new DirectoryNotFoundException($"Could not find a part of the path.");
throw CommonExceptions.CouldNotFindPartOfPath(destDirName);
}

if (mockFileDataAccessor.Directory.Exists(fullDestPath) || mockFileDataAccessor.File.Exists(fullDestPath))
{
throw new IOException(
$"Cannot create '{fullDestPath}' because a file or directory with the same name already exists.");
throw CommonExceptions.CannotCreateBecauseSameNameAlreadyExists(fullDestPath);
}

mockFileDataAccessor.MoveDirectory(fullSourcePath, fullDestPath);
Expand Down Expand Up @@ -570,7 +569,7 @@ public override IFileSystemInfo ResolveLinkTarget(string linkPath, bool returnFi

if (nextContainer.LinkTarget != null)
{
throw new IOException($"The name of the file cannot be resolved by the system. : '{linkPath}'");
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}
}

Expand All @@ -583,7 +582,7 @@ public override IFileSystemInfo ResolveLinkTarget(string linkPath, bool returnFi
return new MockFileInfo(mockFileDataAccessor, nextLocation);
}
}
throw new IOException($"The name of the file cannot be resolved by the system. : '{linkPath}'");
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public override void Copy(string sourceFileName, string destFileName, bool overw
{
if (!overwrite)
{
throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file {0} already exists.", destFileName));
throw CommonExceptions.FileAlreadyExists(destFileName);
}

mockFileDataAccessor.RemoveFile(destFileName);
Expand Down Expand Up @@ -581,7 +581,7 @@ private FileSystemStream OpenInternal(

if (mode == FileMode.CreateNew && exists)
{
throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file '{0}' already exists.", path));
throw CommonExceptions.FileAlreadyExists(path);
}

if ((mode == FileMode.Open || mode == FileMode.Truncate) && !exists)
Expand Down Expand Up @@ -802,7 +802,7 @@ public override IFileSystemInfo ResolveLinkTarget(string linkPath, bool returnFi

if (nextContainer.LinkTarget != null)
{
throw new IOException($"The name of the file cannot be resolved by the system. : '{linkPath}'");
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}
}

Expand All @@ -815,7 +815,7 @@ public override IFileSystemInfo ResolveLinkTarget(string linkPath, bool returnFi
return new MockFileInfo(mockFileDataAccessor, nextLocation);
}
}
throw new IOException($"The name of the file cannot be resolved by the system. : '{linkPath}'");
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,13 @@
<data name="INVALID_ACCESS_COMBINATION" xml:space="preserve">
<value>Combining FileMode: {0} with FileAccess: {1} is invalid.</value>
</data>
<data name="CANNOT_CREATE_BECAUSE_SAME_NAME_ALREADY_EXISTS" xml:space="preserve">
<value>Cannot create '{0}' because a file or directory with the same name already exists.</value>
</data>
<data name="NAME_CANNOT_BE_RESOLVED_BY_THE_SYSTEM" xml:space="preserve">
<value>The name of the file cannot be resolved by the system. : '{0}'</value>
</data>
<data name="PATH_DOES_NOT_EXIST_OR_COULD_NOT_BE_FOUND" xml:space="preserve">
<value>'{0}' does not exist or could not be found.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ public void MockDirectory_Delete_ShouldThrowDirectoryNotFoundException()

var ex = Assert.Throws<DirectoryNotFoundException>(() => fileSystem.Directory.Delete(XFS.Path(@"c:\baz")));

Assert.That(ex.Message, Is.EqualTo(XFS.Path("c:\\baz") + " does not exist or could not be found."));
Assert.That(ex.Message, Is.EqualTo($"'{XFS.Path("c:\\baz")}' does not exist or could not be found."));
}

[Test]
Expand Down