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

fix: throw correct exception in File.Copy when using an invalid path #583

Merged
merged 2 commits into from
Apr 29, 2024
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
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 @@ private static void CheckPathCharacters(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 Copy_SourceAccessedWithWriteShare_ShouldNotThrowOnLinuxOrMac(
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 Copy_SourceIsDirectory_ShouldThrowUnauthorizedAccessException_AndNot
});

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 Copy_SourceMissing_ShouldThrowFileNotFoundException(
});

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