seattlerb/flay

N-way diff output can contain preceding comment lines not in duplicated block.

ggallen opened this issue · 4 comments

Ruby file #1:

# Comment

$:.push("#{File.dirname(__FILE__)}/../VolumeManager")

# Comment 2

    class ConsoleFormatter < Log4r::Formatter
        def format(event)
            (event.data.kind_of?(String) ? event.data : event.data.inspect) + "\n"
        end
    end

Ruby file #2:

# Comment from test3
$:.push("#{File.dirname(__FILE__)}/..")

class ConsoleFormatter < Log4r::Formatter
    def format(event)
        (event.data.kind_of?(String) ? event.data : event.data.inspect) + "\n"
    end
end

Output of "flay --diff file1 file2:

[root@gallen-rhel6 flay]# flay --diff test2.rb test3.rb 
Total score (lower is better) = 68

1) IDENTICAL code found in :class (mass*2 = 68)
  A: test2.rb:7
  B: test3.rb:4

A: # Comment
B: # Comment from test3
B: class ConsoleFormatter < Log4r::Formatter
A: # Comment 2
B:   def format(event)
B:     ((event.data.kind_of?(String) ? (event.data) : (event.data.inspect)) + "\n")
A: class ConsoleFormatter < Log4r::Formatter
B:   end
A:   def format(event)
B: end
A:     ((event.data.kind_of?(String) ? (event.data) : (event.data.inspect)) + "\n")
A:   end
A: end

These may not in fact be legal ruby files. I hacked them up from my sources to create a small test case. But flay runs on them just fine, and I get the same output with the real sources.

Note the comment lines in the diff output. None are part of the identical code.

Why is this happening?

Here's a simpler test case removing the "$:.push" lines.

test1.rb:

code = 0

class ConsoleFormatter < Log4r::Formatter
    def format(event)
        event.data.kind_of?(String) ? event.data : event.data.inspect) + "\n"
    end
end

test2.rb:

# My comment
x = 34 
class ConsoleFormatter < Log4r::Formatter
    def format(event)
        (event.data.kind_of?(String) ? event.data : event.data.inspect) + "\n"
    end
end

And the flay output:

[root@gallen-rhel6 flay]# flay --diff test1.rb test2.rb 
Total score (lower is better) = 68

1) IDENTICAL code found in :class (mass*2 = 68)
  A: test1.rb:3
  B: test2.rb:3

B: # My comment
   class ConsoleFormatter < Log4r::Formatter
     def format(event)
       ((event.data.kind_of?(String) ? (event.data) : (event.data.inspect)) + "\n")
     end
   end
[root@gallen-rhel6 flay]#  

Sorry, I closed this by accident....

This was a bug in ruby_parser and a pretty big PITA at that. It should be fixed now. I'll get a release out when I can (couple more things to wrap up). Closing this issue. Thanks!