Skip to content

Instantly share code, notes, and snippets.

@deanebarker
Last active October 9, 2021 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deanebarker/c5a1546e247c3886798f05729540a4a2 to your computer and use it in GitHub Desktop.
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
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