Skip to content

Commit

Permalink
Start adding some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillOsenkov committed Jan 9, 2016
1 parent 82cabf6 commit ddd9ff3
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ContentSync.sln
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContentSync", "src\ContentSync\ContentSync.csproj", "{80DE8786-A3C6-411F-87C8-270623A9CCB3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContentSync.Tests", "src\ContentSync.Tests\ContentSync.Tests.csproj", "{4F8849F2-F962-4942-87F0-AB76B3BD8E12}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{80DE8786-A3C6-411F-87C8-270623A9CCB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80DE8786-A3C6-411F-87C8-270623A9CCB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80DE8786-A3C6-411F-87C8-270623A9CCB3}.Release|Any CPU.Build.0 = Release|Any CPU
{4F8849F2-F962-4942-87F0-AB76B3BD8E12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F8849F2-F962-4942-87F0-AB76B3BD8E12}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F8849F2-F962-4942-87F0-AB76B3BD8E12}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F8849F2-F962-4942-87F0-AB76B3BD8E12}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
81 changes: 81 additions & 0 deletions src/ContentSync.Tests/ContentSync.Tests.csproj
@@ -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>
140 changes: 140 additions & 0 deletions src/ContentSync.Tests/Folder.cs
@@ -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);
}
}
}
43 changes: 43 additions & 0 deletions src/ContentSync.Tests/Tests.cs
@@ -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);
}
}
}
10 changes: 10 additions & 0 deletions src/ContentSync.Tests/packages.config
@@ -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>
2 changes: 0 additions & 2 deletions src/ContentSync/ContentSync.csproj
Expand Up @@ -19,7 +19,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess>
<IntermediateOutputPath>C:\Users\Kirill\AppData\Local\Temp\vs9A20.tmp\Debug\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -28,7 +27,6 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<IntermediateOutputPath>C:\Users\Kirill\AppData\Local\Temp\vs9A20.tmp\Release\</IntermediateOutputPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
Expand Down
12 changes: 12 additions & 0 deletions src/ContentSync/DiffResults.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;

namespace GuiLabs.FileUtilities
{
Expand All @@ -20,5 +21,16 @@ public class FolderDiffResults
ChangedFiles = changedFiles;
RightOnlyFiles = rightOnlyFiles;
}

public bool AreFullyIdentical
{
get
{
return
!LeftOnlyFiles.Any() &&
!RightOnlyFiles.Any() &&
!ChangedFiles.Any();
}
}
}
}

0 comments on commit ddd9ff3

Please sign in to comment.