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
11 changes: 7 additions & 4 deletions Examples/ZipFile/ZipFile/ZipFileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ public void ExtractZipToDirectory(Stream stream, string directory)
{
_fileSystem.Directory.CreateDirectory(directoryPath);
}

using MemoryStream ms = new();
entry.Open().CopyTo(ms);
_fileSystem.File.WriteAllBytes(filePath, ms.ToArray());

if (!entry.FullName.EndsWith("/"))
{
using MemoryStream ms = new();
entry.Open().CopyTo(ms);
_fileSystem.File.WriteAllBytes(filePath, ms.ToArray());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public void Create()
InMemoryContainer.NewDirectory,
this);

if (Container.Type != FileSystemTypes.Directory)
{
throw ExceptionFactory.CannotCreateFileAsAlreadyExists(FullName);
}

ResetCache(!Execute.IsNetFramework);
}

Expand Down
57 changes: 41 additions & 16 deletions Source/Testably.Abstractions.Testing/FileSystem/FileMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#if FEATURE_FILESYSTEM_ASYNC
using System.Threading;
using System.Threading.Tasks;

#endif

namespace Testably.Abstractions.Testing.FileSystem;
Expand Down Expand Up @@ -73,23 +74,29 @@ public void AppendAllText(string path, string? contents)
/// <inheritdoc cref="IFile.AppendAllText(string, string?, Encoding)" />
public void AppendAllText(string path, string? contents, Encoding encoding)
{
IStorageContainer fileInfo =
IStorageContainer container =
_fileSystem.Storage.GetOrCreateContainer(
_fileSystem.Storage.GetLocation(
path.EnsureValidFormat(FileSystem)),
InMemoryContainer.NewFile);

if (container.Type != FileSystemTypes.File)
{
throw ExceptionFactory.AccessToPathDenied(path);
}

if (contents != null)
{
using (fileInfo.RequestAccess(
using (container.RequestAccess(
FileAccess.ReadWrite,
FileStreamFactoryMock.DefaultShare))
{
if (fileInfo.GetBytes().Length == 0)
if (container.GetBytes().Length == 0)
{
fileInfo.WriteBytes(encoding.GetPreamble());
container.WriteBytes(encoding.GetPreamble());
}

fileInfo.AppendBytes(encoding.GetBytes(contents));
container.AppendBytes(encoding.GetBytes(contents));
}
}
}
Expand Down Expand Up @@ -786,17 +793,25 @@ public void WriteAllBytes(string path, byte[] bytes)
_fileSystem.Storage.GetLocation(
path.EnsureValidFormat(FileSystem)),
InMemoryContainer.NewFile);
if (container is not NullContainer)

if (container is NullContainer)
{
Execute.OnWindowsIf(
container.Attributes.HasFlag(FileAttributes.Hidden),
() => throw ExceptionFactory.AccessToPathDenied());
using (container.RequestAccess(
FileAccess.Write,
FileStreamFactoryMock.DefaultShare))
{
container.WriteBytes(bytes);
}
return;
}

if (container.Type != FileSystemTypes.File)
{
throw ExceptionFactory.AccessToPathDenied(path);
}

Execute.OnWindowsIf(
container.Attributes.HasFlag(FileAttributes.Hidden),
() => throw ExceptionFactory.AccessToPathDenied());
using (container.RequestAccess(
FileAccess.Write,
FileStreamFactoryMock.DefaultShare))
{
container.WriteBytes(bytes);
}
}

Expand Down Expand Up @@ -869,7 +884,17 @@ public void WriteAllText(string path, string? contents, Encoding encoding)
_fileSystem.Storage.GetLocation(
path.EnsureValidFormat(FileSystem)),
InMemoryContainer.NewFile);
if (container is not NullContainer && contents != null)
if (container is NullContainer)
{
return;
}

if (container.Type != FileSystemTypes.File)
{
throw ExceptionFactory.AccessToPathDenied(path);
}

if (contents != null)
{
Execute.OnWindowsIf(
container.Attributes.HasFlag(FileAttributes.Hidden),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ public void CreateDirectory_ReadOnlyParent_ShouldStillCreateDirectory(string par
}
}

[SkippableTheory]
[AutoData]
public void CreateDirectory_FileWithSameNameAlreadyExists_ShouldThrowIOException(string name)
{
FileSystem.File.WriteAllText(name, "");

Exception? exception = Record.Exception(() =>
{
FileSystem.Directory.CreateDirectory(name);
});

exception.Should().BeException<IOException>(
hResult: Test.RunsOnWindows ? -2147024713 : 17);
FileSystem.Directory.Exists(name).Should().BeFalse();
}

[SkippableFact]
public void CreateDirectory_Root_ShouldNotThrowException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
using System.IO;

namespace Testably.Abstractions.Tests.FileSystem.DirectoryInfo;

// ReSharper disable once PartialTypeWithSinglePart
public abstract partial class CreateSubdirectoryTests<TFileSystem>
: FileSystemTestBase<TFileSystem>
where TFileSystem : IFileSystem
{
[SkippableTheory]
[AutoData]
public void CreateSubdirectory_FileWithSameNameAlreadyExists_ShouldThrowIOException(
string name)
{
FileSystem.File.WriteAllText(name, "");
IDirectoryInfo sut = FileSystem.DirectoryInfo.New(".");

Exception? exception = Record.Exception(() =>
{
sut.CreateSubdirectory(name);
});

exception.Should().BeException<IOException>(
hResult: Test.RunsOnWindows ? -2147024713 : 17);
FileSystem.Directory.Exists(name).Should().BeFalse();
}

[SkippableTheory]
[AutoData]
public void CreateSubdirectory_MissingParent_ShouldCreateDirectory(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
using System.IO;

namespace Testably.Abstractions.Tests.FileSystem.DirectoryInfo;

// ReSharper disable once PartialTypeWithSinglePart
public abstract partial class CreateTests<TFileSystem>
: FileSystemTestBase<TFileSystem>
where TFileSystem : IFileSystem
{
[SkippableTheory]
[AutoData]
public void Create_FileWithSameNameAlreadyExists_ShouldThrowIOException(string name)
{
FileSystem.File.WriteAllText(name, "");
IDirectoryInfo sut = FileSystem.DirectoryInfo.New(name);

Exception? exception = Record.Exception(() =>
{
sut.Create();
});

exception.Should().BeException<IOException>(
hResult: Test.RunsOnWindows ? -2147024713 : 17);
FileSystem.Directory.Exists(name).Should().BeFalse();
}

[SkippableTheory]
[AutoData]
public void Create_ShouldCreateDirectory(string path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ public async Task AppendAllLinesAsync_ShouldEndWithNewline(string path)
FileSystem.File.ReadAllText(path).Should().BeEquivalentTo(expectedResult);
}

[SkippableTheory]
[AutoData]
public async Task
AppendAllLinesAsync_WhenDirectoryWithSameNameExists_ShouldThrowUnauthorizedAccessException(
string path, string[] contents)
{
FileSystem.Directory.CreateDirectory(path);

Exception? exception = await Record.ExceptionAsync(async () =>
{
await FileSystem.File.AppendAllLinesAsync(path, contents);
});

exception.Should().BeException<UnauthorizedAccessException>(
hResult: -2147024891);
FileSystem.Directory.Exists(path).Should().BeTrue();
FileSystem.File.Exists(path).Should().BeFalse();
}

[SkippableTheory]
[ClassData(typeof(TestDataGetEncodingDifference))]
public async Task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ public void AppendAllLines_ShouldEndWithNewline(string path)
FileSystem.File.ReadAllText(path).Should().BeEquivalentTo(expectedResult);
}

[SkippableTheory]
[AutoData]
public void
AppendAllLines_WhenDirectoryWithSameNameExists_ShouldThrowUnauthorizedAccessException(
string path, string[] contents)
{
FileSystem.Directory.CreateDirectory(path);

Exception? exception = Record.Exception(() =>
{
FileSystem.File.AppendAllLines(path, contents);
});

exception.Should().BeException<UnauthorizedAccessException>(
hResult: -2147024891);
FileSystem.Directory.Exists(path).Should().BeTrue();
FileSystem.File.Exists(path).Should().BeFalse();
}

[SkippableTheory]
[ClassData(typeof(TestDataGetEncodingDifference))]
public void AppendAllLines_WithDifferentEncoding_ShouldNotReturnWrittenText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ public async Task AppendAllTextAsync_ShouldNotEndWithNewline(string path)
FileSystem.File.ReadAllText(path).Should().BeEquivalentTo(contents);
}

[SkippableTheory]
[AutoData]
public async Task
AppendAllTextAsync_WhenDirectoryWithSameNameExists_ShouldThrowUnauthorizedAccessException(
string path, string contents)
{
FileSystem.Directory.CreateDirectory(path);

Exception? exception = await Record.ExceptionAsync(async () =>
{
await FileSystem.File.AppendAllTextAsync(path, contents);
});

exception.Should().BeException<UnauthorizedAccessException>(
hResult: -2147024891);
FileSystem.Directory.Exists(path).Should().BeTrue();
FileSystem.File.Exists(path).Should().BeFalse();
}

[SkippableTheory]
[ClassData(typeof(TestDataGetEncodingDifference))]
public async Task AppendAllTextAsync_WithDifferentEncoding_ShouldNotReturnWrittenText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,22 @@ public void AppendAllText_ShouldNotEndWithNewline(string path)
}

[SkippableTheory]
[ClassData(typeof(TestDataGetEncodingDifference))]
public void AppendAllText_WithDifferentEncoding_ShouldNotReturnWrittenText(
string contents, Encoding writeEncoding, Encoding readEncoding)
[AutoData]
public void
AppendAllText_WhenDirectoryWithSameNameExists_ShouldThrowUnauthorizedAccessException(
string path)
{
string path = new Fixture().Create<string>();
FileSystem.File.AppendAllText(path, contents, writeEncoding);
FileSystem.Directory.CreateDirectory(path);

string[] result = FileSystem.File.ReadAllLines(path, readEncoding);
Exception? exception = Record.Exception(() =>
{
FileSystem.File.AppendAllText(path, null);
});

result.Should().NotBeEquivalentTo(contents,
$"{contents} should be different when encoding from {writeEncoding} to {readEncoding}.");
exception.Should().BeException<UnauthorizedAccessException>(
hResult: -2147024891);
FileSystem.Directory.Exists(path).Should().BeTrue();
FileSystem.File.Exists(path).Should().BeFalse();
}

[SkippableTheory]
Expand All @@ -152,4 +157,18 @@ public void AppendAllText_WhenFileIsHidden_ShouldNotThrowException(

exception.Should().BeNull();
}

[SkippableTheory]
[ClassData(typeof(TestDataGetEncodingDifference))]
public void AppendAllText_WithDifferentEncoding_ShouldNotReturnWrittenText(
string contents, Encoding writeEncoding, Encoding readEncoding)
{
string path = new Fixture().Create<string>();
FileSystem.File.AppendAllText(path, contents, writeEncoding);

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

result.Should().NotBeEquivalentTo(contents,
$"{contents} should be different when encoding from {writeEncoding} to {readEncoding}.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ public async Task WriteAllBytesAsync_WhenBytesAreNull_ShouldThrowArgumentNullExc
exception.Should().BeException<ArgumentNullException>(paramName: "bytes");
}

[SkippableTheory]
[AutoData]
public async Task
WriteAllBytesAsync_WhenDirectoryWithSameNameExists_ShouldThrowUnauthorizedAccessException(
string path, byte[] bytes)
{
FileSystem.Directory.CreateDirectory(path);

Exception? exception = await Record.ExceptionAsync(async () =>
{
await FileSystem.File.WriteAllBytesAsync(path, bytes);
});

exception.Should().BeException<UnauthorizedAccessException>(
hResult: -2147024891);
FileSystem.Directory.Exists(path).Should().BeTrue();
FileSystem.File.Exists(path).Should().BeFalse();
}

[SkippableTheory]
[AutoData]
public async Task
Expand Down
Loading