Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improved test coverage for MockDirectory (and some more methods implemented) #29

Merged
merged 1 commit into from

2 participants

@martindevans

Improved test coverage on MockDirectory and MockDirectoryInfo
Implemented MockDirectory.GetRootDirectory
Reimplemented MockDirectory.Move (previous system failed a new test I wrote)
Implemented MockFile.CreateText
Implemented MockFileInfo.MoveTo

@martindevans martindevans Improved test coverage on MockDirectory and MockDirectoryInfo
Implemented MockDirectory.GetRootDirectory
Reimplemented MockDirectory.Move (previous system failed a new test I wrote)
Implemented MockFile.CreateText
Implemented MockFileInfo.MoveTo

Signed-off-by: Martin Evans <martindevans@gmail.com>
2532798
@tathamoddie tathamoddie merged commit b725631 into tathamoddie:master
@tathamoddie
Owner

Thanks!

Published to NuGet as of 1.4.0.48.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 24, 2013
  1. @martindevans

    Improved test coverage on MockDirectory and MockDirectoryInfo

    martindevans authored
    Implemented MockDirectory.GetRootDirectory
    Reimplemented MockDirectory.Move (previous system failed a new test I wrote)
    Implemented MockFile.CreateText
    Implemented MockFileInfo.MoveTo
    
    Signed-off-by: Martin Evans <martindevans@gmail.com>
This page is out of date. Refresh to see the latest.
View
65 TestHelpers.Tests/MockDirectoryTests.cs
@@ -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"));
+ }
}
}
View
40 TestingHelpers/MockDirectory.cs
@@ -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)
View
12 TestingHelpers/MockDirectoryInfo.cs
@@ -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));
+ }
}
}
}
View
2  TestingHelpers/MockFile.cs
@@ -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)
View
5 TestingHelpers/MockFileInfo.cs
@@ -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()
View
9 TestingHelpers/MockFileSystem.cs
@@ -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);
}
Something went wrong with that request. Please try again.