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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<PackageVersion Include="xunit" Version="2.5.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="NSubstitute" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<_Parameter1>true</_Parameter1>
<_Parameter1_TypeName>System.Boolean</_Parameter1_TypeName>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Testably.Abstractions.AccessControl.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Testably.Abstractions.Internal;

internal static class ZipUtilities
{
private const string SearchPattern = "*";

internal static IZipArchiveEntry CreateEntryFromFile(
IZipArchive destination,
string sourceFileName,
Expand All @@ -28,7 +30,7 @@ internal static IZipArchiveEntry CreateEntryFromFile(
DateTime lastWrite =
destination.FileSystem.File.GetLastWriteTime(sourceFileName);

if (lastWrite.Year < 1980 || lastWrite.Year > 2107)
if (lastWrite.Year is < 1980 or > 2107)
{
lastWrite = new DateTime(1980, 1, 1, 0, 0, 0);
}
Expand Down Expand Up @@ -79,7 +81,7 @@ internal static void CreateFromDirectory(IFileSystem fileSystem,
}

foreach (IFileSystemInfo file in di
.EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
.EnumerateFileSystemInfos(SearchPattern, SearchOption.AllDirectories))
{
directoryIsEmpty = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
<ProjectReference Include="..\Testably.Abstractions.Interface\Testably.Abstractions.Interface.csproj" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Testably.Abstractions.Compression.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,15 @@ internal IReadOnlyList<IStorageContainer> GetContainers()
.Select(x => x.Value)
.ToList();

/// <summary>
/// Removes the drive with the given <paramref name="driveName" />.
/// </summary>
internal IStorageDrive? RemoveDrive(string driveName)
{
_drives.TryRemove(driveName, out IStorageDrive? drive);
return drive;
}

private void CheckAndAdjustParentDirectoryTimes(IStorageLocation location)
{
IStorageContainer? parentContainer = GetContainer(location.GetParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ internal static class LocationExtensions
{
[return: NotNullIfNotNull("location")]
public static IStorageLocation? ThrowIfNotFound(
this IStorageLocation? location, MockFileSystem fileSystem,
this IStorageLocation? location,
MockFileSystem fileSystem,
Action fileNotFoundException,
Action? directoryNotFoundException = null)
{
Expand Down Expand Up @@ -38,7 +39,8 @@ internal static class LocationExtensions
}

public static IStorageLocation ThrowExceptionIfNotFound(
this IStorageLocation location, MockFileSystem fileSystem,
this IStorageLocation location,
MockFileSystem fileSystem,
bool allowMissingFile = false,
Func<string, Exception>? onDirectoryNotFound = null,
Func<string, Exception>? onFileNotFound = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.IO;

namespace Testably.Abstractions.AccessControl.Tests;

// ReSharper disable once PartialTypeWithSinglePart
public abstract partial class AccessControlHelperTests<TFileSystem>
: FileSystemTestBase<TFileSystem>
where TFileSystem : IFileSystem
{
[Fact]
public void GetExtensibilityOrThrow_DirectoryInfo_ShouldNotThrow()
{
IDirectoryInfo sut = FileSystem.DirectoryInfo.New("foo");

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

exception.Should().BeNull();
}

[Fact]
public void GetExtensibilityOrThrow_FileSystemStream_ShouldNotThrow()
{
FileSystemStream sut = FileSystem.FileStream.New("foo", FileMode.Create);

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

exception.Should().BeNull();
}

[Fact]
public void GetExtensibilityOrThrow_FileInfo_ShouldNotThrow()
{
IFileInfo sut = FileSystem.FileInfo.New("foo");

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

exception.Should().BeNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using NSubstitute;
using System.IO;
using Testably.Abstractions.Helpers;

namespace Testably.Abstractions.AccessControl.Tests.Internal;

public sealed class AccessControlHelperTests
{
[Fact]
public void ThrowIfMissing_MissingDirectoryInfo_ShouldThrowDirectoryNotFoundException()
{
MockFileSystem fileSystem = new();
IDirectoryInfo sut = fileSystem.DirectoryInfo.New("foo");

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

exception.Should().BeOfType<DirectoryNotFoundException>()
.Which.HResult.Should().Be(-2147024893);
exception!.Message.Should().Contain($"'{sut.FullName}'");
}

[Fact]
public void ThrowIfMissing_ExistingDirectoryInfo_ShouldNotThrow()
{
MockFileSystem fileSystem = new();
IDirectoryInfo sut = fileSystem.DirectoryInfo.New("foo");
fileSystem.Directory.CreateDirectory("foo");

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

exception.Should().BeNull();
}

[Fact]
public void ThrowIfMissing_MissingFileInfo_ShouldThrowFileNotFoundException()
{
MockFileSystem fileSystem = new();
IFileInfo sut = fileSystem.FileInfo.New("foo");

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

exception.Should().BeOfType<FileNotFoundException>()
.Which.HResult.Should().Be(-2147024894);
exception!.Message.Should().Contain($"'{sut.FullName}'");
}

[Fact]
public void ThrowIfMissing_ExistingFileInfo_ShouldNotThrow()
{
MockFileSystem fileSystem = new();
IFileInfo sut = fileSystem.FileInfo.New("foo");
fileSystem.File.WriteAllText("foo", "some content");

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

exception.Should().BeNull();
}

[Fact]
public void GetExtensibilityOrThrow_CustomDirectoryInfo_ShouldThrowNotSupportedException()
{
IDirectoryInfo? sut = Substitute.For<IDirectoryInfo>();

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

exception.Should().BeOfType<NotSupportedException>()
.Which.Message.Should()
.Contain(nameof(IFileSystemExtensibility)).And
.Contain(sut.GetType().Name);
}

[Fact]
public void GetExtensibilityOrThrow_CustomFileSystemStream_ShouldThrowNotSupportedException()
{
FileSystemStream sut = new CustomFileSystemStream();

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

exception.Should().BeOfType<NotSupportedException>()
.Which.Message.Should()
.Contain(nameof(IFileSystemExtensibility)).And
.Contain(sut.GetType().Name);
}

[Fact]
public void GetExtensibilityOrThrow_CustomFileInfo_ShouldThrowNotSupportedException()
{
IFileInfo? sut = Substitute.For<IFileInfo>();

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

exception.Should().BeOfType<NotSupportedException>()
.Which.Message.Should()
.Contain(nameof(IFileSystemExtensibility)).And
.Contain(sut.GetType().Name);
}

private class CustomFileSystemStream : FileSystemStream
{
/// <inheritdoc />
public CustomFileSystemStream() : base(Null, ".", false)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference
Include="..\Helpers\Testably.Abstractions.Tests.SourceGenerator\Testably.Abstractions.Tests.SourceGenerator.csproj"
OutputItemType="Analyzer" />
<ProjectReference Include="..\Helpers\Testably.Abstractions.Tests.SourceGenerator\Testably.Abstractions.Tests.SourceGenerator.csproj" OutputItemType="Analyzer" />
<PackageReference Include="NSubstitute" />
</ItemGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Testably.Abstractions.Internal;

namespace Testably.Abstractions.Compression.Tests.Internal;

public sealed class ExecuteTests
{
[Fact]
public void WhenRealFileSystem_MockFileSystem_ShouldExecuteOnMockFileSystem()
{
bool onRealFileSystemExecuted = false;
bool onMockFileSystemExecuted = false;
MockFileSystem fileSystem = new();
Execute.WhenRealFileSystem(fileSystem,
() => onRealFileSystemExecuted = true,
() => onMockFileSystemExecuted = true);

onRealFileSystemExecuted.Should().BeFalse();
onMockFileSystemExecuted.Should().BeTrue();
}

[Fact]
public void WhenRealFileSystem_RealFileSystem_ShouldExecuteOnRealFileSystem()
{
bool onRealFileSystemExecuted = false;
bool onMockFileSystemExecuted = false;
RealFileSystem fileSystem = new();
Execute.WhenRealFileSystem(fileSystem,
() => onRealFileSystemExecuted = true,
() => onMockFileSystemExecuted = true);

onRealFileSystemExecuted.Should().BeTrue();
onMockFileSystemExecuted.Should().BeFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,37 @@ public void ExtractToFile_WithOverwrite_ShouldOverwriteExistingFile()
.Should().Be("FooFooFoo");
}


[SkippableTheory]
[InlineData("2000-01-01T12:14:15")]
[InlineData("1980-01-01T00:00:00")]
[InlineData("2107-12-31T23:59:59")]
public void ExtractToFile_LastWriteTime_ShouldBeCopiedFromFile(string lastWriteTimeString)
{
DateTime lastWriteTime = DateTime.Parse(lastWriteTimeString);
FileSystem.Initialize()
.WithSubdirectory("foo")
.WithSubdirectory("bar").Initialized(s => s
.WithFile("bar.txt"));
FileSystem.File.WriteAllText("bar/foo.txt", "FooFooFoo");
FileSystem.File.SetLastWriteTime("bar/foo.txt", lastWriteTime);
FileSystem.ZipFile()
.CreateFromDirectory("foo", "destination.zip", CompressionLevel.NoCompression,
false);
using FileSystemStream stream = FileSystem.File.Open("destination.zip",
FileMode.Open, FileAccess.ReadWrite);
IZipArchive archive = FileSystem.ZipArchive().New(stream, ZipArchiveMode.Update);
archive.CreateEntryFromFile("bar/foo.txt", "foo/bar.txt",
CompressionLevel.NoCompression);
IZipArchiveEntry entry = archive.Entries.Single();

entry.ExtractToFile("bar/bar.txt", true);

FileSystem.File.ReadAllText("bar/bar.txt")
.Should().Be("FooFooFoo");
FileSystem.FileInfo.New("bar/bar.txt").LastWriteTime.Should().Be(lastWriteTime);
}

[SkippableFact]
public void ExtractToFile_IncorrectEntryType_ShouldThrowIOException()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#if NET6_0_OR_GREATER
#endif

namespace Testably.Abstractions.Testing.Tests;

public sealed class FileSystemInitializerOptionsTests
{
[Fact]
public void InitializeTempDirectory_ShouldBeInitializedToTrue()
{
FileSystemInitializerOptions sut = new();

sut.InitializeTempDirectory.Should().BeTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Storage;

namespace Testably.Abstractions.Testing.Tests;

public class MockFileSystemExtensionsTests
{
[Fact]
public void GetDefaultDrive_WithoutDrives_ShouldThrowInvalidOperationException()
{
MockFileSystem fileSystem = new();
(fileSystem.Storage as InMemoryStorage)?.RemoveDrive(string.Empty.PrefixRoot());

Exception? exception = Record.Exception(() =>
{
fileSystem.GetDefaultDrive();
});

exception.Should().BeOfType<InvalidOperationException>();
}
}
Loading