Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82cabf6
commit ddd9ff3
Showing
7 changed files
with
292 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" /> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<Import Project="..\..\Common.props" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{4F8849F2-F962-4942-87F0-AB76B3BD8E12}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AssemblyName>ContentSync.Tests</AssemblyName> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
<NuGetPackageImportStamp> | ||
</NuGetPackageImportStamp> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<UseVSHostingProcess>false</UseVSHostingProcess> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System" /> | ||
<Reference Include="System.ComponentModel.Composition" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.XML" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL"> | ||
<HintPath>..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL"> | ||
<HintPath>..\..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL"> | ||
<HintPath>..\..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
<Reference Include="xunit.execution.desktop, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL"> | ||
<HintPath>..\..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Folder.cs" /> | ||
<Compile Include="Tests.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\ContentSync\ContentSync.csproj"> | ||
<Project>{80de8786-a3c6-411f-87c8-270623a9ccb3}</Project> | ||
<Name>ContentSync</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> | ||
<PropertyGroup> | ||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> | ||
</PropertyGroup> | ||
<Error Condition="!Exists('..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props'))" /> | ||
</Target> | ||
</Project> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace GuiLabs.FileUtilities | ||
{ | ||
public class Folder | ||
{ | ||
public string Name { get; } | ||
public IList<Folder> Folders { get; } | ||
public IList<File> Files { get; } | ||
|
||
public Folder(string name, IEnumerable<Folder> folders, IEnumerable<File> files) | ||
{ | ||
Name = name; | ||
Folders = folders.OrderBy(f => f.Name).ToArray(); | ||
Files = files.OrderBy(f => f.Name).ToArray(); | ||
} | ||
|
||
public void CreateOnDisk(string parentFolder) | ||
{ | ||
var fullPath = Path.Combine(parentFolder, Name); | ||
Directory.CreateDirectory(fullPath); | ||
foreach (var folder in Folders) | ||
{ | ||
folder.CreateOnDisk(fullPath); | ||
} | ||
|
||
foreach (var file in Files) | ||
{ | ||
file.CreateOnDisk(fullPath); | ||
} | ||
} | ||
|
||
public static Folder FromDisk(string folderPath) | ||
{ | ||
var name = Path.GetFileName(folderPath); | ||
var folders = Directory.GetDirectories(folderPath); | ||
var files = Directory.GetFiles(folderPath); | ||
return new Folder( | ||
name, | ||
folders.Select(f => FromDisk(f)), | ||
files.Select(f => new File( | ||
Path.GetFileName(f), | ||
System.IO.File.ReadAllText(f)))); | ||
} | ||
|
||
/// <summary> | ||
/// Folders can be considered identical if the names are different. | ||
/// Equality is content based. | ||
/// </summary> | ||
public static bool AreIdentical(Folder left, Folder right) | ||
{ | ||
if (left == null || right == null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (left.Folders.Count != right.Folders.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < left.Folders.Count; i++) | ||
{ | ||
if (!string.Equals(left.Folders[i].Name, right.Folders[i].Name, System.StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!AreIdentical(left.Folders[i], right.Folders[i])) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
if (left.Files.Count != right.Files.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < left.Files.Count; i++) | ||
{ | ||
if (!File.AreIdentical(left.Files[i], right.Files[i])) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
public class File | ||
{ | ||
public string Name { get; } | ||
public string Content { get; } | ||
|
||
public File(string name, string content = "") | ||
{ | ||
Name = name; | ||
Content = content; | ||
} | ||
|
||
public void CreateOnDisk(string folder) | ||
{ | ||
var filePath = Path.Combine(folder, Name); | ||
System.IO.File.WriteAllText(filePath, Content ?? ""); | ||
} | ||
|
||
public static bool AreIdentical(File left, File right) | ||
{ | ||
if (left == null || right == null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!string.Equals(left.Name, right.Name, System.StringComparison.OrdinalIgnoreCase) || left.Content != right.Content) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
public class Builder | ||
{ | ||
public static Folder Folder(params object[] items) | ||
{ | ||
string name = (string)items.Single(o => o is string); | ||
return new Folder(name, items.OfType<Folder>(), items.OfType<File>()); | ||
} | ||
|
||
public static File File(string name, string content = "") | ||
{ | ||
return new File(name, content); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.IO; | ||
using System.Reflection; | ||
using Xunit; | ||
using static GuiLabs.FileUtilities.Builder; | ||
|
||
namespace GuiLabs.FileUtilities | ||
{ | ||
public class ContentSyncTests | ||
{ | ||
[Fact] | ||
public void Test1() | ||
{ | ||
T | ||
( | ||
Folder("A", File("a.txt", "123")), | ||
Folder("B") | ||
); | ||
} | ||
|
||
private void T(Folder source, Folder destination) | ||
{ | ||
var entrypointDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | ||
var root = Path.Combine(entrypointDirectory, nameof(ContentSyncTests)); | ||
if (Directory.Exists(root)) | ||
{ | ||
Directory.Delete(root, recursive: true); | ||
} | ||
|
||
Directory.CreateDirectory(root); | ||
|
||
source.CreateOnDisk(root); | ||
destination.CreateOnDisk(root); | ||
|
||
Sync.Directories(Path.Combine(root, source.Name), Path.Combine(root, destination.Name)); | ||
|
||
var actual = Folder.FromDisk(Path.Combine(root, destination.Name)); | ||
|
||
Assert.True(Folder.AreIdentical(source, actual)); | ||
|
||
Directory.Delete(root, recursive: true); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="xunit" version="2.1.0" targetFramework="net45" /> | ||
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" /> | ||
<package id="xunit.assert" version="2.1.0" targetFramework="net45" /> | ||
<package id="xunit.core" version="2.1.0" targetFramework="net45" /> | ||
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" /> | ||
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net45" /> | ||
<package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="net45" /> | ||
</packages> |
This file contains 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
This file contains 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