Skip to content

Commit

Permalink
Implement transform tool (closes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlenPelin committed May 29, 2017
1 parent 0215f77 commit cc72993
Show file tree
Hide file tree
Showing 9 changed files with 512 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ src/Sitecore.Configuration.Roles/obj/
src/Website/bin/
src/Website/obj/
src/packages/
src/TestResults/
src/Transform.UnitTests/bin/
src/Transform.UnitTests/obj/
src/Transform/bin/
src/Transform/obj/
20 changes: 19 additions & 1 deletion src/Sitecore.Configuration.Roles.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
VisualStudioVersion = 15.0.26430.6
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sitecore.Configuration.Roles", "Sitecore.Configuration.Roles\Sitecore.Configuration.Roles.csproj", "{6A31C120-8BA2-42A3-9C9B-4D40B626EC3A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sitecore.Configuration.Roles.UnitTests", "Sitecore.Configuration.Roles.UnitTests\Sitecore.Configuration.Roles.UnitTests.csproj", "{66C9DFB2-5C20-4DFF-9062-50D8A6DE50FF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Transform", "Transform\Transform.csproj", "{D0C52BE8-194D-400C-BC15-6600315BA39E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "App", "App", "{E882ABF3-83DC-447A-89B7-F90AC957F08A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Transform.UnitTests", "Transform.UnitTests\Transform.UnitTests.csproj", "{9F9F65C7-9816-4E51-8A0B-4F5509682708}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,8 +27,20 @@ Global
{66C9DFB2-5C20-4DFF-9062-50D8A6DE50FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66C9DFB2-5C20-4DFF-9062-50D8A6DE50FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66C9DFB2-5C20-4DFF-9062-50D8A6DE50FF}.Release|Any CPU.Build.0 = Release|Any CPU
{D0C52BE8-194D-400C-BC15-6600315BA39E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0C52BE8-194D-400C-BC15-6600315BA39E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0C52BE8-194D-400C-BC15-6600315BA39E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0C52BE8-194D-400C-BC15-6600315BA39E}.Release|Any CPU.Build.0 = Release|Any CPU
{9F9F65C7-9816-4E51-8A0B-4F5509682708}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F9F65C7-9816-4E51-8A0B-4F5509682708}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F9F65C7-9816-4E51-8A0B-4F5509682708}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F9F65C7-9816-4E51-8A0B-4F5509682708}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D0C52BE8-194D-400C-BC15-6600315BA39E} = {E882ABF3-83DC-447A-89B7-F90AC957F08A}
{9F9F65C7-9816-4E51-8A0B-4F5509682708} = {E882ABF3-83DC-447A-89B7-F90AC957F08A}
EndGlobalSection
EndGlobal
143 changes: 143 additions & 0 deletions src/Transform.UnitTests/Program_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Transform.UnitTests
{
using System.Linq;
using System.Xml;

[TestClass]
public class Program_Tests
{
[TestMethod]
public void ParseRole()
{
// arrange
var webConfig = ParseXml(@"
<configuration>
<appSettings>
<add key='role:define' value='abc' />
</appSettings>
</configuration>");

// act
var role = Program.ParseRole(webConfig);

// assert
Assert.AreEqual("abc", role);
}

[TestMethod]
public void ProcessFile_Partial()
{
// arrange
var xml = ParseXml(@"
<configuration xmlns:patch='http://www.sitecore.net/xmlconfig/'>
<sitecore xmlns:role='http://www.sitecore.net/xmlconfig/role/'>
<element role:require='Delivery'>
<delivery1 />
</element>
<element role:require='Authoring'>
<authoring xmlns:role2='http://www.sitecore.net/xmlconfig/role/' />
<element role:require='Delivery'>
<delivery2 />
</element>
</element>
</sitecore>
</configuration>");

var expected = ParseXml(@"
<configuration xmlns:patch='http://www.sitecore.net/xmlconfig/'>
<sitecore>
<element>
<authoring />
</element>
</sitecore>
</configuration>");

// act
var result = Program.ProcessFile(xml.DocumentElement, "Authoring".Split());

// assert
CompareElements(expected.DocumentElement, xml.DocumentElement, "/configuration", 0);
Assert.IsTrue(result);
}

[TestMethod]
public void ProcessFile_Empty()
{
// arrange
var xml = ParseXml(@"
<configuration xmlns:patch='http://www.sitecore.net/xmlconfig/' xmlns:role='http://www.sitecore.net/xmlconfig/role/'>
<sitecore role:require='Delivery'>
<delivery />
</sitecore>
</configuration>");

var expected = ParseXml(@"
<configuration xmlns:patch='http://www.sitecore.net/xmlconfig/'>
</configuration>");

// act
var result = Program.ProcessFile(xml.DocumentElement, "Authoring".Split());

// assert
CompareElements(expected.DocumentElement, xml.DocumentElement, "/configuration", 0);
Assert.IsFalse(result);
}

private void CompareElements(XmlElement expected, XmlElement actual, string path, int childNumber)
{
CompareAttributes(expected.Attributes, actual.Attributes, path, childNumber);
for (var i = 0; i < Math.Max(expected.ChildNodes.Count, actual.ChildNodes.Count); ++i)
{
var exp = i < expected.ChildNodes.Count ? (XmlElement)expected.ChildNodes[i] : null;
var act = i < actual.ChildNodes.Count ? (XmlElement)actual.ChildNodes[i] : null;
if (exp == null)
{
if (act == null)
{
throw new NotImplementedException("This cannot be");
}

Assert.Fail($"Unexpected xml element by path {path}[{childNumber}]: {act}");
}
else if (act == null)
{
Assert.Fail($"Missing xml element by path {path}[{childNumber}]: {exp}");
}
else
{
CompareElements(exp, act, path + "/" + exp.Name, i);
}
}
}

private void CompareAttributes(XmlAttributeCollection expected1, XmlAttributeCollection actual1, string path, int childNumber)
{
var expected = expected1.OfType<XmlAttribute>().ToList();
var actual = actual1.OfType<XmlAttribute>().ToList();
foreach (XmlAttribute exp in expected1)
{
expected.Remove(exp);
var act = actual.FirstOrDefault(x => x.Name == exp.Name);
Assert.IsNotNull(act, $"{path}[{childNumber}][@{exp.Name}]");

Assert.AreEqual(exp.Value, act.Value);
actual.Remove(act);
}

foreach (var act in actual)
{
Assert.Fail($"Unexpected attr {path}[{childNumber}][@{act.Name}='{act.Value}']");
}
}

private static XmlDocument ParseXml(string xml)
{
var webConfig = new XmlDocument();
webConfig.LoadXml(xml.Replace("'", "\"").Trim());
return webConfig;
}
}
}
20 changes: 20 additions & 0 deletions src/Transform.UnitTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Transform.UnitTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Transform.UnitTests")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: ComVisible(false)]

[assembly: Guid("9f9f65c7-9816-4e51-8a0b-4f5509682708")]

// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
74 changes: 74 additions & 0 deletions src/Transform.UnitTests/Transform.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{9F9F65C7-9816-4E51-8A0B-4F5509682708}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Transform.UnitTests</RootNamespace>
<AssemblyName>Transform.UnitTests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program_Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Transform\Transform.csproj">
<Project>{D0C52BE8-194D-400C-BC15-6600315BA39E}</Project>
<Name>Transform</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<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\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets')" />
</Project>
5 changes: 5 additions & 0 deletions src/Transform.UnitTests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="1.1.11" targetFramework="net45" />
<package id="MSTest.TestFramework" version="1.1.11" targetFramework="net45" />
</packages>
Loading

0 comments on commit cc72993

Please sign in to comment.