Skip to content

Commit

Permalink
fix: throw correct exception in File.Copy when using an invalid path (#…
Browse files Browse the repository at this point in the history
…583)

Throw correct exception in File.Copy when using an invalid path
  • Loading branch information
vbreuss committed Apr 29, 2024
1 parent 98358d2 commit 401412b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
5 changes: 1 addition & 4 deletions Source/Testably.Abstractions.Testing/Helpers/PathHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;

namespace Testably.Abstractions.Testing.Helpers;

Expand Down Expand Up @@ -161,9 +160,7 @@ internal static string TrimOnWindows(this string path, MockFileSystem fileSystem
fileSystem.Execute.Path.GetFullPath(path), hResult);
}

fileSystem.Execute.OnWindowsIf(path.LastIndexOf(':') > 1 &&
path.LastIndexOf(':') <
path.IndexOf(Path.DirectorySeparatorChar),
fileSystem.Execute.OnWindowsIf(path.LastIndexOf(':') > 1,
() => throw ExceptionFactory.PathHasIncorrectSyntax(
fileSystem.Execute.Path.GetFullPath(path), hResult));
}
Expand Down
34 changes: 29 additions & 5 deletions Tests/Testably.Abstractions.Tests/FileSystem/File/CopyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ public void
}
else
{
exception.Should().BeException<IOException>(hResult: -2147024773);
exception.Should().BeException<IOException>(
messageContains:
"The filename, directory name, or volume label syntax is incorrect",
hResult: -2147024773);
}
}

Expand Down Expand Up @@ -313,6 +316,27 @@ public void
FileSystem.File.ReadAllText(destinationPath).Should().Be(sourceContents);
}

[SkippableTheory]
[AutoData]
public void Copy_SourceDirectoryMissing_ShouldThrowDirectoryNotFoundException(
string missingDirectory,
string sourceName,
string destinationName)
{
string source = FileSystem.Path.Combine(missingDirectory, sourceName);
Exception? exception = Record.Exception(() =>
{
FileSystem.File.Copy(source, destinationName);
});

exception.Should().BeException<DirectoryNotFoundException>(
messageContains: Test.IsNetFramework
? null
: $"'{FileSystem.Path.GetFullPath(source)}'",
hResult: -2147024893);
FileSystem.Should().NotHaveFile(destinationName);
}

[SkippableTheory]
[AutoData]
public void Copy_SourceIsDirectory_ShouldThrowUnauthorizedAccessException_AndNotCopyFile(
Expand All @@ -327,10 +351,10 @@ public void
});

exception.Should().BeException<UnauthorizedAccessException>(
hResult: -2147024891,
messageContains: Test.IsNetFramework
? $"'{sourceName}'"
: $"'{FileSystem.Path.GetFullPath(sourceName)}'");
: $"'{FileSystem.Path.GetFullPath(sourceName)}'",
hResult: -2147024891);
FileSystem.Should().HaveDirectory(sourceName);
FileSystem.Should().NotHaveFile(destinationName);
}
Expand Down Expand Up @@ -379,10 +403,10 @@ public void
});

exception.Should().BeException<FileNotFoundException>(
hResult: -2147024894,
messageContains: Test.IsNetFramework
? null
: $"'{FileSystem.Path.GetFullPath(sourceName)}'");
: $"'{FileSystem.Path.GetFullPath(sourceName)}'",
hResult: -2147024894);
FileSystem.Should().NotHaveFile(destinationName);
}
}

0 comments on commit 401412b

Please sign in to comment.