Merge pull request #8540 from reitermarkus/shellcheck-json

Split `check_style_impl` into `run_rubocop` and `run_shellcheck`.
This commit is contained in:
Markus Reiter 2020-08-31 20:43:51 +02:00 committed by GitHub
commit f8c3a1bd61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "shellwords"
module Homebrew module Homebrew
# Helper module for running RuboCop. # Helper module for running RuboCop.
# #
@ -20,6 +22,40 @@ module Homebrew
end end
def check_style_impl(files, output_type, def check_style_impl(files, output_type,
fix: false,
except_cops: nil, only_cops: nil,
display_cop_names: false,
debug: false, verbose: false)
raise ArgumentError, "Invalid output type: #{output_type.inspect}" unless [:print, :json].include?(output_type)
shell_files, ruby_files =
Array(files).map(&method(:Pathname))
.partition { |f| f.realpath == HOMEBREW_BREW_FILE.realpath || f.extname == ".sh" }
rubocop_result = if shell_files.any? && ruby_files.none?
output_type == :json ? [] : true
else
run_rubocop(ruby_files, output_type,
fix: fix,
except_cops: except_cops, only_cops: only_cops,
display_cop_names: display_cop_names,
debug: debug, verbose: verbose)
end
shellcheck_result = if ruby_files.any? && shell_files.none?
output_type == :json ? [] : true
else
run_shellcheck(shell_files, output_type)
end
if output_type == :json
RubocopResults.new(rubocop_result + shellcheck_result)
else
rubocop_result && shellcheck_result
end
end
def run_rubocop(files, output_type,
fix: false, except_cops: nil, only_cops: nil, display_cop_names: false, fix: false, except_cops: nil, only_cops: nil, display_cop_names: false,
debug: false, verbose: false) debug: false, verbose: false)
Homebrew.install_bundler_gems! Homebrew.install_bundler_gems!
@ -58,11 +94,11 @@ module Homebrew
args << "--only" << cops_to_include.join(",") args << "--only" << cops_to_include.join(",")
end end
has_non_formula = Array(files).any? do |file| has_non_formula = files.any? do |file|
File.expand_path(file).start_with? HOMEBREW_LIBRARY_PATH File.expand_path(file).start_with? HOMEBREW_LIBRARY_PATH
end end
if files.present? && !has_non_formula if files.any? && !has_non_formula
config = if files.first && File.exist?("#{files.first}/spec") config = if files.first && File.exist?("#{files.first}/spec")
HOMEBREW_LIBRARY/".rubocop_rspec.yml" HOMEBREW_LIBRARY/".rubocop_rspec.yml"
else else
@ -79,63 +115,98 @@ module Homebrew
cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" } cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" }
rubocop_success = false
case output_type case output_type
when :print when :print
args << "--debug" if debug args << "--debug" if debug
args << "--format" << "simple" if files.present? args << "--format" << "simple" if files.present?
system(cache_env, "rubocop", *args)
rubocop_success = $CHILD_STATUS.success? system cache_env, "rubocop", *args
$CHILD_STATUS.success?
when :json when :json
json, err, status = result = system_command "rubocop", args: ["--format", "json", *args], env: cache_env
Open3.capture3(cache_env, "rubocop", "--format", "json", *args) json = json_result!(result)
# exit status of 1 just means violations were found; other numbers mean json["files"]
# execution errors. end
# exitstatus can also be nil if RuboCop process crashes, e.g. due to
# native extension problems.
# JSON needs to be at least 2 characters.
if !(0..1).cover?(status.exitstatus) || json.to_s.length < 2
raise "Error running `rubocop --format json #{args.join " "}`\n#{err}"
end end
return RubocopResults.new(JSON.parse(json)) def run_shellcheck(files, output_type)
else
raise "Invalid output_type for check_style_impl: #{output_type}"
end
return rubocop_success if files.present?
shellcheck = which("shellcheck") shellcheck = which("shellcheck")
shellcheck ||= which("shellcheck", ENV["HOMEBREW_PATH"]) shellcheck ||= which("shellcheck", ENV["HOMEBREW_PATH"])
shellcheck ||= begin shellcheck ||= begin
ohai "Installing `shellcheck` for shell style checks..." ohai "Installing `shellcheck` for shell style checks..."
system HOMEBREW_BREW_FILE, "install", "shellcheck" safe_system HOMEBREW_BREW_FILE, "install", "shellcheck"
which("shellcheck") || which("shellcheck", ENV["HOMEBREW_PATH"]) which("shellcheck") || which("shellcheck", ENV["HOMEBREW_PATH"])
end end
unless shellcheck
opoo "Could not find or install `shellcheck`! Not checking shell style."
return rubocop_success
end
shell_files = [ if files.empty?
files = [
HOMEBREW_BREW_FILE, HOMEBREW_BREW_FILE,
# TODO: HOMEBREW_REPOSITORY/"completions/bash/brew",
*Pathname.glob("#{HOMEBREW_LIBRARY}/Homebrew/*.sh"), *Pathname.glob("#{HOMEBREW_LIBRARY}/Homebrew/*.sh"),
*Pathname.glob("#{HOMEBREW_LIBRARY}/Homebrew/cmd/*.sh"), *Pathname.glob("#{HOMEBREW_LIBRARY}/Homebrew/cmd/*.sh"),
*Pathname.glob("#{HOMEBREW_LIBRARY}/Homebrew/utils/*.sh"), *Pathname.glob("#{HOMEBREW_LIBRARY}/Homebrew/utils/*.sh"),
].select(&:exist?) ]
# TODO: check, fix completions here too. end
# TODO: consider using ShellCheck JSON output
shellcheck_success = system shellcheck, "--shell=bash", *shell_files args = ["--shell=bash", "--", *files] # TODO: Add `--enable=all` to check for more problems.
rubocop_success && shellcheck_success
case output_type
when :print
system shellcheck, "--format=tty", *args
$CHILD_STATUS.success?
when :json
result = system_command shellcheck, args: ["--format=json1", *args]
json = json_result!(result)
# Convert to same format as RuboCop offenses.
json["comments"].group_by { |v| v["file"] }
.map do |k, v|
{
"path" => k,
"offenses" => v.map do |o|
o.delete("file")
o["cop_name"] = "SC#{o.delete("code")}"
level = o.delete("level")
o["severity"] = { "style" => "refactor", "info" => "convention" }.fetch(level, level)
line = o.delete("line")
column = o.delete("column")
o["corrected"] = false
o["correctable"] = o.delete("fix").present?
o["location"] = {
"start_line" => line,
"start_column" => column,
"last_line" => o.delete("endLine"),
"last_column" => o.delete("endColumn"),
"line" => line,
"column" => column,
}
o
end,
}
end
end
end
def json_result!(result)
# An exit status of 1 just means violations were found; other numbers mean
# execution errors.
# JSON needs to be at least 2 characters.
result.assert_success! if !(0..1).cover?(result.status.exitstatus) || result.stdout.length < 2
JSON.parse(result.stdout)
end end
# Result of a RuboCop run. # Result of a RuboCop run.
class RubocopResults class RubocopResults
def initialize(json) def initialize(files)
@metadata = json["metadata"]
@file_offenses = {} @file_offenses = {}
json["files"].each do |f| files.each do |f|
next if f["offenses"].empty? next if f["offenses"].empty?
file = File.realpath(f["path"]) file = File.realpath(f["path"])