diff --git a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/BuiltInCopy.cs b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/BuiltInCopy.cs
new file mode 100644
index 0000000000..ff6eefbbd5
--- /dev/null
+++ b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/BuiltInCopy.cs
@@ -0,0 +1,11 @@
+using Microsoft.VisualBasic.FileIO;
+
+namespace CopyAllContentInDirectory;
+
+public static class BuiltInCopy
+{
+ public static void CopyWithVisualBasic(string sourcePath, string destinationPath)
+ {
+ FileSystem.CopyDirectory(sourcePath, destinationPath, overwrite: true);
+ }
+}
diff --git a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/CopyAllContentInDirectory.csproj b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/CopyAllContentInDirectory.csproj
index bff8f6f395..1c38686e84 100644
--- a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/CopyAllContentInDirectory.csproj
+++ b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/CopyAllContentInDirectory.csproj
@@ -2,13 +2,13 @@
Exe
- net6.0
+ net11.0
enable
enable
-
+
diff --git a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/Extensions/DirectoryInfoExtensions.cs b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/Extensions/DirectoryInfoExtensions.cs
index 1f2d803335..2ca0a99d12 100644
--- a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/Extensions/DirectoryInfoExtensions.cs
+++ b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/Extensions/DirectoryInfoExtensions.cs
@@ -1,29 +1,21 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+namespace CopyAllContentInDirectory.Extensions;
-namespace CopyAllContentInDirectory.Extensions
+public static class DirectoryInfoExtensions
{
- public static class DirectoryInfoExtensions
+ public static void DeepCopy(this DirectoryInfo directory, string destinationDir, bool overwriteFiles)
{
- public static void DeepCopy(this DirectoryInfo directory, string destinationDir)
+ foreach (string dir in Directory.GetDirectories(directory.FullName, "*", SearchOption.AllDirectories))
{
- var allDirectories = Directory.GetDirectories(directory.FullName, "*", SearchOption.AllDirectories);
-
- foreach (string dir in allDirectories)
- {
- string dirToCreate = dir.Replace(directory.FullName, destinationDir);
- Directory.CreateDirectory(dirToCreate);
- }
+ string dirToCreate = Path.Join(destinationDir, Path.GetRelativePath(directory.FullName, dir));
+ Directory.CreateDirectory(dirToCreate);
+ }
- var allFiles = Directory.GetFiles(directory.FullName, "*.*", SearchOption.AllDirectories);
+ Directory.CreateDirectory(destinationDir);
- foreach (string newPath in allFiles)
- {
- File.Copy(newPath, newPath.Replace(directory.FullName, destinationDir), true);
- }
+ foreach (string newPath in Directory.GetFiles(directory.FullName, "*.*", SearchOption.AllDirectories))
+ {
+ string newFile = Path.Join(destinationDir, Path.GetRelativePath(directory.FullName, newPath));
+ File.Copy(newPath, newFile, overwriteFiles);
}
}
}
diff --git a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/Program.cs b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/Program.cs
index 8299e60f89..34ac068369 100644
--- a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/Program.cs
+++ b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectory/Program.cs
@@ -1,25 +1,36 @@
using System.IO.Abstractions;
-namespace CopyAllContentInDirectory
+namespace CopyAllContentInDirectory;
+
+public static class Program
{
- public static class Program
+ public static string SourceDir { get; set; } = "";
+ public static string DestinationDir { get; set; } = "";
+ public static IFileSystem FileSystem { get; set; } = new FileSystem(); // for testing purposes
+
+ public static void Main()
+ {
+ CopyDirectory(SourceDir, DestinationDir, true);
+ }
+
+ public static void CopyDirectory(string sourceDir, string destinationDir, bool overwriteFiles)
{
- public static string SourceDir { get; set; } = "";
- public static string DestinationDir { get; set; } = "";
- public static IFileSystem FileSystem { get; set; } = new FileSystem(); // for testing purposes
+ var allDirectories = FileSystem.Directory.GetDirectories(sourceDir, "*", SearchOption.AllDirectories);
+
+ foreach (string dir in allDirectories)
+ {
+ string dirToCreate = FileSystem.Path.Join(destinationDir, FileSystem.Path.GetRelativePath(sourceDir, dir));
+ FileSystem.Directory.CreateDirectory(dirToCreate);
+ }
+
+ FileSystem.Directory.CreateDirectory(destinationDir);
+
+ var allFiles = FileSystem.Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories);
- public static void Main()
+ foreach (string newPath in allFiles)
{
- foreach (string dir in FileSystem.Directory.GetDirectories(SourceDir, "*", SearchOption.AllDirectories))
- {
- string dirToCreate = dir.Replace(SourceDir, DestinationDir);
- FileSystem.Directory.CreateDirectory(dirToCreate);
- }
-
- foreach (string newPath in FileSystem.Directory.GetFiles(SourceDir, "*.*", SearchOption.AllDirectories))
- {
- FileSystem.File.Copy(newPath, newPath.Replace(SourceDir, DestinationDir), true);
- }
+ string newFile = FileSystem.Path.Join(destinationDir, FileSystem.Path.GetRelativePath(sourceDir, newPath));
+ FileSystem.File.Copy(newPath, newFile, overwriteFiles);
}
}
-}
\ No newline at end of file
+}
diff --git a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/BuiltInCopyTests.cs b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/BuiltInCopyTests.cs
new file mode 100644
index 0000000000..4975307d82
--- /dev/null
+++ b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/BuiltInCopyTests.cs
@@ -0,0 +1,53 @@
+using CopyAllContentInDirectory;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.IO;
+
+namespace CopyAllContentInDirectoryTests;
+
+[TestClass]
+public class BuiltInCopyTests
+{
+ [TestMethod]
+ public void GivenExistingDestination_WhenCopyingWithVisualBasic_ThenContentsAreMerged()
+ {
+ var testRoot = Path.Join(Path.GetTempPath(), "BuiltInCopy-" + Guid.NewGuid());
+ var source = Path.Join(testRoot, "Source");
+ var destination = Path.Join(testRoot, "Destination");
+ Directory.CreateDirectory(Path.Join(source, "ChildDir"));
+ File.WriteAllText(Path.Join(source, "ChildDir", "child.txt"), "new");
+ Directory.CreateDirectory(Path.Join(destination, "ChildDir"));
+ File.WriteAllText(Path.Join(destination, "ChildDir", "child.txt"), "old");
+ File.WriteAllText(Path.Join(destination, "keep.txt"), "mine");
+
+ try
+ {
+ BuiltInCopy.CopyWithVisualBasic(source, destination);
+
+ Assert.AreEqual("new", File.ReadAllText(Path.Join(destination, "ChildDir", "child.txt")));
+ Assert.IsTrue(File.Exists(Path.Join(destination, "keep.txt")));
+ }
+ finally
+ {
+ Directory.Delete(testRoot, true);
+ }
+ }
+
+ [TestMethod]
+ public void GivenDestinationInsideSource_WhenCopyingWithVisualBasic_ThenInvalidOperationExceptionIsThrown()
+ {
+ var source = Path.Join(Path.GetTempPath(), "BuiltInCopy-" + Guid.NewGuid());
+ Directory.CreateDirectory(source);
+ File.WriteAllText(Path.Join(source, "file.txt"), "file");
+
+ try
+ {
+ Assert.ThrowsExactly(() =>
+ BuiltInCopy.CopyWithVisualBasic(source, Path.Join(source, "Backup")));
+ }
+ finally
+ {
+ Directory.Delete(source, true);
+ }
+ }
+}
diff --git a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/CopyAllContentInDirectoryTests.cs b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/CopyAllContentInDirectoryTests.cs
index 4789db4335..63ecef69f9 100644
--- a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/CopyAllContentInDirectoryTests.cs
+++ b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/CopyAllContentInDirectoryTests.cs
@@ -1,4 +1,5 @@
using CopyAllContentInDirectory;
+using CopyAllContentInDirectory.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.IO.Abstractions.TestingHelpers;
@@ -92,7 +93,7 @@ public void GivenDirectoryDoesNotExistOnWindows_WhenRunningMain_ThenExceptionIsT
Program.DestinationDir = "C:\\Destination";
Program.FileSystem = mockFileSystem;
- Assert.ThrowsException(() =>
+ Assert.ThrowsExactly(() =>
{
Program.Main();
});
@@ -114,7 +115,7 @@ public void GivenDirectoryDoesNotExistOnLinux_WhenRunningMain_ThenExceptionIsThr
Program.DestinationDir = "/Destination";
Program.FileSystem = mockFileSystem;
- Assert.ThrowsException(() =>
+ Assert.ThrowsExactly(() =>
{
Program.Main();
});
@@ -141,10 +142,89 @@ public void GivenFilePathIsInvalidOnWindows_WhenRunningMain_ThenExceptionIsThrow
Program.FileSystem = mockFileSystem;
- Assert.ThrowsException(() =>
+ Assert.ThrowsExactly(() =>
{
Program.Main();
});
}
+ private static string Root => OperatingSystem.IsWindows() ? @"C:\" : "/";
+
+ [TestMethod]
+ public void GivenFolderNameStartsWithSourceName_WhenRunningMain_ThenNameIsKept()
+ {
+ var mockFileSystem = new MockFileSystem();
+ Program.SourceDir = Path.Join(Root, "data");
+ Program.DestinationDir = Path.Join(Root, "backup");
+ Program.FileSystem = mockFileSystem;
+ mockFileSystem.AddFile(Path.Join(Root, "data", "database", "notes.txt"), new MockFileData("notes"));
+
+ Program.Main();
+
+ Assert.IsTrue(mockFileSystem.FileExists(Path.Join(Root, "backup", "database", "notes.txt")));
+ }
+
+ [TestMethod]
+ public void GivenSourceWithTrailingSeparator_WhenRunningMain_ThenSubfolderLandsUnderDestination()
+ {
+ var mockFileSystem = new MockFileSystem();
+ Program.SourceDir = Path.Join(Root, "Source") + Path.DirectorySeparatorChar;
+ Program.DestinationDir = Path.Join(Root, "Destination");
+ Program.FileSystem = mockFileSystem;
+ mockFileSystem.AddFile(Path.Join(Root, "Source", "ChildDir", "childfile.txt"), new MockFileData("childfile"));
+
+ Program.Main();
+
+ Assert.IsTrue(mockFileSystem.FileExists(Path.Join(Root, "Destination", "ChildDir", "childfile.txt")));
+ }
+
+ [TestMethod]
+ public void GivenSourceWithoutSubfolders_WhenDestinationIsMissing_ThenFilesAreCopied()
+ {
+ var mockFileSystem = new MockFileSystem();
+ Program.SourceDir = Path.Join(Root, "Source");
+ Program.DestinationDir = Path.Join(Root, "Destination");
+ Program.FileSystem = mockFileSystem;
+ mockFileSystem.AddFile(Path.Join(Root, "Source", "file.txt"), new MockFileData("file"));
+
+ Program.Main();
+
+ Assert.IsTrue(mockFileSystem.FileExists(Path.Join(Root, "Destination", "file.txt")));
+ }
+
+ [TestMethod]
+ public void GivenExistingFile_WhenOverwriteIsFalse_ThenIOExceptionIsThrown()
+ {
+ var mockFileSystem = new MockFileSystem();
+ Program.FileSystem = mockFileSystem;
+ mockFileSystem.AddFile(Path.Join(Root, "Source", "file.txt"), new MockFileData("new"));
+ mockFileSystem.AddFile(Path.Join(Root, "Destination", "file.txt"), new MockFileData("old"));
+
+ Assert.ThrowsExactly(() =>
+ Program.CopyDirectory(Path.Join(Root, "Source"), Path.Join(Root, "Destination"), false));
+ }
+
+ [TestMethod]
+ public void GivenRealDirectory_WhenCallingDeepCopy_ThenTreeIsCopied()
+ {
+ var testRoot = Path.Join(Path.GetTempPath(), "CopyAllContentInDirectory-" + Guid.NewGuid());
+ var source = Path.Join(testRoot, "Source");
+ var destination = Path.Join(testRoot, "Destination");
+ Directory.CreateDirectory(Path.Join(source, "ChildDir", "Empty"));
+ File.WriteAllText(Path.Join(source, "top.txt"), "top");
+ File.WriteAllText(Path.Join(source, "ChildDir", "child.txt"), "child");
+
+ try
+ {
+ new DirectoryInfo(source).DeepCopy(destination, true);
+
+ Assert.AreEqual("top", File.ReadAllText(Path.Join(destination, "top.txt")));
+ Assert.AreEqual("child", File.ReadAllText(Path.Join(destination, "ChildDir", "child.txt")));
+ Assert.IsTrue(Directory.Exists(Path.Join(destination, "ChildDir", "Empty")));
+ }
+ finally
+ {
+ Directory.Delete(testRoot, true);
+ }
+ }
}
-}
\ No newline at end of file
+}
diff --git a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/CopyAllContentInDirectoryTests.csproj b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/CopyAllContentInDirectoryTests.csproj
index 4abe7e8d9a..018ee3f027 100644
--- a/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/CopyAllContentInDirectoryTests.csproj
+++ b/files-csharp/CopyAllContentInDirectory/CopyAllContentInDirectoryTests/CopyAllContentInDirectoryTests.csproj
@@ -1,17 +1,17 @@
- net6.0
+ net11.0
enable
false
-
-
-
-
+
+
+
+