public MemoryFolderFake(MemoryFileSystemFake fileSys, string folderPath, MemoryFolderFake parent) { this._fileSys = fileSys; this.Name = folderPath == null ? null : this._fileSys.GetFileName(folderPath); this.Path = folderPath; this.Parent = parent; this.Files = new HashSet <string>(this._fileSys.EntryNameComparer); this.Folders = new Dictionary <string, MemoryFolderFake>(this._fileSys.EntryNameComparer); }
public void AddFilesAndFolders(string[] filePaths, string[] folderPaths) { foreach (string filePath in filePaths) { string folderPath = this.GetFolderPath(filePath); MemoryFolderFake folder = this.FindFolder(folderPath, createIfDoesNotExist: true); folder.Files.Add(this.GetFileName(filePath)); } foreach (string folderPath in folderPaths) { this.FindFolder(folderPath, createIfDoesNotExist: true); } }
#pragma warning disable CS1998 // Async methods lacks 'await' operators and will run synchronously public async Task <IFile> GetFileFromPathAsync(string path, CancellationToken cancellationToken = new CancellationToken()) { string folderPath = this.GetFolderPath(path); string fileName = this.GetFileName(path); MemoryFolderFake folder = this.FindFolder(folderPath); if (folder.Files.All(file => this.EntryNameComparer.Compare(file, fileName) != 0)) { if (this.ThrowOnError) { throw new FileNotFoundException($"File not found: \"{path}\""); } return(null); } return(new MemoryFileFake(this, path)); }
internal MemoryFolderFake FindFolder(string folderPath, bool createIfDoesNotExist = false) { if (folderPath == null) { return(this.CurrentWorkingFolder); } string[] pathSegments = this.GetPathSegments(folderPath); MemoryFolderFake curFolder = this.IsPathRooted(folderPath) ? this.RootFolder : this.CurrentWorkingFolder; for (int index = 0; index < pathSegments.Length; index++) { string curSegment = pathSegments[index]; MemoryFolderFake foundFolder; if (curSegment == ".") { continue; } if (curSegment == "..") { if (curFolder.Parent == null) { if (this.ThrowOnError) { throw new DirectoryNotFoundException("Root folder does NOT have a parent"); } return(null); } curFolder = curFolder.Parent; continue; } if (!curFolder.Folders.TryGetValue(curSegment, out foundFolder)) { if (createIfDoesNotExist) { foundFolder = new MemoryFolderFake(this, curFolder.Path == null ? curSegment : PortablePath.Combine(curFolder.Path, curSegment), curFolder); curFolder.Folders.Add(curSegment, foundFolder); } else if (this.ThrowOnError) { throw new DirectoryNotFoundException( $"Folder not found: {this.BuildPathString(pathSegments.Take(index + 1), false)}"); } else { return(null); } } curFolder = foundFolder; } return(curFolder); }