vkononov / compare-xml

CompareXML is a fast, lightweight and feature-rich tool that will solve your XML/HTML comparison or diffing needs. its purpose is to compare two instances of Nokogiri::XML::Node or Nokogiri::XML::NodeSet for equality or equivalency.
MIT License
12 stars 3 forks source link

Element additions are shown as element changes? #6

Open josb opened 1 year ago

josb commented 1 year ago

Consider this code:

require 'compare-xml'

left_s = <<EOS
<?xml version="1.0" encoding="UTF-8"?>
<project>
    <keepDependencies>false</keepDependencies>
    <properties>
        <hudson.plugins.buildblocker.BuildBlockerProperty plugin="build-blocker-plugin@1.7.8">
            <useBuildBlocker>false</useBuildBlocker>
            <blockLevel>GLOBAL</blockLevel>
            <scanQueueFor>DISABLED</scanQueueFor>
            <blockingJobs/>
        </hudson.plugins.buildblocker.BuildBlockerProperty>
        <com.sonyericsson.rebuild.RebuildSettings plugin="rebuild@1.34">
            <autoRebuild>false</autoRebuild>
            <rebuildDisabled>false</rebuildDisabled>
        </com.sonyericsson.rebuild.RebuildSettings>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>approval_message</name>
                    <defaultValue/>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
</project>
EOS

right_s = <<EOS
<?xml version="1.0" encoding="UTF-8"?>
<project>
    <keepDependencies>false</keepDependencies>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                <hudson.model.StringParameterDefinition>
                    <name>approval_message</name>
                    <defaultValue/>
                </hudson.model.StringParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
</project>
EOS

left = Nokogiri::XML left_s
right = Nokogiri::XML right_s
res = CompareXML.equivalent? left, right, verbose: true
pp res

produces

[{:node1=>
   #(Element:0x3c {
     name = "hudson.plugins.buildblocker.BuildBlockerProperty",
     attributes = [
       #(Attr:0x50 { name = "plugin", value = "build-blocker-plugin@1.7.8" })],
     children = [
       #(Text "\n" + "            "),
       #(Element:0x64 {
         name = "useBuildBlocker",
         children = [ #(Text "false")]
         }),
       #(Text "\n" + "            "),
       #(Element:0x78 { name = "blockLevel", children = [ #(Text "GLOBAL")] }),
       #(Text "\n" + "            "),
       #(Element:0x8c {
         name = "scanQueueFor",
         children = [ #(Text "DISABLED")]
         }),
       #(Text "\n" + "            "),
       #(Element:0xa0 { name = "blockingJobs" }),
       #(Text "\n" + "        ")]
     }),
  :node2=>
   #(Element:0xb4 {
     name = "hudson.model.ParametersDefinitionProperty",
     children = [
       #(Text "\n" + "            "),
       #(Element:0xc8 {
         name = "parameterDefinitions",
         children = [
           #(Text "\n" + "                "),
           #(Element:0xdc {
             name = "hudson.model.StringParameterDefinition",
             children = [
               #(Text "\n" + "                    "),
               #(Element:0xf0 {
                 name = "name",
                 children = [ #(Text "approval_message")]
                 }),
               #(Text "\n" + "                    "),
               #(Element:0x104 { name = "defaultValue" }),
               #(Text "\n" + "                ")]
             }),
           #(Text "\n" + "            ")]
         }),
       #(Text "\n" + "        ")]
     }),
  :diff1=>"hudson.plugins.buildblocker.BuildBlockerProperty",
  :diff2=>"hudson.model.ParametersDefinitionProperty"}]

Since hudson.plugins.buildblocker.BuildBlockerProperty and com.sonyericsson.rebuild.RebuildSettings are added to the left XML blob but the XML blobs are otherwise the same, I'd expect node1 to show what it does but node2 to be empty. Instead, node2 seems to be showing what is at the spot in the XML tree where it found what is in node1. That doesn't seem right - it should just show the addition.