diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..b21d5906 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,18 @@ +# Normalize line endings +* text=auto + +# Prevent files from being exported +.gitattributes export-ignore +.gitignore export-ignore + +# ReSharper DotSettings files are in Unix Format +*.DotSettings text eol=lf + +# MonoDevelop on Linux uses CRLF for these files +*.sln text eol=crlf +packages/repositories.config text eol=crlf + +# Allows checking out and developing in Windows +# while mounting and running tests in Linux +*.sh text eol=lf +build text eol=lf diff --git a/Tests/Helpers/Testably.Abstractions.TestHelpers/Test.cs b/Tests/Helpers/Testably.Abstractions.TestHelpers/Test.cs index a2a1f4f3..4c71ded9 100644 --- a/Tests/Helpers/Testably.Abstractions.TestHelpers/Test.cs +++ b/Tests/Helpers/Testably.Abstractions.TestHelpers/Test.cs @@ -1,66 +1,66 @@ -using System.IO.Abstractions; -using System.Runtime.InteropServices; -using Xunit; - -namespace Testably.Abstractions.TestHelpers; - -public class Test -{ - public static bool IsNet7OrGreater -#if NET7_0_OR_GREATER - => true; -#else - => false; -#endif - public bool IsNetFramework { get; } - - public bool RunsOnLinux { get; } - - public bool RunsOnMac { get; } - - public bool RunsOnWindows { get; } - - public Test() - { - RunsOnLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); - RunsOnMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); - RunsOnWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - IsNetFramework = RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework"); - } - - public static void SkipBrittleTestsOnRealFileSystem( - IFileSystem fileSystem, bool condition = true) - { - Skip.If(fileSystem is RealFileSystem && condition, - "Brittle tests are skipped on the real file system."); - } - - public static void SkipBrittleTestsOnRealTimeSystem( - ITimeSystem timeSystem, bool condition = true) - { - Skip.If(timeSystem is RealTimeSystem && condition, - "Brittle tests are skipped on the real time system."); - } - - public static void SkipIfLongRunningTestsShouldBeSkipped(IFileSystem fileSystem) - { -#if DEBUG && !INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE - Skip.If(fileSystem is RealFileSystem, - "Long-Running tests are skipped in DEBUG mode unless the build constant 'INCLUDE_LONG_RUNNING_TESTS_ALSO_IN_DEBUG_MODE' is set."); -#endif - // ReSharper disable once CommentTypo - // Do nothing when in release mode or `INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE` is set - } - - public static void SkipIfTestsOnRealFileSystemShouldBeSkipped(IFileSystem fileSystem) - { -#if NCRUNCH - Skip.If(fileSystem is RealFileSystem, "NCrunch should not test the real file system."); -#endif -#if DEBUG && SKIP_TESTS_ON_REAL_FILESYSTEM - Skip.If(fileSystem is RealFileSystem, - "Tests against real FileSystem are skipped in DEBUG mode with the build constant 'SKIP_TESTS_ON_REAL_FILESYSTEM'."); -#endif - // Do nothing when in release mode or `SKIP_TESTS_ON_REAL_FILESYSTEM` is not set - } -} +using System.IO.Abstractions; +using System.Runtime.InteropServices; +using Xunit; + +namespace Testably.Abstractions.TestHelpers; + +public class Test +{ + public static bool IsNet7OrGreater +#if NET7_0_OR_GREATER + => true; +#else + => false; +#endif + public bool IsNetFramework { get; } + + public bool RunsOnLinux { get; } + + public bool RunsOnMac { get; } + + public bool RunsOnWindows { get; } + + public Test() + { + RunsOnLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + RunsOnMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + RunsOnWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + IsNetFramework = RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework"); + } + + public static void SkipBrittleTestsOnRealFileSystem( + IFileSystem fileSystem, bool condition = true) + { + Skip.If(fileSystem is RealFileSystem && condition, + "Brittle tests are skipped on the real file system."); + } + + public static void SkipBrittleTestsOnRealTimeSystem( + ITimeSystem timeSystem, bool condition = true) + { + Skip.If(timeSystem is RealTimeSystem && condition, + "Brittle tests are skipped on the real time system."); + } + + public static void SkipIfLongRunningTestsShouldBeSkipped(IFileSystem fileSystem) + { +#if DEBUG && !INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE + Skip.If(fileSystem is RealFileSystem, + "Long-Running tests are skipped in DEBUG mode unless the build constant 'INCLUDE_LONG_RUNNING_TESTS_ALSO_IN_DEBUG_MODE' is set."); +#endif + // ReSharper disable once CommentTypo + // Do nothing when in release mode or `INCLUDE_LONGRUNNING_TESTS_ALSO_IN_DEBUG_MODE` is set + } + + public static void SkipIfTestsOnRealFileSystemShouldBeSkipped(IFileSystem fileSystem) + { +#if NCRUNCH + Skip.If(fileSystem is RealFileSystem, "NCrunch should not test the real file system."); +#endif +#if DEBUG && SKIP_TESTS_ON_REAL_FILESYSTEM + Skip.If(fileSystem is RealFileSystem, + "Tests against real FileSystem are skipped in DEBUG mode with the build constant 'SKIP_TESTS_ON_REAL_FILESYSTEM'."); +#endif + // Do nothing when in release mode or `SKIP_TESTS_ON_REAL_FILESYSTEM` is not set + } +} diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/Directory/ExceptionTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/Directory/ExceptionTests.cs index b5d02df6..05d06b6f 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/Directory/ExceptionTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/Directory/ExceptionTests.cs @@ -1,347 +1,347 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Linq.Expressions; - -namespace Testably.Abstractions.Tests.FileSystem.Directory; - -// ReSharper disable once PartialTypeWithSinglePart -public abstract partial class ExceptionTests - : FileSystemTestBase - where TFileSystem : IFileSystem -{ - [SkippableTheory] - [MemberData(nameof(GetDirectoryCallbacks), parameters: "Illegal\tCharacter?InPath")] - public void - Operations_WhenValueContainsIllegalPathCharacters_ShouldThrowCorrectException_OnWindows( - Expression> callback, string paramName, - bool ignoreParamCheck, Func skipTest) - { - Skip.If(skipTest(Test)); - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.Directory); - }); - - if (!Test.RunsOnWindows) - { - if (exception is IOException ioException) - { - ioException.HResult.Should().NotBe(-2147024809, - $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); - } - } - else - { - if (Test.IsNetFramework) - { - exception.Should().BeException( - hResult: -2147024809, - because: - $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); - } - else - { - exception.Should().BeException( - hResult: -2147024773, - because: - $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); - } - } - } - - [SkippableTheory] - [MemberData(nameof(GetDirectoryCallbacks), parameters: "")] - public void Operations_WhenValueIsEmpty_ShouldThrowArgumentException( - Expression> callback, string paramName, bool ignoreParamCheck, - Func skipTest) - { - Skip.If(skipTest(Test)); - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.Directory); - }); - - exception.Should().BeException( - hResult: -2147024809, - paramName: ignoreParamCheck || Test.IsNetFramework ? null : paramName, - because: - $"\n{callback}\n has empty parameter for '{paramName}' (ignored: {ignoreParamCheck})"); - } - - [SkippableTheory] - [MemberData(nameof(GetDirectoryCallbacks), parameters: (string?)null)] - public void Operations_WhenValueIsNull_ShouldThrowArgumentNullException( - Expression> callback, string paramName, bool ignoreParamCheck, - Func skipTest) - { - Skip.If(skipTest(Test)); - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.Directory); - }); - - exception.Should().BeException( - paramName: ignoreParamCheck ? null : paramName, - because: - $"\n{callback}\n has `null` parameter for '{paramName}' (ignored: {ignoreParamCheck})"); - } - - [SkippableTheory] - [MemberData(nameof(GetDirectoryCallbacks), parameters: " ")] - public void Operations_WhenValueIsWhitespace_ShouldThrowArgumentException( - Expression> callback, string paramName, bool ignoreParamCheck, - Func skipTest) - { - Skip.If(skipTest(Test)); - - Skip.IfNot(Test.RunsOnWindows); - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.Directory); - }); - - exception.Should().BeException( - hResult: -2147024809, - paramName: ignoreParamCheck || Test.IsNetFramework ? null : paramName, - because: - $"\n{callback}\n has whitespace parameter for '{paramName}' (ignored: {ignoreParamCheck})"); - } - - #region Helpers - - public static TheoryData>, string, bool, Func> - GetDirectoryCallbacks(string? path) - { - TheoryData>, string, bool, Func> theoryData = - new(); - foreach ((ExceptionTestHelper.TestTypes TestType, string ParamName, - Expression> Callback, Func? SkipTest) item in - GetDirectoryCallbackTestParameters(path!) - .Where(item => item.TestType.HasFlag(path.ToTestType()))) - { - theoryData.Add( - item.Callback, - item.ParamName, - item.TestType.HasFlag(ExceptionTestHelper.TestTypes.IgnoreParamNameCheck), - item.SkipTest ?? (_ => false)); - } - - return theoryData; - } - - private static IEnumerable<(ExceptionTestHelper.TestTypes TestType, string ParamName, - Expression> Callback, Func? SkipTest)> - GetDirectoryCallbackTestParameters(string value) - { - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.CreateDirectory(value), - null); -#if FEATURE_FILESYSTEM_UNIXFILEMODE - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.CreateDirectory(value, UnixFileMode.None), - test => test.RunsOnWindows); -#endif -#if FEATURE_FILESYSTEM_LINK - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.CreateSymbolicLink(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "pathToTarget", - directory - => directory.CreateSymbolicLink("foo", value), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.Delete(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.Delete(value, true), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateDirectories(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateDirectories(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateDirectories(value, "foo", SearchOption.AllDirectories), - null); -#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateDirectories(value, "foo", new EnumerationOptions()), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateFiles(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateFiles(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateFiles(value, "foo", SearchOption.AllDirectories), - null); -#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateFiles(value, "foo", new EnumerationOptions()), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateFileSystemEntries(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateFileSystemEntries(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateFileSystemEntries(value, "foo", SearchOption.AllDirectories), - null); -#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.EnumerateFileSystemEntries(value, "foo", new EnumerationOptions()), - null); -#endif - // `Directory.Exists` doesn't throw an exception on `null` - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetCreationTime(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetCreationTimeUtc(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetDirectories(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetDirectories(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetDirectories(value, "foo", SearchOption.AllDirectories), - null); -#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetDirectories(value, "foo", new EnumerationOptions()), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetFiles(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetFiles(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetFiles(value, "foo", SearchOption.AllDirectories), - null); -#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetFiles(value, "foo", new EnumerationOptions()), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetFileSystemEntries(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetFileSystemEntries(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetFileSystemEntries(value, "foo", SearchOption.AllDirectories), - null); -#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetFileSystemEntries(value, "foo", new EnumerationOptions()), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetLastAccessTime(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetLastAccessTimeUtc(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetLastWriteTime(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.GetLastWriteTimeUtc(value), - null); - yield return (ExceptionTestHelper.TestTypes.AllExceptInvalidPath, "path", - directory - => directory.GetParent(value), - null); - yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "sourceDirName", - directory - => directory.Move(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destDirName", - directory - => directory.Move("foo", value), - null); -#if FEATURE_FILESYSTEM_LINK - yield return (ExceptionTestHelper.TestTypes.AllExceptWhitespace, "linkPath", - directory - => directory.ResolveLinkTarget(value, false), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.SetCreationTime(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.SetCreationTimeUtc(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.SetLastAccessTime(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.SetLastAccessTimeUtc(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.SetLastWriteTime(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - directory - => directory.SetLastWriteTimeUtc(value, DateTime.Now), - null); - } - - #endregion -} +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Linq.Expressions; + +namespace Testably.Abstractions.Tests.FileSystem.Directory; + +// ReSharper disable once PartialTypeWithSinglePart +public abstract partial class ExceptionTests + : FileSystemTestBase + where TFileSystem : IFileSystem +{ + [SkippableTheory] + [MemberData(nameof(GetDirectoryCallbacks), parameters: "Illegal\tCharacter?InPath")] + public void + Operations_WhenValueContainsIllegalPathCharacters_ShouldThrowCorrectException_OnWindows( + Expression> callback, string paramName, + bool ignoreParamCheck, Func skipTest) + { + Skip.If(skipTest(Test)); + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.Directory); + }); + + if (!Test.RunsOnWindows) + { + if (exception is IOException ioException) + { + ioException.HResult.Should().NotBe(-2147024809, + $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); + } + } + else + { + if (Test.IsNetFramework) + { + exception.Should().BeException( + hResult: -2147024809, + because: + $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); + } + else + { + exception.Should().BeException( + hResult: -2147024773, + because: + $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); + } + } + } + + [SkippableTheory] + [MemberData(nameof(GetDirectoryCallbacks), parameters: "")] + public void Operations_WhenValueIsEmpty_ShouldThrowArgumentException( + Expression> callback, string paramName, bool ignoreParamCheck, + Func skipTest) + { + Skip.If(skipTest(Test)); + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.Directory); + }); + + exception.Should().BeException( + hResult: -2147024809, + paramName: ignoreParamCheck || Test.IsNetFramework ? null : paramName, + because: + $"\n{callback}\n has empty parameter for '{paramName}' (ignored: {ignoreParamCheck})"); + } + + [SkippableTheory] + [MemberData(nameof(GetDirectoryCallbacks), parameters: (string?)null)] + public void Operations_WhenValueIsNull_ShouldThrowArgumentNullException( + Expression> callback, string paramName, bool ignoreParamCheck, + Func skipTest) + { + Skip.If(skipTest(Test)); + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.Directory); + }); + + exception.Should().BeException( + paramName: ignoreParamCheck ? null : paramName, + because: + $"\n{callback}\n has `null` parameter for '{paramName}' (ignored: {ignoreParamCheck})"); + } + + [SkippableTheory] + [MemberData(nameof(GetDirectoryCallbacks), parameters: " ")] + public void Operations_WhenValueIsWhitespace_ShouldThrowArgumentException( + Expression> callback, string paramName, bool ignoreParamCheck, + Func skipTest) + { + Skip.If(skipTest(Test)); + + Skip.IfNot(Test.RunsOnWindows); + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.Directory); + }); + + exception.Should().BeException( + hResult: -2147024809, + paramName: ignoreParamCheck || Test.IsNetFramework ? null : paramName, + because: + $"\n{callback}\n has whitespace parameter for '{paramName}' (ignored: {ignoreParamCheck})"); + } + + #region Helpers + + public static TheoryData>, string, bool, Func> + GetDirectoryCallbacks(string? path) + { + TheoryData>, string, bool, Func> theoryData = + new(); + foreach ((ExceptionTestHelper.TestTypes TestType, string ParamName, + Expression> Callback, Func? SkipTest) item in + GetDirectoryCallbackTestParameters(path!) + .Where(item => item.TestType.HasFlag(path.ToTestType()))) + { + theoryData.Add( + item.Callback, + item.ParamName, + item.TestType.HasFlag(ExceptionTestHelper.TestTypes.IgnoreParamNameCheck), + item.SkipTest ?? (_ => false)); + } + + return theoryData; + } + + private static IEnumerable<(ExceptionTestHelper.TestTypes TestType, string ParamName, + Expression> Callback, Func? SkipTest)> + GetDirectoryCallbackTestParameters(string value) + { + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.CreateDirectory(value), + null); +#if FEATURE_FILESYSTEM_UNIXFILEMODE + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.CreateDirectory(value, UnixFileMode.None), + test => test.RunsOnWindows); +#endif +#if FEATURE_FILESYSTEM_LINK + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.CreateSymbolicLink(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "pathToTarget", + directory + => directory.CreateSymbolicLink("foo", value), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.Delete(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.Delete(value, true), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateDirectories(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateDirectories(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateDirectories(value, "foo", SearchOption.AllDirectories), + null); +#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateDirectories(value, "foo", new EnumerationOptions()), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateFiles(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateFiles(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateFiles(value, "foo", SearchOption.AllDirectories), + null); +#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateFiles(value, "foo", new EnumerationOptions()), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateFileSystemEntries(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateFileSystemEntries(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateFileSystemEntries(value, "foo", SearchOption.AllDirectories), + null); +#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.EnumerateFileSystemEntries(value, "foo", new EnumerationOptions()), + null); +#endif + // `Directory.Exists` doesn't throw an exception on `null` + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetCreationTime(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetCreationTimeUtc(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetDirectories(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetDirectories(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetDirectories(value, "foo", SearchOption.AllDirectories), + null); +#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetDirectories(value, "foo", new EnumerationOptions()), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetFiles(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetFiles(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetFiles(value, "foo", SearchOption.AllDirectories), + null); +#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetFiles(value, "foo", new EnumerationOptions()), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetFileSystemEntries(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetFileSystemEntries(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetFileSystemEntries(value, "foo", SearchOption.AllDirectories), + null); +#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetFileSystemEntries(value, "foo", new EnumerationOptions()), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetLastAccessTime(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetLastAccessTimeUtc(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetLastWriteTime(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.GetLastWriteTimeUtc(value), + null); + yield return (ExceptionTestHelper.TestTypes.AllExceptInvalidPath, "path", + directory + => directory.GetParent(value), + null); + yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "sourceDirName", + directory + => directory.Move(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destDirName", + directory + => directory.Move("foo", value), + null); +#if FEATURE_FILESYSTEM_LINK + yield return (ExceptionTestHelper.TestTypes.AllExceptWhitespace, "linkPath", + directory + => directory.ResolveLinkTarget(value, false), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.SetCreationTime(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.SetCreationTimeUtc(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.SetLastAccessTime(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.SetLastAccessTimeUtc(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.SetLastWriteTime(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + directory + => directory.SetLastWriteTimeUtc(value, DateTime.Now), + null); + } + + #endregion +} diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/File/ExceptionMissingFileTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/File/ExceptionMissingFileTests.cs index 39857dc6..88175bd2 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/File/ExceptionMissingFileTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/File/ExceptionMissingFileTests.cs @@ -1,471 +1,471 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -#if FEATURE_FILESYSTEM_ASYNC -using System.Threading; -#endif - -namespace Testably.Abstractions.Tests.FileSystem.File; - -// ReSharper disable once PartialTypeWithSinglePart -public abstract partial class ExceptionMissingFileTests - : FileSystemTestBase - where TFileSystem : IFileSystem -{ - [SkippableTheory] - [MemberData(nameof(GetFileCallbacks), parameters: (int)MissingFileTestCase.DirectoryMissing)] - public void Operations_WhenDirectoryIsMissing_ShouldThrowDirectoryNotFoundException( - Expression> callback, Func skipTest) - { - Skip.If(skipTest(Test)); - - string path = FileSystem.Path.Combine("missing-directory", "file.txt"); - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.File, path); - }); - - if (Test.RunsOnWindows) - { - exception.Should().BeException( - hResult: -2147024893, - because: $"\n{callback}\n was called with a missing directory"); - } - else - { - exception.Should() - .BeFileOrDirectoryNotFoundException( - $"\n{callback}\n was called with a missing directory"); - } - } - - [SkippableTheory] - [MemberData(nameof(GetFileCallbacks), parameters: (int)MissingFileTestCase.FileMissing)] - public void Operations_WhenFileIsMissing_ShouldThrowFileNotFoundException( - Expression> callback, Func skipTest) - { - Skip.If(skipTest(Test)); - - string path = "missing-file.txt"; - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.File, path); - }); - - if (Test.RunsOnWindows) - { - exception.Should().BeException( - hResult: -2147024894, - because: $"\n{callback}\n was called with a missing file"); - } - else - { - exception.Should() - .BeFileOrDirectoryNotFoundException( - $"\n{callback}\n was called with a missing file"); - } - } - - #region Helpers - - public static TheoryData>, Func> GetFileCallbacks( - int testCases) - { - TheoryData>, Func> theoryData = new(); - foreach ((MissingFileTestCase TestCase, ExpectedExceptionType ExceptionType, - Expression> Callback, Func? SkipTest) item in - GetFileCallbackTestParameters() - .Where(item => (item.TestCase & (MissingFileTestCase)testCases) != 0)) - { - theoryData.Add(item.Callback, item.SkipTest ?? (_ => false)); - } - - return theoryData; - } - - private static IEnumerable<(MissingFileTestCase TestCase, ExpectedExceptionType ExceptionType, - Expression> Callback, Func? SkipTest)> - GetFileCallbackTestParameters() - { - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.GetAttributes(path), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.SetAttributes(path, FileAttributes.ReadOnly), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.Copy(path, "destination.txt"), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendAllLines(path, new[] - { - "foo" - }), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendAllLines(path, new[] - { - "foo" - }), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendAllLines(path, new[] - { - "foo" - }, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendAllLinesAsync(path, new[] - { - "foo" - }, CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendAllLinesAsync(path, new[] - { - "foo" - }, Encoding.UTF8, - CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendAllText(path, "foo"), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendAllText(path, "foo", Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendAllTextAsync(path, "foo", CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendAllTextAsync(path, "foo", Encoding.UTF8, - CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.AppendText(path), - null); - yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, - (file, path) - => file.Copy(path, "foo"), - null); - yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, - (file, path) - => file.Copy("foo", path), - null); - yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, - (file, path) - => file.Copy(path, "foo", false), - null); - yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, - (file, path) - => file.Copy("foo", path, false), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.Create(path), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.Create(path, 1023), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.Create(path, 1023, FileOptions.None), - null); -#if FEATURE_FILESYSTEM_LINK - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.CreateSymbolicLink(path, "foo"), - test => !test.RunsOnWindows); -#endif - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.CreateText(path), - null); - - #pragma warning disable CA1416 - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.Decrypt(path), - test => !test.RunsOnWindows); - #pragma warning restore CA1416 - - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.Delete(path), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.GetAttributes(path), - null); - yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, - (file, path) - => file.Move(path, "foo"), - null); - yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, - (file, path) - => file.Move("foo", path), - null); -#if FEATURE_FILE_MOVETO_OVERWRITE - yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, - (file, path) - => file.Move(path, "foo", false), - null); - yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, - (file, path) - => file.Move("foo", path, false), - null); -#endif - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.Open(path, FileMode.Open), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.Open(path, FileMode.Open, FileAccess.ReadWrite), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None), - null); -#if FEATURE_FILESYSTEM_STREAM_OPTIONS - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.Open(path, new FileStreamOptions()), - null); -#endif - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.OpenRead(path), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.OpenText(path), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.OpenWrite(path), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllBytes(path), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllBytesAsync(path, CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllLines(path), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllLines(path, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllLinesAsync(path, CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllLinesAsync(path, Encoding.UTF8, CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllText(path), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllText(path, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllTextAsync(path, CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadAllTextAsync(path, Encoding.UTF8, CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadLines(path), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadLines(path, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_NET7 - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadLinesAsync(path, CancellationToken.None), - null); - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.ReadLinesAsync(path, Encoding.UTF8, CancellationToken.None), - null); -#endif -#if FEATURE_FILESYSTEM_LINK - yield return (MissingFileTestCase.All, - ExpectedExceptionType.Default, - (file, path) - => file.ResolveLinkTarget(path, false), - null); -#endif - yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, - (file, path) - => file.SetAttributes(path, FileAttributes.ReadOnly), - null); - yield return (MissingFileTestCase.All, - ExpectedExceptionType.Default, - (file, path) - => file.SetCreationTime(path, DateTime.Now), - null); - yield return (MissingFileTestCase.All, - ExpectedExceptionType.Default, - (file, path) - => file.SetCreationTimeUtc(path, DateTime.Now), - null); - yield return (MissingFileTestCase.All, - ExpectedExceptionType.Default, - (file, path) - => file.SetLastAccessTime(path, DateTime.Now), - null); - yield return (MissingFileTestCase.All, - ExpectedExceptionType.Default, - (file, path) - => file.SetLastAccessTimeUtc(path, DateTime.Now), - null); - yield return (MissingFileTestCase.All, - ExpectedExceptionType.Default, - (file, path) - => file.SetLastWriteTime(path, DateTime.Now), - null); - yield return (MissingFileTestCase.All, - ExpectedExceptionType.Default, - (file, path) - => file.SetLastWriteTimeUtc(path, DateTime.Now), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllBytes(path, new byte[] - { - 0, 1 - }), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllBytesAsync(path, new byte[] - { - 0, 1 - }, - CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllLines(path, new[] - { - "foo" - }), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllLines(path, new[] - { - "foo" - }, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllLinesAsync(path, new[] - { - "foo" - }, CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllLinesAsync(path, new[] - { - "foo" - }, Encoding.UTF8, - CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllText(path, "foo"), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllText(path, "foo", Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllTextAsync(path, "foo", CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, - (file, path) - => file.WriteAllTextAsync(path, "foo", Encoding.UTF8, - CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - } - - #endregion - - public enum ExpectedExceptionType - { - Default, - } - - [Flags] - public enum MissingFileTestCase - { - FileMissing = 1, - DirectoryMissing = 2, - All = FileMissing | DirectoryMissing - } -} +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +#if FEATURE_FILESYSTEM_ASYNC +using System.Threading; +#endif + +namespace Testably.Abstractions.Tests.FileSystem.File; + +// ReSharper disable once PartialTypeWithSinglePart +public abstract partial class ExceptionMissingFileTests + : FileSystemTestBase + where TFileSystem : IFileSystem +{ + [SkippableTheory] + [MemberData(nameof(GetFileCallbacks), parameters: (int)MissingFileTestCase.DirectoryMissing)] + public void Operations_WhenDirectoryIsMissing_ShouldThrowDirectoryNotFoundException( + Expression> callback, Func skipTest) + { + Skip.If(skipTest(Test)); + + string path = FileSystem.Path.Combine("missing-directory", "file.txt"); + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.File, path); + }); + + if (Test.RunsOnWindows) + { + exception.Should().BeException( + hResult: -2147024893, + because: $"\n{callback}\n was called with a missing directory"); + } + else + { + exception.Should() + .BeFileOrDirectoryNotFoundException( + $"\n{callback}\n was called with a missing directory"); + } + } + + [SkippableTheory] + [MemberData(nameof(GetFileCallbacks), parameters: (int)MissingFileTestCase.FileMissing)] + public void Operations_WhenFileIsMissing_ShouldThrowFileNotFoundException( + Expression> callback, Func skipTest) + { + Skip.If(skipTest(Test)); + + string path = "missing-file.txt"; + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.File, path); + }); + + if (Test.RunsOnWindows) + { + exception.Should().BeException( + hResult: -2147024894, + because: $"\n{callback}\n was called with a missing file"); + } + else + { + exception.Should() + .BeFileOrDirectoryNotFoundException( + $"\n{callback}\n was called with a missing file"); + } + } + + #region Helpers + + public static TheoryData>, Func> GetFileCallbacks( + int testCases) + { + TheoryData>, Func> theoryData = new(); + foreach ((MissingFileTestCase TestCase, ExpectedExceptionType ExceptionType, + Expression> Callback, Func? SkipTest) item in + GetFileCallbackTestParameters() + .Where(item => (item.TestCase & (MissingFileTestCase)testCases) != 0)) + { + theoryData.Add(item.Callback, item.SkipTest ?? (_ => false)); + } + + return theoryData; + } + + private static IEnumerable<(MissingFileTestCase TestCase, ExpectedExceptionType ExceptionType, + Expression> Callback, Func? SkipTest)> + GetFileCallbackTestParameters() + { + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.GetAttributes(path), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.SetAttributes(path, FileAttributes.ReadOnly), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.Copy(path, "destination.txt"), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendAllLines(path, new[] + { + "foo" + }), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendAllLines(path, new[] + { + "foo" + }), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendAllLines(path, new[] + { + "foo" + }, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendAllLinesAsync(path, new[] + { + "foo" + }, CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendAllLinesAsync(path, new[] + { + "foo" + }, Encoding.UTF8, + CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendAllText(path, "foo"), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendAllText(path, "foo", Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendAllTextAsync(path, "foo", CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendAllTextAsync(path, "foo", Encoding.UTF8, + CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.AppendText(path), + null); + yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, + (file, path) + => file.Copy(path, "foo"), + null); + yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, + (file, path) + => file.Copy("foo", path), + null); + yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, + (file, path) + => file.Copy(path, "foo", false), + null); + yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, + (file, path) + => file.Copy("foo", path, false), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.Create(path), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.Create(path, 1023), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.Create(path, 1023, FileOptions.None), + null); +#if FEATURE_FILESYSTEM_LINK + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.CreateSymbolicLink(path, "foo"), + test => !test.RunsOnWindows); +#endif + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.CreateText(path), + null); + + #pragma warning disable CA1416 + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.Decrypt(path), + test => !test.RunsOnWindows); + #pragma warning restore CA1416 + + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.Delete(path), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.GetAttributes(path), + null); + yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, + (file, path) + => file.Move(path, "foo"), + null); + yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, + (file, path) + => file.Move("foo", path), + null); +#if FEATURE_FILE_MOVETO_OVERWRITE + yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, + (file, path) + => file.Move(path, "foo", false), + null); + yield return (MissingFileTestCase.FileMissing, ExpectedExceptionType.Default, + (file, path) + => file.Move("foo", path, false), + null); +#endif + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.Open(path, FileMode.Open), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.Open(path, FileMode.Open, FileAccess.ReadWrite), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None), + null); +#if FEATURE_FILESYSTEM_STREAM_OPTIONS + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.Open(path, new FileStreamOptions()), + null); +#endif + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.OpenRead(path), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.OpenText(path), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.OpenWrite(path), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllBytes(path), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllBytesAsync(path, CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllLines(path), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllLines(path, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllLinesAsync(path, CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllLinesAsync(path, Encoding.UTF8, CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllText(path), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllText(path, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllTextAsync(path, CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadAllTextAsync(path, Encoding.UTF8, CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadLines(path), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadLines(path, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_NET7 + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadLinesAsync(path, CancellationToken.None), + null); + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.ReadLinesAsync(path, Encoding.UTF8, CancellationToken.None), + null); +#endif +#if FEATURE_FILESYSTEM_LINK + yield return (MissingFileTestCase.All, + ExpectedExceptionType.Default, + (file, path) + => file.ResolveLinkTarget(path, false), + null); +#endif + yield return (MissingFileTestCase.All, ExpectedExceptionType.Default, + (file, path) + => file.SetAttributes(path, FileAttributes.ReadOnly), + null); + yield return (MissingFileTestCase.All, + ExpectedExceptionType.Default, + (file, path) + => file.SetCreationTime(path, DateTime.Now), + null); + yield return (MissingFileTestCase.All, + ExpectedExceptionType.Default, + (file, path) + => file.SetCreationTimeUtc(path, DateTime.Now), + null); + yield return (MissingFileTestCase.All, + ExpectedExceptionType.Default, + (file, path) + => file.SetLastAccessTime(path, DateTime.Now), + null); + yield return (MissingFileTestCase.All, + ExpectedExceptionType.Default, + (file, path) + => file.SetLastAccessTimeUtc(path, DateTime.Now), + null); + yield return (MissingFileTestCase.All, + ExpectedExceptionType.Default, + (file, path) + => file.SetLastWriteTime(path, DateTime.Now), + null); + yield return (MissingFileTestCase.All, + ExpectedExceptionType.Default, + (file, path) + => file.SetLastWriteTimeUtc(path, DateTime.Now), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllBytes(path, new byte[] + { + 0, 1 + }), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllBytesAsync(path, new byte[] + { + 0, 1 + }, + CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllLines(path, new[] + { + "foo" + }), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllLines(path, new[] + { + "foo" + }, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllLinesAsync(path, new[] + { + "foo" + }, CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllLinesAsync(path, new[] + { + "foo" + }, Encoding.UTF8, + CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllText(path, "foo"), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllText(path, "foo", Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllTextAsync(path, "foo", CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (MissingFileTestCase.DirectoryMissing, ExpectedExceptionType.Default, + (file, path) + => file.WriteAllTextAsync(path, "foo", Encoding.UTF8, + CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + } + + #endregion + + public enum ExpectedExceptionType + { + Default, + } + + [Flags] + public enum MissingFileTestCase + { + FileMissing = 1, + DirectoryMissing = 2, + All = FileMissing | DirectoryMissing + } +} diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/File/ExceptionTests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/File/ExceptionTests.cs index 38f5081c..da9f844c 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/File/ExceptionTests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/File/ExceptionTests.cs @@ -1,592 +1,592 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -#if FEATURE_FILESYSTEM_ASYNC -using System.Threading; -#endif - -namespace Testably.Abstractions.Tests.FileSystem.File; - -// ReSharper disable once PartialTypeWithSinglePart -public abstract partial class ExceptionTests - : FileSystemTestBase - where TFileSystem : IFileSystem -{ - [SkippableTheory] - [MemberData(nameof(GetFileCallbacks), parameters: "Illegal\tCharacter?InPath")] - public void - Operations_WhenValueContainsIllegalPathCharacters_ShouldThrowCorrectException_OnWindows( - Expression> callback, string paramName, bool ignoreParamCheck, - Func skipTest) - { - Skip.If(skipTest(Test)); - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.File); - }); - - if (!Test.RunsOnWindows) - { - if (exception is IOException ioException) - { - ioException.HResult.Should().NotBe(-2147024809, - $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); - } - } - else - { - if (Test.IsNetFramework) - { - exception.Should().BeException( - hResult: -2147024809, - because: - $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); - } - else - { - exception.Should().BeException( - hResult: -2147024773, - because: - $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); - } - } - } - - [SkippableTheory] - [MemberData(nameof(GetFileCallbacks), parameters: "")] - public void Operations_WhenValueIsEmpty_ShouldThrowArgumentException( - Expression> callback, string paramName, bool ignoreParamCheck, - Func skipTest) - { - Skip.If(skipTest(Test)); - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.File); - }); - - exception.Should().BeException( - hResult: -2147024809, - paramName: ignoreParamCheck || Test.IsNetFramework ? null : paramName, - because: - $"\n{callback}\n has empty parameter for '{paramName}' (ignored: {ignoreParamCheck})"); - } - - [SkippableTheory] - [MemberData(nameof(GetFileCallbacks), parameters: (string?)null)] - public void Operations_WhenValueIsNull_ShouldThrowArgumentNullException( - Expression> callback, string paramName, bool ignoreParamCheck, - Func skipTest) - { - Skip.If(skipTest(Test)); - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.File); - }); - - exception.Should().BeException( - paramName: ignoreParamCheck ? null : paramName, - because: - $"\n{callback}\n has `null` parameter for '{paramName}' (ignored: {ignoreParamCheck})"); - } - - [SkippableTheory] - [MemberData(nameof(GetFileCallbacks), parameters: " ")] - public void Operations_WhenValueIsWhitespace_ShouldThrowArgumentException( - Expression> callback, string paramName, bool ignoreParamCheck, - Func skipTest) - { - Skip.If(skipTest(Test)); - Skip.IfNot(Test.RunsOnWindows); - - Exception? exception = Record.Exception(() => - { - callback.Compile().Invoke(FileSystem.File); - }); - - exception.Should().BeException( - hResult: -2147024809, - paramName: ignoreParamCheck || Test.IsNetFramework ? null : paramName, - because: - $"\n{callback}\n has whitespace parameter for '{paramName}' (ignored: {ignoreParamCheck})"); - } - - #region Helpers - - public static TheoryData>, string, bool, Func> - GetFileCallbacks(string? path) - { - TheoryData>, string, bool, Func> theoryData = new(); - foreach ((ExceptionTestHelper.TestTypes TestType, string ParamName, - Expression> Callback, Func? SkipTest) item in - GetFileCallbackTestParameters(path!) - .Where(item => item.TestType.HasFlag(path.ToTestType()))) - { - theoryData.Add( - item.Callback, - item.ParamName, - item.TestType.HasFlag(ExceptionTestHelper.TestTypes.IgnoreParamNameCheck), - item.SkipTest ?? (_ => false)); - } - - return theoryData; - } - - private static IEnumerable<(ExceptionTestHelper.TestTypes TestType, string ParamName, - Expression> Callback, Func? SkipTest)> - GetFileCallbackTestParameters(string value) - { - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.AppendAllLines(value, new[] - { - "foo" - }), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.AppendAllLines(value, new[] - { - "foo" - }, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.AppendAllLinesAsync(value, new[] - { - "foo" - }, CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.AppendAllLinesAsync(value, new[] - { - "foo" - }, Encoding.UTF8, - CancellationToken.None).GetAwaiter().GetResult(), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.AppendAllText(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.AppendAllText(value, "foo", Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.AppendAllTextAsync(value, "foo", CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.AppendAllTextAsync(value, "foo", Encoding.UTF8, CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.NullOrInvalidPath, "path", - file - => file.AppendText(value), - null); - yield return (ExceptionTestHelper.TestTypes.AllExceptWhitespace, "sourceFileName", - file - => file.Copy(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destFileName", - file - => file.Copy("foo", value), - null); - yield return (ExceptionTestHelper.TestTypes.AllExceptWhitespace, "sourceFileName", - file - => file.Copy(value, "foo", false), - null); - yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destFileName", - file - => file.Copy("foo", value, false), null); - yield return ( - ExceptionTestHelper.TestTypes.Whitespace | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", - file - => file.Copy(value, "foo"), - null); - yield return ( - ExceptionTestHelper.TestTypes.Whitespace | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destFileName", - file - => file.Copy("foo", value), - null); - yield return ( - ExceptionTestHelper.TestTypes.Whitespace | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", - file - => file.Copy(value, "foo", false), - null); - yield return ( - ExceptionTestHelper.TestTypes.Whitespace | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destFileName", - file - => file.Copy("foo", value, false), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.Create(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.Create(value, 1023), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.Create(value, 1023, FileOptions.None), - null); -#if FEATURE_FILESYSTEM_LINK - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.CreateSymbolicLink(value, "foo"), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.NullOrInvalidPath, "path", - file - => file.CreateText(value), - null); - #pragma warning disable CA1416 - yield return (ExceptionTestHelper.TestTypes.AllExceptInvalidPath, "path", - file - => file.Decrypt(value), - test => !test.RunsOnWindows); - #pragma warning restore CA1416 - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.Delete(value), - null); - #pragma warning disable CA1416 - yield return (ExceptionTestHelper.TestTypes.AllExceptInvalidPath, "path", - file - => file.Encrypt(value), - test => !test.RunsOnWindows); - #pragma warning restore CA1416 - // `File.Exists` doesn't throw an exception on `null` - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.GetAttributes(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.GetCreationTime(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.GetCreationTimeUtc(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.GetLastAccessTime(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.GetLastAccessTimeUtc(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.GetLastWriteTime(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.GetLastWriteTimeUtc(value), - null); -#if FEATURE_FILESYSTEM_UNIXFILEMODE - #pragma warning disable CA1416 - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.GetUnixFileMode(value), - test => test.RunsOnWindows); - #pragma warning restore CA1416 -#endif - yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "sourceFileName", - file - => file.Move(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destFileName", - file - => file.Move("foo", value), - null); - yield return ( - ExceptionTestHelper.TestTypes.Whitespace | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", - file - => file.Move(value, "foo"), - null); - yield return ( - ExceptionTestHelper.TestTypes.Whitespace | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destFileName", - file - => file.Move("foo", value), - null); -#if FEATURE_FILE_MOVETO_OVERWRITE - yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "sourceFileName", - file - => file.Move(value, "foo", false), - null); - yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destFileName", - file - => file.Move("foo", value, false), - null); - yield return ( - ExceptionTestHelper.TestTypes.Whitespace | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", - file - => file.Move(value, "foo", false), - null); - yield return ( - ExceptionTestHelper.TestTypes.Whitespace | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destFileName", - file - => file.Move("foo", value, false), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.Open(value, FileMode.Open), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.Open(value, FileMode.Open, FileAccess.ReadWrite), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.Open(value, FileMode.Open, FileAccess.ReadWrite, FileShare.None), - null); -#if FEATURE_FILESYSTEM_STREAM_OPTIONS - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.Open(value, new FileStreamOptions()), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.OpenRead(value), - null); - yield return (ExceptionTestHelper.TestTypes.NullOrInvalidPath, "path", - file - => file.OpenText(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.OpenWrite(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllBytes(value), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllBytesAsync(value, CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllLines(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllLines(value, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllLinesAsync(value, CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllLinesAsync(value, Encoding.UTF8, CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllText(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllText(value, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllTextAsync(value, CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadAllTextAsync(value, Encoding.UTF8, CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadLines(value), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadLines(value, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_NET7 - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadLinesAsync(value, CancellationToken.None), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.ReadLinesAsync(value, Encoding.UTF8, CancellationToken.None), - null); -#endif - yield return ( - ExceptionTestHelper.TestTypes.AllExceptInvalidPath | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", - file - => file.Replace(value, "foo", "bar"), - null); - yield return ( - ExceptionTestHelper.TestTypes.All | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destinationFileName", - file - => file.Replace("foo", value, "bar"), - null); - yield return ( - ExceptionTestHelper.TestTypes.AllExceptInvalidPath | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", - file - => file.Replace(value, "foo", "bar", false), - null); - yield return ( - ExceptionTestHelper.TestTypes.All | - ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destinationFileName", - file - => file.Replace("foo", value, "bar", false), - null); -#if FEATURE_FILESYSTEM_LINK - yield return (ExceptionTestHelper.TestTypes.AllExceptWhitespace, "linkPath", - file - => file.ResolveLinkTarget(value, false), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.SetAttributes(value, FileAttributes.ReadOnly), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.SetCreationTime(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.SetCreationTimeUtc(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.SetLastAccessTime(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.SetLastAccessTimeUtc(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.SetLastWriteTime(value, DateTime.Now), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.SetLastWriteTimeUtc(value, DateTime.Now), - null); -#if FEATURE_FILESYSTEM_UNIXFILEMODE - #pragma warning disable CA1416 - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.SetUnixFileMode(value, UnixFileMode.None), - test => test.RunsOnWindows); - #pragma warning restore CA1416 -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllBytes(value, new byte[] - { - 0, 1 - }), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllBytesAsync(value, new byte[] - { - 0, 1 - }, - CancellationToken.None).GetAwaiter().GetResult(), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllLines(value, new[] - { - "foo" - }), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllLines(value, new[] - { - "foo" - }, Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllLinesAsync(value, new[] - { - "foo" - }, CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllLinesAsync(value, new[] - { - "foo" - }, Encoding.UTF8, - CancellationToken.None).GetAwaiter().GetResult(), - null); -#endif - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllText(value, "foo"), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllText(value, "foo", Encoding.UTF8), - null); -#if FEATURE_FILESYSTEM_ASYNC - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllTextAsync(value, "foo", CancellationToken.None) - .GetAwaiter().GetResult(), - null); - yield return (ExceptionTestHelper.TestTypes.All, "path", - file - => file.WriteAllTextAsync(value, "foo", Encoding.UTF8, CancellationToken.None) - .GetAwaiter().GetResult(), - null); -#endif - } - - #endregion -} +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +#if FEATURE_FILESYSTEM_ASYNC +using System.Threading; +#endif + +namespace Testably.Abstractions.Tests.FileSystem.File; + +// ReSharper disable once PartialTypeWithSinglePart +public abstract partial class ExceptionTests + : FileSystemTestBase + where TFileSystem : IFileSystem +{ + [SkippableTheory] + [MemberData(nameof(GetFileCallbacks), parameters: "Illegal\tCharacter?InPath")] + public void + Operations_WhenValueContainsIllegalPathCharacters_ShouldThrowCorrectException_OnWindows( + Expression> callback, string paramName, bool ignoreParamCheck, + Func skipTest) + { + Skip.If(skipTest(Test)); + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.File); + }); + + if (!Test.RunsOnWindows) + { + if (exception is IOException ioException) + { + ioException.HResult.Should().NotBe(-2147024809, + $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); + } + } + else + { + if (Test.IsNetFramework) + { + exception.Should().BeException( + hResult: -2147024809, + because: + $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); + } + else + { + exception.Should().BeException( + hResult: -2147024773, + because: + $"\n{callback}\n contains invalid path characters for '{paramName}' (ignored: {ignoreParamCheck})"); + } + } + } + + [SkippableTheory] + [MemberData(nameof(GetFileCallbacks), parameters: "")] + public void Operations_WhenValueIsEmpty_ShouldThrowArgumentException( + Expression> callback, string paramName, bool ignoreParamCheck, + Func skipTest) + { + Skip.If(skipTest(Test)); + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.File); + }); + + exception.Should().BeException( + hResult: -2147024809, + paramName: ignoreParamCheck || Test.IsNetFramework ? null : paramName, + because: + $"\n{callback}\n has empty parameter for '{paramName}' (ignored: {ignoreParamCheck})"); + } + + [SkippableTheory] + [MemberData(nameof(GetFileCallbacks), parameters: (string?)null)] + public void Operations_WhenValueIsNull_ShouldThrowArgumentNullException( + Expression> callback, string paramName, bool ignoreParamCheck, + Func skipTest) + { + Skip.If(skipTest(Test)); + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.File); + }); + + exception.Should().BeException( + paramName: ignoreParamCheck ? null : paramName, + because: + $"\n{callback}\n has `null` parameter for '{paramName}' (ignored: {ignoreParamCheck})"); + } + + [SkippableTheory] + [MemberData(nameof(GetFileCallbacks), parameters: " ")] + public void Operations_WhenValueIsWhitespace_ShouldThrowArgumentException( + Expression> callback, string paramName, bool ignoreParamCheck, + Func skipTest) + { + Skip.If(skipTest(Test)); + Skip.IfNot(Test.RunsOnWindows); + + Exception? exception = Record.Exception(() => + { + callback.Compile().Invoke(FileSystem.File); + }); + + exception.Should().BeException( + hResult: -2147024809, + paramName: ignoreParamCheck || Test.IsNetFramework ? null : paramName, + because: + $"\n{callback}\n has whitespace parameter for '{paramName}' (ignored: {ignoreParamCheck})"); + } + + #region Helpers + + public static TheoryData>, string, bool, Func> + GetFileCallbacks(string? path) + { + TheoryData>, string, bool, Func> theoryData = new(); + foreach ((ExceptionTestHelper.TestTypes TestType, string ParamName, + Expression> Callback, Func? SkipTest) item in + GetFileCallbackTestParameters(path!) + .Where(item => item.TestType.HasFlag(path.ToTestType()))) + { + theoryData.Add( + item.Callback, + item.ParamName, + item.TestType.HasFlag(ExceptionTestHelper.TestTypes.IgnoreParamNameCheck), + item.SkipTest ?? (_ => false)); + } + + return theoryData; + } + + private static IEnumerable<(ExceptionTestHelper.TestTypes TestType, string ParamName, + Expression> Callback, Func? SkipTest)> + GetFileCallbackTestParameters(string value) + { + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.AppendAllLines(value, new[] + { + "foo" + }), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.AppendAllLines(value, new[] + { + "foo" + }, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.AppendAllLinesAsync(value, new[] + { + "foo" + }, CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.AppendAllLinesAsync(value, new[] + { + "foo" + }, Encoding.UTF8, + CancellationToken.None).GetAwaiter().GetResult(), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.AppendAllText(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.AppendAllText(value, "foo", Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.AppendAllTextAsync(value, "foo", CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.AppendAllTextAsync(value, "foo", Encoding.UTF8, CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.NullOrInvalidPath, "path", + file + => file.AppendText(value), + null); + yield return (ExceptionTestHelper.TestTypes.AllExceptWhitespace, "sourceFileName", + file + => file.Copy(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destFileName", + file + => file.Copy("foo", value), + null); + yield return (ExceptionTestHelper.TestTypes.AllExceptWhitespace, "sourceFileName", + file + => file.Copy(value, "foo", false), + null); + yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destFileName", + file + => file.Copy("foo", value, false), null); + yield return ( + ExceptionTestHelper.TestTypes.Whitespace | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", + file + => file.Copy(value, "foo"), + null); + yield return ( + ExceptionTestHelper.TestTypes.Whitespace | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destFileName", + file + => file.Copy("foo", value), + null); + yield return ( + ExceptionTestHelper.TestTypes.Whitespace | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", + file + => file.Copy(value, "foo", false), + null); + yield return ( + ExceptionTestHelper.TestTypes.Whitespace | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destFileName", + file + => file.Copy("foo", value, false), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.Create(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.Create(value, 1023), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.Create(value, 1023, FileOptions.None), + null); +#if FEATURE_FILESYSTEM_LINK + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.CreateSymbolicLink(value, "foo"), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.NullOrInvalidPath, "path", + file + => file.CreateText(value), + null); + #pragma warning disable CA1416 + yield return (ExceptionTestHelper.TestTypes.AllExceptInvalidPath, "path", + file + => file.Decrypt(value), + test => !test.RunsOnWindows); + #pragma warning restore CA1416 + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.Delete(value), + null); + #pragma warning disable CA1416 + yield return (ExceptionTestHelper.TestTypes.AllExceptInvalidPath, "path", + file + => file.Encrypt(value), + test => !test.RunsOnWindows); + #pragma warning restore CA1416 + // `File.Exists` doesn't throw an exception on `null` + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.GetAttributes(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.GetCreationTime(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.GetCreationTimeUtc(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.GetLastAccessTime(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.GetLastAccessTimeUtc(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.GetLastWriteTime(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.GetLastWriteTimeUtc(value), + null); +#if FEATURE_FILESYSTEM_UNIXFILEMODE + #pragma warning disable CA1416 + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.GetUnixFileMode(value), + test => test.RunsOnWindows); + #pragma warning restore CA1416 +#endif + yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "sourceFileName", + file + => file.Move(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destFileName", + file + => file.Move("foo", value), + null); + yield return ( + ExceptionTestHelper.TestTypes.Whitespace | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", + file + => file.Move(value, "foo"), + null); + yield return ( + ExceptionTestHelper.TestTypes.Whitespace | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destFileName", + file + => file.Move("foo", value), + null); +#if FEATURE_FILE_MOVETO_OVERWRITE + yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "sourceFileName", + file + => file.Move(value, "foo", false), + null); + yield return (ExceptionTestHelper.TestTypes.NullOrEmpty, "destFileName", + file + => file.Move("foo", value, false), + null); + yield return ( + ExceptionTestHelper.TestTypes.Whitespace | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", + file + => file.Move(value, "foo", false), + null); + yield return ( + ExceptionTestHelper.TestTypes.Whitespace | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destFileName", + file + => file.Move("foo", value, false), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.Open(value, FileMode.Open), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.Open(value, FileMode.Open, FileAccess.ReadWrite), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.Open(value, FileMode.Open, FileAccess.ReadWrite, FileShare.None), + null); +#if FEATURE_FILESYSTEM_STREAM_OPTIONS + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.Open(value, new FileStreamOptions()), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.OpenRead(value), + null); + yield return (ExceptionTestHelper.TestTypes.NullOrInvalidPath, "path", + file + => file.OpenText(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.OpenWrite(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllBytes(value), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllBytesAsync(value, CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllLines(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllLines(value, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllLinesAsync(value, CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllLinesAsync(value, Encoding.UTF8, CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllText(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllText(value, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllTextAsync(value, CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadAllTextAsync(value, Encoding.UTF8, CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadLines(value), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadLines(value, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_NET7 + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadLinesAsync(value, CancellationToken.None), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.ReadLinesAsync(value, Encoding.UTF8, CancellationToken.None), + null); +#endif + yield return ( + ExceptionTestHelper.TestTypes.AllExceptInvalidPath | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", + file + => file.Replace(value, "foo", "bar"), + null); + yield return ( + ExceptionTestHelper.TestTypes.All | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destinationFileName", + file + => file.Replace("foo", value, "bar"), + null); + yield return ( + ExceptionTestHelper.TestTypes.AllExceptInvalidPath | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "sourceFileName", + file + => file.Replace(value, "foo", "bar", false), + null); + yield return ( + ExceptionTestHelper.TestTypes.All | + ExceptionTestHelper.TestTypes.IgnoreParamNameCheck, "destinationFileName", + file + => file.Replace("foo", value, "bar", false), + null); +#if FEATURE_FILESYSTEM_LINK + yield return (ExceptionTestHelper.TestTypes.AllExceptWhitespace, "linkPath", + file + => file.ResolveLinkTarget(value, false), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.SetAttributes(value, FileAttributes.ReadOnly), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.SetCreationTime(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.SetCreationTimeUtc(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.SetLastAccessTime(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.SetLastAccessTimeUtc(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.SetLastWriteTime(value, DateTime.Now), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.SetLastWriteTimeUtc(value, DateTime.Now), + null); +#if FEATURE_FILESYSTEM_UNIXFILEMODE + #pragma warning disable CA1416 + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.SetUnixFileMode(value, UnixFileMode.None), + test => test.RunsOnWindows); + #pragma warning restore CA1416 +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllBytes(value, new byte[] + { + 0, 1 + }), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllBytesAsync(value, new byte[] + { + 0, 1 + }, + CancellationToken.None).GetAwaiter().GetResult(), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllLines(value, new[] + { + "foo" + }), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllLines(value, new[] + { + "foo" + }, Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllLinesAsync(value, new[] + { + "foo" + }, CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllLinesAsync(value, new[] + { + "foo" + }, Encoding.UTF8, + CancellationToken.None).GetAwaiter().GetResult(), + null); +#endif + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllText(value, "foo"), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllText(value, "foo", Encoding.UTF8), + null); +#if FEATURE_FILESYSTEM_ASYNC + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllTextAsync(value, "foo", CancellationToken.None) + .GetAwaiter().GetResult(), + null); + yield return (ExceptionTestHelper.TestTypes.All, "path", + file + => file.WriteAllTextAsync(value, "foo", Encoding.UTF8, CancellationToken.None) + .GetAwaiter().GetResult(), + null); +#endif + } + + #endregion +}