Example #1
0
        public void MoveFile(FakeFile fakeFile, FilePath destination)
        {
            // Copy the file to the new location.
            CopyFile(fakeFile, destination, false);

            // Delete the original file.
            fakeFile.Delete();
        }
Example #2
0
 /// <summary>
 /// Hides the specified file.
 /// </summary>
 /// <param name="file">The file.</param>
 /// <returns>The same <see cref="FakeFile"/> instance so that multiple calls can be chained.</returns>
 public static FakeFile Hide(this FakeFile file)
 {
     if (file == null)
     {
         throw new ArgumentNullException(nameof(file));
     }
     file.Hidden = true;
     return(file);
 }
Example #3
0
 /// <summary>
 /// Sets the content of the provided file.
 /// </summary>
 /// <param name="file">The file.</param>
 /// <param name="content">The content.</param>
 /// <returns>The same <see cref="FakeFile"/> instance so that multiple calls can be chained.</returns>
 public static FakeFile SetContent(this FakeFile file, string content)
 {
     using (var stream = file.Open(FileMode.Create, FileAccess.Write, FileShare.None))
         using (var writer = new StreamWriter(stream))
         {
             writer.Write(content);
             writer.Close();
             stream.Close();
             return(file);
         }
 }
Example #4
0
 /// <summary>
 /// Gets the text content of the file (UTF-8 encoding).
 /// </summary>
 /// <param name="file">The file.</param>
 /// <returns>The text content of the file.</returns>
 public static string GetTextContent(this FakeFile file)
 {
     if (!file.Exists)
     {
         throw new FileNotFoundException("File could not be found.", file.Path.FullPath);
     }
     using (var stream = file.OpenRead())
         using (var reader = new StreamReader(stream))
         {
             return(reader.ReadToEnd());
         }
 }
Example #5
0
 public static string GetTextContent(this FakeFile file, Encoding encoding)
 {
     if (file == null)
     {
         throw new ArgumentNullException(nameof(file));
     }
     if (!file.Exists)
     {
         throw new FileNotFoundException("File could not be found.", file.Path.FullPath);
     }
     using (var stream = file.OpenRead())
         using (var reader = new StreamReader(stream, encoding))
         {
             return(reader.ReadToEnd());
         }
 }
Example #6
0
        public void CreateFile(FakeFile file)
        {
            // Get the directory that the file exists in.
            var directory = FindDirectory(file.Path.GetDirectory());

            if (directory == null)
            {
                file.Exists = false;
                throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", file.Path.FullPath));
            }

            if (!directory.Content.Files.ContainsKey(file.Path))
            {
                // Add the file to the directory.
                file.Exists = true;
                directory.Content.Add(file);
            }
        }
Example #7
0
        public void DeleteFile(FakeFile file)
        {
            if (!file.Exists)
            {
                throw new FileNotFoundException("File does not exist.", file.Path.FullPath);
            }

            // Find the directory.
            var directory = FindDirectory(file.Path.GetDirectory());

            // Remove the file from the directory.
            directory.Content.Remove(file);

            // Reset all properties.
            file.Exists        = false;
            file.Content       = null;
            file.ContentLength = 0;
        }
Example #8
0
 public static byte[] GetBinaryContent(this FakeFile file)
 {
     if (file == null)
     {
         throw new ArgumentNullException(nameof(file));
     }
     if (!file.Exists)
     {
         throw new FileNotFoundException("File could not be found.", file.Path.FullPath);
     }
     using (var stream = file.OpenRead())
         using (var reader = new BinaryReader(stream))
             using (var memory = new MemoryStream())
             {
                 reader.BaseStream.CopyTo(memory);
                 return(memory.ToArray());
             }
 }
Example #9
0
 public static FakeFile SetContent(this FakeFile file, string content)
 {
     if (file == null)
     {
         throw new ArgumentNullException(nameof(file));
     }
     if (content == null)
     {
         throw new ArgumentNullException(nameof(content));
     }
     using (var stream = file.Open(FileMode.Create, FileAccess.Write, FileShare.None))
         using (var writer = new StreamWriter(stream))
         {
             writer.Write(content);
             file.LastWriteTime = DateTime.Now;
             return(file);
         }
 }
Example #10
0
 public static FakeFile SetContent(this FakeFile file, string content)
 {
     if (file == null)
     {
         throw new ArgumentNullException("file");
     }
     if (content == null)
     {
         throw new ArgumentNullException("content");
     }
     using (var stream = file.Open(FileMode.Create, FileAccess.Write, FileShare.None))
         using (var writer = new StreamWriter(stream))
         {
             writer.Write(content);
             writer.Close();
             stream.Close();
             return(file);
         }
 }
Example #11
0
        public void CopyFile(FakeFile file, FilePath destination, bool overwrite)
        {
            if (!file.Exists)
            {
                throw new FileNotFoundException("File does not exist.");
            }

            // Already exists?
            var destinationFile = FindFile(destination);

            if (destinationFile != null)
            {
                if (!overwrite)
                {
                    const string format  = "{0} exists and overwrite is false.";
                    var          message = string.Format(CultureInfo.InvariantCulture, format, destination.FullPath);
                    throw new IOException(message);
                }
            }

            // Directory exists?
            var directory = FindDirectory(destination.GetDirectory());

            if (directory == null || !directory.Exists)
            {
                throw new DirectoryNotFoundException("The destination path {0} does not exist.");
            }

            // Make sure the file exist.
            if (destinationFile == null)
            {
                destinationFile = new FakeFile(this, destination);
            }

            // Copy the data from the original file to the destination.
            using (var input = file.OpenRead())
                using (var output = destinationFile.OpenWrite())
                {
                    input.CopyTo(output);
                }
        }
Example #12
0
        public void DeleteFile(FakeFile file)
        {
            if (!file.Exists)
            {
                throw new FileNotFoundException("File does not exist.", file.Path.FullPath);
            }

            if (file.Attributes.HasFlag(FileAttributes.ReadOnly))
            {
                throw new IOException($"Cannot delete readonly file '{file.Path.FullPath}'.");
            }

            // Find the directory.
            var directory = FindDirectory(file.Path.GetDirectory());

            // Remove the file from the directory.
            directory.Content.Remove(file);

            // Reset all properties.
            file.Exists        = false;
            file.Content       = null;
            file.ContentLength = 0;
        }
Example #13
0
 /// <summary>
 /// Hides the specified file.
 /// </summary>
 /// <param name="file">The file.</param>
 /// <returns>The same <see cref="FakeFile"/> instance so that multiple calls can be chained.</returns>
 public static FakeFile Hide(this FakeFile file)
 {
     file.Hidden = true;
     return(file);
 }
Example #14
0
 /// <summary>
 /// Gets the text content of the file.
 /// </summary>
 /// <param name="file">The file.</param>
 /// <returns>The text content of the file.</returns>
 public static string GetTextContent(this FakeFile file)
 {
     return(file?.GetTextContent(Encoding.UTF8));
 }
 public void Remove(FakeFile file)
 {
     _files.Remove(file.Path);
 }
 public void Add(FakeFile file)
 {
     _files.Add(file.Path, file);
 }
Example #17
0
 public FakeFileStream(FakeFile file)
 {
     _file     = file;
     _position = 0;
 }