mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 06:36:31 +00:00
68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
using System.IO;
|
|
using Azaion.Common.Services;
|
|
|
|
namespace Azaion.Test;
|
|
|
|
public class InMemoryFileSystem : IFileSystem
|
|
{
|
|
private readonly Dictionary<string, byte[]> _files = new();
|
|
private readonly HashSet<string> _directories = new();
|
|
|
|
public Task<byte[]> ReadAllBytesAsync(string path, CancellationToken ct = default)
|
|
{
|
|
if (!_files.TryGetValue(path, out var content))
|
|
throw new FileNotFoundException($"File not found: {path}");
|
|
return Task.FromResult(content);
|
|
}
|
|
|
|
public Task WriteAllBytesAsync(string path, byte[] content, CancellationToken ct = default)
|
|
{
|
|
_files[path] = content;
|
|
var directory = Path.GetDirectoryName(path);
|
|
if (directory != null)
|
|
_directories.Add(directory);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public bool FileExists(string path)
|
|
{
|
|
return _files.ContainsKey(path);
|
|
}
|
|
|
|
public void DeleteFile(string path)
|
|
{
|
|
_files.Remove(path);
|
|
}
|
|
|
|
public IEnumerable<string> GetFiles(string directory, string searchPattern)
|
|
{
|
|
var pattern = searchPattern.Replace("*", ".*").Replace("?", ".");
|
|
var regex = new System.Text.RegularExpressions.Regex(pattern);
|
|
return _files.Keys.Where(f => Path.GetDirectoryName(f) == directory && regex.IsMatch(Path.GetFileName(f)));
|
|
}
|
|
|
|
public IEnumerable<FileInfo> GetFileInfos(string directory, string[] searchPatterns)
|
|
{
|
|
var files = searchPatterns.SelectMany(pattern => GetFiles(directory, pattern));
|
|
return files.Select(f => new FileInfo(f));
|
|
}
|
|
|
|
public DirectoryInfo GetDirectoryInfo(string path)
|
|
{
|
|
if (!_directories.Contains(path))
|
|
_directories.Add(path);
|
|
return new DirectoryInfo(path);
|
|
}
|
|
|
|
public bool DirectoryExists(string path)
|
|
{
|
|
return _directories.Contains(path);
|
|
}
|
|
|
|
public void CreateDirectory(string path)
|
|
{
|
|
_directories.Add(path);
|
|
}
|
|
}
|
|
|