-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathMockFileProvider.cs
More file actions
79 lines (65 loc) · 2.8 KB
/
Copy pathMockFileProvider.cs
File metadata and controls
79 lines (65 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
namespace WebOptimizer.Core.Test.Mocks
{
// grabbed the logic from https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.TagHelpers/test/GlobbingUrlBuilderTest.cs
// and made it recursive
internal class MockFileProvider : IFileProvider
{
private readonly MockChangeToken _changeToken = new();
private readonly Dictionary<string, MockFileInfo> _files;
private readonly Dictionary<string, MockDirectoryContents> _directories;
private MockFileProvider(Dictionary<string, MockFileInfo> files, Dictionary<string, MockDirectoryContents> directories)
{
_files = files;
_directories = directories;
}
public static IFileProvider Create(MockFileInfo rootNode)
{
if (rootNode.Files == null || !rootNode.Files.Any())
{
throw new ArgumentException($"{nameof(rootNode)} must have children.", nameof(rootNode));
}
Dictionary<string, MockFileInfo> files = [];
Dictionary<string, MockDirectoryContents> directories = [];
var stack = new Stack<(MockFileInfo fileInfo, string directoryPath)>();
stack.Push((rootNode, string.Empty));
while (stack.Count > 0)
{
var (fileInfo, directoryPath) = stack.Pop();
if (fileInfo.IsDirectory)
{
var children = fileInfo.Files;
var fullPath = string.IsNullOrEmpty(directoryPath) ? fileInfo.Name : directoryPath + "/" + fileInfo.Name;
directories[fullPath] = new MockDirectoryContents(children);
foreach (var child in children)
{
stack.Push((child, fullPath));
}
}
else
{
var fullPath = string.IsNullOrEmpty(directoryPath) ? fileInfo.Name : directoryPath + "/" + fileInfo.Name;
files[fullPath] = fileInfo;
files["/" + fullPath] = fileInfo;
}
}
return new MockFileProvider(files, directories);
}
public IFileInfo GetFileInfo(string subpath)
{
return _files.TryGetValue(subpath, out var fileInfo) ? fileInfo : new NotFoundFileInfo(subpath);
}
public IDirectoryContents GetDirectoryContents(string subpath)
{
return _directories.TryGetValue(subpath, out var directoryContents) ? directoryContents : new NotFoundDirectoryContents();
}
public IChangeToken Watch(string filter)
{
return _changeToken;
}
}
}