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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,7 @@ private void DeleteInternal(bool recursive)
}
catch (UnauthorizedAccessException ex)
{
#pragma warning disable MA0054 // Embed the caught exception as innerException
throw new IOException(ex.Message);
#pragma warning restore MA0054
throw ExceptionFactory.DirectoryDeleteAccessDenied(ex.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ public void Delete(string path)
}
catch (UnauthorizedAccessException ex)
{
#pragma warning disable MA0054 // Embed the caught exception as innerException
throw new IOException(ex.Message);
#pragma warning restore MA0054
throw ExceptionFactory.DirectoryDeleteAccessDenied(ex.Message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ private void ThrowIfDisposed()
// A stream on a handle cannot be used once the handle is closed, even before the stream is disposed.
if (_isDisposed || _ownedHandle is { IsClosed: true })
{
throw new ObjectDisposedException("", "Cannot access a closed file.");
throw ExceptionFactory.FileClosed();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal static EnumerationOptions FromSearchOption(SearchOption searchOption)
if (searchOption != SearchOption.TopDirectoryOnly &&
searchOption != SearchOption.AllDirectories)
{
throw new ArgumentOutOfRangeException(nameof(searchOption));
throw ExceptionFactory.EnumValueOutOfRange(nameof(searchOption));
}

return searchOption == SearchOption.AllDirectories
Expand Down
20 changes: 17 additions & 3 deletions Source/Testably.Abstractions.Testing/Helpers/ExceptionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ internal static IOException CannotCreateFileAsAlreadyExists(Execute execute, str
internal static IOException CannotCreateFileWhenAlreadyExists(int hResult)
=> new("Cannot create a file when that file already exists.", hResult);

internal static IOException DirectoryDeleteAccessDenied(string message)
=> new(message);

internal static ArgumentException DirectoryNameDoesNotExist(
string path, string paramName = "path")
=> new($"The directory name '{path}' does not exist.", paramName)
Expand Down Expand Up @@ -75,6 +78,9 @@ internal static ArgumentOutOfRangeException EnumValueOutOfRange(string paramName
internal static IOException FileAlreadyExists(string path, int hResult)
=> new($"The file '{path}' already exists.", hResult);

internal static ObjectDisposedException FileClosed()
=> new(null, "Cannot access a closed file.");

internal static IOException FileNameCannotBeResolved(
string path, int hResult = -2147022975)
=> new($"The name of the file cannot be resolved by the system. : '{path}'",
Expand Down Expand Up @@ -187,6 +193,9 @@ internal static ArgumentException NullCharacterInPath(string paramName)
=> new("Illegal characters in path.", paramName);
#endif

internal static ObjectDisposedException ObjectDisposed()
=> new(null, "Cannot access a disposed object.");

internal static PlatformNotSupportedException OperationNotSupportedOnThisPlatform()
=> new("Operation is not supported on this platform.")
{
Expand Down Expand Up @@ -329,14 +338,19 @@ internal static TimeoutException TimeoutExpired(int timeoutMilliseconds)
internal static TimeoutException TimeoutExpired(TimeSpan timeout)
=> new($"The timeout of {timeout} expired in the awaitable callback.");

internal static ArgumentOutOfRangeException TimerArgumentOutOfRange(string propertyName)
=> new(propertyName,
"Number must be either non-negative and less than or equal to Int32.MaxValue or -1")
internal static ArgumentOutOfRangeException TimerArgumentOutOfRange(
string paramName, long value)
#if NET8_0_OR_GREATER
=> new(paramName, value, $"{paramName} ('{value}') must be greater than or equal to '-1'.");
#else
=> new(paramName,
"Number must be either non-negative and less than or equal to Int32.MaxValue or -1.")
{
#if FEATURE_EXCEPTION_HRESULT
HResult = -2146233086,
#endif
};
#endif

internal static TimeoutException TimerWaitTimeoutException(int executionCount, int timeout)
=> new($"The execution count {executionCount} was not reached in {timeout}ms.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if !FEATURE_FILESYSTEM_ENUMERATION_OPTIONS
using System.Diagnostics.CodeAnalysis;
using Testably.Abstractions.Testing.Helpers;

// ReSharper disable once CheckNamespace
namespace System.IO;
Expand Down Expand Up @@ -92,7 +93,7 @@ public int MaxRecursionDepth
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
throw ExceptionFactory.NonNegativeNumberRequired(nameof(value));
}

field = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public static AdjustedLocation AdjustLocationFromSearchPattern(

parentDirectories.Push(fileSystem.Execute.Path.GetFileName(location.FullPath));
location = location.GetParent() ??
throw new UnauthorizedAccessException(
$"The searchPattern '{searchPattern}' has too many '../' for path '{path}'");
throw ExceptionFactory.AccessToPathDenied(location.FullPath);
#pragma warning disable CA1846
givenPathPrefix.Append(searchPattern, 0, 3);
#pragma warning restore CA1846
Expand Down
14 changes: 9 additions & 5 deletions Source/Testably.Abstractions.Testing/TimeSystem/TimerMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ internal TimerMock(MockTimeSystem timeSystem,
{
if (dueTime.TotalMilliseconds < -1)
{
throw ExceptionFactory.TimerArgumentOutOfRange(nameof(dueTime));
throw ExceptionFactory.TimerArgumentOutOfRange(nameof(dueTime),
(long)dueTime.TotalMilliseconds);
}

if (period.TotalMilliseconds < -1)
{
throw new ArgumentOutOfRangeException(nameof(period));
throw ExceptionFactory.TimerArgumentOutOfRange(nameof(period),
(long)period.TotalMilliseconds);
}

_mockTimeSystem = timeSystem;
Expand Down Expand Up @@ -86,18 +88,20 @@ public bool Change(TimeSpan dueTime, TimeSpan period)
#if NET8_0_OR_GREATER
return false;
#else
throw new ObjectDisposedException(nameof(Change), "Cannot access a disposed object.");
throw ExceptionFactory.ObjectDisposed();
#endif
}

if (dueTime.TotalMilliseconds < -1)
{
throw ExceptionFactory.TimerArgumentOutOfRange(nameof(dueTime));
throw ExceptionFactory.TimerArgumentOutOfRange(nameof(dueTime),
(long)dueTime.TotalMilliseconds);
}

if (period.TotalMilliseconds < -1)
{
throw new ArgumentOutOfRangeException(nameof(period));
throw ExceptionFactory.TimerArgumentOutOfRange(nameof(period),
(long)period.TotalMilliseconds);
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ void Act()
EnumerationOptionsHelper.FromSearchOption(invalidSearchOption);
}

await That(Act).ThrowsExactly<ArgumentOutOfRangeException>().WithParamName("searchOption");
await That(Act).ThrowsExactly<ArgumentOutOfRangeException>().WithParamName("searchOption").And
.WithMessage("Enum value was out of legal range.*").AsWildcard();
}

#if NET6_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ void Act()
await That(Act).DoesNotThrow();
#else
await That(Act).Throws<ObjectDisposedException>()
.Whose(x => x.Message,
it => it.Satisfies(m
=> m!.Contains("Cannot access a disposed object.", StringComparison.Ordinal) &&
m!.Contains(nameof(ITimer.Change), StringComparison.Ordinal)));
.WithMessage("Cannot access a disposed object.");
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ namespace Testably.Abstractions.Tests.FileSystem.Directory;
[FileSystemTests]
public class SearchFilterTests(FileSystemTestData testData) : FileSystemTestBase(testData)
{
[Test]
public async Task SearchOption_Invalid_ShouldThrowArgumentOutOfRangeException()
{
SearchOption invalidSearchOption = (SearchOption)(-1);

void Act()
{
FileSystem.Directory.GetFileSystemEntries(".", "*", invalidSearchOption);
}

await That(Act).ThrowsExactly<ArgumentOutOfRangeException>()
.WithParamName("searchOption").And
.WithMessage("Enum value was out of legal range.*").AsWildcard();
}

[Test]
[AutoArguments("../", 4)]
[AutoArguments("../*", 4)]
Expand Down Expand Up @@ -155,7 +170,8 @@ void Act()
.GetFileSystemEntries(".", searchPattern, SearchOption.AllDirectories);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891).And
.WithMessage("Access to the path '*' is denied.").AsWildcard();
}

[Test]
Expand Down
18 changes: 16 additions & 2 deletions Tests/Testably.Abstractions.Tests/TimeSystem/TimerFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ void Act()
}

await That(Act).Throws<ArgumentOutOfRangeException>().WithHResult(-2146233086).And
.WithParamName(nameof(dueTime));
.WithParamName(nameof(dueTime)).And
#if NETFRAMEWORK
.WithMessage(
$"Number must be either non-negative and less than or equal to Int32.MaxValue or -1.{Environment.NewLine}Parameter name: dueTime");
#else
.WithMessage(
$"dueTime ('{dueTime}') must be greater than or equal to '-1'. (Parameter 'dueTime'){Environment.NewLine}Actual value was {dueTime}.");
#endif
}

[Test]
Expand All @@ -107,7 +114,14 @@ void Act()
}

await That(Act).Throws<ArgumentOutOfRangeException>().WithHResult(-2146233086).And
.WithParamName(nameof(period));
.WithParamName(nameof(period)).And
#if NETFRAMEWORK
.WithMessage(
$"Number must be either non-negative and less than or equal to Int32.MaxValue or -1.{Environment.NewLine}Parameter name: period");
#else
.WithMessage(
$"period ('{period}') must be greater than or equal to '-1'. (Parameter 'period'){Environment.NewLine}Actual value was {period}.");
#endif
}

[Test]
Expand Down
21 changes: 18 additions & 3 deletions Tests/Testably.Abstractions.Tests/TimeSystem/TimerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ void Act()
_ = timer.Change(100, 200);
}

await That(Act).ThrowsExactly<ObjectDisposedException>();
await That(Act).ThrowsExactly<ObjectDisposedException>()
.WithMessage("Cannot access a disposed object.");
}
#endif

Expand Down Expand Up @@ -103,7 +104,14 @@ void Act()
}

await That(Act).Throws<ArgumentOutOfRangeException>().WithHResult(-2146233086).And
.WithParamName(nameof(dueTime));
.WithParamName(nameof(dueTime)).And
#if NETFRAMEWORK
.WithMessage(
$"Number must be either non-negative and less than or equal to Int32.MaxValue or -1.{Environment.NewLine}Parameter name: dueTime");
#else
.WithMessage(
$"dueTime ('{dueTime}') must be greater than or equal to '-1'. (Parameter 'dueTime'){Environment.NewLine}Actual value was {dueTime}.");
#endif
}

[Test]
Expand All @@ -122,7 +130,14 @@ void Act()
}

await That(Act).Throws<ArgumentOutOfRangeException>().WithHResult(-2146233086).And
.WithParamName(nameof(period));
.WithParamName(nameof(period)).And
#if NETFRAMEWORK
.WithMessage(
$"Number must be either non-negative and less than or equal to Int32.MaxValue or -1.{Environment.NewLine}Parameter name: period");
#else
.WithMessage(
$"period ('{period}') must be greater than or equal to '-1'. (Parameter 'period'){Environment.NewLine}Actual value was {period}.");
#endif
}

[Test]
Expand Down
Loading