forked from OpenTabletDriver/OpenTabletDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} | ||
} |