コード例 #1
0
        public void MockFile_Copy_ShouldThrowNotSupportedExceptionWhenTargetPathContainsInvalidChars_Message()
        {
            if (XFS.IsUnixPlatform())
            {
                Assert.True(true, "Path.GetInvalidChars() does not return anything on Mono");
                return;
            }

            var sourceFilePath = XFS.Path(@"c:\something\demo.txt");
            var fileSystem     = new MockFileSystem();

            foreach (var invalidChar in fileSystem.Path.GetInvalidPathChars())
            {
                var destFilePath = XFS.Path(@"c:\some" + invalidChar + @"thing\demo.txt");

                var exception =
                    Assert.Throws <ArgumentException>(() => fileSystem.File.Copy(sourceFilePath, destFilePath));

                exception.Message.Should().Be("Illegal characters in path.", exception.Message,
                                              string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
            }
        }
コード例 #2
0
        public void MockDirectoryInfo_LastWriteTime_ShouldSetCreationTimeOfFolderInMemoryFileSystem()
        {
            // Arrange
            var lastWriteTime = DateTime.Now.AddHours(-4);
            var fileSystem    = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { XFS.Path(@"c:\temp\folder\folder"), new MockDirectoryData() },
            });

            var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\temp\folder"))
            {
                LastWriteTime = lastWriteTime
            };

            // Act
            var newTime = DateTime.Now;

            directoryInfo.LastWriteTime = newTime;

            // Assert
            Assert.AreEqual(newTime, directoryInfo.LastWriteTime);
        }
        public void MockFileInfo_LastWriteTimeUtc_ShouldSetLastWriteTimeUtcOfFileInMemoryFileSystem()
        {
            // Arrange
            var lastWriteTime = DateTime.Now.AddHours(-4);
            var fileData      = new MockFileData("Demo text content")
            {
                LastWriteTime = lastWriteTime
            };
            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
            var newUtcTime = DateTime.UtcNow;

            fileInfo.LastWriteTime = newUtcTime;

            // Assert
            Assert.AreEqual(newUtcTime, fileInfo.LastWriteTime);
        }
        public void MockFileInfo_AppendText_ShouldAddTextToFileInMemoryFileSystem()
        {
            // 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
            using (var file = fileInfo.AppendText())
                file.WriteLine("This should be at the end");

            string newcontents;

            using (var newfile = fileInfo.OpenText())
                newcontents = newfile.ReadToEnd();

            // Assert
            Assert.AreEqual("Demo text contentThis should be at the end\r\n", newcontents);
        }
        public void MockFileInfo_CreationTime_ShouldSetCreationTimeOfFileInMemoryFileSystem()
        {
            // Arrange
            var creationTime = DateTime.Now.AddHours(-4);
            var fileData     = new MockFileData("Demo text content")
            {
                CreationTime = creationTime
            };
            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
            var newTime = DateTime.Now;

            fileInfo.CreationTime = newTime;

            // Assert
            Assert.AreEqual(newTime, fileInfo.CreationTime);
        }
コード例 #6
0
        public void MockFile_WriteAllTextMultipleLines_ShouldWriteTextFileToMemoryFileSystem()
        {
            // Arrange
            string path = XFS.Path(@"c:\something\demo.txt");

            var fileContent = new List <string> {
                "Hello there!", "Second line!"
            };
            var expected = "Hello there!" + Environment.NewLine + "Second line!" + Environment.NewLine;

            var fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(@"c:\something");

            // Act
            fileSystem.File.WriteAllLines(path, fileContent);

            // Assert
            Assert.Equal(
                expected,
                fileSystem.GetFile(path).TextContents);
        }
コード例 #7
0
        public void MockDirectory_EnumerateDirectories_ShouldThrowWhenPathIsNotMocked()
        {
            // Arrange
            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { XFS.Path(@"c:\a.gif"), new MockFileData("Demo text content") },
                { XFS.Path(@"c:\b.txt"), new MockFileData("Demo text content") },
                { XFS.Path(@"c:\c.txt"), new MockFileData("Demo text content") },
                { XFS.Path(@"c:\a\a.txt"), new MockFileData("Demo text content") },
                { XFS.Path(@"c:\a\b.gif"), new MockFileData("Demo text content") },
                { XFS.Path(@"c:\a\c.txt"), new MockFileData("Demo text content") },
                { XFS.Path(@"c:\a\a\a.txt"), new MockFileData("Demo text content") },
                { XFS.Path(@"c:\a\a\b.txt"), new MockFileData("Demo text content") },
                { XFS.Path(@"c:\a\a\c.gif"), new MockFileData("Demo text content") },
            });

            // Act
            TestDelegate action = () => fileSystem.Directory.EnumerateDirectories(XFS.Path(@"c:\d"));

            // Assert
            Assert.Throws <DirectoryNotFoundException>(action);
        }
コード例 #8
0
        public void MockFileInfo_LastAccessTimeUtc_ShouldSetCreationTimeUtcOfFileInMemoryFileSystem()
        {
            // Arrange
            DateTime     lastAccessTime = DateTime.Now.AddHours(-4);
            MockFileData fileData       = new MockFileData("Demo text content")
            {
                LastAccessTime = lastAccessTime
            };
            MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { XFS.Path(@"c:\a.txt"), fileData }
            });
            MockFileInfo fileInfo = new MockFileInfo(fileSystem, XFS.Path(@"c:\a.txt"));

            // Act
            DateTime newUtcTime = DateTime.UtcNow;

            fileInfo.LastAccessTimeUtc = newUtcTime;

            // Assert
            Assert.Equal(newUtcTime, fileInfo.LastAccessTimeUtc);
        }
コード例 #9
0
        public void TestMethod1()
        {
            string path       = XFS.Path(@"c:\something\demo.txt");
            var    fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { path, new MockFileData("Demo text content") }
            });

            WizardImplementationClass   wizard = new WizardImplementationClass(fileSystem);
            Dictionary <string, string> replacementDictionary = new Dictionary <string, string>();

            string[] customParameters = { @"TemplatePath\MyTemplate.vstemplate" };

            try
            {
                wizard.RunStarted(null, replacementDictionary, WizardRunKind.AsNewProject, customParameters);
                Assert.Fail(); // never should get here
            }
            catch (WizardCancelledException)
            {
            }
        }
コード例 #10
0
        public void MockDirectory_SetAccessControl_ShouldReturnAccessControlOfDirectoryData()
        {
            // Arrange
            var filePath = XFS.Path(@"c:\a\");
            var fileData = new MockDirectoryData();

            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>()
            {
                { filePath, fileData }
            });

            // Act
            var expectedAccessControl = new DirectorySecurity();

            expectedAccessControl.SetAccessRuleProtection(false, false);
            fileSystem.Directory.SetAccessControl(filePath, expectedAccessControl);

            // Assert
            var accessControl = fileSystem.Directory.GetAccessControl(filePath);

            Assert.That(accessControl, Is.EqualTo(expectedAccessControl));
        }
コード例 #11
0
        public void MockFile_Open_OpensExistingFileOnOpenOrCreate()
        {
            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.OpenOrCreate);
            MockFileData file   = filesystem.GetFile(filepath);

            Assert.Equal(stream.Position, 0);
            Assert.Equal(stream.Length, file.Contents.Length);

            byte[] data;
            using (BinaryReader br = new BinaryReader(stream))
            {
                data = br.ReadBytes((int)stream.Length);
            }

            Assert.Equal(file.Contents, data);
        }
コード例 #12
0
        public void MockDirectoryInfo_EnumerateFiles_ShouldReturnAllFiles()
        {
            // Arrange
            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                //Files "above" in folder we're querying
                { XFS.Path(@"c:\temp\a.txt"), "" },

                //Files in the folder we're querying
                { XFS.Path(@"c:\temp\folder\b.txt"), "" },
                { XFS.Path(@"c:\temp\folder\c.txt"), "" },

                //Files "below" the folder we're querying
                { XFS.Path(@"c:\temp\folder\deeper\d.txt"), "" }
            });

            // Act
            var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\temp\folder"));

            // Assert
            Assert.AreEqual(new[] { "b.txt", "c.txt" }, directoryInfo.EnumerateFiles().ToList().Select(x => x.Name).ToArray());
        }
        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));
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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_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:\\"));
            }
        }
コード例 #18
0
        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));
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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));
        }
コード例 #22
0
 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") },
コード例 #23
0
        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 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\") }));
        }
コード例 #26
0
 public void TrimSlashes_RootedPath_DontAlterPathWithoutTrailingSlashes()
 {
     Assert.AreEqual(XFS.Path(@"c:\x"), XFS.Path(@"c:\x").TrimSlashes());
 }
        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 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"));
        }
コード例 #30
0
        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")));
        }