Skip to content

Commit

Permalink
Add xmldiff.exe command line tool and ad a unit test that captures bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lovettchris committed Nov 28, 2023
1 parent 0472d20 commit 0cc21e7
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/UnitTests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ public void SideBySideDiffView()
}
}

[Fact]
public void RegressionMix()
{
using (var col = new TempFileCollection())
{
var xmlSource = col.CreateTempFile("<Profile><field><field>Approval_Workflow__c.Description__c</field></field><field><editable>false</editable><field>Approval_Workflow__c.Migration_Id__c</field><readable>false</readable></field><field><field>Approval_Workflow__c.Reject_PO_Substage__c</field></field><field><field>Location.VisitorAddressId</field></field><flow><flow>TestAccListApexFlow</flow></flow><layout><layout>Lead-LeadLayout</layout></layout><layout><layout>Macro-MacroLayout</layout></layout><layout><layout>OFAC__SDN_Match__c-OFAC__MatchLayout</layout></layout><layout><layout>OFAC__SDN_Search__c-OFAC__OFACRequestLayout</layout></layout><layout><layout>Oak_Office__c-OakOfficeLayout</layout></layout><objectPermissions><allowCreate>false</allowCreate><allowDelete>false</allowDelete><allowEdit>false</allowEdit><allowRead>true</allowRead><modifyAllRecords>false</modifyAllRecords></objectPermissions><objectPermissions><object>Asset</object><viewAllRecords>false</viewAllRecords></objectPermissions></Profile>");
var xmlChanged = col.CreateTempFile("<Profile><field><field>Approval_Workflow__c.Description__c</field></field><field><editable>true</editable><field>Approval_Workflow__c.Migration_Id__c</field><readable>true</readable></field><field><field>Approval_Workflow__c.Reject_PO_Substage__c</field></field><field><field>Location.VisitorAddressId</field></field><layout><layout>Lead-LeadLayout</layout></layout><layout><layout>Location-LocationLayout</layout></layout><layout><layout>Macro-MacroLayout</layout></layout><layout><layout>Oak_Office__c-OakOfficeLayout</layout></layout><objectPermissions><allowCreate>false</allowCreate><allowDelete>false</allowDelete><allowEdit>false</allowEdit><allowRead>true</allowRead><modifyAllRecords>false</modifyAllRecords><object>Address</object><viewAllRecords>false</viewAllRecords></objectPermissions><objectPermissions><allowCreate>false</allowCreate><allowDelete>false</allowDelete><allowEdit>false</allowEdit><allowRead>true</allowRead><modifyAllRecords>false</modifyAllRecords><object>Asset</object><viewAllRecords>false</viewAllRecords></objectPermissions></Profile>");

var view = new XmlDiffView();
var options = XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreWhitespace;
var results = view.DifferencesSideBySideAsHtml(xmlSource, xmlChanged, false, options);

var diff = results.ReadToEnd();
Assert.Contains("<span class=\"remove\">&lt;temp</span>", diff);
Assert.Contains("<span class=\"add\">id=\"1\"</span>", diff);
Assert.Contains("<span class=\"add\">&lt;c</span>", diff);
}
}

[Fact]
public void SideBySideDiffViewCompact()
{
Expand Down
6 changes: 6 additions & 0 deletions src/XmlDiff.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Common\version.txt = Common\version.txt
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XmlDiffTool", "XmlDiffTool\XmlDiffTool.csproj", "{BE99AC80-E1C4-48B8-8415-AF413073399C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -34,6 +36,10 @@ Global
{D2FBED29-536D-4FA9-94F4-EB72BF89A900}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2FBED29-536D-4FA9-94F4-EB72BF89A900}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2FBED29-536D-4FA9-94F4-EB72BF89A900}.Release|Any CPU.Build.0 = Release|Any CPU
{BE99AC80-E1C4-48B8-8415-AF413073399C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE99AC80-E1C4-48B8-8415-AF413073399C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE99AC80-E1C4-48B8-8415-AF413073399C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE99AC80-E1C4-48B8-8415-AF413073399C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
134 changes: 134 additions & 0 deletions src/XmlDiffTool/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using Microsoft.XmlDiffPatch;
using System.IO;
using System.Numerics;
using System.Text;

class Program
{
string file1;
string file2;
string outputFile;
bool compact;
enum OutputFormat
{
Xml,
Html
}
OutputFormat format;

bool ParseCommandLine(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if (arg[0] == '-')
{
switch (arg.TrimStart('-').ToLowerInvariant())
{
case "?":
case "h":
case "help":
return false;
case "format":
if (i + 1 >= args.Length)
{
WriteError($"Missing --format parameter 'xml|html'");
return false;
}
i++;
arg = args[i];
if (arg == "xml")
{
format = OutputFormat.Xml;
}
else if (arg == "html")
{
format = OutputFormat.Html;
}
else
{
WriteError($"### Error: Unknown --format parameter {arg}, expecting 'xml' or 'html'");
return false;
}
break;
case "compact":
compact = true;
break;
default:
WriteError($"### Error: Unknown argument: {args[i]}");
return false;
}
}
else if (file1 == null)
{
file1 = arg;
}
else if (file2 == null)
{
file2 = arg;
}
else if (outputFile == null)
{
outputFile = arg;
}
else
{
WriteError("### Error: Too many arguments");
return false;
}
}
if (file1 == null || file2 == null || outputFile == null)
{
WriteError("### Error: Not enough arguments");
return false;
}

return true;
}

void WriteError(string msg) {
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(msg);
Console.ForegroundColor = color;
}

void PrintUsage()
{
Console.WriteLine("Usage: xmldiff file1 file2 outputFile --format [xml|html] --compact");
}

static void Main(string[] args)
{
Program p = new Program();
if (p.ParseCommandLine(args))
{
p.Run();
}
else
{
p.PrintUsage();
}
}

void Run()
{
var view = new XmlDiffView();
var options = XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreWhitespace;
if (File.Exists(outputFile))
{
File.Delete(outputFile);
}
switch (format)
{
case OutputFormat.Xml:
view.DifferencesAsFormattedText(file1, file2, outputFile, false, options);
break;
case OutputFormat.Html:
view.DifferencesSideBySideAsHtml(file1, file2, outputFile, false, options, compact);
break;
default:
break;
}
}
}
9 changes: 9 additions & 0 deletions src/XmlDiffTool/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"profiles": {
"XmlDiffTool": {
"commandName": "Project",
"commandLineArgs": "\"file1.xml\" \"file2.xml\" --format html out.htm",
"workingDirectory": "D:\\git\\lovettchris\\xmldiff"
}
}
}
15 changes: 15 additions & 0 deletions src/XmlDiffTool/XmlDiffTool.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>xmldiff</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\XmlDiffView\XmlDiffView.csproj" />
<ProjectReference Include="..\XmlDiff\XmlDiff.csproj" />
</ItemGroup>

</Project>

0 comments on commit 0cc21e7

Please sign in to comment.