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 @@ -108,6 +108,40 @@ public static void InitializeEmbeddedResourcesFromAssembly(this IFileSystem file
}
#pragma warning restore MA0051 // Method is too long

/// <summary>
/// Initializes the <see cref="IFileSystem" /> from the real <paramref name="sourceDirectory" /> into the
/// <paramref name="targetDirectory" /> on the provided file system.
/// </summary>
/// <param name="fileSystem">The target file system to which the data is copied.</param>
/// <param name="sourceDirectory">
/// The source directory on the real file system that is copied to the
/// <paramref name="fileSystem" />.
/// </param>
/// <param name="targetDirectory">
/// The target directory on the <paramref name="fileSystem" /> to which the data is copied to.<br />
/// If no <paramref name="targetDirectory" /> is set, <paramref name="sourceDirectory" /> is used instead.
/// </param>
/// <remarks>
/// <b>Warning</b>:<br />
/// This method will recursively copy the content of all files and directories from the
/// <paramref name="sourceDirectory" /> to the <paramref name="targetDirectory" />. With large files, this can be very
/// resource intensive!<br />
/// </remarks>
public static void InitializeFromRealDirectory(this IFileSystem fileSystem,
string sourceDirectory, string? targetDirectory = null)
{
using IDisposable release = fileSystem.IgnoreStatistics();
targetDirectory ??= sourceDirectory;
if (fileSystem.Path.IsPathRooted(targetDirectory) &&
fileSystem is MockFileSystem mockFileSystem)
{
string? drive = fileSystem.Path.GetPathRoot(targetDirectory);
mockFileSystem.WithDrive(drive);
}

CopyDirectory(fileSystem, sourceDirectory, targetDirectory);
}

/// <summary>
/// Initializes the <see cref="IFileSystem" /> in the <paramref name="basePath" /> with test data.
/// </summary>
Expand Down Expand Up @@ -158,6 +192,31 @@ public static IDirectoryCleaner SetCurrentDirectoryToEmptyTemporaryDirectory(
return new DirectoryCleaner(fileSystem, prefix, logger);
}

private static void CopyDirectory(
IFileSystem fileSystem, string sourceDirectory, string targetDirectory)
{
if (!Directory.Exists(sourceDirectory))
{
throw new DirectoryNotFoundException(
$"The directory '{sourceDirectory}' does not exist.");
}

fileSystem.Directory.CreateDirectory(targetDirectory);
foreach (string file in Directory.EnumerateFiles(sourceDirectory))
{
string fileName = Path.GetFileName(file);
fileSystem.File.WriteAllBytes(fileSystem.Path.Combine(targetDirectory, fileName),
File.ReadAllBytes(file));
}

foreach (string directory in Directory.EnumerateDirectories(sourceDirectory))
{
string directoryName = Path.GetFileName(directory);
CopyDirectory(fileSystem, directory,
fileSystem.Path.Combine(targetDirectory, directoryName));
}
}

private static void InitializeFileFromEmbeddedResource(this IFileSystem fileSystem,
string path,
Assembly assembly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> Initialize<TFileSystem>(this TFileSystem fileSystem, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static void InitializeEmbeddedResourcesFromAssembly(this System.IO.Abstractions.IFileSystem fileSystem, string directoryPath, System.Reflection.Assembly assembly, string? relativePath = null, string searchPattern = "*", System.IO.SearchOption searchOption = 1) { }
public static void InitializeFromRealDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string sourceDirectory, string? targetDirectory = null) { }
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> InitializeIn<TFileSystem>(this TFileSystem fileSystem, string basePath, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static Testably.Abstractions.Testing.Initializer.IDirectoryCleaner SetCurrentDirectoryToEmptyTemporaryDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string? prefix = null, System.Action<string>? logger = null) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> Initialize<TFileSystem>(this TFileSystem fileSystem, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static void InitializeEmbeddedResourcesFromAssembly(this System.IO.Abstractions.IFileSystem fileSystem, string directoryPath, System.Reflection.Assembly assembly, string? relativePath = null, string searchPattern = "*", System.IO.SearchOption searchOption = 1) { }
public static void InitializeFromRealDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string sourceDirectory, string? targetDirectory = null) { }
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> InitializeIn<TFileSystem>(this TFileSystem fileSystem, string basePath, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static Testably.Abstractions.Testing.Initializer.IDirectoryCleaner SetCurrentDirectoryToEmptyTemporaryDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string? prefix = null, System.Action<string>? logger = null) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> Initialize<TFileSystem>(this TFileSystem fileSystem, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static void InitializeEmbeddedResourcesFromAssembly(this System.IO.Abstractions.IFileSystem fileSystem, string directoryPath, System.Reflection.Assembly assembly, string? relativePath = null, string searchPattern = "*", System.IO.SearchOption searchOption = 1) { }
public static void InitializeFromRealDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string sourceDirectory, string? targetDirectory = null) { }
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> InitializeIn<TFileSystem>(this TFileSystem fileSystem, string basePath, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static Testably.Abstractions.Testing.Initializer.IDirectoryCleaner SetCurrentDirectoryToEmptyTemporaryDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string? prefix = null, System.Action<string>? logger = null) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> Initialize<TFileSystem>(this TFileSystem fileSystem, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static void InitializeEmbeddedResourcesFromAssembly(this System.IO.Abstractions.IFileSystem fileSystem, string directoryPath, System.Reflection.Assembly assembly, string? relativePath = null, string searchPattern = "*", System.IO.SearchOption searchOption = 1) { }
public static void InitializeFromRealDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string sourceDirectory, string? targetDirectory = null) { }
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> InitializeIn<TFileSystem>(this TFileSystem fileSystem, string basePath, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static Testably.Abstractions.Testing.Initializer.IDirectoryCleaner SetCurrentDirectoryToEmptyTemporaryDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string? prefix = null, System.Action<string>? logger = null) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> Initialize<TFileSystem>(this TFileSystem fileSystem, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static void InitializeEmbeddedResourcesFromAssembly(this System.IO.Abstractions.IFileSystem fileSystem, string directoryPath, System.Reflection.Assembly assembly, string? relativePath = null, string searchPattern = "*", System.IO.SearchOption searchOption = 1) { }
public static void InitializeFromRealDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string sourceDirectory, string? targetDirectory = null) { }
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> InitializeIn<TFileSystem>(this TFileSystem fileSystem, string basePath, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static Testably.Abstractions.Testing.Initializer.IDirectoryCleaner SetCurrentDirectoryToEmptyTemporaryDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string? prefix = null, System.Action<string>? logger = null) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Testably.Abstractions.Testing
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> Initialize<TFileSystem>(this TFileSystem fileSystem, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static void InitializeEmbeddedResourcesFromAssembly(this System.IO.Abstractions.IFileSystem fileSystem, string directoryPath, System.Reflection.Assembly assembly, string? relativePath = null, string searchPattern = "*", System.IO.SearchOption searchOption = 1) { }
public static void InitializeFromRealDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string sourceDirectory, string? targetDirectory = null) { }
public static Testably.Abstractions.Testing.Initializer.IFileSystemInitializer<TFileSystem> InitializeIn<TFileSystem>(this TFileSystem fileSystem, string basePath, System.Action<Testably.Abstractions.Testing.FileSystemInitializerOptions>? options = null)
where TFileSystem : System.IO.Abstractions.IFileSystem { }
public static Testably.Abstractions.Testing.Initializer.IDirectoryCleaner SetCurrentDirectoryToEmptyTemporaryDirectory(this System.IO.Abstractions.IFileSystem fileSystem, string? prefix = null, System.Action<string>? logger = null) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using aweXpect.Testably;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -243,6 +244,108 @@ await That(result)
.Contains(x => x.EndsWith("SubResourceFile1.txt", StringComparison.Ordinal));
}

[Theory]
[AutoData]
public async Task InitializeFromRealDirectory_MissingDrive_ShouldCreateDrive(
string directoryName)
{
Skip.IfNot(Test.RunsOnWindows);

string tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
try
{
Directory.CreateDirectory(tempPath);
MockFileSystem sut = new();
IDriveInfo[] drives = sut.DriveInfo.GetDrives();
for (char c = 'D'; c <= 'Z'; c++)
{
if (drives.Any(d => d.Name.StartsWith($"{c}", StringComparison.Ordinal)))
{
continue;
}

directoryName = Path.Combine($"{c}:\\", directoryName);
break;
}

sut.InitializeFromRealDirectory(tempPath, directoryName);

await That(sut.Directory.Exists(directoryName)).IsTrue();
await That(sut.DriveInfo.GetDrives()).HasCount(drives.Length + 1);
}
finally
{
Directory.Delete(tempPath, true);
}
}

[Fact]
public async Task InitializeFromRealDirectory_ShouldCopyFileToTargetDirectory()
{
MockFileSystem fileSystem = new();
string tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
try
{
Directory.CreateDirectory(tempPath);
string filePath = Path.Combine(tempPath, "test.txt");
File.WriteAllText(filePath, "Hello, World!");

fileSystem.InitializeFromRealDirectory(tempPath, "foo");

await That(fileSystem).HasDirectory("foo");
await That(fileSystem).HasFile("foo/test.txt").WithContent("Hello, World!");
}
finally
{
Directory.Delete(tempPath, true);
}
}

[Fact]
public async Task
InitializeFromRealDirectory_ShouldRecursivelyCopyDirectoriesToTargetDirectory()
{
MockFileSystem fileSystem = new();
string tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
try
{
Directory.CreateDirectory(tempPath);
Directory.CreateDirectory(Path.Combine(tempPath, "subdir"));
File.WriteAllText(Path.Combine(tempPath, "subdir", "test1.txt"), "foo");
File.WriteAllText(Path.Combine(tempPath, "subdir", "test2.txt"), "bar");

fileSystem.InitializeFromRealDirectory(tempPath);

await That(fileSystem).HasDirectory(fileSystem.Path.Combine(tempPath, "subdir"));
await That(fileSystem).HasFile(fileSystem.Path.Combine(tempPath, "subdir", "test1.txt"))
.WithContent("foo");
await That(fileSystem).HasFile(fileSystem.Path.Combine(tempPath, "subdir", "test2.txt"))
.WithContent("bar");
}
finally
{
Directory.Delete(tempPath, true);
}
}

[Fact]
public async Task
InitializeFromRealDirectory_WhenDirectoryDoesNotExist_ShouldThrowDirectoryNotFoundException()
{
MockFileSystem fileSystem = new();
string tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
while (Directory.Exists(tempPath))
{
tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
}

void Act()
=> fileSystem.InitializeFromRealDirectory(tempPath, "foo");

await That(Act).Throws<DirectoryNotFoundException>()
.WithMessage($"The directory '{tempPath}' does not exist.");
}

[Theory]
[AutoData]
public async Task InitializeIn_MissingDrive_ShouldCreateDrive(string directoryName)
Expand Down
Loading