Last active
August 29, 2015 14:26
-
-
Save borismod/096d50b3020e3c8a598c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO.Abstractions; | |
| public class CleanFileCopier | |
| { | |
| private readonly IFileSystem _fileSystem; | |
| public CleanFileCopier(): this(new FileSystem()) | |
| { | |
| } | |
| internal CleanFileCopier(IFileSystem fileSystem) | |
| { | |
| _fileSystem = fileSystem; | |
| } | |
| public string CopyFile(string sourceFilePath, string destinationDirectoryPath) | |
| { | |
| if (!_fileSystem.Directory.Exists(destinationDirectoryPath)) | |
| { | |
| _fileSystem.Directory.CreateDirectory(destinationDirectoryPath); | |
| } | |
| var fileName = _fileSystem.Path.GetFileName(sourceFilePath); | |
| var destFileName = _fileSystem.Path.Combine(destinationDirectoryPath, fileName); | |
| _fileSystem.File.Copy(sourceFilePath, destFileName); | |
| return destFileName; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO.Abstractions.TestingHelpers; | |
| [TestFixture] | |
| public class FileCopierTests | |
| { | |
| [Test] | |
| public void CopyFile_SourceFileExistsDestinationDirDoesNotExist_FileCopiedToDestination() | |
| { | |
| // Arrange | |
| var mockFileSystem = new MockFileSystem(); | |
| mockFileSystem.AddFile(@"c:\file.txt", MockFileData.NullObject); | |
| var cleanFileCopier = new CleanFileCopier(mockFileSystem); | |
| // Act | |
| var destFilePath = cleanFileCopier.CopyFile(@"c:\file.txt", @"c:\users\borismod.net"); | |
| // Assert | |
| Assert.AreEqual(@"c:\users\borismod.net\file.txt", destFilePath); | |
| Assert.IsTrue(mockFileSystem.File.Exists(destFilePath)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO; | |
| public class LegacyFileCopier | |
| { | |
| public string CopyFile(string sourceFilePath, string destinationDirectoryPath) | |
| { | |
| if (!Directory.Exists(destinationDirectoryPath)) | |
| { | |
| Directory.CreateDirectory(destinationDirectoryPath); | |
| } | |
| var fileName = Path.GetFileName(sourceFilePath); | |
| var destFileName = Path.Combine(destinationDirectoryPath, fileName); | |
| File.Copy(sourceFilePath, destFileName); | |
| return destFileName; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment