Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change output timing of GitHubActionsFormatter #10730

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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