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
238 changes: 147 additions & 91 deletions Source/Testably.Abstractions.Testing/FileSystem/FileMock.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Testably.Abstractions.Tests.FileSystem.File;

Expand Down Expand Up @@ -43,16 +42,32 @@ await FileSystem.File.AppendAllLinesAsync(path, contents, Encoding.UTF8,
await That(Act).Throws<TaskCanceledException>().WithHResult(-2146233029);
}

[Theory]
[AutoData]
public async Task AppendAllLinesAsync_Enumerable_WithoutEncoding_ShouldUseUtf8(
string path)
{
string[] contents = ["breuß"];

await FileSystem.File.AppendAllLinesAsync(path, contents.AsEnumerable(),
CancellationToken.None);

byte[] bytes = FileSystem.File.ReadAllBytes(path);
await That(bytes.Length).IsEqualTo(6 + Environment.NewLine.Length);
}

[Theory]
[AutoData]
public async Task AppendAllLinesAsync_ExistingFile_ShouldAppendLinesToFile(
string path, List<string> previousContents, List<string> contents)
{
string expectedContent = string.Join(Environment.NewLine, previousContents.Concat(contents))
+ Environment.NewLine;
await FileSystem.File.AppendAllLinesAsync(path, previousContents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllLinesAsync(path, previousContents,
TestContext.Current.CancellationToken);

await FileSystem.File.AppendAllLinesAsync(path, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllLinesAsync(path, contents,
TestContext.Current.CancellationToken);

await That(FileSystem.File.Exists(path)).IsTrue();
await That(FileSystem.File.ReadAllText(path)).IsEqualTo(expectedContent);
Expand All @@ -67,7 +82,8 @@ public async Task AppendAllLinesAsync_MissingDirectory_ShouldThrowDirectoryNotFo

async Task Act()
{
await FileSystem.File.AppendAllLinesAsync(filePath, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllLinesAsync(filePath, contents,
TestContext.Current.CancellationToken);
}

await That(Act).Throws<DirectoryNotFoundException>().WithHResult(-2147024893);
Expand All @@ -81,7 +97,8 @@ public async Task AppendAllLinesAsync_MissingFile_ShouldCreateFile(
string expectedContent = string.Join(Environment.NewLine, contents)
+ Environment.NewLine;

await FileSystem.File.AppendAllLinesAsync(path, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllLinesAsync(path, contents,
TestContext.Current.CancellationToken);

await That(FileSystem.File.Exists(path)).IsTrue();
await That(FileSystem.File.ReadAllText(path)).IsEqualTo(expectedContent);
Expand All @@ -94,7 +111,8 @@ public async Task AppendAllLinesAsync_NullContent_ShouldThrowArgumentNullExcepti
{
async Task Act()
{
await FileSystem.File.AppendAllLinesAsync(path, null!, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllLinesAsync(path, null!,
TestContext.Current.CancellationToken);
}

await That(Act).Throws<ArgumentNullException>()
Expand All @@ -109,7 +127,8 @@ public async Task AppendAllLinesAsync_NullEncoding_ShouldThrowArgumentNullExcept
{
async Task Act()
{
await FileSystem.File.AppendAllLinesAsync(path, [], null!, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllLinesAsync(path, [], null!,
TestContext.Current.CancellationToken);
}

await That(Act).Throws<ArgumentNullException>()
Expand All @@ -124,7 +143,8 @@ public async Task AppendAllLinesAsync_ShouldEndWithNewline(string path)
string[] contents = ["foo", "bar"];
string expectedResult = "foo" + Environment.NewLine + "bar" + Environment.NewLine;

await FileSystem.File.AppendAllLinesAsync(path, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllLinesAsync(path, contents,
TestContext.Current.CancellationToken);

await That(FileSystem.File.Exists(path)).IsTrue();
await That(FileSystem.File.ReadAllText(path)).IsEqualTo(expectedResult);
Expand All @@ -140,7 +160,8 @@ public async Task

async Task Act()
{
await FileSystem.File.AppendAllLinesAsync(path, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllLinesAsync(path, contents,
TestContext.Current.CancellationToken);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
Expand All @@ -157,7 +178,8 @@ public async Task
string path = new Fixture().Create<string>();
string[] lines = new Fixture().Create<string[]>();
lines[1] = specialLine;
await FileSystem.File.AppendAllLinesAsync(path, lines, writeEncoding, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllLinesAsync(path, lines, writeEncoding,
TestContext.Current.CancellationToken);

string[] result = FileSystem.File.ReadAllLines(path, readEncoding);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ namespace Testably.Abstractions.Tests.FileSystem.File;
[FileSystemTests]
public partial class AppendAllLinesTests
{
[Theory]
[AutoData]
public async Task AppendAllLines_Enumerable_WithoutEncoding_ShouldUseUtf8(
string path)
{
string[] contents = ["breuß"];

FileSystem.File.AppendAllLines(path, contents.AsEnumerable());

byte[] bytes = FileSystem.File.ReadAllBytes(path);
await That(bytes.Length).IsEqualTo(6 + Environment.NewLine.Length);
}

[Theory]
[AutoData]
public async Task AppendAllLines_ExistingFile_ShouldAppendLinesToFile(
Expand Down Expand Up @@ -112,4 +125,17 @@ public async Task AppendAllLines_WithDifferentEncoding_ShouldNotReturnWrittenTex
await That(result).IsNotEqualTo(lines).InAnyOrder();
await That(result[0]).IsEqualTo(lines[0]);
}

[Theory]
[AutoData]
public async Task AppendAllLines_WithoutEncoding_ShouldUseUtf8(
string path)
{
string[] contents = ["breuß"];

FileSystem.File.AppendAllLines(path, contents);

byte[] bytes = FileSystem.File.ReadAllBytes(path);
await That(bytes.Length).IsEqualTo(6 + Environment.NewLine.Length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ async Task Act() =>
public async Task AppendAllTextAsync_ExistingFile_ShouldAppendLinesToFile(
string path, string previousContents, string contents)
{
await FileSystem.File.AppendAllTextAsync(path, previousContents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, previousContents,
TestContext.Current.CancellationToken);

await FileSystem.File.AppendAllTextAsync(path, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents,
TestContext.Current.CancellationToken);

await That(FileSystem.File.Exists(path)).IsTrue();
await That(FileSystem.File.ReadAllText(path)).IsEqualTo(previousContents + contents);
Expand All @@ -61,7 +63,8 @@ public async Task AppendAllTextAsync_MissingDirectory_ShouldThrowDirectoryNotFou

async Task Act()
{
await FileSystem.File.AppendAllTextAsync(filePath, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(filePath, contents,
TestContext.Current.CancellationToken);
}

await That(Act).Throws<DirectoryNotFoundException>().WithHResult(-2147024893);
Expand All @@ -72,7 +75,8 @@ async Task Act()
public async Task AppendAllTextAsync_MissingFile_ShouldCreateFile(
string path, string contents)
{
await FileSystem.File.AppendAllTextAsync(path, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents,
TestContext.Current.CancellationToken);

await That(FileSystem.File.Exists(path)).IsTrue();
await That(FileSystem.File.ReadAllText(path)).IsEqualTo(contents);
Expand All @@ -84,7 +88,8 @@ public async Task AppendAllTextAsync_ShouldNotEndWithNewline(string path)
{
string contents = "foo";

await FileSystem.File.AppendAllTextAsync(path, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents,
TestContext.Current.CancellationToken);

await That(FileSystem.File.Exists(path)).IsTrue();
await That(FileSystem.File.ReadAllText(path)).IsEqualTo(contents);
Expand All @@ -100,7 +105,8 @@ public async Task

async Task Act()
{
await FileSystem.File.AppendAllTextAsync(path, contents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents,
TestContext.Current.CancellationToken);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
Expand All @@ -114,13 +120,27 @@ public async Task AppendAllTextAsync_WithDifferentEncoding_ShouldNotReturnWritte
string contents, Encoding writeEncoding, Encoding readEncoding)
{
string path = new Fixture().Create<string>();
await FileSystem.File.AppendAllTextAsync(path, contents, writeEncoding, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents, writeEncoding,
TestContext.Current.CancellationToken);

string[] result = FileSystem.File.ReadAllLines(path, readEncoding);

await That(result).IsNotEqualTo([contents]);
}

[Theory]
[AutoData]
public async Task AppendAllTextAsync_WithoutEncoding_ShouldUseUtf8(
string path)
{
string contents = "breuß";

await FileSystem.File.AppendAllTextAsync(path, contents, CancellationToken.None);

byte[] bytes = FileSystem.File.ReadAllBytes(path);
await That(bytes.Length).IsEqualTo(6);
}

#if FEATURE_FILE_SPAN
[Theory]
[AutoData]
Expand All @@ -146,7 +166,8 @@ public async Task
cts.Cancel();

async Task Act() =>
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(), Encoding.UTF8, cts.Token);
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(), Encoding.UTF8,
cts.Token);

await That(Act).Throws<TaskCanceledException>().WithHResult(-2146233029);
}
Expand All @@ -156,24 +177,28 @@ async Task Act() =>
public async Task AppendAllTextAsync_ReadOnlyMemory_ExistingFile_ShouldAppendLinesToFile(
string path, string previousContents, string contents)
{
await FileSystem.File.AppendAllTextAsync(path, previousContents, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, previousContents,
TestContext.Current.CancellationToken);

await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(), TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(),
TestContext.Current.CancellationToken);

await That(FileSystem.File.Exists(path)).IsTrue();
await That(FileSystem.File.ReadAllText(path)).IsEqualTo(previousContents + contents);
}

[Theory]
[AutoData]
public async Task AppendAllTextAsync_ReadOnlyMemory_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string missingPath, string fileName, string contents)
public async Task
AppendAllTextAsync_ReadOnlyMemory_MissingDirectory_ShouldThrowDirectoryNotFoundException(
string missingPath, string fileName, string contents)
{
string filePath = FileSystem.Path.Combine(missingPath, fileName);

async Task Act()
{
await FileSystem.File.AppendAllTextAsync(filePath, contents.AsMemory(), TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(filePath, contents.AsMemory(),
TestContext.Current.CancellationToken);
}

await That(Act).Throws<DirectoryNotFoundException>().WithHResult(-2147024893);
Expand All @@ -184,7 +209,8 @@ async Task Act()
public async Task AppendAllTextAsync_ReadOnlyMemory_MissingFile_ShouldCreateFile(
string path, string contents)
{
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(), TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(),
TestContext.Current.CancellationToken);

await That(FileSystem.File.Exists(path)).IsTrue();
await That(FileSystem.File.ReadAllText(path)).IsEqualTo(contents);
Expand All @@ -196,7 +222,8 @@ public async Task AppendAllTextAsync_ReadOnlyMemory_ShouldNotEndWithNewline(stri
{
string contents = "foo";

await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(), TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(),
TestContext.Current.CancellationToken);

await That(FileSystem.File.Exists(path)).IsTrue();
await That(FileSystem.File.ReadAllText(path)).IsEqualTo(contents);
Expand All @@ -212,7 +239,8 @@ public async Task

async Task Act()
{
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(), TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(),
TestContext.Current.CancellationToken);
}

await That(Act).Throws<UnauthorizedAccessException>().WithHResult(-2147024891);
Expand All @@ -222,16 +250,31 @@ async Task Act()

[Theory]
[ClassData(typeof(TestDataGetEncodingDifference))]
public async Task AppendAllTextAsync_ReadOnlyMemory_WithDifferentEncoding_ShouldNotReturnWrittenText(
string contents, Encoding writeEncoding, Encoding readEncoding)
public async Task
AppendAllTextAsync_ReadOnlyMemory_WithDifferentEncoding_ShouldNotReturnWrittenText(
string contents, Encoding writeEncoding, Encoding readEncoding)
{
string path = new Fixture().Create<string>();
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(), writeEncoding, TestContext.Current.CancellationToken);
await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(), writeEncoding,
TestContext.Current.CancellationToken);

string[] result = FileSystem.File.ReadAllLines(path, readEncoding);

await That(result).IsNotEqualTo([contents]);
}

[Theory]
[AutoData]
public async Task AppendAllTextAsync_ReadOnlyMemory_WithoutEncoding_ShouldUseUtf8(
string path)
{
string contents = "breuß";

await FileSystem.File.AppendAllTextAsync(path, contents.AsMemory(), CancellationToken.None);

byte[] bytes = FileSystem.File.ReadAllBytes(path);
await That(bytes.Length).IsEqualTo(6);
}
#endif
}
#endif
Loading
Loading