Fix Style/GuardClause.

This commit is contained in:
Markus Reiter 2016-09-22 20:12:28 +02:00
parent 6d3de5cf60
commit a5b11a6a5c
13 changed files with 150 additions and 175 deletions

View File

@ -1,6 +1,6 @@
# This configuration was generated by # This configuration was generated by
# `rubocop --auto-gen-config --exclude-limit 100` # `rubocop --auto-gen-config --exclude-limit 100`
# on 2016-09-21 14:04:48 +0200 using RuboCop version 0.43.0. # on 2016-09-22 20:07:41 +0200 using RuboCop version 0.43.0.
# The point is for the user to remove these configuration records # The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base. # one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new # Note that changes in the inspected code, or installation of new
@ -74,7 +74,7 @@ Metrics/BlockNesting:
# Offense count: 20 # Offense count: 20
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
Metrics/ModuleLength: Metrics/ModuleLength:
Max: 373 Max: 370
# Offense count: 2 # Offense count: 2
# Configuration parameters: CountKeywordArgs. # Configuration parameters: CountKeywordArgs.
@ -119,13 +119,6 @@ Style/ConstantName:
Exclude: Exclude:
- 'Homebrew/os/mac.rb' - 'Homebrew/os/mac.rb'
# Offense count: 3
Style/DoubleNegation:
Exclude:
- 'Homebrew/os/mac/cctools_keg.rb'
- 'Homebrew/os/mac/ruby_keg.rb'
- 'Homebrew/os/mac/xcode.rb'
# Offense count: 1 # Offense count: 1
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: format, sprintf, percent # SupportedStyles: format, sprintf, percent
@ -140,25 +133,14 @@ Style/GlobalVars:
- 'Homebrew/diagnostic.rb' - 'Homebrew/diagnostic.rb'
- 'Homebrew/utils.rb' - 'Homebrew/utils.rb'
# Offense count: 97 # Offense count: 70
# Configuration parameters: MinBodyLength. # Configuration parameters: MinBodyLength.
Style/GuardClause: Style/GuardClause:
Exclude: Exclude:
- 'Taps/**/*' - 'Taps/**/*'
- 'Homebrew/brew.rb'
- 'Homebrew/build.rb'
- 'Homebrew/caveats.rb'
- 'Homebrew/cleaner.rb'
- 'Homebrew/cmd/cleanup.rb'
- 'Homebrew/cmd/diy.rb'
- 'Homebrew/cmd/info.rb'
- 'Homebrew/cmd/install.rb'
- 'Homebrew/cmd/reinstall.rb'
- 'Homebrew/cmd/search.rb'
- 'Homebrew/cmd/update-report.rb' - 'Homebrew/cmd/update-report.rb'
- 'Homebrew/dependency_collector.rb' - 'Homebrew/dependency_collector.rb'
- 'Homebrew/dev-cmd/audit.rb' - 'Homebrew/dev-cmd/audit.rb'
- 'Homebrew/dev-cmd/bottle.rb'
- 'Homebrew/dev-cmd/pull.rb' - 'Homebrew/dev-cmd/pull.rb'
- 'Homebrew/dev-cmd/test-bot.rb' - 'Homebrew/dev-cmd/test-bot.rb'
- 'Homebrew/download_strategy.rb' - 'Homebrew/download_strategy.rb'
@ -188,7 +170,7 @@ Style/GuardClause:
- 'Homebrew/utils/popen.rb' - 'Homebrew/utils/popen.rb'
- 'Homebrew/version.rb' - 'Homebrew/version.rb'
# Offense count: 52 # Offense count: 51
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: MaxLineLength. # Configuration parameters: MaxLineLength.
Style/IfUnlessModifier: Style/IfUnlessModifier:
@ -221,7 +203,6 @@ Style/IfUnlessModifier:
- 'Homebrew/formulary.rb' - 'Homebrew/formulary.rb'
- 'Homebrew/language/haskell.rb' - 'Homebrew/language/haskell.rb'
- 'Homebrew/migrator.rb' - 'Homebrew/migrator.rb'
- 'Homebrew/os/mac/cctools_mach.rb'
- 'Homebrew/tab.rb' - 'Homebrew/tab.rb'
- 'Homebrew/utils/git.rb' - 'Homebrew/utils/git.rb'
@ -277,11 +258,3 @@ Style/OpMethod:
# SupportedStyles: use_perl_names, use_english_names # SupportedStyles: use_perl_names, use_english_names
Style/SpecialGlobalVars: Style/SpecialGlobalVars:
EnforcedStyle: use_perl_names EnforcedStyle: use_perl_names
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, AllowSafeAssignment.
# SupportedStyles: require_parentheses, require_no_parentheses
Style/TernaryParentheses:
Exclude:
- 'Homebrew/formula.rb'

View File

@ -37,9 +37,9 @@ begin
cmd = nil cmd = nil
ARGV.dup.each_with_index do |arg, i| ARGV.dup.each_with_index do |arg, i|
if help_flag && cmd break if help_flag && cmd
break
elsif help_flag_list.include?(arg) if help_flag_list.include?(arg)
# Option-style help: Both `--help <cmd>` and `<cmd> --help` are fine. # Option-style help: Both `--help <cmd>` and `<cmd> --help` are fine.
help_flag = true help_flag = true
elsif arg == "help" && !cmd elsif arg == "help" && !cmd

View File

@ -31,10 +31,9 @@ class Build
def post_superenv_hacks def post_superenv_hacks
# Only allow Homebrew-approved directories into the PATH, unless # Only allow Homebrew-approved directories into the PATH, unless
# a formula opts-in to allowing the user's path. # a formula opts-in to allowing the user's path.
if formula.env.userpaths? || reqs.any? { |rq| rq.env.userpaths? } return unless formula.env.userpaths? || reqs.any? { |rq| rq.env.userpaths? }
ENV.userpaths! ENV.userpaths!
end end
end
def effective_build_options_for(dependent) def effective_build_options_for(dependent)
args = dependent.build.used_options args = dependent.build.used_options

View File

@ -69,28 +69,35 @@ class Caveats
end end
def bash_completion_caveats def bash_completion_caveats
if keg && keg.completion_installed?(:bash) then <<-EOS.undent return unless keg
return unless keg.completion_installed?(:bash)
<<-EOS.undent
Bash completion has been installed to: Bash completion has been installed to:
#{HOMEBREW_PREFIX}/etc/bash_completion.d #{HOMEBREW_PREFIX}/etc/bash_completion.d
EOS EOS
end end
end
def zsh_completion_caveats def zsh_completion_caveats
if keg && keg.completion_installed?(:zsh) then <<-EOS.undent return unless keg
return unless keg.completion_installed?(:zsh)
<<-EOS.undent
zsh completion has been installed to: zsh completion has been installed to:
#{HOMEBREW_PREFIX}/share/zsh/site-functions #{HOMEBREW_PREFIX}/share/zsh/site-functions
EOS EOS
end end
end
def fish_completion_caveats def fish_completion_caveats
if keg && keg.completion_installed?(:fish) && which("fish") then <<-EOS.undent return unless keg
return unless keg.completion_installed?(:fish)
return unless which("fish")
<<-EOS.undent
fish completion has been installed to: fish completion has been installed to:
#{HOMEBREW_PREFIX}/share/fish/vendor_completions.d #{HOMEBREW_PREFIX}/share/fish/vendor_completions.d
EOS EOS
end end
end
def python_caveats def python_caveats
return unless keg return unless keg
@ -140,23 +147,25 @@ class Caveats
end end
def app_caveats def app_caveats
if keg && keg.app_installed? return unless keg
return unless keg.app_installed?
<<-EOS.undent <<-EOS.undent
.app bundles were installed. .app bundles were installed.
Run `brew linkapps #{keg.name}` to symlink these to /Applications. Run `brew linkapps #{keg.name}` to symlink these to /Applications.
EOS EOS
end end
end
def elisp_caveats def elisp_caveats
return if f.keg_only? return if f.keg_only?
if keg && keg.elisp_installed? return unless keg
return unless keg.elisp_installed?
<<-EOS.undent <<-EOS.undent
Emacs Lisp files have been installed to: Emacs Lisp files have been installed to:
#{HOMEBREW_PREFIX}/share/emacs/site-lisp/#{f.name} #{HOMEBREW_PREFIX}/share/emacs/site-lisp/#{f.name}
EOS EOS
end end
end
def plist_caveats def plist_caveats
s = [] s = []

View File

@ -87,9 +87,9 @@ class Cleaner
Find.prune if @f.skip_clean? path Find.prune if @f.skip_clean? path
if path.symlink? || path.directory? next if path.symlink? || path.directory?
next
elsif path.extname == ".la" if path.extname == ".la"
path.unlink path.unlink
elsif path.basename.to_s == "perllocal.pod" elsif path.basename.to_s == "perllocal.pod"
# Both this file & the .packlist one below are completely unnecessary # Both this file & the .packlist one below are completely unnecessary

View File

@ -22,7 +22,8 @@ module Homebrew
ARGV.resolved_formulae.each { |f| Cleanup.cleanup_formula f } ARGV.resolved_formulae.each { |f| Cleanup.cleanup_formula f }
end end
if Cleanup.disk_cleanup_size > 0 return if Cleanup.disk_cleanup_size.zero?
disk_space = disk_usage_readable(Cleanup.disk_cleanup_size) disk_space = disk_usage_readable(Cleanup.disk_cleanup_size)
if ARGV.dry_run? if ARGV.dry_run?
ohai "This operation would free approximately #{disk_space} of disk space." ohai "This operation would free approximately #{disk_space} of disk space."
@ -31,4 +32,3 @@ module Homebrew
end end
end end
end end
end

View File

@ -31,12 +31,10 @@ module Homebrew
def detect_version(path) def detect_version(path)
version = path.version.to_s version = path.version.to_s
if version.empty? raise "Couldn't determine version, set it with --version=<version>" if version.empty?
raise "Couldn't determine version, set it with --version=<version>"
else
version version
end end
end
def detect_name(path, version) def detect_name(path, version)
basename = path.basename.to_s basename = path.basename.to_s

View File

@ -54,11 +54,8 @@ module Homebrew
end end
rescue FormulaUnavailableError rescue FormulaUnavailableError
# No formula with this name, try a blacklist lookup # No formula with this name, try a blacklist lookup
if (blacklist = blacklisted?(f)) raise unless (blacklist = blacklisted?(f))
puts blacklist puts blacklist
else
raise
end
end end
end end
end end

View File

@ -245,12 +245,12 @@ module Homebrew
end end
def check_macports def check_macports
unless MacOS.macports_or_fink.empty? return if MacOS.macports_or_fink.empty?
opoo "It appears you have MacPorts or Fink installed." opoo "It appears you have MacPorts or Fink installed."
puts "Software installed with other package managers causes known problems for" puts "Software installed with other package managers causes known problems for"
puts "Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again." puts "Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again."
end end
end
def check_cellar def check_cellar
FileUtils.mkdir_p HOMEBREW_CELLAR unless File.exist? HOMEBREW_CELLAR FileUtils.mkdir_p HOMEBREW_CELLAR unless File.exist? HOMEBREW_CELLAR

View File

@ -58,11 +58,12 @@ module Homebrew
def restore_backup(keg, formula) def restore_backup(keg, formula)
path = backup_path(keg) path = backup_path(keg)
if path.directory?
return unless path.directory?
path.rename keg path.rename keg
keg.link unless formula.keg_only? keg.link unless formula.keg_only?
end end
end
def backup_path(path) def backup_path(path)
Pathname.new "#{path}.reinstall" Pathname.new "#{path}.reinstall"

View File

@ -171,10 +171,11 @@ module Homebrew
rescue rescue
canonical_name = canonical_full_name = name canonical_name = canonical_full_name = name
end end
# Ignore aliases from results when the full name was also found # Ignore aliases from results when the full name was also found
if aliases.include?(name) && results.include?(canonical_full_name) next if aliases.include?(name) && results.include?(canonical_full_name)
next
elsif (HOMEBREW_CELLAR/canonical_name).directory? if (HOMEBREW_CELLAR/canonical_name).directory?
pretty_installed(name) pretty_installed(name)
else else
name name

View File

@ -83,13 +83,13 @@ module Homebrew
end end
end end
unless problem_count.zero? return if problem_count.zero?
problems = "problem" + plural(problem_count) problems = "problem" + plural(problem_count)
formulae = "formula" + plural(formula_count, "e") formulae = "formula" + plural(formula_count, "e")
ofail "#{problem_count} #{problems} in #{formula_count} #{formulae}" ofail "#{problem_count} #{problems} in #{formula_count} #{formulae}"
end end
end end
end
class FormulaText class FormulaText
def initialize(path) def initialize(path)
@ -279,9 +279,11 @@ class FormulaAuditor
end end
end end
end end
if present.include?("head") && present.include?("head block") if present.include?("head") && present.include?("head block")
problem "Should not have both `head` and `head do`" problem "Should not have both `head` and `head do`"
end end
if present.include?("bottle modifier") && present.include?("bottle block") if present.include?("bottle modifier") && present.include?("bottle block")
problem "Should not have `bottle :unneeded/:disable` and `bottle do`" problem "Should not have `bottle :unneeded/:disable` and `bottle do`"
end end
@ -668,7 +670,9 @@ class FormulaAuditor
end end
revision_map = attributes_map[:revision] revision_map = attributes_map[:revision]
if formula.revision.nonzero?
return if formula.revision.zero?
if formula.stable if formula.stable
if revision_map[formula.stable.version].empty? # check stable spec if revision_map[formula.stable.version].empty? # check stable spec
problem "'revision #{formula.revision}' should be removed" problem "'revision #{formula.revision}' should be removed"
@ -677,16 +681,16 @@ class FormulaAuditor
problem "'revision #{formula.revision}' should be removed" problem "'revision #{formula.revision}' should be removed"
end end
end end
end
def audit_legacy_patches def audit_legacy_patches
return unless formula.respond_to?(:patches) return unless formula.respond_to?(:patches)
legacy_patches = Patch.normalize_legacy_patches(formula.patches).grep(LegacyPatch) legacy_patches = Patch.normalize_legacy_patches(formula.patches).grep(LegacyPatch)
unless legacy_patches.empty?
return if legacy_patches.empty?
problem "Use the patch DSL instead of defining a 'patches' method" problem "Use the patch DSL instead of defining a 'patches' method"
legacy_patches.each { |p| audit_patch(p) } legacy_patches.each { |p| audit_patch(p) }
end end
end
def audit_patch(patch) def audit_patch(patch)
case patch.url case patch.url
@ -961,7 +965,8 @@ class FormulaAuditor
problem "Use Language::Node for npm install args" problem "Use Language::Node for npm install args"
end end
if @strict return unless @strict
if line =~ /system ((["'])[^"' ]*(?:\s[^"' ]*)+\2)/ if line =~ /system ((["'])[^"' ]*(?:\s[^"' ]*)+\2)/
bad_system = $1 bad_system = $1
unless %w[| < > & ; *].any? { |c| bad_system.include? c } unless %w[| < > & ; *].any? { |c| bad_system.include? c }
@ -982,41 +987,35 @@ class FormulaAuditor
problem "Use pkgshare instead of (share#{$1}\"#{formula.name}\")" problem "Use pkgshare instead of (share#{$1}\"#{formula.name}\")"
end end
end end
end
def audit_caveats def audit_caveats
caveats = formula.caveats.to_s return unless formula.caveats.to_s.include?("setuid")
if caveats.include?("setuid")
problem "Don't recommend setuid in the caveats, suggest sudo instead." problem "Don't recommend setuid in the caveats, suggest sudo instead."
end end
end
def audit_reverse_migration def audit_reverse_migration
# Only enforce for new formula being re-added to core and official taps # Only enforce for new formula being re-added to core and official taps
return unless @strict return unless @strict
return unless formula.tap && formula.tap.official? return unless formula.tap && formula.tap.official?
return unless formula.tap.tap_migrations.key?(formula.name)
if formula.tap.tap_migrations.key?(formula.name)
problem <<-EOS.undent problem <<-EOS.undent
#{formula.name} seems to be listed in tap_migrations.json! #{formula.name} seems to be listed in tap_migrations.json!
Please remove #{formula.name} from present tap & tap_migrations.json Please remove #{formula.name} from present tap & tap_migrations.json
before submitting it to Homebrew/homebrew-#{formula.tap.repo}. before submitting it to Homebrew/homebrew-#{formula.tap.repo}.
EOS EOS
end end
end
def audit_prefix_has_contents def audit_prefix_has_contents
return unless formula.prefix.directory? return unless formula.prefix.directory?
return unless Keg.new(formula.prefix).empty_installation?
if Keg.new(formula.prefix).empty_installation?
problem <<-EOS.undent problem <<-EOS.undent
The installation seems to be empty. Please ensure the prefix The installation seems to be empty. Please ensure the prefix
is set correctly and expected files are installed. is set correctly and expected files are installed.
The prefix configure/make argument may be case-sensitive. The prefix configure/make argument may be case-sensitive.
EOS EOS
end end
end
def audit_conditional_dep(dep, condition, line) def audit_conditional_dep(dep, condition, line)
quoted_dep = quote_dep(dep) quoted_dep = quote_dep(dep)
@ -1184,9 +1183,7 @@ class ResourceAuditor
end end
end end
using_strategy = DownloadStrategyDetector.detect("", using) if url_strategy == DownloadStrategyDetector.detect("", using)
if url_strategy == using_strategy
problem "Redundant :using value in URL" problem "Redundant :using value in URL"
end end
end end

View File

@ -51,11 +51,12 @@ module Homebrew
end end
@put_filenames ||= [] @put_filenames ||= []
unless @put_filenames.include? filename
return if @put_filenames.include? filename
puts "#{Tty.red}#{filename}#{Tty.reset}" puts "#{Tty.red}#{filename}#{Tty.reset}"
@put_filenames << filename @put_filenames << filename
end end
end
result = false result = false
@ -137,11 +138,11 @@ module Homebrew
tap = f.tap tap = f.tap
unless tap unless tap
if ARGV.include?("--force-core-tap") unless ARGV.include?("--force-core-tap")
tap = CoreTap.instance
else
return ofail "Formula not from core or any taps: #{f.full_name}" return ofail "Formula not from core or any taps: #{f.full_name}"
end end
tap = CoreTap.instance
end end
if f.bottle_disabled? if f.bottle_disabled?
@ -323,7 +324,7 @@ module Homebrew
puts "./#{filename}" puts "./#{filename}"
puts output puts output
if ARGV.include? "--json" return unless ARGV.include? "--json"
json = { json = {
f.full_name => { f.full_name => {
"formula" => { "formula" => {
@ -352,7 +353,6 @@ module Homebrew
file.write Utils::JSON.dump json file.write Utils::JSON.dump json
end end
end end
end
def merge def merge
write = ARGV.include? "--write" write = ARGV.include? "--write"