public void MockDirectory_GetLogicalDrives_Returns_LogicalDrives() { var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\foo\bar\"), new MockDirectoryData() }, { XFS.Path(@"c:\foo\baz\"), new MockDirectoryData() }, { XFS.Path(@"d:\bash\"), new MockDirectoryData() }, }); var drives = fileSystem.Directory.GetLogicalDrives(); if (XFS.IsUnixPlatform()) { Assert.AreEqual(1, drives.Length); Assert.IsTrue(drives.Contains("/")); } else { Assert.AreEqual(2, drives.Length); Assert.IsTrue(drives.Contains("c:\\")); Assert.IsTrue(drives.Contains("d:\\")); } }
public void MockFile_AppendAllText_ShouldFailIfNotExistButDirectoryAlsoNotExist() { // Arrange string path = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { path, new MockFileData("Demo text content") } }); // Act path = XFS.Path(@"c:\something2\demo.txt"); // Assert Exception ex; ex = Assert.Throws <DirectoryNotFoundException>(() => fileSystem.File.AppendAllText(path, "some text")); Assert.Equal(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path), ex.Message); ex = Assert.Throws <DirectoryNotFoundException>( () => fileSystem.File.AppendAllText(path, "some text", Encoding.Unicode)); Assert.Equal(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path), ex.Message); }
public void MockFileInfo_OpenWrite_ShouldAddDataToFileInMemoryFileSystem() { // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); var bytesToAdd = new byte[] { 65, 66, 67, 68, 69 }; // Act using (var file = fileInfo.OpenWrite()) file.Write(bytesToAdd, 0, bytesToAdd.Length); string newcontents; using (var newfile = fileInfo.OpenText()) newcontents = newfile.ReadToEnd(); // Assert Assert.AreEqual("ABCDEtext content", newcontents); }
public void MockFileInfo_Encrypt_ShouldReturnXorOfFileInMemoryFileSystem() { // Arrange var fileData = new MockFileData("Demo text content"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\a.txt"), fileData } }); var fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt")); // Act fileInfo.Encrypt(); string newcontents; using (var newfile = fileInfo.OpenText()) { newcontents = newfile.ReadToEnd(); } // Assert Assert.AreNotEqual("Demo text content", newcontents); }
public void MockDirectory_GetFiles_ShouldFilterByExtensionBasedSearchPatternWithThreeCharacterLongFileExtension_RepectingAllDirectorySearchOption() { // Arrange var additionalFilePath = XFS.Path(@"c:\a\a\c.gifx"); var fileSystem = SetupFileSystem(); fileSystem.AddFile(additionalFilePath, new MockFileData(string.Empty)); fileSystem.AddFile(XFS.Path(@"c:\a\a\c.gifx.xyz"), new MockFileData(string.Empty)); fileSystem.AddFile(XFS.Path(@"c:\a\a\c.gifx\xyz"), new MockFileData(string.Empty)); var expected = new[] { XFS.Path(@"c:\a.gif"), XFS.Path(@"c:\a\b.gif"), XFS.Path(@"c:\a\a\c.gif"), additionalFilePath }; // Act var result = fileSystem.Directory.GetFiles(XFS.Path(@"c:\"), "*.gif", SearchOption.AllDirectories); // Assert Assert.That(result, Is.EquivalentTo(expected)); }
public void MockFile_AppendAllText_ShouldPersistNewTextWithCustomEncoding() { // Arrange string path = XFS.Path(@"c:\something\demo.txt"); MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { path, new MockFileData("Demo text content") } }); MockFile file = new MockFile(fileSystem); // Act file.AppendAllText(path, "+ some text", Encoding.BigEndianUnicode); // Assert byte[] expected = new byte[] { 68, 101, 109, 111, 32, 116, 101, 120, 116, 32, 99, 111, 110, 116, 101, 110, 116, 0, 43, 0, 32, 0, 115, 0, 111, 0, 109, 0, 101, 0, 32, 0, 116, 0, 101, 0, 120, 0, 116 }; if (XFS.IsUnixPlatform()) { // Remove EOF on mono expected = new byte[] { 68, 101, 109, 111, 32, 116, 101, 120, 116, 32, 99, 111, 110, 116, 101, 110, 0, 43, 0, 32, 0, 115, 0, 111, 0, 109, 0, 101, 0, 32, 0, 116, 0, 101, 0, 120, 0, 116 }; } Assert.Equal( expected, file.ReadAllBytes(path)); }
private IDirectoryInfo CreateDirectoryInternal(string path, DirectorySecurity directorySecurity) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (path.Length == 0) { throw new ArgumentException(StringResources.Manager.GetString("PATH_CANNOT_BE_THE_EMPTY_STRING_OR_ALL_WHITESPACE"), "path"); } if (mockFileDataAccessor.PathVerifier.HasIllegalCharacters(path, true)) { throw CommonExceptions.IllegalCharactersInPath(nameof(path)); } path = mockFileDataAccessor.Path.GetFullPath(path).TrimSlashes(); if (XFS.IsWindowsPlatform()) { path = path.TrimEnd(' '); } if (!Exists(path)) { mockFileDataAccessor.AddDirectory(path); } var created = new MockDirectoryInfo(mockFileDataAccessor, path); if (directorySecurity != null) { created.SetAccessControl(directorySecurity); } return(created); }
public void MockFile_Open_OpensExistingFileOnAppend() { string filepath = XFS.Path(@"c:\something\does\exist.txt"); MockFileSystem filesystem = new MockFileSystem(new Dictionary <string, MockFileData> { { filepath, new MockFileData("I'm here") } }); Stream stream = filesystem.File.Open(filepath, FileMode.Append); MockFileData file = filesystem.GetFile(filepath); Assert.Equal(stream.Position, file.Contents.Length); Assert.Equal(stream.Length, file.Contents.Length); stream.Seek(0, SeekOrigin.Begin); byte[] data; using (BinaryReader br = new BinaryReader(stream)) { data = br.ReadBytes((int)stream.Length); } Assert.Equal(file.Contents, data); }
public void MockFile_Decrypt_ShouldDecryptTheFile() { // Arrange const string Content = "Demo text content"; var fileData = new MockFileData(Content); var filePath = XFS.Path(@"c:\a.txt"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { filePath, fileData } }); // Act fileSystem.File.Decrypt(filePath); string newcontents; using (var newfile = fileSystem.File.OpenText(filePath)) { newcontents = newfile.ReadToEnd(); } // Assert Assert.AreNotEqual(Content, newcontents); }
public void MockDirectory_GetAccessControl_ShouldReturnAccessControlOfDirectoryData() { // Arrange var expectedDirectorySecurity = new DirectorySecurity(); expectedDirectorySecurity.SetAccessRuleProtection(false, false); var filePath = XFS.Path(@"c:\a\"); var fileData = new MockDirectoryData() { AccessControl = expectedDirectorySecurity, }; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>() { { filePath, fileData } }); // Act var directorySecurity = fileSystem.Directory.GetAccessControl(filePath); // Assert Assert.That(directorySecurity, Is.EqualTo(expectedDirectorySecurity)); }
public void MockFile_GetAccessControl_ShouldReturnAccessControlOfFileData() { // Arrange var expectedFileSecurity = new FileSecurity(); expectedFileSecurity.SetAccessRuleProtection(false, false); var filePath = XFS.Path(@"c:\a.txt"); var fileData = new MockFileData("Test content") { AccessControl = expectedFileSecurity, }; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>() { { filePath, fileData } }); // Act var fileSecurity = fileSystem.File.GetAccessControl(filePath); // Assert Assert.That(fileSecurity, Is.EqualTo(expectedFileSecurity)); }
public void MockFile_Encrypt_ShouldEncryptTheFile() { // Arrange const string CONTENT = "Demo text content"; MockFileData fileData = new MockFileData(CONTENT); string filePath = XFS.Path(@"c:\a.txt"); MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { filePath, fileData } }); // Act fileSystem.File.Encrypt(filePath); string newcontents; using (StreamReader newfile = fileSystem.File.OpenText(filePath)) { newcontents = newfile.ReadToEnd(); } // Assert Assert.NotEqual(CONTENT, newcontents); }
public void MockDirectory_EnumerateDirectories_WithAllDirectories_ShouldReturnsAllMatchingSubFolders() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo.foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\.foo")); fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty)); // Act var actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"c:\Folder\"), "*.foo", SearchOption.AllDirectories); // Assert Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo\"), XFS.Path(@"C:\Folder\foo.foo\"), XFS.Path(@"C:\Folder\.foo\.foo\") })); }
public void MockDirectory_GetDirectories_WithTopDirectories_ShouldOnlyReturnTopDirectories() { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo.foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\.foo")); fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty)); // Act var actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\Folder\"), "*.foo"); // Assert Assert.That(actualResult, Is.EquivalentTo(new [] { XFS.Path(@"C:\Folder\.foo\"), XFS.Path(@"C:\Folder\foo.foo\") })); }
public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories() { string testPath = XFS.Path(@"c:\foo\bar.txt"); string testDir = XFS.Path(@"c:\foo\bar\"); MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { testPath, new MockFileData("Demo text content") }, { testDir, new MockDirectoryData() } }); IOrderedEnumerable <string> entries = fileSystem.Directory.GetFileSystemEntries(XFS.Path(@"c:\foo")).OrderBy(k => k); Assert.Equal(2, entries.Count()); Assert.Equal(testDir, entries.Last()); Assert.Equal(testPath, entries.First()); }
public void MockDirectory_Delete_ShouldThrowIOException() { // Arrange var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\bar\foo.txt"), new MockFileData("Demo text content") }, { XFS.Path(@"c:\bar\baz.txt"), new MockFileData("Demo text content") } }); var ex = Assert.Throws <IOException>(() => fileSystem.Directory.Delete(XFS.Path(@"c:\bar"))); Assert.That(ex.Message, Is.EqualTo("The directory specified by " + XFS.Path("c:\\bar\\") + " is read-only, or recursive is false and " + XFS.Path("c:\\bar\\") + " is not an empty directory.")); }
public void MockDirectory_Delete_ShouldThrowDirectoryNotFoundException() { // Arrange var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\bar\foo.txt"), new MockFileData("Demo text content") } }); var ex = Assert.Throws <DirectoryNotFoundException>(() => fileSystem.Directory.Delete(XFS.Path(@"c:\baz"))); Assert.That(ex.Message, Is.EqualTo(XFS.Path("c:\\baz\\") + " does not exist or could not be found.")); }
public void Should_Remove_Drive_Letter_On_Unix() { Assert.AreEqual("/test/", XFS.Path(@"c:\test\")); }
private string[] GetFilesInternal(IEnumerable <string> files, string path, string searchPattern, SearchOption searchOption) { CheckSearchPattern(searchPattern); path = EnsurePathEndsWithDirectorySeparator(path); bool isUnix = XFS.IsUnixPlatform(); string allDirectoriesPattern = isUnix ? @"([^<>:""/|?*]*/)*" : @"([^<>:""/\\|?*]*\\)*"; string fileNamePattern; string pathPatternSpecial = null; if (searchPattern == "*") { fileNamePattern = isUnix ? @"[^/]*?/?" : @"[^\\]*?\\?"; } else { fileNamePattern = Regex.Escape(searchPattern) .Replace(@"\*", isUnix ? @"[^<>:""/|?*]*?" : @"[^<>:""/\\|?*]*?") .Replace(@"\?", isUnix ? @"[^<>:""/|?*]?" : @"[^<>:""/\\|?*]?"); var extension = Path.GetExtension(searchPattern); bool hasExtensionLengthOfThree = extension != null && extension.Length == 4 && !extension.Contains("*") && !extension.Contains("?"); if (hasExtensionLengthOfThree) { var fileNamePatternSpecial = string.Format(CultureInfo.InvariantCulture, "{0}[^.]", fileNamePattern); pathPatternSpecial = string.Format( CultureInfo.InvariantCulture, isUnix ? @"(?i:^{0}{1}{2}(?:/?)$)" : @"(?i:^{0}{1}{2}(?:\\?)$)", Regex.Escape(path), searchOption == SearchOption.AllDirectories ? allDirectoriesPattern : string.Empty, fileNamePatternSpecial); } } var pathPattern = string.Format( CultureInfo.InvariantCulture, isUnix ? @"(?i:^{0}{1}{2}(?:/?)$)" : @"(?i:^{0}{1}{2}(?:\\?)$)", Regex.Escape(path), searchOption == SearchOption.AllDirectories ? allDirectoriesPattern : string.Empty, fileNamePattern); return(files .Where(p => { if (Regex.IsMatch(p, pathPattern)) { return true; } if (pathPatternSpecial != null && Regex.IsMatch(p, pathPatternSpecial)) { return true; } return false; }) .ToArray()); }
public void TrimSlashes_RootedPath_DontAlterPathWithoutTrailingSlashes() { Assert.AreEqual(XFS.Path(@"c:\x"), XFS.Path(@"c:\x").TrimSlashes()); }
public void MockDirectory_GetDirectories_WithAllDirectories_ShouldReturnsAllMatchingSubFolders() { // Arrange MockFileSystem fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo.foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\.foo")); fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty)); // Act string[] actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\Folder\"), "*.foo", SearchOption.AllDirectories); // Assert Assert.Equal(actualResult, new[] { XFS.Path(@"C:\Folder\.foo\"), XFS.Path(@"C:\Folder\foo.foo\"), XFS.Path(@"C:\Folder\.foo\.foo\") }); }
public void MockDirectory_EnumerateDirectories_WithTopDirectories_ShouldOnlyReturnTopDirectories() { // Arrange MockFileSystem fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\foo.foo")); fileSystem.AddDirectory(XFS.Path(@"C:\Folder\.foo\.foo")); fileSystem.AddFile(XFS.Path(@"C:\Folder\.foo\bar"), new MockFileData(string.Empty)); // Act IEnumerable <string> actualResult = fileSystem.Directory.EnumerateDirectories(XFS.Path(@"c:\Folder\"), "*.foo"); // Assert Assert.Equal(actualResult, new[] { XFS.Path(@"C:\Folder\.foo\"), XFS.Path(@"C:\Folder\foo.foo\") }); }
public void MockDirectory_GetAccessControl_ShouldThrowExceptionOnDirectoryNotFound() { // Arrange var fileSystem = new MockFileSystem(); // Act Assert.Throws <DirectoryNotFoundException>(() => fileSystem.Directory.GetAccessControl(XFS.Path(@"c:\foo"))); }
public void MockFile_GetAttributeOfNonExistantFile_ShouldThrowOneDirectoryNotFoundException() { // Arrange MockFileSystem fileSystem = new MockFileSystem(); // Act // Assert Assert.Throws <DirectoryNotFoundException>(() => fileSystem.File.GetAttributes(XFS.Path(@"c:\something\demo.txt"))); }
public void MockDirectory_GetFiles_ShouldFilterByExtensionBasedSearchPatternWithDotsInFilenames() { // Arrange var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"c:\a.there.are.dots.in.this.filename.gif"), new MockFileData("Demo text content") },
public void MockDirectory_Move_ShouldMove() { var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { XFS.Path(@"A:\folder1\file.txt"), new MockFileData("aaa") }, { XFS.Path(@"A:\folder1\folder2\file2.txt"), new MockFileData("bbb") }, }); fileSystem.DirectoryInfo.FromDirectoryName(XFS.Path(@"A:\folder1")).MoveTo(XFS.Path(@"B:\folder3")); Assert.IsFalse(fileSystem.Directory.Exists(XFS.Path(@"A:\folder1"))); Assert.IsTrue(fileSystem.Directory.Exists(XFS.Path(@"B:\folder3"))); Assert.IsTrue(fileSystem.Directory.Exists(XFS.Path(@"B:\folder3\folder2"))); Assert.IsTrue(fileSystem.File.Exists(XFS.Path(@"B:\folder3\file.txt"))); Assert.IsTrue(fileSystem.File.Exists(XFS.Path(@"B:\folder3\folder2\file2.txt"))); }
private string[] GetFilesInternal( IEnumerable <string> files, string path, string searchPattern, SearchOption searchOption) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (path.Any(c => Path.GetInvalidPathChars().Contains(c))) { throw new ArgumentException("Invalid character(s) in path", nameof(path)); } CheckSearchPattern(searchPattern); path = path.TrimSlashes(); path = path.NormalizeSlashes(); if (!Exists(path)) { throw CommonExceptions.CouldNotFindPartOfPath(path); } path = EnsureAbsolutePath(path); if (!path.EndsWith(Path.DirectorySeparatorChar.ToString())) { path += Path.DirectorySeparatorChar; } var isUnix = XFS.IsUnixPlatform(); var allDirectoriesPattern = isUnix ? @"([^<>:""/|?*]*/)*" : @"([^<>:""/\\|?*]*\\)*"; string fileNamePattern; string pathPatternSpecial = null; if (searchPattern == "*") { fileNamePattern = isUnix ? @"[^/]*?/?" : @"[^\\]*?\\?"; } else { fileNamePattern = Regex.Escape(searchPattern) .Replace(@"\*", isUnix ? @"[^<>:""/|?*]*?" : @"[^<>:""/\\|?*]*?") .Replace(@"\?", isUnix ? @"[^<>:""/|?*]?" : @"[^<>:""/\\|?*]?"); var extension = Path.GetExtension(searchPattern); bool hasExtensionLengthOfThree = extension != null && extension.Length == 4 && !extension.Contains("*") && !extension.Contains("?"); if (hasExtensionLengthOfThree) { var fileNamePatternSpecial = string.Format(CultureInfo.InvariantCulture, "{0}[^.]", fileNamePattern); pathPatternSpecial = string.Format( CultureInfo.InvariantCulture, isUnix ? @"(?i:^{0}{1}{2}(?:/?)$)" : @"(?i:^{0}{1}{2}(?:\\?)$)", Regex.Escape(path), searchOption == SearchOption.AllDirectories ? allDirectoriesPattern : string.Empty, fileNamePatternSpecial); } } var pathPattern = string.Format( CultureInfo.InvariantCulture, isUnix ? @"(?i:^{0}{1}{2}(?:/?)$)" : @"(?i:^{0}{1}{2}(?:\\?)$)", Regex.Escape(path), searchOption == SearchOption.AllDirectories ? allDirectoriesPattern : string.Empty, fileNamePattern); return(files .Where(p => Regex.IsMatch(p, pathPattern) || (pathPatternSpecial != null && Regex.IsMatch(p, pathPatternSpecial))) .ToArray()); }
public void MockDirectory_CreateDirectory_ShouldFailIfTryingToCreateUNCPathOnlyServer() { // Arrange var fileSystem = new MockFileSystem(); // Act var ex = Assert.Throws <ArgumentException>(() => fileSystem.Directory.CreateDirectory(XFS.Path(@"\\server", () => false))); // Assert StringAssert.StartsWith("The UNC path should be of the form \\\\server\\share.", ex.Message); Assert.That(ex.ParamName, Is.EqualTo("path")); }
public void MockFileStreamFactory_CreateInNonExistingDirectory_ShouldThrowDirectoryNotFoundException(FileMode fileMode) { // Arrange var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"C:\Test")); // Act var fileStreamFactory = new MockFileStreamFactory(fileSystem); // Assert Assert.Throws <DirectoryNotFoundException>(() => fileStreamFactory.Create(XFS.Path(@"C:\Test\NonExistingDirectory\some_random_file.txt"), fileMode)); }
public void Should_Convert_Backslashes_To_Slashes_On_Unix() { Assert.AreEqual("/test/", XFS.Path(@"\test\")); }