public void DeleteFile(FakeFile file) { if (!file.Exists) { throw new FileNotFoundException("File does not exist.", file.Path.FullPath); } if ((file.Attributes & FileAttributes.ReadOnly) != 0) { throw new IOException($"Cannot delete readonly file '{file.Path.FullPath}'."); } // Find the directory. var directory = FindDirectory(file.Path.GetDirectory()); if (directory != null) { // Remove the file from the directory. directory.Content.Remove(file); } // Reset all properties. file.Exists = false; file.Content = new byte[4096]; file.ContentLength = 0; }
/// <summary> /// Determines if a specified file has a UTF-8 BOM. /// </summary> /// <param name="file">The file.</param> /// <returns>Whether or not the specified file has a UTF-8 BOM.</returns> public static bool HasUTF8BOM(this FakeFile file) { var content = GetBinaryContent(file); var preamble = Encoding.UTF8.GetPreamble(); return(content.StartsWith(preamble)); }
public void MoveFile(FakeFile fakeFile, FilePath destination) { // Copy the file to the new location. CopyFile(fakeFile, destination, false); // Delete the original file. fakeFile.Delete(); }
/// <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); }
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); } }
public static string GetTextContent(this FakeFile file, Encoding?encoding = null) { if (file == null) { throw new ArgumentNullException(nameof(file)); } if (!file.Exists) { throw new FileNotFoundException("File could not be found.", file.Path.FullPath); } encoding ??= Encoding.Default; using (var stream = file.OpenRead()) using (var reader = new StreamReader(stream, encoding)) { return(reader.ReadToEnd()); } }
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()); } }
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?.Exists != true) { 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); } }
public static FakeFile SetTextContent(this FakeFile file, string content, Encoding?encoding = null) { if (file == null) { throw new ArgumentNullException(nameof(file)); } if (content == null) { throw new ArgumentNullException(nameof(content)); } encoding ??= Encoding.Default; using (var stream = file.Open(FileMode.Create, FileAccess.Write, FileShare.None)) { var bytes = encoding.GetBytes(content); stream.Write(bytes, 0, bytes.Length); return(file); } }
public FakeFileStream(FakeFile file) { _file = file; _position = 0; }
public void Remove(FakeFile file) { _files.Remove(file.Path); }
public void Add(FakeFile file) { _files.Add(file.Path, file); }