When creating a MockFileSystem based on a real directory using IFileSystem.InitializeFromRealDirectory, the CopyDirectory method only copies the file contents of the given real directory.
It would be good if it included file metadata so that the MockFileSystem was a better "recreation" of the real directory. This is particularly useful for testing tools that rely on file metadata. For what it's worth, here's how I'm doing it in my own project:
public static void CopyMetadataTo(this IFileInfo source, IFileInfo target, byte[]? contents = null)
{
contents ??= source.Exists
? source.FileSystem.TryReadAllBytes(source.FullName, out var bytes)
? bytes : new byte[source.Length]
: new byte[source.Length];
if (contents.Length > 0 && target.FileSystem is MockFileSystem) {
target.FileSystem.File.WriteAllBytes(target.FullName, contents);
}
// 1. Basic timestamps
target.CreationTimeUtc = source.CreationTimeUtc;
target.LastWriteTimeUtc = source.LastWriteTimeUtc;
target.LastAccessTimeUtc = source.LastAccessTimeUtc;
// 2. Attributes (ReadOnly, Hidden, System, etc.)
target.Attributes = source.Attributes;
}
Obviously this requires manually including each item of metadata to be copied, but dates and attributes seems like a decent baseline, and there might be a better way to do this.
When creating a MockFileSystem based on a real directory using IFileSystem.InitializeFromRealDirectory, the CopyDirectory method only copies the file contents of the given real directory.
It would be good if it included file metadata so that the MockFileSystem was a better "recreation" of the real directory. This is particularly useful for testing tools that rely on file metadata. For what it's worth, here's how I'm doing it in my own project:
Obviously this requires manually including each item of metadata to be copied, but dates and attributes seems like a decent baseline, and there might be a better way to do this.