public static void SafeAction(this FileBase fileBase, string file, Action <string> action) { if (fileBase == null) { throw new ArgumentNullException("fileBase"); } if (string.IsNullOrEmpty(file)) { throw new ArgumentNullException("file"); } if (action == null) { throw new ArgumentNullException("action"); } var tmp = String.Concat(file, ".tmp"); action(tmp); if (!fileBase.Exists(tmp)) { throw new InvalidOperationException("action must create a file"); } //fileBase.Replace(tmp, file, null); write a TestHelper for Replace and make a pull request first fileBase.Delete(file); fileBase.Move(tmp, file); }
public override void Move(string sourceName, string destName) { //if we're moving a file, not a directory, call the appropriate file moving function. var fileData = mockFileDataAccessor.GetFile((mockFileDataAccessor.Path.GetFullPath(sourceName))); if (fileData != null && (fileData.Attributes & FileAttributes.Directory) == 0) { fileBase.Move(sourceName, destName); return; } var fullSourcePath = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(sourceName)); var fullDestPath = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(destName)); if (string.Equals(fullSourcePath, fullDestPath, StringComparison.OrdinalIgnoreCase)) { throw new IOException("Source and destination path must be different."); } var sourceRoot = mockFileDataAccessor.Path.GetPathRoot(fullSourcePath); var destinationRoot = mockFileDataAccessor.Path.GetPathRoot(fullDestPath); if (!string.Equals(sourceRoot, destinationRoot, StringComparison.OrdinalIgnoreCase)) { throw new IOException("Source and destination path must have identical roots. Move will not work across volumes."); } //Make sure that the destination exists mockFileDataAccessor.Directory.CreateDirectory(fullDestPath); //Recursively move all the subdirectories from the source into the destination directory var subdirectories = GetDirectories(fullSourcePath); foreach (var subdirectory in subdirectories) { var newSubdirPath = subdirectory.Replace(fullSourcePath, fullDestPath, StringComparison.OrdinalIgnoreCase); Move(subdirectory, newSubdirPath); } //Move the files in destination directory var files = GetFiles(fullSourcePath); foreach (var file in files) { var newFilePath = file.Replace(fullSourcePath, fullDestPath, StringComparison.OrdinalIgnoreCase); mockFileDataAccessor.FileInfo.FromFileName(file).MoveTo(newFilePath); } //Delete the source directory Delete(fullSourcePath); }