Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modified the MockDirectory_Delete tests to correctly specify recursive

Modified CreateDirectory to explicitly create all directories in the chain up to the one being created
Modified getFilesInternal to correctly handle directories and files
Modified MockFileSystem to explicitly create all directories in the test data

Signed-off-by: Martin Evans <martindevans@gmail.com>
  • Loading branch information...
commit 1207720034c969a5509d06a9849ebbfeac36876f 1 parent 6a69941
@martindevans martindevans authored
View
30 TestHelpers.Tests/MockDirectoryTests.cs
@@ -542,7 +542,7 @@ public void MockDirectory_Delete_ShouldDeleteDirectory()
});
// Act
- fileSystem.Directory.Delete(@"c:\bar");
+ fileSystem.Directory.Delete(@"c:\bar", true);
// Assert
Assert.IsFalse(fileSystem.Directory.Exists(@"c:\bar"));
@@ -558,7 +558,7 @@ public void MockDirectory_Delete_ShouldDeleteDirectoryCaseInsensitively()
});
// Act
- fileSystem.Directory.Delete(@"c:\BAR");
+ fileSystem.Directory.Delete(@"c:\BAR", true);
// Assert
Assert.IsFalse(fileSystem.Directory.Exists(@"c:\bar"));
@@ -630,7 +630,7 @@ public void MockDirectory_Delete_ShouldDeleteDirectoryRecursively()
public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
{
const string testPath = @"c:\foo\bar.txt";
- const string testDir = @"c:\foo\bar";
+ const string testDir = @"c:\foo\bar\";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ testPath, new MockFileData("Demo text content") },
@@ -639,8 +639,28 @@ public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
var entries = fileSystem.Directory.GetFileSystemEntries(@"c:\foo").OrderBy(k => k);
Assert.AreEqual(2, entries.Count());
- Assert.AreEqual(testDir, entries.First());
- Assert.AreEqual(testPath, entries.Last());
+ Assert.AreEqual(testDir, entries.Last());
+ Assert.AreEqual(testPath, entries.First());
+ }
+
+ [Test]
+ public void MockDirectory_GetDirectories_Returns_Child_Directories()
+ {
+ var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
+ {
+ { @"A:\folder1\folder2\folder3\file.txt", new MockFileData("Demo text content") },
+ { @"A:\folder1\folder4\file2.txt", new MockFileData("Demo text content 2") },
+ });
+
+ var directories = fileSystem.Directory.GetDirectories(@"A:\folder1").ToArray();
+
+ //Check that it does not returns itself
+ Assert.IsFalse(directories.Contains(@"A:\folder1\"));
+
+ //Check that it correctly returns all child directories
+ Assert.AreEqual(2, directories.Count());
+ Assert.IsTrue(directories.Contains(@"A:\folder1\folder2\"));
+ Assert.IsTrue(directories.Contains(@"A:\folder1\folder4\"));
}
}
}
View
27 TestingHelpers/MockDirectory.cs
@@ -26,7 +26,13 @@ public override DirectoryInfoBase CreateDirectory(string path, DirectorySecurity
{
path = EnsurePathEndsWithDirectorySeparator(path);
mockFileDataAccessor.AddFile(path, new MockDirectoryData());
- return new MockDirectoryInfo(mockFileDataAccessor, path);
+ var created = new MockDirectoryInfo(mockFileDataAccessor, path);
+
+ var parent = GetParent(path);
+ if (parent != null && !parent.Exists)
+ CreateDirectory(GetParent(path).FullName, directorySecurity);
+
+ return created;
}
public override void Delete(string path)
@@ -55,8 +61,10 @@ public override void Delete(string path, bool recursive)
public override bool Exists(string path)
{
- path = EnsurePathEndsWithDirectorySeparator(path);
- return mockFileDataAccessor.AllPaths.Any(p => p.StartsWith(path));
+ if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
+ path += Path.DirectorySeparatorChar;
+
+ return mockFileDataAccessor.AllDirectories.Any(p => p.Equals(path, StringComparison.InvariantCultureIgnoreCase));
}
public override DirectorySecurity GetAccessControl(string path)
@@ -96,7 +104,11 @@ public override string[] GetDirectories(string path, string searchPattern)
public override string[] GetDirectories(string path, string searchPattern, SearchOption searchOption)
{
- return getFilesInternal(mockFileDataAccessor.AllDirectories, path, searchPattern, searchOption);
+ if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
+ path += Path.DirectorySeparatorChar;
+
+ var dirs = getFilesInternal(mockFileDataAccessor.AllDirectories, path, searchPattern, searchOption);
+ return dirs.Where(p => p != path).ToArray();
}
public override string GetDirectoryRoot(string path)
@@ -129,7 +141,7 @@ public string[] getFilesInternal(IEnumerable<string> files, string path, string
const string allDirectoriesPattern = @"([\w\d\s-\.]*\\)*";
var fileNamePattern = searchPattern == "*"
- ? @"[^\\]*?"
+ ? @"[^\\]*?\\?"
: Regex.Escape(searchPattern)
.Replace(@"\*", @"[\w\d\s-\.]*?")
.Replace(@"\?", @"[\w\d\s-\.]?");
@@ -152,7 +164,10 @@ public override string[] GetFileSystemEntries(string path)
public override string[] GetFileSystemEntries(string path, string searchPattern)
{
- return GetDirectories(path, searchPattern).Union(GetFiles(path, searchPattern)).ToArray();
+ var dirs = GetDirectories(path, searchPattern);
+ var files = GetFiles(path, searchPattern);
+
+ return dirs.Union(files).ToArray();
}
public override DateTime GetLastAccessTime(string path)
View
20 TestingHelpers/MockFileSystem.cs
@@ -27,22 +27,12 @@ public MockFileSystem(IDictionary<string, MockFileData> files)
this.files = new Dictionary<string, MockFileData>(StringComparer.InvariantCultureIgnoreCase);
foreach (var entry in files)
{
- this.files.Add(entry.Key, entry.Value);
+ var directoryPath = Path.GetDirectoryName(entry.Key);
+ if (!directory.Exists(directoryPath))
+ directory.CreateDirectory(directoryPath);
- var remainingPath = entry.Key;
- do
- {
- remainingPath = remainingPath.Substring(0, remainingPath.LastIndexOf(IO.Path.DirectorySeparatorChar));
-
- //Don't add the volume as a directory
- if (remainingPath.EndsWith(Path.VolumeSeparatorChar.ToString(CultureInfo.InvariantCulture)))
- break;
-
- //Don't create duplicate directories
- if (!this.files.ContainsKey(remainingPath))
- AddFile(remainingPath, new MockDirectoryData());
-
- } while (remainingPath.LastIndexOf(IO.Path.DirectorySeparatorChar) != -1);
+ if (!file.Exists(entry.Key))
+ this.files.Add(entry.Key, entry.Value);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.