Last active
October 9, 2021 13:59
-
-
Save deanebarker/c5a1546e247c3886798f05729540a4a2 to your computer and use it in GitHub Desktop.
The minimum (I think) you need to do to mock the FileProvider for the Fluid templating engine
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
namespace DeaneBarker | |
{ | |
public class FakeFileProvider : IFileProvider | |
{ | |
// I don't think Fluid ever calls these (fingers crossed) | |
public IDirectoryContents GetDirectoryContents(string subpath) { throw new NotImplementedException(); } | |
public IChangeToken Watch(string filter) { throw new NotImplementedException(); } | |
public IFileInfo GetFileInfo(string path) | |
{ | |
return new FakeFile(path); | |
} | |
} | |
public class FakeFile : IFileInfo | |
{ | |
private string fileContents; | |
public bool Exists => true; | |
public long Length => fileContents.Length; | |
public string PhysicalPath => null; | |
public string Name => null; | |
public DateTimeOffset LastModified => DateTimeOffset.Now; | |
public bool IsDirectory => false; | |
public FakeFile(string name) | |
{ | |
// The "name" could be anything -- a URL, a database key, whatever. | |
// Using that string, retrieve the file contents from... well, anywhere... | |
fileContents = $"This file was supposedly at {name}."; | |
} | |
public Stream CreateReadStream() | |
{ | |
return new MemoryStream(Encoding.UTF8.GetBytes(fileContents)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment