Skip to content

Commit

Permalink
Support multiple SrcDir in findbugs result.
Browse files Browse the repository at this point in the history
Sometimes, the result XML file of findbugs contains
multiple `SrcDir` elements. In such case, this library
tries to use correct `SrcDir`.
  • Loading branch information
seikichi committed Jun 5, 2020
1 parent fb6ba7b commit 3461e2c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
8 changes: 5 additions & 3 deletions lib/pronto/findbugs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ def read_findbugs_report(path)
src_dirs = REXML::XPath.match(doc, '/BugCollection/Project/SrcDir/text()')
return [] if src_dirs.empty?

src = src_dirs.first.to_s
REXML::XPath.match(doc, '/BugCollection/BugInstance').map do |bug|
Offence.new(path_from(bug, src), line_from(bug), message_from(bug))
end
src = src_dirs.find { |s| File.exist?(path_from(bug, s.to_s)) }
next unless src

Offence.new(path_from(bug, src.to_s), line_from(bug), message_from(bug))
end.compact
end

def path_from(bug_node, root)
Expand Down
69 changes: 38 additions & 31 deletions test/pronto/findbugs_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'test_helper'

require 'fileutils'
require 'pathname'
require 'tmpdir'

Expand All @@ -17,28 +18,31 @@ def blame(_path, _lineno)
end

test '#run returns messages when violations are found' do
xml = <<-REPORT_XML
<?xml version="1.0" encoding="UTF-8"?>
<BugCollection>
<Project>
<SrcDir>/path/to/src/main/java</SrcDir>
</Project>
<BugInstance type="ES_COMPARING_STRINGS_WITH_EQ" abbrev="ES" category="BAD_PRACTICE">
<LongMessage>FOO</LongMessage>
<SourceLine start="1" end="1" sourcepath="foo/bar/App.java"></SourceLine>
</BugInstance>
<BugInstance type="ES_COMPARING_STRINGS_WITH_EQ" abbrev="ES" category="BAD_PRACTICE">
<LongMessage>BAR</LongMessage>
<SourceLine start="42" end="42" sourcepath="foo/bar/App.java"></SourceLine>
</BugInstance>
</BugCollection>
REPORT_XML

Dir.mktmpdir do |dir|
xml = <<-REPORT_XML
<?xml version="1.0" encoding="UTF-8"?>
<BugCollection>
<Project>
<SrcDir>#{dir}/src/main/java</SrcDir>
</Project>
<BugInstance type="ES_COMPARING_STRINGS_WITH_EQ" abbrev="ES" category="BAD_PRACTICE">
<LongMessage>FOO</LongMessage>
<SourceLine start="1" end="1" sourcepath="foo/bar/App.java"></SourceLine>
</BugInstance>
<BugInstance type="ES_COMPARING_STRINGS_WITH_EQ" abbrev="ES" category="BAD_PRACTICE">
<LongMessage>BAR</LongMessage>
<SourceLine start="42" end="42" sourcepath="foo/bar/App.java"></SourceLine>
</BugInstance>
</BugCollection>
REPORT_XML

ENV.store('PRONTO_FINDBUGS_REPORTS_DIR', dir)
File.write(File.join(dir, 'main.xml'), xml)

findbugs = Pronto::Findbugs.new(create_new_file_patches('/path/to', 'src/main/java/foo/bar/App.java'))
FileUtils.mkdir_p(File.join(dir, 'src/main/java/foo/bar/'))
File.write(File.join(dir, 'src/main/java/foo/bar/App.java'), '// dummy')

findbugs = Pronto::Findbugs.new(create_new_file_patches(dir, 'src/main/java/foo/bar/App.java'))
messages = findbugs.run

assert_equal(1, messages.size)
Expand All @@ -48,23 +52,26 @@ def blame(_path, _lineno)
end

test '#run works against findbugs reports without messages' do
xml = <<-REPORT_XML
<?xml version="1.0" encoding="UTF-8"?>
<BugCollection>
<Project>
<SrcDir>/path/to/src/main/java</SrcDir>
</Project>
<BugInstance type="ES_COMPARING_STRINGS_WITH_EQ" abbrev="ES" category="BAD_PRACTICE">
<SourceLine start="1" end="1" sourcepath="foo/bar/App.java"></SourceLine>
</BugInstance>
</BugCollection>
REPORT_XML

Dir.mktmpdir do |dir|
xml = <<-REPORT_XML
<?xml version="1.0" encoding="UTF-8"?>
<BugCollection>
<Project>
<SrcDir>#{dir}/src/main/java</SrcDir>
</Project>
<BugInstance type="ES_COMPARING_STRINGS_WITH_EQ" abbrev="ES" category="BAD_PRACTICE">
<SourceLine start="1" end="1" sourcepath="foo/bar/App.java"></SourceLine>
</BugInstance>
</BugCollection>
REPORT_XML

ENV.store('PRONTO_FINDBUGS_REPORTS_DIR', dir)
File.write(File.join(dir, 'main.xml'), xml)

findbugs = Pronto::Findbugs.new(create_new_file_patches('/path/to', 'src/main/java/foo/bar/App.java'))
FileUtils.mkdir_p(File.join(dir, 'src/main/java/foo/bar/'))
File.write(File.join(dir, 'src/main/java/foo/bar/App.java'), '// dummy')

findbugs = Pronto::Findbugs.new(create_new_file_patches(dir, 'src/main/java/foo/bar/App.java'))
messages = findbugs.run

assert_equal(1, messages.size)
Expand Down

0 comments on commit 3461e2c

Please sign in to comment.