Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.IO.Abstractions" Version="16.1.25" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="23.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<InvalidOperationException>(() =>
BuiltInCopy.CopyWithVisualBasic(source, Path.Join(source, "Backup")));
}
finally
{
Directory.Delete(source, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CopyAllContentInDirectory;
using CopyAllContentInDirectory.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.IO.Abstractions.TestingHelpers;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void GivenDirectoryDoesNotExistOnWindows_WhenRunningMain_ThenExceptionIsT
Program.DestinationDir = "C:\\Destination";
Program.FileSystem = mockFileSystem;

Assert.ThrowsException<DirectoryNotFoundException>(() =>
Assert.ThrowsExactly<DirectoryNotFoundException>(() =>
{
Program.Main();
});
Expand All @@ -114,7 +115,7 @@ public void GivenDirectoryDoesNotExistOnLinux_WhenRunningMain_ThenExceptionIsThr
Program.DestinationDir = "/Destination";
Program.FileSystem = mockFileSystem;

Assert.ThrowsException<DirectoryNotFoundException>(() =>
Assert.ThrowsExactly<DirectoryNotFoundException>(() =>
{
Program.Main();
});
Expand All @@ -141,10 +142,89 @@ public void GivenFilePathIsInvalidOnWindows_WhenRunningMain_ThenExceptionIsThrow

Program.FileSystem = mockFileSystem;

Assert.ThrowsException<ArgumentException>(() =>
Assert.ThrowsExactly<ArgumentException>(() =>
{
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<IOException>(() =>
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);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="16.1.26" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.3" />
<PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="23.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading