public bool EqualTo(params string[] content) { DirectoryEntryCollection entryCollection = new DirectoryEntryCollection(content); DirectoryEntryCollection contents = GetContent(); return(entryCollection.EqualTo(contents)); }
public void AssertEqual(DirectoryEntryCollection shouldBeContent, string message = null) { if (shouldBeContent == null) { throw new ArgumentNullException(nameof(shouldBeContent)); } var directoryContent = GetContent(); message = message ?? "Directory contents not equal"; foreach (var shouldBeItem in shouldBeContent) { var directoryItem = directoryContent.FirstOrDefault(d => d.FileName == shouldBeItem.FileName); if (directoryItem == null) { throw new DirectoryMismatchException($"{message} (missing file {shouldBeItem})"); } if (shouldBeItem.Time != null && directoryItem.Time != shouldBeItem.Time) { throw new DirectoryMismatchException($"{message} (time stamp mismatch {shouldBeItem} != {directoryItem})"); } if (shouldBeItem.Content != null && directoryItem.Content != shouldBeItem.Content) { throw new DirectoryMismatchException($"{message} (content mismatch {shouldBeItem} != {directoryItem})"); } if (shouldBeItem.IsDirectory != directoryItem.IsDirectory) { throw new DirectoryMismatchException($"{message} (isdirectory mismatch {shouldBeItem} != {directoryItem})"); } } foreach (var directoryItem in directoryContent) { if (!shouldBeContent.Any(e => e.FileName == directoryItem.FileName)) { throw new DirectoryMismatchException($"{message} (extra file {directoryItem})"); } } }
public DirectoryEntryCollection GetContent() { List <DirectoryEntry> entries = new List <DirectoryEntry>(); if (!Directory.Exists(DirectoryPath)) { return(new DirectoryEntryCollection("")); } var dirInfo = new DirectoryInfo(DirectoryPath); foreach (FileSystemInfo entry in dirInfo.GetFileSystemInfos("*", SearchOption.AllDirectories)) { int time = (int)(entry.LastWriteTimeUtc - (new DateTime(2010, 1, 1))).TotalDays; bool isDirectory = entry is DirectoryInfo; string content; if (isDirectory) { content = ""; } else { content = File.ReadAllText(entry.FullName); } string fileName = entry.FullName.Substring((dirInfo.FullName + "/").Length); if (ExcludedItems == null || !ExcludedItems.IsMatch(fileName)) { entries.Add(new DirectoryEntry(fileName, isDirectory, time, content)); } } DirectoryEntryCollection entryCollection = new DirectoryEntryCollection(entries.ToArray()); return(entryCollection); }
/// <summary> /// Updates the directory to match the files and content provided in the content string /// /// </summary> /// <param name="content">fileName [> content]</param> public void UpdateTo(params string[] content) { DirectoryEntryCollection directoryEntries = new DirectoryEntryCollection(content); Directory.CreateDirectory(DirectoryPath); Clear(false); foreach (DirectoryEntry entry in directoryEntries) { string filePath = Path.Combine(DirectoryPath, entry.FileName).Replace('/', Path.DirectorySeparatorChar); DateTime writeTime = (new DateTime(2010, 1, 1)).AddDays(entry.Time ?? 0); if (entry.IsDirectory) { Directory.CreateDirectory(filePath); Directory.SetLastWriteTimeUtc(filePath, writeTime); } else { if (!string.IsNullOrEmpty(Path.GetDirectoryName(filePath))) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)); } File.WriteAllText(filePath, entry.Content); File.SetLastWriteTimeUtc(filePath, writeTime); } } //Directory timestamps change if a child record is also changed foreach (DirectoryEntry entry in directoryEntries.Where(e => e.IsDirectory)) { string filePath = Path.Combine(DirectoryPath, entry.FileName).Replace('/', Path.DirectorySeparatorChar); DateTime writeTime = (new DateTime(2010, 1, 1)).AddDays(entry.Time ?? 0); Directory.SetLastWriteTimeUtc(filePath, writeTime); } }
public void AssertEqual(string content, string message = null) { var shouldBeContent = new DirectoryEntryCollection(content); AssertEqual(shouldBeContent, message); }