Merge pull request #14826 from dduugg/no-open-struct

Enable Style/OpenStructUse cop
This commit is contained in:
Douglas Eichelberger 2023-03-02 12:30:28 -08:00 committed by GitHub
commit 0384b82adc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 61 deletions

View File

@ -423,9 +423,13 @@ Style/NumericLiterals:
Exclude: Exclude:
- "**/Brewfile" - "**/Brewfile"
# OpenStruct is a nice helper. # TODO: These are pre-existing violations and should be corrected
# to define methods so that call sites can be type-checked.
Style/OpenStructUse: Style/OpenStructUse:
Enabled: false Exclude:
- "Homebrew/cli/args.rb"
- "Homebrew/tab.rb"
- "Taps/**/*.rb"
# Rescuing `StandardError` is an understood default. # Rescuing `StandardError` is an understood default.
Style/RescueStandardError: Style/RescueStandardError:

View File

@ -67,7 +67,10 @@ module Homebrew
opoo e opoo e
used_formulae_missing = true used_formulae_missing = true
# If the formula doesn't exist: fake the needed formula object name. # If the formula doesn't exist: fake the needed formula object name.
# This is a legacy use of OpenStruct that should be refactored.
# rubocop:disable Style/OpenStructUse
args.named.map { |name| OpenStruct.new name: name, full_name: name } args.named.map { |name| OpenStruct.new name: name, full_name: name }
# rubocop:enable Style/OpenStructUse
end end
use_runtime_dependents = args.installed? && use_runtime_dependents = args.installed? &&

View File

@ -12,6 +12,14 @@ module Homebrew
module Completions module Completions
extend T::Sig extend T::Sig
Variables = Struct.new(
:aliases,
:builtin_command_descriptions,
:completion_functions,
:function_mappings,
keyword_init: true,
)
module_function module_function
COMPLETIONS_DIR = (HOMEBREW_REPOSITORY/"completions").freeze COMPLETIONS_DIR = (HOMEBREW_REPOSITORY/"completions").freeze
@ -195,17 +203,16 @@ module Homebrew
sig { params(commands: T::Array[String]).returns(String) } sig { params(commands: T::Array[String]).returns(String) }
def generate_bash_completion_file(commands) def generate_bash_completion_file(commands)
variables = OpenStruct.new variables = Variables.new(
completion_functions: commands.map do |command|
generate_bash_subcommand_completion command
end.compact,
function_mappings: commands.map do |command|
next unless command_gets_completions? command
variables[:completion_functions] = commands.map do |command| "#{command}) _brew_#{Commands.method_name command} ;;"
generate_bash_subcommand_completion command end.compact,
end.compact )
variables[:function_mappings] = commands.map do |command|
next unless command_gets_completions? command
"#{command}) _brew_#{Commands.method_name command} ;;"
end.compact
ERB.new((TEMPLATE_DIR/"bash.erb").read, trim_mode: ">").result(variables.instance_eval { binding }) ERB.new((TEMPLATE_DIR/"bash.erb").read, trim_mode: ">").result(variables.instance_eval { binding })
end end
@ -272,27 +279,27 @@ module Homebrew
sig { params(commands: T::Array[String]).returns(String) } sig { params(commands: T::Array[String]).returns(String) }
def generate_zsh_completion_file(commands) def generate_zsh_completion_file(commands)
variables = OpenStruct.new variables = Variables.new(
aliases: Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.map do |alias_command, command|
alias_command = "'#{alias_command}'" if alias_command.start_with? "-"
command = "'#{command}'" if command.start_with? "-"
"#{alias_command} #{command}"
end.compact,
variables[:aliases] = Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.map do |alias_command, command| builtin_command_descriptions: commands.map do |command|
alias_command = "'#{alias_command}'" if alias_command.start_with? "-" next if Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.key? command
command = "'#{command}'" if command.start_with? "-"
"#{alias_command} #{command}"
end.compact
variables[:builtin_command_descriptions] = commands.map do |command| description = Commands.command_description(command, short: true)
next if Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.key? command next if description.blank?
description = Commands.command_description(command, short: true) description = format_description description
next if description.blank? "'#{command}:#{description}'"
end.compact,
description = format_description description completion_functions: commands.map do |command|
"'#{command}:#{description}'" generate_zsh_subcommand_completion command
end.compact end.compact,
)
variables[:completion_functions] = commands.map do |command|
generate_zsh_subcommand_completion command
end.compact
ERB.new((TEMPLATE_DIR/"zsh.erb").read, trim_mode: ">").result(variables.instance_eval { binding }) ERB.new((TEMPLATE_DIR/"zsh.erb").read, trim_mode: ">").result(variables.instance_eval { binding })
end end
@ -346,11 +353,11 @@ module Homebrew
sig { params(commands: T::Array[String]).returns(String) } sig { params(commands: T::Array[String]).returns(String) }
def generate_fish_completion_file(commands) def generate_fish_completion_file(commands)
variables = OpenStruct.new variables = Variables.new(
completion_functions: commands.map do |command|
variables[:completion_functions] = commands.map do |command| generate_fish_subcommand_completion command
generate_fish_subcommand_completion command end.compact,
end.compact )
ERB.new((TEMPLATE_DIR/"fish.erb").read, trim_mode: ">").result(variables.instance_eval { binding }) ERB.new((TEMPLATE_DIR/"fish.erb").read, trim_mode: ">").result(variables.instance_eval { binding })
end end

View File

@ -14,6 +14,21 @@ module Homebrew
module Manpages module Manpages
extend T::Sig extend T::Sig
Variables = Struct.new(
:alumni,
:commands,
:developer_commands,
:environment_variables,
:global_cask_options,
:global_options,
:lead,
:maintainers,
:official_external_commands,
:plc,
:tsc,
keyword_init: true,
)
module_function module_function
def regenerate_man_pages(quiet:) def regenerate_man_pages(quiet:)
@ -27,32 +42,25 @@ module Homebrew
def build_man_page(quiet:) def build_man_page(quiet:)
template = (SOURCE_PATH/"brew.1.md.erb").read template = (SOURCE_PATH/"brew.1.md.erb").read
variables = OpenStruct.new
variables[:commands] = generate_cmd_manpages(Commands.internal_commands_paths)
variables[:developer_commands] = generate_cmd_manpages(Commands.internal_developer_commands_paths)
variables[:official_external_commands] =
generate_cmd_manpages(Commands.official_external_commands_paths(quiet: quiet))
variables[:global_cask_options] = global_cask_options_manpage
variables[:global_options] = global_options_manpage
variables[:environment_variables] = env_vars_manpage
readme = HOMEBREW_REPOSITORY/"README.md" readme = HOMEBREW_REPOSITORY/"README.md"
variables[:lead] = variables = Variables.new(
readme.read[/(Homebrew's \[Project Leader.*\.)/, 1] commands: generate_cmd_manpages(Commands.internal_commands_paths),
.gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') developer_commands: generate_cmd_manpages(Commands.internal_developer_commands_paths),
variables[:plc] = official_external_commands: generate_cmd_manpages(Commands.official_external_commands_paths(quiet: quiet)),
readme.read[/(Homebrew's \[Project Leadership Committee.*\.)/, 1] global_cask_options: global_cask_options_manpage,
.gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') global_options: global_options_manpage,
variables[:tsc] = environment_variables: env_vars_manpage,
readme.read[/(Homebrew's \[Technical Steering Committee.*\.)/, 1] lead: readme.read[/(Homebrew's \[Project Leader.*\.)/, 1]
.gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1'),
variables[:maintainers] = plc: readme.read[/(Homebrew's \[Project Leadership Committee.*\.)/, 1]
readme.read[/(Homebrew's maintainers .*\.)/, 1] .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1'),
.gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') tsc: readme.read[/(Homebrew's \[Technical Steering Committee.*\.)/, 1]
variables[:alumni] = .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1'),
readme.read[/(Former maintainers .*\.)/, 1] maintainers: readme.read[/(Homebrew's maintainers .*\.)/, 1]
.gsub(/\[([^\]]+)\]\([^)]+\)/, '\1') .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1'),
alumni: readme.read[/(Former maintainers .*\.)/, 1]
.gsub(/\[([^\]]+)\]\([^)]+\)/, '\1'),
)
ERB.new(template, trim_mode: ">").result(variables.instance_eval { binding }) ERB.new(template, trim_mode: ">").result(variables.instance_eval { binding })
end end

View File

@ -49,7 +49,7 @@ describe Homebrew::API::Cask do
describe "::fetch_source" do describe "::fetch_source" do
it "fetches the source of a cask (defaulting to master when no `git_head` is passed)" do it "fetches the source of a cask (defaulting to master when no `git_head` is passed)" do
curl_output = OpenStruct.new(stdout: "foo", success?: true) curl_output = instance_double(SystemCommand::Result, stdout: "foo", success?: true)
expect(Utils::Curl).to receive(:curl_output) expect(Utils::Curl).to receive(:curl_output)
.with("--fail", "https://raw.githubusercontent.com/Homebrew/homebrew-cask/master/Casks/foo.rb") .with("--fail", "https://raw.githubusercontent.com/Homebrew/homebrew-cask/master/Casks/foo.rb")
.and_return(curl_output) .and_return(curl_output)

View File

@ -14,7 +14,7 @@ describe Homebrew::API do
end end
def mock_curl_output(stdout: "", success: true) def mock_curl_output(stdout: "", success: true)
curl_output = OpenStruct.new(stdout: stdout, success?: success) curl_output = instance_double(SystemCommand::Result, stdout: stdout, success?: success)
allow(Utils::Curl).to receive(:curl_output).and_return curl_output allow(Utils::Curl).to receive(:curl_output).and_return curl_output
end end