Permalink
Browse files

Merge pull request #28 from martindevans/master

Explicitly creating all directories in MockFileSystem
  • Loading branch information...
2 parents 98eaee9 + dff19c0 commit 3ed19bdfd1ed525c3cc67bc246fc48dbb94f24aa @tathamoddie committed Mar 24, 2013
@@ -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\"));
}
}
}
@@ -2,6 +2,7 @@
using System.Linq;
using System.Text;
using NUnit.Framework;
+using System.Linq;
namespace System.IO.Abstractions.TestingHelpers.Tests
{
@@ -571,18 +572,69 @@ public void MockFile_Copy_ShouldThrowExceptionWhenFileExistsAtDestination()
}
[Test]
+ public void MockFile_Delete_ShouldRemoveFileFromFileSystem()
+ {
+ const string fullPath = @"c:\something\demo.txt";
+ var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
+ {
+ { fullPath, new MockFileData("Demo text content") }
+ });
+
+ var file = new MockFile(fileSystem);
+
+ file.Delete(fullPath);
+
+ Assert.That(fileSystem.FileExists(fullPath), Is.False);
+ }
+
+ [Test]
+ public void Mockfile_Create_ShouldCreateNewStream()
+ {
+ const string fullPath = @"c:\something\demo.txt";
+ var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
+
+ var sut = new MockFile(fileSystem);
+
+ Assert.That(fileSystem.FileExists(fullPath), Is.False);
+
+ sut.Create(fullPath).Close();
+
+ Assert.That(fileSystem.FileExists(fullPath), Is.True);
+ }
+
+ [Test]
+ public void Mockfile_Create_CanWriteToNewStream()
+ {
+ const string fullPath = @"c:\something\demo.txt";
+ var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
+ var data = new UTF8Encoding(false).GetBytes("Test string");
+
+ var sut = new MockFile(fileSystem);
+ using (var stream = sut.Create(fullPath))
+ {
+ stream.Write(data, 0, data.Length);
+ }
+
+ var mockFileData = fileSystem.GetFile(fullPath);
+ var fileData = mockFileData.Contents;
+
+ Assert.That(fileData, Is.EqualTo(data));
+ }
+
+ [Test]
public void MockFile_Delete_Should_RemoveFiles()
{
const string filePath = @"c:\something\demo.txt";
const string fileContent = "this is some content";
- var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>{{filePath, new MockFileData(fileContent) }});
+ var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { filePath, new MockFileData(fileContent) } });
Assert.AreEqual(1, fileSystem.AllFiles.Count());
fileSystem.File.Delete(filePath);
Assert.AreEqual(0, fileSystem.AllFiles.Count());
}
[Test]
- public void MockFile_Delete_No_File_Does_Nothing() {
+ public void MockFile_Delete_No_File_Does_Nothing()
+ {
const string filePath = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
fileSystem.File.Delete(filePath);
@@ -59,14 +59,9 @@
<AssemblyOriginatorKeyFile>..\StrongName.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
- <Reference Include="nunit.framework">
- <HintPath>..\packages\NUnit.2.5.10.11092\lib\nunit.framework.dll</HintPath>
- </Reference>
- <Reference Include="nunit.mocks">
- <HintPath>..\packages\NUnit.2.5.10.11092\lib\nunit.mocks.dll</HintPath>
- </Reference>
- <Reference Include="pnunit.framework">
- <HintPath>..\packages\NUnit.2.5.10.11092\lib\pnunit.framework.dll</HintPath>
+ <Reference Include="nunit.framework, Version=2.6.2.12296, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
- <package id="NUnit" version="2.5.10.11092" />
+ <package id="NUnit" version="2.6.2" targetFramework="net40" />
</packages>
@@ -25,8 +25,15 @@ public override DirectoryInfoBase CreateDirectory(string path)
public override DirectoryInfoBase CreateDirectory(string path, DirectorySecurity directorySecurity)
{
path = EnsurePathEndsWithDirectorySeparator(path);
- mockFileDataAccessor.AddFile(path, new MockDirectoryData());
- return new MockDirectoryInfo(mockFileDataAccessor, path);
+ if (!Exists(path))
+ mockFileDataAccessor.AddFile(path, new MockDirectoryData());
+ var created = new MockDirectoryInfo(mockFileDataAccessor, path);
+
+ var parent = GetParent(path);
+ if (parent != null)
+ CreateDirectory(GetParent(path).FullName, directorySecurity);
+
+ return created;
}
public override void Delete(string path)
@@ -55,8 +62,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 +105,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 +142,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 +165,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)
@@ -182,7 +198,11 @@ public override string[] GetLogicalDrives()
public override DirectoryInfoBase GetParent(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.");
+ var parent = new DirectoryInfo(path).Parent;
+ if (parent == null)
+ return null;
+
+ return new MockDirectoryInfo(mockFileDataAccessor, parent.FullName);
}
public override void Move(string sourceDirName, string destDirName) {
Oops, something went wrong. Retry.

0 comments on commit 3ed19bd

Please sign in to comment.