From fe800ad9719c4c7d0b1809973f16cb7515fef205 Mon Sep 17 00:00:00 2001 From: Ryo Nakamura Date: Tue, 21 Jun 2022 06:49:52 +0900 Subject: [PATCH] Change output timing of GitHubActionsFormatter --- changelog/change_change_output_timing_of.md | 1 + .../formatter/git_hub_actions_formatter.rb | 17 +++++++++++++++-- .../formatter/git_hub_actions_formatter_spec.rb | 10 +++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 changelog/change_change_output_timing_of.md diff --git a/changelog/change_change_output_timing_of.md b/changelog/change_change_output_timing_of.md new file mode 100644 index 00000000000..18499012bb3 --- /dev/null +++ b/changelog/change_change_output_timing_of.md @@ -0,0 +1 @@ +* [#10730](https://github.com/rubocop/rubocop/pull/10730): Change output timing of GitHubActionsFormatter. ([@r7kamura][]) diff --git a/lib/rubocop/formatter/git_hub_actions_formatter.rb b/lib/rubocop/formatter/git_hub_actions_formatter.rb index 0628778463b..f6de0bd5d19 100644 --- a/lib/rubocop/formatter/git_hub_actions_formatter.rb +++ b/lib/rubocop/formatter/git_hub_actions_formatter.rb @@ -7,8 +7,21 @@ module Formatter class GitHubActionsFormatter < BaseFormatter ESCAPE_MAP = { '%' => '%25', "\n" => '%0A', "\r" => '%0D' }.freeze + def started(_target_files) + @offenses_for_files = {} + end + def file_finished(file, offenses) - offenses.each { |offense| report_offense(file, offense) } + @offenses_for_files[file] = offenses unless offenses.empty? + end + + def finished(_inspected_files) + @offenses_for_files.each do |file, offenses| + offenses.each do |offense| + report_offense(file, offense) + end + end + output.puts end private @@ -31,7 +44,7 @@ def github_severity(offense) def report_offense(file, offense) output.printf( - "\n::%s file=%s,line=%d,col=%d::%s\n", + "\n::%s file=%s,line=%d,col=%d::%s", severity: github_severity(offense), file: PathUtil.smart_path(file), line: offense.line, diff --git a/spec/rubocop/formatter/git_hub_actions_formatter_spec.rb b/spec/rubocop/formatter/git_hub_actions_formatter_spec.rb index fe7562164ad..f66f41efa72 100644 --- a/spec/rubocop/formatter/git_hub_actions_formatter_spec.rb +++ b/spec/rubocop/formatter/git_hub_actions_formatter_spec.rb @@ -7,7 +7,7 @@ let(:cop_class) { RuboCop::Cop::Cop } let(:output) { StringIO.new } - describe '#file_finished' do + describe '#finished' do let(:file) { '/path/to/file' } let(:message) { 'This is a message.' } let(:status) { :uncorrected } @@ -22,7 +22,11 @@ let(:location) { source_range(0...1) } - before { formatter.file_finished(file, offenses) } + before do + formatter.started([file]) + formatter.file_finished(file, offenses) + formatter.finished([file]) + end context 'when offenses are detected' do it 'reports offenses as errors' do @@ -43,7 +47,7 @@ let(:offenses) { [] } it 'does not print anything' do - expect(output.string).to eq '' + expect(output.string).to eq "\n" end end