From a07363d7392470b71ea44801356af7ab09808c18 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 7 Mar 2020 01:57:28 +0900 Subject: [PATCH] [Fix #7733] Fix rubocop-junit-formatter imcompatibility XML ## Summary Fixes #7733. This PR fixes rubocop-junit-formatter imcompatibility XML for JUnit formatter The original rubocop-junit-formatter displayed all cops with or without offenses. https://github.com/mikian/rubocop-junit-formatter/blob/v0.1.4/lib/rubocop/formatter/junit_formatter.rb#L9 It then displays all cops in XML elements, like the original rubocop-junit-formatter. ## Other Information In the future, it would be preferable to display only enabled cops. https://github.com/mikian/rubocop-junit-formatter/blob/v0.1.4/lib/rubocop/formatter/junit_formatter.rb#L7-L8 --- CHANGELOG.md | 4 ++ lib/rubocop/formatter/junit_formatter.rb | 23 +++++-- .../rubocop/formatter/junit_formatter_spec.rb | 61 +++++++++++++------ 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d96d89895f..c958e4758be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ * [#7654](https://github.com/rubocop-hq/rubocop/issues/7654): Support `with_fixed_indentation` option for `Layout/ArrayAlignment` cop. ([@nikitasakov][]) +### Bug fixes + +* [#7733](https://github.com/rubocop-hq/rubocop/issues/7733): Fix rubocop-junit-formatter imcompatibility XML for JUnit formatter. ([@koic][]) + ## 0.80.1 (2020-02-29) ### Bug fixes diff --git a/lib/rubocop/formatter/junit_formatter.rb b/lib/rubocop/formatter/junit_formatter.rb index 3bfc5f6567a..fe20f09d23a 100644 --- a/lib/rubocop/formatter/junit_formatter.rb +++ b/lib/rubocop/formatter/junit_formatter.rb @@ -29,18 +29,29 @@ def initialize(output, options = {}) end def file_finished(file, offenses) - offenses.group_by(&:cop_name).each do |cop_name, grouped_offenses| + # TODO: Returns all cops with the same behavior as + # the original rubocop-junit-formatter. + # https://github.com/mikian/rubocop-junit-formatter/blob/v0.1.4/lib/rubocop/formatter/junit_formatter.rb#L9 + # + # In the future, it would be preferable to return only enabled cops. + Cop::Cop.all.each do |cop| REXML::Element.new('testcase', @testsuite).tap do |testcase| - testcase.attributes['classname'] = file.gsub( - /\.rb\Z/, '' - ).gsub("#{Dir.pwd}/", '').tr('/', '.') - testcase.attributes['name'] = cop_name + testcase.attributes['classname'] = classname_attribute_value(file) + testcase.attributes['name'] = cop.cop_name - add_failure_to(testcase, grouped_offenses, cop_name) + target_offenses = offenses.select do |offense| + offense.cop_name == cop.cop_name + end + + add_failure_to(testcase, target_offenses, cop.cop_name) end end end + def classname_attribute_value(file) + file.gsub(/\.rb\Z/, '').gsub("#{Dir.pwd}/", '').tr('/', '.') + end + def finished(_inspected_files) @document.write(output, 2) end diff --git a/spec/rubocop/formatter/junit_formatter_spec.rb b/spec/rubocop/formatter/junit_formatter_spec.rb index 4057a5604cc..f713a3f73ee 100644 --- a/spec/rubocop/formatter/junit_formatter_spec.rb +++ b/spec/rubocop/formatter/junit_formatter_spec.rb @@ -6,8 +6,8 @@ let(:output) { StringIO.new } describe '#file_finished' do - it 'displays parsable text' do - cop = RuboCop::Cop::Cop.new + before do + cop = RuboCop::Cop::Layout::SpaceInsideBlockBraces.new source_buffer = Parser::Source::Buffer.new('test', 1) source_buffer.source = %w[foo bar baz].join("\n") @@ -26,30 +26,53 @@ formatter.file_finished('test_2', cop.offenses) formatter.finished(nil) + end - expect(output.string).to eq(<<~XML.chop) + it 'displays start of parsable text' do + expect(output.string).to start_with(<<~XML) - - - test:1:1 - - - test:3:2 - - - - - test:1:1 - - - test:3:2 - - + XML + end + + it 'displays end of parsable text' do + expect(output.string).to end_with(<<~XML.chop) XML end + + it "displays an offfense for `classname='test_1` in parsable text" do + expect(output.string).to include(<<-XML) + + + test:1:1 + + + test:3:2 + + + XML + end + + it "displays an offfense for `classname='test_2` in parsable text" do + expect(output.string).to include(<<-XML) + + + test:1:1 + + + test:3:2 + + + XML + end + + it 'displays a non-offfense element in parsable text' do + expect(output.string).to include(<<~XML) + + XML + end end end