Exemple #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();
        }
Exemple #2
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);
         }
 }
Exemple #3
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());
         }
 }
Exemple #4
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("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);
            }
        }
Exemple #5
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;
        }
Exemple #6
0
        public void CopyFile(FakeFile file, FilePath destination, bool overwrite)
        {
            if (!file.Exists)
            {
                throw new FileNotFoundException("File do 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(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} do 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);
                }
        }
Exemple #7
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);
 }
Exemple #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FakeFileStream"/> class.
 /// </summary>
 /// <param name="file">The file.</param>
 public FakeFileStream(FakeFile file)
 {
     _file     = file;
     _position = 0;
 }
Exemple #9
0
 public void Remove(FakeFile file)
 {
     _files.Remove(file.Path);
 }
Exemple #10
0
 public void Add(FakeFile file)
 {
     _files.Add(file.Path, file);
 }