Port Homebrew::DevCmd::Audit
This commit is contained in:
parent
cae62e0175
commit
0f2efd3939
@ -1,6 +1,7 @@
|
||||
# typed: true
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "abstract_command"
|
||||
require "formula"
|
||||
require "formula_versions"
|
||||
require "utils/curl"
|
||||
@ -20,9 +21,9 @@ require "formula_auditor"
|
||||
require "tap_auditor"
|
||||
|
||||
module Homebrew
|
||||
sig { returns(CLI::Parser) }
|
||||
def self.audit_args
|
||||
Homebrew::CLI::Parser.new do
|
||||
module DevCmd
|
||||
class Audit < AbstractCommand
|
||||
cmd_args do
|
||||
description <<~EOS
|
||||
Check <formula> for Homebrew coding style violations. This should be run before
|
||||
submitting a new formula or cask. If no <formula>|<cask> are provided, check all
|
||||
@ -102,12 +103,9 @@ module Homebrew
|
||||
|
||||
named_args [:formula, :cask], without_api: true
|
||||
end
|
||||
end
|
||||
|
||||
sig { void }
|
||||
def self.audit
|
||||
args = audit_args.parse
|
||||
|
||||
sig { override.void }
|
||||
def run
|
||||
new_cask = args.new? || args.new_cask?
|
||||
new_formula = args.new? || args.new_formula?
|
||||
|
||||
@ -116,7 +114,7 @@ module Homebrew
|
||||
os_arch_combinations = args.os_arch_combinations
|
||||
|
||||
Homebrew.auditing = true
|
||||
inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug?
|
||||
Homebrew.inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug?
|
||||
|
||||
strict = new_formula || args.strict?
|
||||
online = new_formula || args.online?
|
||||
@ -129,7 +127,7 @@ module Homebrew
|
||||
|
||||
audit_formulae, audit_casks = Homebrew.with_no_api_env do # audit requires full Ruby source
|
||||
if args.tap
|
||||
Tap.fetch(args.tap).then do |tap|
|
||||
Tap.fetch(T.must(args.tap)).then do |tap|
|
||||
[
|
||||
tap.formula_files.map { |path| Formulary.factory(path) },
|
||||
tap.cask_files.map { |path| Cask::CaskLoader.load(path) },
|
||||
@ -229,7 +227,8 @@ module Homebrew
|
||||
audit_proc = proc { FormulaAuditor.new(Formulary.factory(path), **options).tap(&:audit) }
|
||||
|
||||
# Audit requires full Ruby source so disable API.
|
||||
# We shouldn't do this for taps however so that we don't unnecessarily require a full Homebrew/core clone.
|
||||
# We shouldn't do this for taps however so that we don't unnecessarily require a full Homebrew/core
|
||||
# clone.
|
||||
fa = if f.core_formula?
|
||||
Homebrew.with_no_api_env(&audit_proc)
|
||||
else
|
||||
@ -278,9 +277,9 @@ module Homebrew
|
||||
problems[[cask.full_name, path]] = errors if errors.any?
|
||||
end
|
||||
|
||||
print_problems(tap_problems, display_filename: args.display_filename?)
|
||||
print_problems(formula_problems, display_filename: args.display_filename?)
|
||||
print_problems(cask_problems, display_filename: args.display_filename?)
|
||||
print_problems(tap_problems)
|
||||
print_problems(formula_problems)
|
||||
print_problems(cask_problems)
|
||||
|
||||
tap_count = tap_problems.keys.count
|
||||
formula_count = formula_problems.keys.count
|
||||
@ -309,7 +308,8 @@ module Homebrew
|
||||
errors_summary += " detected"
|
||||
|
||||
if corrected_problem_count.positive?
|
||||
errors_summary += ", #{Utils.pluralize("problem", corrected_problem_count, include_count: true)} corrected"
|
||||
errors_summary += ", #{Utils.pluralize("problem", corrected_problem_count,
|
||||
include_count: true)} corrected"
|
||||
end
|
||||
|
||||
ofail "#{errors_summary}."
|
||||
@ -334,11 +334,13 @@ module Homebrew
|
||||
end
|
||||
end
|
||||
|
||||
def self.print_problems(results, display_filename:)
|
||||
private
|
||||
|
||||
def print_problems(results)
|
||||
results.each do |(name, path), problems|
|
||||
problem_lines = format_problem_lines(problems)
|
||||
|
||||
if display_filename
|
||||
if args.display_filename?
|
||||
problem_lines.each do |l|
|
||||
puts "#{path}: #{l}"
|
||||
end
|
||||
@ -348,13 +350,17 @@ module Homebrew
|
||||
end
|
||||
end
|
||||
|
||||
def self.format_problem_lines(problems)
|
||||
def format_problem_lines(problems)
|
||||
problems.map do |problem|
|
||||
status = " #{Formatter.success("[corrected]")}" if problem.fetch(:corrected)
|
||||
location = problem.fetch(:location)
|
||||
location = "#{location.line&.to_s&.prepend("line ")}#{location.column&.to_s&.prepend(", col ")}: " if location
|
||||
if location
|
||||
location = "#{location.line&.to_s&.prepend("line ")}#{location.column&.to_s&.prepend(", col ")}: "
|
||||
end
|
||||
message = problem.fetch(:message)
|
||||
"* #{location}#{message.chomp.gsub("\n", "\n ")}#{status}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -5,7 +5,7 @@ require "formulary"
|
||||
require "cmd/shared_examples/args_parse"
|
||||
require "utils/spdx"
|
||||
|
||||
RSpec.describe "brew audit" do
|
||||
RSpec.describe Homebrew::DevCmd::Audit do
|
||||
it_behaves_like "parseable arguments"
|
||||
end
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user