public void FilePathWithDirectoryCanBeParsed() { var path = "a/b/c.txt"; var expected = "test"; var dir = new MemoryDirectory("root", null); var file = dir.CreateFileFromText(path, expected); string actual; using (StreamReader reader = new StreamReader(file.StreamProvider.OpenRead())) { actual = reader.ReadToEnd(); } Assert.Equal(expected, actual); Assert.Equal("b", file.Directory.Name); Assert.Equal("a", file.Directory.Parent.Name); Assert.Equal(dir, file.Directory.Parent.Parent); }
// TODO: convert this class to extension methods for IDirectory /// <summary> /// This method is used to convert legacy code to IDirectory source. /// </summary> /// <returns></returns> public IDirectory ToDirectory() { var root = new MemoryDirectory("root", null); var bin = root.CreateDirectory("bin"); // generated all text based files. they are mostly configuration files. if (TextFiles != null) { foreach (var textFileName in TextFiles.Keys) { root.CreateFileFromText(textFileName, TextFiles[textFileName]); } } var currentAssemblyLocation = Assembly.GetExecutingAssembly().Location; if (!BinaryFiles.Contains(currentAssemblyLocation)) { BinaryFiles.Add(currentAssemblyLocation); } // copy all required binary files if (BinaryFiles != null) { foreach (var file in BinaryFiles) { var fileInfo = new FileInfo(file); if (fileInfo.Exists) { bin.CreateFileFromDisk(fileInfo.Name, fileInfo); } } } return root; }
public void DirectoryCanBeCopiedToDisk() { string expected = "test"; string expectedFileName = "test.html"; var root = new MemoryDirectory("root", null); root.CreateFileFromText(expectedFileName, expected); var dir = root.CreateDirectory(expected); dir.CreateFileFromText(expectedFileName, expected); var diskDir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "root")); root.CopyToDisk(diskDir); Assert.True(Directory.Exists(Path.Combine(diskDir.FullName, expected))); Assert.True(File.Exists(Path.Combine(diskDir.FullName, expectedFileName))); Assert.True(File.Exists(Path.Combine(diskDir.FullName, expected, expectedFileName))); string content; using (var reader = new StreamReader(Path.Combine(diskDir.FullName, expectedFileName))) { content = reader.ReadToEnd(); } Assert.Equal(expected, content); using (var reader = new StreamReader(Path.Combine(diskDir.FullName, expected, expectedFileName))) { content = reader.ReadToEnd(); } Assert.Equal(expected, content); diskDir.Delete(true); }