Skip to content

Commit 0755df0

Browse files
authored
refactor: use CommonExceptions to create exceptions (#1027)
Replace the creation of exceptions to throw from a couple of places to the static `CommonExceptions` helper class.
1 parent 34ae96a commit 0755df0

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace System.IO.Abstractions.TestingHelpers
55
internal static class CommonExceptions
66
{
77
private const int _fileLockHResult = unchecked((int)0x80070020);
8-
8+
99
public static FileNotFoundException FileNotFound(string path) =>
1010
new FileNotFoundException(
1111
string.Format(
@@ -34,10 +34,10 @@ public static UnauthorizedAccessException AccessDenied(string path) =>
3434
)
3535
);
3636

37-
public static Exception InvalidUseOfVolumeSeparator() =>
37+
public static NotSupportedException InvalidUseOfVolumeSeparator() =>
3838
new NotSupportedException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"));
3939

40-
public static Exception PathIsNotOfALegalForm(string paramName) =>
40+
public static ArgumentException PathIsNotOfALegalForm(string paramName) =>
4141
new ArgumentException(
4242
StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"),
4343
paramName
@@ -54,7 +54,7 @@ public static ArgumentException IllegalCharactersInPath(string paramName = null)
5454
? new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"), paramName)
5555
: new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"));
5656

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

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

7474
public static NotImplementedException NotImplemented() =>
7575
new NotImplementedException(StringResources.Manager.GetString("NOT_IMPLEMENTED_EXCEPTION"));
76+
77+
public static IOException CannotCreateBecauseSameNameAlreadyExists(string path) =>
78+
new IOException(
79+
string.Format(
80+
CultureInfo.InvariantCulture,
81+
StringResources.Manager.GetString("CANNOT_CREATE_BECAUSE_SAME_NAME_ALREADY_EXISTS"),
82+
path
83+
)
84+
);
85+
86+
public static IOException NameCannotBeResolvedByTheSystem(string path) =>
87+
new IOException(
88+
string.Format(
89+
CultureInfo.InvariantCulture,
90+
StringResources.Manager.GetString("NAME_CANNOT_BE_RESOLVED_BY_THE_SYSTEM"),
91+
path
92+
)
93+
);
94+
95+
public static DirectoryNotFoundException PathDoesNotExistOrCouldNotBeFound(string path) =>
96+
new DirectoryNotFoundException(
97+
string.Format(
98+
CultureInfo.InvariantCulture,
99+
StringResources.Manager.GetString("PATH_DOES_NOT_EXIST_OR_COULD_NOT_BE_FOUND"),
100+
path
101+
)
102+
);
76103
}
77104
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public override void Delete(string path, bool recursive)
153153

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

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

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

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

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

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

571570
if (nextContainer.LinkTarget != null)
572571
{
573-
throw new IOException($"The name of the file cannot be resolved by the system. : '{linkPath}'");
572+
throw CommonExceptions.NameCannotBeResolvedByTheSystem(linkPath);
574573
}
575574
}
576575

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

589588
#endif

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public override void Copy(string sourceFileName, string destFileName, bool overw
117117
{
118118
if (!overwrite)
119119
{
120-
throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file {0} already exists.", destFileName));
120+
throw CommonExceptions.FileAlreadyExists(destFileName);
121121
}
122122

123123
mockFileDataAccessor.RemoveFile(destFileName);
@@ -581,7 +581,7 @@ private FileSystemStream OpenInternal(
581581

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

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

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

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

src/TestableIO.System.IO.Abstractions.TestingHelpers/Properties/Resources.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,13 @@
162162
<data name="INVALID_ACCESS_COMBINATION" xml:space="preserve">
163163
<value>Combining FileMode: {0} with FileAccess: {1} is invalid.</value>
164164
</data>
165+
<data name="CANNOT_CREATE_BECAUSE_SAME_NAME_ALREADY_EXISTS" xml:space="preserve">
166+
<value>Cannot create '{0}' because a file or directory with the same name already exists.</value>
167+
</data>
168+
<data name="NAME_CANNOT_BE_RESOLVED_BY_THE_SYSTEM" xml:space="preserve">
169+
<value>The name of the file cannot be resolved by the system. : '{0}'</value>
170+
</data>
171+
<data name="PATH_DOES_NOT_EXIST_OR_COULD_NOT_BE_FOUND" xml:space="preserve">
172+
<value>'{0}' does not exist or could not be found.</value>
173+
</data>
165174
</root>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ public void MockDirectory_Delete_ShouldThrowDirectoryNotFoundException()
982982

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

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

988988
[Test]

0 commit comments

Comments
 (0)