Ejemplo n.º 1
0
        public void MockDirectoryInfo_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 }
            });

            var directorInfo = fileSystem.DirectoryInfo.FromDirectoryName(filePath);

            // Act
            var directorySecurity = directorInfo.GetAccessControl();

            // Assert
            Assert.That(directorySecurity, Is.EqualTo(expectedDirectorySecurity));
        }
        public void MockFileSystemWatcherFactory_FromPath_ShouldThrowNotImplementedException()
        {
            var path    = XFS.Path(@"y:\test");
            var factory = new MockFileSystemWatcherFactory();

            Assert.Throws <NotImplementedException>(() => factory.FromPath(path));
        }
            public static IEnumerable ForContentsIsNull()
            {
                var fileSystem   = new MockFileSystem();
                var path         = XFS.Path(@"c:\something\file.txt");
                var mockFileData = new MockFileData(string.Empty);

                mockFileData.Attributes = FileAttributes.ReadOnly;
                fileSystem.AddDirectory(@"c:\something");
                fileSystem.AddFile(path, mockFileData);
                List <string> fileContentEnumerable = null;

                string[] fileContentArray = null;

                // ReSharper disable ExpressionIsAlwaysNull
                Action writeEnumberable      = () => fileSystem.File.WriteAllLines(path, fileContentEnumerable);
                Action writeEnumberableUtf32 = () => fileSystem.File.WriteAllLines(path, fileContentEnumerable, Encoding.UTF32);
                Action writeArray            = () => fileSystem.File.WriteAllLines(path, fileContentArray);
                Action writeArrayUtf32       = () => fileSystem.File.WriteAllLines(path, fileContentArray, Encoding.UTF32);

                // ReSharper restore ExpressionIsAlwaysNull

                // IEnumerable
                yield return(new object[] { writeEnumberable, "WriteAllLines(string, IEnumerable<string>)" });

                yield return(new object[] { writeEnumberableUtf32, "WriteAllLines(string, IEnumerable<string>, Encoding.UTF32)" });

                // string[]
                yield return(new object[] { writeArray, "WriteAllLines(string, string[])" });

                yield return(new object[] { writeArrayUtf32, "WriteAllLines(string, string[], Encoding.UTF32)" });
            }
            public static IEnumerable ForFileIsReadOnly()
            {
                var fileSystem   = new MockFileSystem();
                var path         = XFS.Path(@"c:\something\file.txt");
                var mockFileData = new MockFileData(string.Empty);

                mockFileData.Attributes = FileAttributes.ReadOnly;
                fileSystem.AddFile(path, mockFileData);
                var    fileContentEnumerable = new List <string>();
                var    fileContentArray      = fileContentEnumerable.ToArray();
                Action writeEnumberable      = () => fileSystem.File.WriteAllLines(path, fileContentEnumerable);
                Action writeEnumberableUtf32 = () => fileSystem.File.WriteAllLines(path, fileContentEnumerable, Encoding.UTF32);
                Action writeArray            = () => fileSystem.File.WriteAllLines(path, fileContentArray);
                Action writeArrayUtf32       = () => fileSystem.File.WriteAllLines(path, fileContentArray, Encoding.UTF32);

                // IEnumerable
                yield return(new object[] { writeEnumberable, path, "WriteAllLines(string, IEnumerable<string>)" });

                yield return
                    (new object[] { writeEnumberableUtf32, path, "WriteAllLines(string, IEnumerable<string>, Encoding.UTF32)" });

                // string[]
                yield return(new object[] { writeArray, path, "WriteAllLines(string, string[])" });

                yield return(new object[] { writeArrayUtf32, path, "WriteAllLines(string, string[], Encoding.UTF32)" });
            }
Ejemplo n.º 5
0
        public MockFileSystem(IDictionary <string, MockFileData> files, string currentDirectory = "")
        {
            if (string.IsNullOrEmpty(currentDirectory))
            {
                currentDirectory = XFS.Path(DEFAULT_CURRENT_DIRECTORY);
            }

            pathVerifier = new PathVerifier(this);

            this.files = new Dictionary <string, MockFileData>(StringComparer.OrdinalIgnoreCase);

            Path              = new MockPath(this);
            File              = new MockFile(this);
            Directory         = new MockDirectory(this, File, currentDirectory);
            FileInfo          = new MockFileInfoFactory(this);
            FileStream        = new MockFileStreamFactory(this);
            DirectoryInfo     = new MockDirectoryInfoFactory(this);
            DriveInfo         = new MockDriveInfoFactory(this);
            FileSystemWatcher = new MockFileSystemWatcherFactory();

            if (files != null)
            {
                foreach (var entry in files)
                {
                    AddFile(entry.Key, entry.Value);
                }
            }

            if (!FileExists(currentDirectory))
            {
                AddDirectory(currentDirectory);
            }
        }
        public void MockFileInfo_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 }
            });

            var fileInfo = fileSystem.FileInfo.FromFileName(filePath);

            // Act
            var fileSecurity = fileInfo.GetAccessControl();

            // Assert
            Assert.That(fileSecurity, Is.EqualTo(expectedFileSecurity));
        }
        public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDirectory)
        {
            var fs = new MockFileSystem(null, XFS.Path(currentDirectory));

            var actualCurrentDirectory = fs.DirectoryInfo.FromDirectoryName(".");

            Assert.IsTrue(actualCurrentDirectory.Exists);
        }
        public void MockFileSystemWatcherFactory_CreateNewWithPathAndFilter_ShouldThrowNotImplementedException()
        {
            var path    = XFS.Path(@"y:\test");
            var filter  = "*.txt";
            var factory = new MockFileSystemWatcherFactory();

            Assert.Throws <NotImplementedException>(() => factory.CreateNew(path, filter));
        }
        public void MockFileSystem_AddDirectory_ShouldCreateDirectory()
        {
            string baseDirectory = XFS.Path(@"C:\Test");
            var    fileSystem    = new MockFileSystem();

            fileSystem.AddDirectory(baseDirectory);

            Assert.IsTrue(fileSystem.Directory.Exists(baseDirectory));
        }
        public void MockFileSystem_AddFilesFromEmbeddedResource_ShouldAddAllTheFiles()
        {
            var fileSystem = new MockFileSystem();

            fileSystem.AddFilesFromEmbeddedNamespace(XFS.Path(@"C:\"), Assembly.GetExecutingAssembly(), "System.IO.Abstractions.TestingHelpers.Tests.TestFiles");

            Assert.Contains(XFS.Path(@"C:\TestFile.txt"), fileSystem.AllFiles.ToList());
            Assert.Contains(XFS.Path(@"C:\SecondTestFile.txt"), fileSystem.AllFiles.ToList());
        }
        public void MockFileSystem_AddDirectory_TrailingSlashAllowedButNotRequired(string path)
        {
            var fileSystem = new MockFileSystem();
            var path2      = XFS.Path(path);

            fileSystem.AddDirectory(path2);

            Assert.IsTrue(fileSystem.FileExists(path2));
        }
        public void MockFileSystem_AddFileFromEmbeddedResource_ShouldAddTheFile()
        {
            var fileSystem = new MockFileSystem();

            fileSystem.AddFileFromEmbeddedResource(XFS.Path(@"C:\TestFile.txt"), Assembly.GetExecutingAssembly(), "System.IO.Abstractions.TestingHelpers.Tests.TestFiles.TestFile.txt");
            var result = fileSystem.GetFile(XFS.Path(@"C:\TestFile.txt"));

            Assert.AreEqual(new UTF8Encoding().GetBytes("This is a test file."), result.Contents);
        }
        public void MockFileSystem_DriveInfo_ShouldNotThrowAnyException()
        {
            var fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(XFS.Path(@"C:\Test"));

            var actualResults = fileSystem.DriveInfo.GetDrives();

            Assert.IsNotNull(actualResults);
        }
        public void MockFile_WriteAllBytesAsync_ShouldThrowDirectoryNotFoundExceptionIfPathDoesNotExists()
        {
            var    fileSystem  = new MockFileSystem();
            string path        = XFS.Path(@"c:\something\file.txt");
            var    fileContent = new byte[] { 1, 2, 3, 4 };

            AsyncTestDelegate action = () => fileSystem.File.WriteAllBytesAsync(path, fileContent);

            Assert.ThrowsAsync <DirectoryNotFoundException>(action);
        }
        public void MockFileSystem_DefaultState_DefaultTempDirectoryExists()
        {
            var tempDirectory = XFS.Path(@"C:\temp");

            var mockFileSystem         = new MockFileSystem();
            var mockFileSystemOverload = new MockFileSystem(null, string.Empty);

            Assert.IsTrue(mockFileSystem.Directory.Exists(tempDirectory));
            Assert.IsTrue(mockFileSystemOverload.Directory.Exists(tempDirectory));
        }
        public void MockFile_WriteAllBytes_ShouldThrowAnArgumentExceptionIfContainsIllegalCharacters()
        {
            var fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(XFS.Path(@"C:"));

            TestDelegate action = () => fileSystem.File.WriteAllBytes(XFS.Path(@"C:\a<b.txt"), new byte[] { 123 });

            Assert.Throws <ArgumentException>(action);
        }
        public void MockFileSystem_FileSystemWatcher_ShouldBeAssignable()
        {
            var path       = XFS.Path(@"C:\root");
            var fileSystem = new MockFileSystem {
                FileSystemWatcher = new TestFileSystemWatcherFactory()
            };
            var watcher = fileSystem.FileSystemWatcher.FromPath(path);

            Assert.AreEqual(path, watcher.Path);
        }
Ejemplo n.º 18
0
        public void MockFile_ReadAllBytes_ShouldTolerateAltDirectorySeparatorInPath()
        {
            var fileSystem = new MockFileSystem();
            var path       = XFS.Path("C:" + fileSystem.Path.DirectorySeparatorChar + "test.dat");
            var altPath    = XFS.Path("C:" + fileSystem.Path.AltDirectorySeparatorChar + "test.dat");
            var data       = new byte[] { 0x12, 0x34, 0x56, 0x78 };

            fileSystem.AddFile(path, new MockFileData(data));

            Assert.AreEqual(data, fileSystem.File.ReadAllBytes(altPath));
        }
        public void MockFileSystem_AddDirectory_ShouldThrowExceptionIfDirectoryIsReadOnly()
        {
            string baseDirectory = XFS.Path(@"C:\Test");
            var    fileSystem    = new MockFileSystem();

            fileSystem.AddFile(baseDirectory, new MockFileData(string.Empty));
            fileSystem.File.SetAttributes(baseDirectory, FileAttributes.ReadOnly);

            TestDelegate action = () => fileSystem.AddDirectory(baseDirectory);

            Assert.Throws <UnauthorizedAccessException>(action);
        }
        public void MockFile_WriteAllBytes_ShouldWriteDataToMemoryFileSystem()
        {
            string path        = XFS.Path(@"c:\something\demo.txt");
            var    fileSystem  = new MockFileSystem();
            var    fileContent = new byte[] { 1, 2, 3, 4 };

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

            fileSystem.File.WriteAllBytes(path, fileContent);

            Assert.AreEqual(fileContent, fileSystem.GetFile(path).Contents);
        }
        public void MockFileSystem_FileSystemWatcher_PathAndFilterShouldBeAssignable()
        {
            var path       = XFS.Path(@"C:\root");
            var filter     = "*.txt";
            var fileSystem = new MockFileSystem {
                FileSystemWatcher = new TestFileSystemWatcherFactory()
            };
            var watcher = fileSystem.FileSystemWatcher.CreateNew(path, filter);

            Assert.AreEqual(path, watcher.Path);
            Assert.AreEqual(filter, watcher.Filter);
        }
Ejemplo n.º 22
0
        public void MockFile_ReadAllBytes_ShouldReturnDataSavedByWriteAllBytes()
        {
            string path        = XFS.Path(@"c:\something\demo.txt");
            var    fileSystem  = new MockFileSystem();
            var    fileContent = new byte[] { 1, 2, 3, 4 };

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

            fileSystem.File.WriteAllBytes(path, fileContent);

            Assert.AreEqual(fileContent, fileSystem.File.ReadAllBytes(path));
        }
        public void MockFile_SetAttributes_ShouldThrowFileNotFoundExceptionIfMissingDirectory()
        {
            var path       = XFS.Path(@"C:\something");
            var attributes = FileAttributes.Normal;
            var fileSystem = new MockFileSystem();

            TestDelegate action = () => fileSystem.File.SetAttributes(path, attributes);

            var exception = Assert.Throws <FileNotFoundException>(action);

            Assert.That(exception.FileName, Is.EqualTo(path));
        }
        public void MockFileSystem_AddFile_ShouldHandleNullFileDataAsEmpty()
        {
            var path       = XFS.Path(@"c:\something\nullish.txt");
            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { path, null }
            });

            var result = fileSystem.File.ReadAllText(path);

            Assert.IsEmpty(result, "Null MockFileData should be allowed for and result in an empty file.");
        }
        public void MockFile_WriteAllBytes_ShouldThrowAnArgumentNullExceptionIfBytesAreNull()
        {
            var    fileSystem = new MockFileSystem();
            string path       = XFS.Path(@"c:\something\demo.txt");

            TestDelegate action = () => fileSystem.File.WriteAllBytes(path, null);

            var exception = Assert.Throws <ArgumentNullException>(action);

            Assert.That(exception.Message, Does.StartWith("Value cannot be null."));
            Assert.That(exception.ParamName, Is.EqualTo("bytes"));
        }
        public void MockFile_WriteAllBytes_ShouldThrowDirectoryNotFoundExceptionIfPathDoesNotExists()
        {
            // Arrange
            var    fileSystem  = new MockFileSystem();
            string path        = XFS.Path(@"c:\something\file.txt");
            var    fileContent = new byte[] { 1, 2, 3, 4 };

            // Act
            Action action = () => fileSystem.File.WriteAllBytes(path, fileContent);

            // Assert
            Assert.Throws <DirectoryNotFoundException>(action);
        }
        public void MockFileSystem_GetFiles_ThrowsArgumentExceptionForInvalidCharacters()
        {
            // Arrange
            const string path       = @"c:\";
            var          fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(XFS.Path(path));

            // Act
            TestDelegate getFilesWithInvalidCharacterInPath = () => fileSystem.Directory.GetFiles($"{path}{'\0'}.txt");

            // Assert
            Assert.Throws <ArgumentException>(getFilesWithInvalidCharacterInPath);
        }
        public void MockFile_WriteAllBytes_ShouldThrowAnUnauthorizedAccessExceptionIfFileIsHidden()
        {
            string path       = XFS.Path(@"c:\something\demo.txt");
            var    fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>
            {
                { path, new MockFileData("this is hidden") },
            });

            fileSystem.File.SetAttributes(path, FileAttributes.Hidden);

            TestDelegate action = () => fileSystem.File.WriteAllBytes(path, new byte[] { 123 });

            Assert.Throws <UnauthorizedAccessException>(action, "Access to the path '{0}' is denied.", path);
        }
        public void MockFileSystem_AddFile_ShouldHandleUnnormalizedSlashes()
        {
            var path                = XFS.Path(@"c:\d1\d2\file.txt");
            var alternatePath       = XFS.Path("c:/d1/d2/file.txt");
            var alternateParentPath = XFS.Path("c://d1//d2/");
            var fs = new MockFileSystem();

            fs.AddFile(path, new MockFileData("Hello"));

            var fileCount  = fs.Directory.GetFiles(alternateParentPath).Length;
            var fileExists = fs.File.Exists(alternatePath);

            Assert.AreEqual(1, fileCount);
            Assert.IsTrue(fileExists);
        }
        public void MockFile_WriteAllBytes_ShouldThrowAnArgumentNullExceptionIfBytesAreNull()
        {
            // Arrange
            var    fileSystem = new MockFileSystem();
            string path       = XFS.Path(@"c:\something\demo.txt");

            // Act
            Action action = () => fileSystem.File.WriteAllBytes(path, null);

            // Assert
            var exception = Assert.Throws <ArgumentNullException>(action);

            exception.Message.Should().StartWith("Value cannot be null.");
            Assert.Equal("bytes", exception.ParamName);
        }