Esempio n. 1
0
        /// <summary>
        /// Creates a file at the specified path.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The path.</param>
        /// <param name="contentsBytes">The file contents.</param>
        /// <returns>The same <see cref="FakeFile"/> instance so that multiple calls can be chained.</returns>
        public static FakeFile CreateFile(this FakeFileSystem fileSystem, FilePath path, byte[] contentsBytes)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException("fileSystem");
            }
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (contentsBytes == null)
            {
                throw new ArgumentNullException("contentsBytes");
            }
            CreateDirectory(fileSystem, path.GetDirectory());
            var file = fileSystem.GetFile(path);

            if (!file.Exists)
            {
                using (var stream = file.OpenWrite())
                {
                    using (var ms = new MemoryStream(contentsBytes))
                    {
                        ms.CopyTo(stream);
                    }
                }
            }
            return(file);
        }
Esempio n. 2
0
 /// <summary>
 /// Ensures that the specified file do not exist.
 /// </summary>
 /// <param name="fileSystem">The file system.</param>
 /// <param name="path">The path.</param>
 public static void EnsureFileDoNotExist(this FakeFileSystem fileSystem, FilePath path)
 {
     var file = fileSystem.GetFile(path);
     if (file != null && file.Exists)
     {
         file.Delete();
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Creates a file at the specified path.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The path.</param>
        /// <returns>The same <see cref="FakeFile"/> instance so that multiple calls can be chained.</returns>
        public static FakeFile CreateFile(this FakeFileSystem fileSystem, FilePath path)
        {
            CreateDirectory(fileSystem, path.GetDirectory());

            var file = fileSystem.GetFile(path);
            if (!file.Exists)
            {
                file.OpenWrite().Close();
            }

            return file;
        }
Esempio n. 4
0
        /// <summary>
        /// Ensures that the specified file do not exist.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The path.</param>
        public static void EnsureFileDoNotExist(this FakeFileSystem fileSystem, FilePath path)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException("fileSystem");
            }
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            var file = fileSystem.GetFile(path);

            if (file != null && file.Exists)
            {
                file.Delete();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a file at the specified path.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The path.</param>
        /// <param name="contentsBytes">The file contents.</param>
        /// <returns>The same <see cref="FakeFile"/> instance so that multiple calls can be chained.</returns>
        public static FakeFile CreateFile(this FakeFileSystem fileSystem, FilePath path, byte[] contentsBytes)
        {
            CreateDirectory(fileSystem, path.GetDirectory());

            var file = fileSystem.GetFile(path);
            if (!file.Exists)
            {
                using (var stream = file.OpenWrite())
                {
                    using (var ms = new MemoryStream(contentsBytes))
                    {
                        ms.CopyTo(stream);
                    }
                }
            }

            return file;
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a file at the specified path.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The path.</param>
        /// <returns>The same <see cref="FakeFile"/> instance so that multiple calls can be chained.</returns>
        public static FakeFile CreateFile(this FakeFileSystem fileSystem, FilePath path)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException("fileSystem");
            }
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            CreateDirectory(fileSystem, path.GetDirectory());
            var file = fileSystem.GetFile(path);

            if (!file.Exists)
            {
                file.OpenWrite().Close();
            }
            return(file);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a file at the specified path.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The path.</param>
        /// <param name="attributes">The file attributes to set.</param>
        /// <returns>The same <see cref="FakeFile"/> instance so that multiple calls can be chained.</returns>
        public static FakeFile CreateFile(this FakeFileSystem fileSystem, FilePath path, FileAttributes attributes = 0)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            CreateDirectory(fileSystem, path.GetDirectory());
            var file = fileSystem.GetFile(path);

            if (!file.Exists)
            {
                file.OpenWrite().Dispose();
            }
            file.Attributes = attributes;
            return(file);
        }