Skip to content

Commit

Permalink
Add FileSystemEntry family classes
Browse files Browse the repository at this point in the history
  • Loading branch information
X9VoiD committed Dec 29, 2022
1 parent e79f4e6 commit d6e1a1d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
9 changes: 9 additions & 0 deletions OpenTabletDriver.Tests/Fakes/FakeDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace OpenTabletDriver.Tests.Fakes
{
public class FakeDirectory : FakeFileSystemEntry
{
public List<FakeFileSystemEntry> Entries { get; } = new();
}
}
9 changes: 9 additions & 0 deletions OpenTabletDriver.Tests/Fakes/FakeFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace OpenTabletDriver.Tests.Fakes
{
public class FakeFile : FakeFileSystemEntry
{
public byte[] Data { get; init; } = Array.Empty<byte>();
}
}
65 changes: 65 additions & 0 deletions OpenTabletDriver.Tests/Fakes/FakeFileSystemEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit.Sdk;

namespace OpenTabletDriver.Tests.Fakes
{
public abstract class FakeFileSystemEntry
{
public string Name { get; init; } = null!;

public static async Task SetupFilesAsync(string directory, IEnumerable<FakeFileSystemEntry> entries)
{
foreach (var entry in entries)
{
if (entry is FakeDirectory dir)
{
var dirPath = Path.Join(directory, dir.Name);
Directory.CreateDirectory(dirPath);
await SetupFilesAsync(dirPath, dir.Entries);
}
else if (entry is FakeFile file)
{
var filePath = Path.Join(directory, file.Name);
await File.WriteAllBytesAsync(filePath, file.Data);
}
}
}

public static async Task VerifyFilesAsync(string directory, IEnumerable<FakeFileSystemEntry> entries, bool strict = true)
{
var entriesCache = entries.ToArray();
var dirEntries = Directory.GetFileSystemEntries(directory);
var dirEntriesMap = dirEntries.ToDictionary(e => Path.GetFileName(e));

if (strict && (entriesCache.Length != dirEntries.Length))
throw new XunitException($"Expected {entriesCache.Length} file system entries, got {dirEntries.Length}");

foreach (var entry in entriesCache)
{
if (!dirEntriesMap.TryGetValue(entry.Name, out var path))
throw new XunitException($"Expected file system entry '{entry.Name}' not found");

if (entry is FakeDirectory dir)
{
if (!Directory.Exists(path))
throw new DirectoryNotFoundException($"Directory '{path}' does not exist.");

await VerifyFilesAsync(path, dir.Entries);
}
else if (entry is FakeFile file)
{
if (!File.Exists(path))
throw new FileNotFoundException($"File '{path}' does not exist.");

var data = await File.ReadAllBytesAsync(path);
if (!data.AsSpan().SequenceEqual(file.Data))
throw new Exception($"File {path} does not match expected data");
}
}
}
}
}

0 comments on commit d6e1a1d

Please sign in to comment.