Skip to content

Commit 9eb0837

Browse files
authored
Merge pull request #12 from reclaim-the-stack/flexible-output
Better output handling including --debug support
2 parents 867494b + 70c0b2a commit 9eb0837

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

rubocop.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
# Setup
77

8+
require "open3"
9+
810
puts "::group::Installing Rubocop gems"
911

1012
if ENV.fetch("RUBOCOP_GEM_VERSIONS").downcase == "gemfile"
@@ -55,7 +57,23 @@
5557
command = "#{rubocop_command} #{changed_ruby_files.map(&:path).join(' ')} --format json --force-exclusion #{ARGV.join(' ')}"
5658

5759
puts "Running rubocop with: #{command}"
58-
JSON.parse(`#{command}`).fetch("files")
60+
stdout, stderr, status = Open3.capture3(command)
61+
62+
if status.success?
63+
puts "Rubocop finished successfully"
64+
else
65+
puts "Rubocop failed with status #{status.exitstatus}"
66+
puts "Rubocop output:\n#{stdout}" unless stdout.empty?
67+
puts "Rubocop error output:\n#{stderr}" unless stderr.empty?
68+
end
69+
70+
# In case --debug is passed to rubocop, the output is not valid JSON so we have to substring it
71+
json_start = stdout.index("{")
72+
json_end = stdout.rindex("}")
73+
74+
abort "ERROR: No JSON found in rubocop output" unless json_start && json_end
75+
76+
JSON.parse(stdout[json_start..json_end]).fetch("files")
5977
else
6078
puts "No changed Ruby files, skipping rubocop"
6179

0 commit comments

Comments
 (0)