Skip to content

Commit

Permalink
Merge pull request #10730 from r7kamura/feature/improve-github-format…
Browse files Browse the repository at this point in the history
…ter-timing

Change output timing of GitHubActionsFormatter
  • Loading branch information
koic committed Jun 21, 2022
2 parents dfc6e87 + fe800ad commit 1dd8ee3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/change_change_output_timing_of.md
@@ -0,0 +1 @@
* [#10730](https://github.com/rubocop/rubocop/pull/10730): Change output timing of GitHubActionsFormatter. ([@r7kamura][])
17 changes: 15 additions & 2 deletions lib/rubocop/formatter/git_hub_actions_formatter.rb
Expand Up @@ -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
Expand All @@ -31,7 +44,7 @@ def github_severity(offense)

def report_offense(file, offense)
output.printf(
"\n::%<severity>s file=%<file>s,line=%<line>d,col=%<column>d::%<message>s\n",
"\n::%<severity>s file=%<file>s,line=%<line>d,col=%<column>d::%<message>s",
severity: github_severity(offense),
file: PathUtil.smart_path(file),
line: offense.line,
Expand Down
10 changes: 7 additions & 3 deletions spec/rubocop/formatter/git_hub_actions_formatter_spec.rb
Expand Up @@ -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 }
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 1dd8ee3

Please sign in to comment.