System.IO.Abstractions.TestingHelpers.MockFileSystem.AddFile C# (CSharp) Method

AddFile() public method

public AddFile ( string path, MockFileData mockFile ) : void
path string
mockFile MockFileData
return void
        public void AddFile(string path, MockFileData mockFile)
        {
            var fixedPath = FixPath(path);
            lock (files)
            {
                if (FileExists(fixedPath))
                {
                    var isReadOnly = (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
                    var isHidden = (files[fixedPath].Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;

                    if (isReadOnly || isHidden)
                    {
                        throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ACCESS_TO_THE_PATH_IS_DENIED, path));
                    }
                }

                var directoryPath = Path.GetDirectoryName(fixedPath);

                if (!directory.Exists(directoryPath))
                {
                    AddDirectory(directoryPath);
                }

                files[fixedPath] = mockFile;
            }
        }

Usage Example

Example #1
0
        public void TestCleanByPattern()
        {
            // Prepare the source and target directories and files.
            var fileSystem = new System.IO.Abstractions.TestingHelpers.MockFileSystem();
            fileSystem.AddDirectory(@"C:\Source");
            fileSystem.AddFile(@"C:\Target\File.txt", new MockFileData("Data"));
            fileSystem.AddFile(@"C:\Target\File.doc", new MockFileData("Data"));

            // Run the test.
            Cleaner.CleanResult result = Cleaner.Clean(fileSystem, @"C:\Target", @"C:\Source", "*.txt");

            // Check the clean has worked properly.
            Assert.IsFalse(fileSystem.File.Exists(@"C:\Target\File.txt"));
            Assert.IsTrue(fileSystem.File.Exists(@"C:\Target\File.doc"));
        }
All Usage Examples Of System.IO.Abstractions.TestingHelpers.MockFileSystem::AddFile