public void MockFile_Open_OpensExistingFileOnOpenOrCreate()
        {
            string filepath   = XFS.Path(@"c:\something\does\exist.txt");
            var    filesystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { filepath, new MockFileData("I'm here") }
            });

            var stream = filesystem.File.Open(filepath, FileMode.OpenOrCreate);
            var file   = filesystem.GetFile(filepath);

            Assert.That(stream.Position, Is.EqualTo(0));
            Assert.That(stream.Length, Is.EqualTo(file.Contents.Length));

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

            CollectionAssert.AreEqual(file.Contents, data);
        }
        public void MockFile_OpenText_ShouldRetainCreationTime()
        {
            // Arrange
            var    fs           = new MockFileSystem();
            string filepath     = XFS.Path(@"C:\TestData\test.txt");
            var    file         = new MockFileData(@"I'm here");
            var    creationTime = new DateTime(2012, 03, 21);

            file.CreationTime = creationTime;
            fs.AddFile(filepath, file);

            // Act
            using (var reader = fs.File.OpenText(filepath))
            {
                reader.ReadLine();
            }

            // Assert
            Assert.AreEqual(creationTime, fs.FileInfo.FromFileName(filepath).CreationTime);
        }
Пример #3
0
        public void Mockfile_Create_CanWriteToNewStream()
        {
            string fullPath   = XFS.Path(@"c:\something\demo.txt");
            var    fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(XFS.Path(@"c:\something"));
            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));
        }
Пример #4
0
        public void MockFile_Copy_ShouldCloneContents()
        {
            var sourceFileName = XFS.Path(@"c:\source\demo.txt");
            var destFileName   = XFS.Path(@"c:\source\demo_copy.txt");

            var mockFileSystem = new MockFileSystem();

            mockFileSystem.AddFile(sourceFileName, "Original");
            mockFileSystem.File.Copy(sourceFileName, destFileName);

            using (var stream = mockFileSystem.File.Open(sourceFileName, FileMode.Open, FileAccess.ReadWrite))
            {
                var binaryWriter = new System.IO.BinaryWriter(stream);

                binaryWriter.Seek(0, SeekOrigin.Begin);
                binaryWriter.Write("Modified");
            }

            Assert.AreEqual("Original", mockFileSystem.File.ReadAllText(destFileName));
        }
        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();

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

            // Assert
            Assert.AreEqual(
                expected,
                fileSystem.GetFile(path).TextContents);
        }
Пример #6
0
        public void MockFile_WriteAllText_Encoding_ShouldWriteTextFileToMemoryFileSystem(KeyValuePair <Encoding, byte[]> encodingsWithContents)
        {
            // Arrange
            const string FileContent = "Hello there! Dzięki.";
            string       path        = XFS.Path(@"c:\something\demo.txt");

            byte[]   expectedBytes = encodingsWithContents.Value;
            Encoding encoding      = encodingsWithContents.Key;
            var      fileSystem    = new MockFileSystem();

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

            // Act
            fileSystem.File.WriteAllText(path, FileContent, encoding);

            // Assert
            var actualBytes = fileSystem.GetFile(path).Contents;

            Assert.AreEqual(expectedBytes, actualBytes);
        }
Пример #7
0
        public void MockFile_ReadLines_ShouldReturnOriginalDataWithCustomEncoding()
        {
            // Arrange
            string text        = "Hello\r\nthere\rBob\nBob!";
            var    encodedText = Encoding.BigEndianUnicode.GetBytes(text);
            var    fileSystem  = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { XFS.Path(@"c:\something\demo.txt"), new MockFileData(encodedText) }
            });

            var file = new MockFile(fileSystem);

            // Act
            var result = file.ReadLines(XFS.Path(@"c:\something\demo.txt"), Encoding.BigEndianUnicode);

            // Assert
            CollectionAssert.AreEqual(
                new[] { "Hello", "there", "Bob", "Bob!" },
                result);
        }
        public void MockFile_Copy_ShouldCloneBinaryContents()
        {
            var sourceFileName = XFS.Path(@"c:\source\demo.bin");
            var destFileName   = XFS.Path(@"c:\source\demo_copy.bin");

            byte[] original       = new byte[] { 0xC0 };
            var    mockFileSystem = new MockFileSystem();

            mockFileSystem.AddFile(sourceFileName, new MockFileData(original));
            mockFileSystem.File.Copy(sourceFileName, destFileName);

            using (var stream = mockFileSystem.File.Open(sourceFileName, FileMode.Open, FileAccess.ReadWrite))
            {
                var binaryWriter = new System.IO.BinaryWriter(stream);

                binaryWriter.Seek(0, SeekOrigin.Begin);
                binaryWriter.Write("Modified");
            }

            CollectionAssert.AreEqual(original, mockFileSystem.File.ReadAllBytes(destFileName));
        }
        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.That(ex.Message, Is.EqualTo(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path)));

            ex = Assert.Throws <DirectoryNotFoundException>(() => fileSystem.File.AppendAllText(path, "some text", Encoding.Unicode));
            Assert.That(ex.Message, Is.EqualTo(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path)));
        }
Пример #10
0
        public void MockFileStream_Dispose_ShouldNotResurrectFile()
        {
            var fileSystem = new MockFileSystem();
            var path       = XFS.Path("C:\\test");
            var directory  = fileSystem.Path.GetDirectoryName(path);

            fileSystem.AddFile(path, new MockFileData("Bla"));
            var stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Delete);

            var fileCount1 = fileSystem.Directory.GetFiles(directory, "*").Length;

            fileSystem.File.Delete(path);
            var fileCount2 = fileSystem.Directory.GetFiles(directory, "*").Length;

            stream.Dispose();
            var fileCount3 = fileSystem.Directory.GetFiles(directory, "*").Length;

            Assert.AreEqual(1, fileCount1, "File should have existed");
            Assert.AreEqual(0, fileCount2, "File should have been deleted");
            Assert.AreEqual(0, fileCount3, "Disposing stream should not have resurrected the file");
        }
        public void MockFile_Copy_ShouldThrowNotSupportedExceptionWhenTargetFileNameContainsInvalidChars_Message()
        {
            if (XFS.IsUnixPlatform())
            {
                Assert.Pass("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.GetInvalidFileNameChars().Where(x => x != fileSystem.Path.DirectorySeparatorChar))
            {
                var destFilePath = XFS.Path(@"c:\something\demo.txt") + invalidChar;

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

                Assert.That(exception.Message, Is.EqualTo("Illegal characters in path."),
                            string.Format("Testing char: [{0:c}] \\{1:X4}", invalidChar, (int)invalidChar));
            }
        }
        public void MockFileStream_Dispose_ShouldNotResurrectFile()
        {
            // path in this test case is a subject to Directory.GetParent(path) Linux issue
            // https://github.com/System-IO-Abstractions/System.IO.Abstractions/issues/395
            var fileSystem = new MockFileSystem();
            var path       = XFS.Path("C:\\some_folder\\test");
            var directory  = fileSystem.Path.GetDirectoryName(path);

            fileSystem.AddFile(path, new MockFileData("Bla"));
            var stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Delete);

            var fileCount1 = fileSystem.Directory.GetFiles(directory, "*").Length;

            fileSystem.File.Delete(path);
            var fileCount2 = fileSystem.Directory.GetFiles(directory, "*").Length;

            stream.Dispose();
            var fileCount3 = fileSystem.Directory.GetFiles(directory, "*").Length;

            Assert.AreEqual(1, fileCount1, "File should have existed");
            Assert.AreEqual(0, fileCount2, "File should have been deleted");
            Assert.AreEqual(0, fileCount3, "Disposing stream should not have resurrected the file");
        }
        public void MockFile_Create_TruncateShouldClearFileContentsOnOpen()
        {
            // Arrange
            string testFileName = XFS.Path(@"c:\someFile.txt");
            var    fileSystem   = new MockFileSystem();

            using (var stream = fileSystem.FileStream.Create(testFileName, FileMode.Create, FileAccess.Write))
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write("original_text");
                }
            }

            // Act
            using (var stream = fileSystem.FileStream.Create(testFileName, FileMode.Truncate, FileAccess.Write))
            {
                // Opening the stream is enough to reset the contents
            }

            // Assert
            Assert.That(fileSystem.File.ReadAllText(testFileName), Is.EqualTo(string.Empty));
        }
        public void MockFile_AppendAllText_ShouldPersistNewTextWithCustomEncoding()
        {
            // Arrange
            string path       = XFS.Path(@"c:\something\demo.txt");
            var    fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { path, new MockFileData("Demo text content") }
            });

            var file = new MockFile(fileSystem);

            // Act
            file.AppendAllText(path, "+ some text", Encoding.BigEndianUnicode);

            // Assert
            var 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
                };
            }

            CollectionAssert.AreEqual(
                expected,
                file.ReadAllBytes(path));
        }
        public void MockFile_Copy_ShouldThrowExceptionWhenFileExistsAtDestination()
        {
            string sourceFileName = XFS.Path(@"c:\source\demo.txt");
            var    sourceContents = new MockFileData("Source content");
            string destFileName   = XFS.Path(@"c:\destination\demo.txt");
            var    fileSystem     = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { sourceFileName, sourceContents },
                { destFileName, new MockFileData("Destination content") }
            });

            Assert.Throws <IOException>(() => fileSystem.File.Copy(sourceFileName, destFileName), XFS.Path(@"The file c:\destination\demo.txt already exists."));
        }
 public void Should_Convert_Backslashes_To_Slashes_On_Unix()
 {
     Assert.AreEqual("/test/", MockUnixSupport.Path(@"\test\", () => true));
 }
 public void Should_Remove_Drive_Letter_On_Unix()
 {
     Assert.AreEqual("/test/", MockUnixSupport.Path(@"c:\test\", () => true));
 }