Skip to content

Commit

Permalink
Improved test coverage on MockDirectory and MockDirectoryInfo
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
martindevans committed Mar 24, 2013
1 parent dff19c0 commit 2532798
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 22 deletions.
65 changes: 64 additions & 1 deletion TestHelpers.Tests/MockDirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -643,6 +643,51 @@ public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
Assert.AreEqual(testPath, entries.First());
}

[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()
{
Expand All @@ -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"));
}
}
}
40 changes: 25 additions & 15 deletions TestingHelpers/MockDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions TestingHelpers/MockDirectoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ MockFileData MockFileData {

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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}

Expand All @@ -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));
}
}
}
}
2 changes: 1 addition & 1 deletion TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions TestingHelpers/MockFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions TestingHelpers/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 2532798

Please sign in to comment.