Refactor brew style.

This commit is contained in:
Markus Reiter 2019-10-04 23:39:11 +02:00
parent b6b9cd248c
commit e719744248
3 changed files with 19 additions and 19 deletions

View File

@ -95,7 +95,7 @@ module Homebrew
odie "--only-cops/--except-cops and --strict/--only cannot be used simultaneously!" odie "--only-cops/--except-cops and --strict/--only cannot be used simultaneously!"
end end
options = { fix: args.fix?, realpath: true } options = { fix: args.fix? }
if only_cops if only_cops
options[:only_cops] = only_cops options[:only_cops] = only_cops

View File

@ -6,19 +6,17 @@ module Homebrew
# Checks style for a list of files, printing simple RuboCop output. # Checks style for a list of files, printing simple RuboCop output.
# Returns true if violations were found, false otherwise. # Returns true if violations were found, false otherwise.
def check_style_and_print(files, options = {}) def check_style_and_print(files, **options)
check_style_impl(files, :print, options) check_style_impl(files, :print, **options)
end end
# Checks style for a list of files, returning results as a RubocopResults # Checks style for a list of files, returning results as a RubocopResults
# object parsed from its JSON output. # object parsed from its JSON output.
def check_style_json(files, options = {}) def check_style_json(files, **options)
check_style_impl(files, :json, options) check_style_impl(files, :json, **options)
end end
def check_style_impl(files, output_type, options = {}) def check_style_impl(files, output_type, fix: false, except_cops: nil, only_cops: nil)
fix = options[:fix]
Homebrew.install_bundler_gems! Homebrew.install_bundler_gems!
require "rubocop" require "rubocop"
require "rubocops" require "rubocops"
@ -34,22 +32,22 @@ module Homebrew
args += ["--extra-details", "--display-cop-names"] if ARGV.verbose? args += ["--extra-details", "--display-cop-names"] if ARGV.verbose?
if options[:except_cops] if except_cops
options[:except_cops].map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop.to_s, "") } except_cops.map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop.to_s, "") }
cops_to_exclude = options[:except_cops].select do |cop| cops_to_exclude = except_cops.select do |cop|
RuboCop::Cop::Cop.registry.names.include?(cop) || RuboCop::Cop::Cop.registry.names.include?(cop) ||
RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym) RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym)
end end
args << "--except" << cops_to_exclude.join(",") unless cops_to_exclude.empty? args << "--except" << cops_to_exclude.join(",") unless cops_to_exclude.empty?
elsif options[:only_cops] elsif only_cops
options[:only_cops].map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop.to_s, "") } only_cops.map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop.to_s, "") }
cops_to_include = options[:only_cops].select do |cop| cops_to_include = only_cops.select do |cop|
RuboCop::Cop::Cop.registry.names.include?(cop) || RuboCop::Cop::Cop.registry.names.include?(cop) ||
RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym) RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym)
end end
odie "RuboCops #{options[:only_cops].join(",")} were not found" if cops_to_include.empty? odie "RuboCops #{only_cops.join(",")} were not found" if cops_to_include.empty?
args << "--only" << cops_to_include.join(",") args << "--only" << cops_to_include.join(",")
end end
@ -168,8 +166,8 @@ module Homebrew
"[Corrected] " if corrected? "[Corrected] " if corrected?
end end
def to_s(options = {}) def to_s(display_cop_name: false)
if options[:display_cop_name] if display_cop_name
"#{severity_code}: #{location.to_short_s}: #{cop_name}: " \ "#{severity_code}: #{location.to_short_s}: #{cop_name}: " \
"#{Tty.green}#{correction_status}#{Tty.reset}#{message}" "#{Tty.green}#{correction_status}#{Tty.reset}#{message}"
else else

View File

@ -62,8 +62,10 @@ describe "brew style" do
end end
end end
EOS EOS
options = { fix: true, only_cops: ["NewFormulaAudit/DependencyOrder"], realpath: true } rubocop_result = Homebrew::Style.check_style_json(
rubocop_result = Homebrew::Style.check_style_json([formula], options) [formula],
fix: true, only_cops: ["NewFormulaAudit/DependencyOrder"],
)
offense_string = rubocop_result.file_offenses(formula.realpath).first.to_s offense_string = rubocop_result.file_offenses(formula.realpath).first.to_s
expect(offense_string).to match(/\[Corrected\]/) expect(offense_string).to match(/\[Corrected\]/)
end end