private VirtualDirectory CreateDirectory(string name, string fullName) { IDirectoryContentResolver resolver = Substitute.For <IDirectoryContentResolver>(); VirtualDirectory directory = new VirtualDirectory(name, resolver); SetupDirectoryResolver(resolver, directory); directoryContents[directory] = new HashSet <VirtualEntry>(); return(directory); void SetupDirectoryResolver(IDirectoryContentResolver directoryContentResolver, VirtualDirectory currentDirectory) { directoryContentResolver.FullName.Returns(fullName); directoryContentResolver.Created.Returns(create); directoryContentResolver.Exists.Returns(info => DirectoryExists(fullName, string.Empty)); directoryContentResolver.GetContent().Returns((callinfo) => directoryContents[currentDirectory]); directoryContentResolver.Create <VirtualDirectory>(null).ReturnsForAnyArgs(info => { VirtualDirectory child = CreateDirectory(info.Arg <string>(), Path.Combine(fullName, info.Arg <string>())); printMessage?.Invoke($"{DateTime.Now:dd/MM/yyyy hh:mm:ss.fff}: Create {child} in {currentDirectory.FullName}"); directoryContents[currentDirectory].Add(child); return(child); }); directoryContentResolver.Create <VirtualFile>(null) .ReturnsForAnyArgs(info => { VirtualFile result = CreateFile(info.Arg <string>(), Path.Combine(fullName, info.Arg <string>())); printMessage?.Invoke($"{DateTime.Now:dd/MM/yyyy hh:mm:ss.fff}: Create {result} in {currentDirectory.FullName}"); directoryContents[currentDirectory].Add(result); return(result); }); directoryContentResolver.When(r => r.Delete()).Do((info) => { printMessage?.Invoke($"{DateTime.Now:dd/MM/yyyy hh:mm:ss.fff}: Delete {currentDirectory}"); directoryContents.Remove(currentDirectory); }); directoryContentResolver.CreatePath(null).ReturnsForAnyArgs(info => Path.Combine(info.Arg <string[]>())); directoryContentResolver.SplitPath(null).ReturnsForAnyArgs(info => info.Arg <string>().Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries)); VirtualFile CreateFile(string fileName, string fileFullName) { IFileContentResolver fileContentResolver = Substitute.For <IFileContentResolver>(); VirtualFile file = new VirtualFile(fileName, fileContentResolver); fileContentResolver.FullName.Returns(fileFullName); fileContentResolver.Created.Returns(create); fileContentResolver.Exists.Returns(info => FileExists(fileFullName)); fileContentResolver.CheckAccess().Returns(true); fileContents[file] = new MemoryStream(); fileContentResolver.GetContent() .ReturnsForAnyArgs(info => GetFileContent(file, info.ArgAt <bool>(0))); fileContentResolver.When(r => r.Delete()).Do((info) => { if (fileContents.ContainsKey(file)) { deletedFileContents[file.FullName] = fileContents[file]; } printMessage?.Invoke($"{DateTime.Now:dd/MM/yyyy hh:mm:ss.fff}: Delete {file}"); fileContents.Remove(file); }); fileContentResolver.When(r => r.UnDelete()).Do(info => { fileContents[file] = new MemoryStream(); printMessage?.Invoke($"{DateTime.Now:dd/MM/yyyy hh:mm:ss.fff}: Undelete {file}"); }); fileContentResolver.CreatePath(null).ReturnsForAnyArgs(info => Path.Combine(info.Arg <string[]>())); fileContentResolver.SplitPath(null).ReturnsForAnyArgs(info => info.Arg <string>().Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries)); return(file); } } }
public bool CheckAccess() { return(contentResolver.CheckAccess()); }