Permalink
Browse files

Merge pull request #29 from martindevans/master

Improved test coverage for MockDirectory (and some more methods implemented)
  • Loading branch information...
2 parents 1c7e351 + 2532798 commit b725631ee771d7788f86328ad22ca4dab8bb3239 @tathamoddie committed Mar 24, 2013
@@ -619,7 +619,7 @@ public void MockDirectory_Delete_ShouldDeleteDirectoryRecursively()
});
// Act
- fileSystem.Directory.Delete(@"c:\bar", true);
+ fileSystem.DirectoryInfo.FromDirectoryName(@"c:\bar").Delete(true);
// Assert
Assert.IsFalse(fileSystem.Directory.Exists(@"c:\bar"));
@@ -644,6 +644,51 @@ public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
}
[Test]
+ public void MockDirectory_GetFiles_Returns_Files()
+ {
+ const string testPath = @"c:\foo\bar.txt";
+ const string testDir = @"c:\foo\bar\";
+ var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
+ {
+ { testPath, new MockFileData("Demo text content") },
+ { testDir, new MockDirectoryData() }
+ });
+
+ var entries = fileSystem.Directory.GetFiles(@"c:\foo").OrderBy(k => k);
+ Assert.AreEqual(1, entries.Count());
+ Assert.AreEqual(testPath, entries.First());
+ }
+
+ [Test]
+ public void MockDirectory_GetRoot_Returns_Root()
+ {
+ const string testDir = @"c:\foo\bar\";
+ var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
+ {
+ { testDir, new MockDirectoryData() }
+ });
+
+ Assert.AreEqual("C:\\", fileSystem.Directory.GetDirectoryRoot(@"C:\foo\bar"));
+ }
+
+ [Test]
+ public void MockDirectory_GetLogicalDrives_Returns_LogicalDrives()
+ {
+ var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
+ {
+ {@"c:\foo\bar\", new MockDirectoryData()},
+ {@"c:\foo\baz\", new MockDirectoryData()},
+ {@"d:\bash\", new MockDirectoryData()},
+ });
+
+ var drives = fileSystem.Directory.GetLogicalDrives();
+
+ Assert.AreEqual(2, drives.Length);
+ Assert.IsTrue(drives.Contains("c:\\"));
+ Assert.IsTrue(drives.Contains("d:\\"));
+ }
+
+ [Test]
public void MockDirectory_GetDirectories_Returns_Child_Directories()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
@@ -662,5 +707,23 @@ public void MockDirectory_GetDirectories_Returns_Child_Directories()
Assert.IsTrue(directories.Contains(@"A:\folder1\folder2\"));
Assert.IsTrue(directories.Contains(@"A:\folder1\folder4\"));
}
+
+ [Test]
+ public void MockDirectory_Move_ShouldMove()
+ {
+ var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
+ {
+ { @"A:\folder1\file.txt", new MockFileData("aaa") },
+ { @"A:\folder1\folder2\file2.txt", new MockFileData("bbb") },
+ });
+
+ fileSystem.DirectoryInfo.FromDirectoryName(@"A:\folder1").MoveTo(@"B:\folder1");
+
+ Assert.IsFalse(fileSystem.Directory.Exists(@"A:\folder1"));
+ Assert.IsTrue(fileSystem.Directory.Exists(@"B:\folder1"));
+ Assert.IsTrue(fileSystem.Directory.Exists(@"B:\folder1\folder2"));
+ Assert.IsTrue(fileSystem.File.Exists(@"B:\folder1\file.txt"));
+ Assert.IsTrue(fileSystem.File.Exists(@"B:\folder1\folder2\file2.txt"));
+ }
}
}
@@ -114,7 +114,7 @@ public override string[] GetDirectories(string path, string searchPattern, Searc
public override string GetDirectoryRoot(string path)
{
- throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
+ return Path.GetPathRoot(path);
}
public override string[] GetFiles(string path)
@@ -193,7 +193,12 @@ public override DateTime GetLastWriteTimeUtc(string path)
public override string[] GetLogicalDrives()
{
- throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
+ return mockFileDataAccessor
+ .AllDirectories
+ .Select(d => new MockDirectoryInfo(mockFileDataAccessor, d).Root.FullName)
+ .Select(r => r.ToLowerInvariant())
+ .Distinct()
+ .ToArray();
}
public override DirectoryInfoBase GetParent(string path)
@@ -206,22 +211,27 @@ public override DirectoryInfoBase GetParent(string path)
}
public override void Move(string sourceDirName, string destDirName) {
- var existingFiles =
- this.GetFiles(sourceDirName, "*", SearchOption.AllDirectories)
- .Union(GetDirectories(sourceDirName, "*", SearchOption.AllDirectories))
- .ToList();
- existingFiles.Add(sourceDirName);
-
- var existingData = existingFiles.ToDictionary(k => k, k => mockFileDataAccessor.GetFile(k));
-
- foreach (var file in existingFiles) {
- mockFileDataAccessor.RemoveFile(file);
+ //Make sure that the destination exists
+ mockFileDataAccessor.Directory.CreateDirectory(destDirName);
+
+ //Recursively move all the subdirectories
+ var subdirectories = GetDirectories(sourceDirName);
+ foreach (var subdirectory in subdirectories)
+ {
+ var newSubdirPath = subdirectory.Replace(sourceDirName, destDirName);
+ Move(subdirectory, newSubdirPath);
}
- foreach (var file in existingFiles) {
- var newFile = file.Replace(sourceDirName, destDirName);
- mockFileDataAccessor.AddFile(newFile, existingData[file]);
+ //Move the files in this directory
+ var files = GetFiles(sourceDirName);
+ foreach (var file in files)
+ {
+ var newFilePath = file.Replace(sourceDirName, destDirName);
+ mockFileDataAccessor.FileInfo.FromFileName(file).MoveTo(newFilePath);
}
+
+ //Delete this directory
+ Delete(sourceDirName);
}
public override void SetAccessControl(string path, DirectorySecurity directorySecurity)
@@ -25,7 +25,7 @@ public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string dire
public override void Delete()
{
- throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
+ mockFileDataAccessor.Directory.Delete(directoryPath);
}
public override void Refresh()
@@ -94,7 +94,7 @@ public override DateTime LastWriteTimeUtc
public override string Name
{
- get { return new MockPath().GetFileName(directoryPath); }
+ get { return new MockPath().GetFileName(directoryPath.TrimEnd('\\')); }
}
public override void Create()
@@ -189,6 +189,9 @@ public override FileSystemInfoBase[] GetFileSystemInfos(string searchPattern)
public override void MoveTo(string destDirName)
{
+ if (!destDirName.EndsWith(Path.DirectorySeparatorChar.ToString()))
+ destDirName += Path.DirectorySeparatorChar;
+
mockFileDataAccessor.Directory.Move(directoryPath, destDirName);
}
@@ -207,7 +210,10 @@ public override DirectoryInfoBase Parent
public override DirectoryInfoBase Root
{
- get { throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all."); }
+ get
+ {
+ return new MockDirectoryInfo(mockFileDataAccessor, mockFileDataAccessor.Directory.GetDirectoryRoot(FullName));
+ }
}
}
}
@@ -81,7 +81,7 @@ public override Stream Create(string path, int bufferSize, FileOptions options,
public override StreamWriter CreateText(string path)
{
- throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
+ return new StreamWriter(Create(path));
}
public override void Decrypt(string path)
@@ -155,7 +155,8 @@ public override FileSecurity GetAccessControl(AccessControlSections includeSecti
public override void MoveTo(string destFileName)
{
- throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
+ CopyTo(destFileName);
+ Delete();
}
public override Stream Open(FileMode mode)
@@ -175,7 +176,7 @@ public override Stream Open(FileMode mode, FileAccess access, FileShare share)
public override Stream OpenRead()
{
- throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
+ return new MockFileStream(mockFileSystem, path);
}
public override StreamReader OpenText()
@@ -61,23 +61,32 @@ public IDirectoryInfoFactory DirectoryInfo
get { return directoryInfoFactory; }
}
+ private string FixPath(string path)
+ {
+ return path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
+ }
+
public MockFileData GetFile(string path)
{
+ path = FixPath(path);
return FileExists(path) ? files[path] : null;
}
public void AddFile(string path, MockFileData mockFile)
{
+ path = FixPath(path);
files.Add(path, mockFile);
}
public void RemoveFile(string path)
{
+ path = FixPath(path);
files.Remove(path);
}
public bool FileExists(string path)
{
+ path = FixPath(path);
return files.ContainsKey(path);
}

0 comments on commit b725631

Please sign in to comment.