|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Text; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Microsoft.DotNet.ImageBuilder.Tests.Helpers; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// In-memory <see cref="IFileSystem"/> implementation for unit tests. |
| 15 | +/// Stores file contents in a dictionary and tracks all operations for assertions. |
| 16 | +/// </summary> |
| 17 | +internal sealed class InMemoryFileSystem : IFileSystem |
| 18 | +{ |
| 19 | + private readonly Dictionary<string, byte[]> _files = []; |
| 20 | + private readonly HashSet<string> _directories = []; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Paths written via <see cref="WriteAllText"/> or <see cref="WriteAllTextAsync"/>. |
| 24 | + /// </summary> |
| 25 | + public List<string> FilesWritten { get; } = []; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Paths read via <see cref="ReadAllBytes"/>, <see cref="ReadAllBytesAsync"/>, or <see cref="ReadAllTextAsync"/>. |
| 29 | + /// </summary> |
| 30 | + public List<string> FilesRead { get; } = []; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Paths deleted via <see cref="DeleteFile"/>. |
| 34 | + /// </summary> |
| 35 | + public List<string> FilesDeleted { get; } = []; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Paths created via <see cref="CreateDirectory"/>. |
| 39 | + /// </summary> |
| 40 | + public List<string> DirectoriesCreated { get; } = []; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Seeds a file with text content before a test runs. |
| 44 | + /// </summary> |
| 45 | + public void AddFile(string path, string contents) => |
| 46 | + _files[path] = Encoding.UTF8.GetBytes(contents); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Seeds a file with binary content before a test runs. |
| 50 | + /// </summary> |
| 51 | + public void AddFile(string path, byte[] contents) => |
| 52 | + _files[path] = contents; |
| 53 | + |
| 54 | + public void WriteAllText(string path, string contents) |
| 55 | + { |
| 56 | + _files[path] = Encoding.UTF8.GetBytes(contents); |
| 57 | + FilesWritten.Add(path); |
| 58 | + } |
| 59 | + |
| 60 | + public Task WriteAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default) |
| 61 | + { |
| 62 | + _files[path] = Encoding.UTF8.GetBytes(contents ?? string.Empty); |
| 63 | + FilesWritten.Add(path); |
| 64 | + return Task.CompletedTask; |
| 65 | + } |
| 66 | + |
| 67 | + public byte[] ReadAllBytes(string path) |
| 68 | + { |
| 69 | + FilesRead.Add(path); |
| 70 | + return _files.TryGetValue(path, out var bytes) |
| 71 | + ? bytes |
| 72 | + : throw new FileNotFoundException("File not found", path); |
| 73 | + } |
| 74 | + |
| 75 | + public Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default) => |
| 76 | + Task.FromResult(ReadAllBytes(path)); |
| 77 | + |
| 78 | + public Task<string> ReadAllTextAsync(string path, CancellationToken cancellationToken = default) |
| 79 | + { |
| 80 | + FilesRead.Add(path); |
| 81 | + return _files.TryGetValue(path, out var bytes) |
| 82 | + ? Task.FromResult(Encoding.UTF8.GetString(bytes)) |
| 83 | + : throw new FileNotFoundException("File not found", path); |
| 84 | + } |
| 85 | + |
| 86 | + public bool FileExists(string path) => _files.ContainsKey(path); |
| 87 | + |
| 88 | + public void DeleteFile(string path) |
| 89 | + { |
| 90 | + _files.Remove(path); |
| 91 | + FilesDeleted.Add(path); |
| 92 | + } |
| 93 | + |
| 94 | + public DirectoryInfo CreateDirectory(string path) |
| 95 | + { |
| 96 | + _directories.Add(path); |
| 97 | + DirectoriesCreated.Add(path); |
| 98 | + return new DirectoryInfo(path); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Gets the text content of a file, for test assertions. |
| 103 | + /// </summary> |
| 104 | + public string GetFileText(string path) => |
| 105 | + Encoding.UTF8.GetString(_files[path]); |
| 106 | +} |
0 commit comments