Skip to content

Commit

Permalink
remove overwrite checks
Browse files Browse the repository at this point in the history
  • Loading branch information
alwaysbusy committed Dec 29, 2023
1 parent 93dca83 commit 9e75133
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 80 deletions.
16 changes: 0 additions & 16 deletions src/System.IO.Abstractions.Extensions/IDirectoryInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,6 @@ public static void CopyTo(
string directoriesSearchPattern = "*",
bool overwrite = false)
{
if (!overwrite)
{
source.ForEachFile(
(file, destDir) => {
var destPath = destDir.GetFilePath(file.Name);
if (destination.FileSystem.File.Exists(destPath))
{
throw new IOException(StringResources.Format("CANNOT_OVERWRITE", destPath));
}
},
subDirectory => source.TranslatePaths(subDirectory, destination, true),
recursive,
filesSearchPattern,
directoriesSearchPattern);
}

source.ForEachFile(
(file, destDir) => file.CopyTo(destDir.GetFilePath(file.Name), overwrite),
subDirectory => source.TranslatePaths(subDirectory, destination, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,60 +390,7 @@ public void CopyTo_TargetDirAndParentDoesNotExist_CreatesTargetDirectoryHierarch
}

[Test]
public void CopyTo_Overwrite_OverwritesWhenSet([Values] bool preExisting)
{
//arrange
var fs = new FileSystem();
var workingDir = fs.DirectoryInfo.New(fs.Directory.GetCurrentDirectory()).CreateSubdirectory(Guid.NewGuid().ToString());

//create directories
var source = fs.DirectoryInfo.New(fs.Path.Combine(workingDir.FullName, "SourceDir"));
var dest = fs.DirectoryInfo.New(fs.Path.Combine(workingDir.FullName, "DestDir"));

source.Create();
dest.Create();

//create files
var sourceFile = fs.FileInfo.New(fs.Path.Combine(source.FullName, "file.txt"));
var destFile = fs.FileInfo.New(fs.Path.Combine(dest.FullName, "file.txt"));

var sourceFileContent = new[] { nameof(sourceFile) };
sourceFile.WriteLines(sourceFileContent);
var destFileContent = new[] { nameof(destFile) };
if (preExisting)
{
destFile.WriteLines(destFileContent);
}

//make sure everything is set up as expected
Assert.IsTrue(fs.Directory.Exists(source.FullName));
Assert.IsTrue(fs.File.Exists(sourceFile.FullName));
Assert.AreEqual(fs.File.ReadAllLines(sourceFile.FullName), sourceFileContent);
Assert.IsTrue(fs.Directory.Exists(dest.FullName));
if (preExisting)
{
Assert.IsTrue(fs.File.Exists(destFile.FullName));
Assert.AreEqual(fs.File.ReadAllLines(destFile.FullName), destFileContent);
}
else
{
Assert.IsFalse(fs.File.Exists(destFile.FullName));
}

//act
source.CopyTo(dest, overwrite: true);

//assert
Assert.AreEqual(fs.File.ReadAllLines(destFile.FullName), sourceFileContent);

//cleanup
workingDir.Delete(recursive: true);

Assert.IsFalse(fs.File.Exists(workingDir.FullName));
}

[Test]
public void CopyTo_Overwrite_DoesNotOverwriteSingleFileWhenNotSet()
public void CopyTo_Overwrite_OverwritesWhenSet()
{
//arrange
var fs = new FileSystem();
Expand Down Expand Up @@ -474,10 +421,10 @@ public void CopyTo_Overwrite_DoesNotOverwriteSingleFileWhenNotSet()
Assert.AreEqual(fs.File.ReadAllLines(destFile.FullName), destFileContent);

//act
Assert.That(() => source.CopyTo(dest, overwrite: false), Throws.Exception.TypeOf<IOException>().And.Message.Contains(destFile.FullName));
source.CopyTo(dest, overwrite: true);

//assert
Assert.AreEqual(fs.File.ReadAllLines(destFile.FullName), destFileContent);
Assert.AreEqual(fs.File.ReadAllLines(destFile.FullName), sourceFileContent);

//cleanup
workingDir.Delete(recursive: true);
Expand All @@ -486,7 +433,7 @@ public void CopyTo_Overwrite_DoesNotOverwriteSingleFileWhenNotSet()
}

[Test]
public void CopyTo_Overwrite_DoesNotOverwriteMultipleFilesWhenNotSet()
public void CopyTo_Overwrite_DoesNotOverwritesWhenNotSet()
{
//arrange
var fs = new FileSystem();
Expand All @@ -501,33 +448,26 @@ public void CopyTo_Overwrite_DoesNotOverwriteMultipleFilesWhenNotSet()

//create files
var sourceFile = fs.FileInfo.New(fs.Path.Combine(source.FullName, "file.txt"));
var secondSourceFile = fs.FileInfo.New(fs.Path.Combine(source.FullName, "another.txt")); // This will show up alphabetically first
var destFile = fs.FileInfo.New(fs.Path.Combine(dest.FullName, "file.txt"));
var secondDestFile = fs.FileInfo.New(fs.Path.Combine(dest.FullName, "another.txt"));

var sourceFileContent = new[] { nameof(sourceFile) };
sourceFile.WriteLines(sourceFileContent);
secondSourceFile.WriteLines(sourceFileContent);
var destFileContent = new[] { nameof(destFile) };
destFile.WriteLines(destFileContent);

//make sure everything is set up as expected
Assert.IsTrue(fs.Directory.Exists(source.FullName));
Assert.IsTrue(fs.File.Exists(sourceFile.FullName));
Assert.AreEqual(fs.File.ReadAllLines(sourceFile.FullName), sourceFileContent);
Assert.IsTrue(fs.File.Exists(secondSourceFile.FullName));
Assert.AreEqual(fs.File.ReadAllLines(secondSourceFile.FullName), sourceFileContent);
Assert.IsTrue(fs.Directory.Exists(dest.FullName));
Assert.IsTrue(fs.File.Exists(destFile.FullName));
Assert.AreEqual(fs.File.ReadAllLines(destFile.FullName), destFileContent);
Assert.IsFalse(fs.File.Exists(secondDestFile.FullName));

//act
Assert.That(() => source.CopyTo(dest, overwrite: false), Throws.Exception.TypeOf<IOException>().And.Message.Contains(destFile.FullName));

//assert
Assert.AreEqual(fs.File.ReadAllLines(destFile.FullName), destFileContent);
Assert.IsFalse(fs.File.Exists(secondDestFile.FullName));

//cleanup
workingDir.Delete(recursive: true);
Expand Down

0 comments on commit 9e75133

Please sign in to comment.