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
18 changes: 18 additions & 0 deletions Source/Testably.Abstractions.Testing/FileSystem/FileMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public void AppendAllBytes(string path, byte[] bytes)
throw ExceptionFactory.AccessToPathDenied(path);
}

ThrowIfReadOnly(container, path);

using (container.RequestAccess(
FileAccess.ReadWrite,
FileStreamFactoryMock.DefaultShare))
Expand Down Expand Up @@ -1428,6 +1430,8 @@ public void WriteAllBytes(string path, byte[] bytes)
throw ExceptionFactory.AccessToPathDenied();
}

ThrowIfReadOnly(container, path);

using (container.RequestAccess(
FileAccess.Write,
FileStreamFactoryMock.DefaultShare))
Expand Down Expand Up @@ -1735,6 +1739,18 @@ private IStorageContainer GetWritableContainerFromSafeFileHandle(SafeFileHandle
}
#endif

/// <summary>
/// Opening a read-only file for writing fails on every operating system, so the write methods that bypass
/// <see cref="FileModeHelper" /> throw what a file stream would.
/// </summary>
private void ThrowIfReadOnly(IStorageContainer container, string path)
{
if (container.Attributes.HasFlag(FileAttributes.ReadOnly))
{
throw ExceptionFactory.AccessToPathDenied(_fileSystem.Path.GetFullPath(path));
}
}

private void WriteText(string path, string? contents, Encoding? encoding, bool append = false)
{
IStorageContainer container =
Expand All @@ -1752,6 +1768,8 @@ private void WriteText(string path, string? contents, Encoding? encoding, bool a
throw ExceptionFactory.AccessToPathDenied(path);
}

ThrowIfReadOnly(container, path);

if (contents != null)
{
if (!append &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,22 @@ void Act()

await That(Act).DoesNotThrow();
}

[Test]
[AutoArguments]
public async Task AppendAllBytes_WhenFileIsReadOnly_ShouldThrowUnauthorizedAccessException(
string path, byte[] bytes)
{
FileSystem.File.WriteAllText(path, "foo");
FileSystem.File.SetAttributes(path, FileAttributes.ReadOnly);

void Act()
{
FileSystem.File.AppendAllBytes(path, bytes);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
await That(FileSystem.File.ReadAllText(path)).IsEqualTo("foo");
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoFixture;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

Expand Down Expand Up @@ -107,6 +108,23 @@ void Act()
await That(FileSystem.File.Exists(path)).IsFalse();
}

[Test]
[AutoArguments]
public async Task AppendAllLines_WhenFileIsReadOnly_ShouldThrowUnauthorizedAccessException(
string path, string[] contents)
{
FileSystem.File.WriteAllText(path, "foo");
FileSystem.File.SetAttributes(path, FileAttributes.ReadOnly);

void Act()
{
FileSystem.File.AppendAllLines(path, contents);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
await That(FileSystem.File.ReadAllText(path)).IsEqualTo("foo");
}

[Test]
[MethodDataSource(typeof(TestData), nameof(TestData.GetEncodingDifference))]
public async Task AppendAllLines_WithDifferentEncoding_ShouldNotReturnWrittenText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ void Act()
await That(Act).DoesNotThrow();
}

[Test]
[AutoArguments]
public async Task AppendAllText_WhenFileIsReadOnly_ShouldThrowUnauthorizedAccessException(
string path, string contents)
{
FileSystem.File.WriteAllText(path, "foo");
FileSystem.File.SetAttributes(path, FileAttributes.ReadOnly);
Comment thread
vbreuss marked this conversation as resolved.
Resolved

void Act()
{
FileSystem.File.AppendAllText(path, contents);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
await That(FileSystem.File.ReadAllText(path)).IsEqualTo("foo");
}

[Test]
[MethodDataSource(typeof(TestData), nameof(TestData.GetEncodingDifference))]
public async Task AppendAllText_WithDifferentEncoding_ShouldNotReturnWrittenText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ void Act()
await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
}

[Test]
[AutoArguments]
public async Task WriteAllBytes_WhenFileIsReadOnly_ShouldThrowUnauthorizedAccessException(
string path, byte[] bytes)
{
FileSystem.File.WriteAllText(path, "foo");
FileSystem.File.SetAttributes(path, FileAttributes.ReadOnly);

void Act()
{
FileSystem.File.WriteAllBytes(path, bytes);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
await That(FileSystem.File.ReadAllText(path)).IsEqualTo("foo");
}

#if FEATURE_FILE_SPAN
[Test]
[AutoArguments]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ void Act()
await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
}

[Test]
[AutoArguments]
public async Task WriteAllLines_WhenFileIsReadOnly_ShouldThrowUnauthorizedAccessException(
string path, string[] contents)
{
FileSystem.File.WriteAllText(path, "foo");
FileSystem.File.SetAttributes(path, FileAttributes.ReadOnly);

void Act()
{
FileSystem.File.WriteAllLines(path, contents);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
await That(FileSystem.File.ReadAllText(path)).IsEqualTo("foo");
}

[Test]
[AutoArguments]
public async Task WriteAllLines_WithEncoding_ShouldCreateFileWithText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,40 @@ void Act()
await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
}

[Test]
[AutoArguments]
public async Task WriteAllText_WhenFileIsReadOnlyAndContentIsNull_ShouldThrowUnauthorizedAccessException(
string path)
{
FileSystem.File.WriteAllText(path, "foo");
FileSystem.File.SetAttributes(path, FileAttributes.ReadOnly);

void Act()
{
FileSystem.File.WriteAllText(path, null);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
await That(FileSystem.File.ReadAllText(path)).IsEqualTo("foo");
}

[Test]
[AutoArguments]
public async Task WriteAllText_WhenFileIsReadOnly_ShouldThrowUnauthorizedAccessException(
string path, string contents)
{
FileSystem.File.WriteAllText(path, "foo");
FileSystem.File.SetAttributes(path, FileAttributes.ReadOnly);

void Act()
{
FileSystem.File.WriteAllText(path, contents);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
await That(FileSystem.File.ReadAllText(path)).IsEqualTo("foo");
}

[Test]
public async Task
WriteAllText_WithoutAccessRightsToParentDirectory_ShouldThrowUnauthorizedAccessException()
Expand Down
Loading