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,9 +31,8 @@ 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)

View File

@ -69,27 +69,34 @@ 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
@ -140,22 +147,24 @@ class Caveats
end end
def app_caveats def app_caveats
if keg && keg.app_installed? return unless keg
<<-EOS.undent return unless keg.app_installed?
.app bundles were installed.
Run `brew linkapps #{keg.name}` to symlink these to /Applications. <<-EOS.undent
EOS .app bundles were installed.
end Run `brew linkapps #{keg.name}` to symlink these to /Applications.
EOS
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
<<-EOS.undent return unless keg.elisp_installed?
Emacs Lisp files have been installed to:
#{HOMEBREW_PREFIX}/share/emacs/site-lisp/#{f.name} <<-EOS.undent
EOS Emacs Lisp files have been installed to:
end #{HOMEBREW_PREFIX}/share/emacs/site-lisp/#{f.name}
EOS
end end
def plist_caveats def plist_caveats

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,13 +22,13 @@ 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)
if ARGV.dry_run? disk_space = disk_usage_readable(Cleanup.disk_cleanup_size)
ohai "This operation would free approximately #{disk_space} of disk space." if ARGV.dry_run?
else ohai "This operation would free approximately #{disk_space} of disk space."
ohai "This operation has freed approximately #{disk_space} of disk space." else
end ohai "This operation has freed approximately #{disk_space} of disk space."
end end
end end
end end

View File

@ -31,11 +31,9 @@ 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)

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,11 +245,11 @@ 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."
puts "Software installed with other package managers causes known problems for" opoo "It appears you have MacPorts or Fink installed."
puts "Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again." puts "Software installed with other package managers causes known problems for"
end puts "Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again."
end end
def check_cellar def check_cellar

View File

@ -58,10 +58,11 @@ module Homebrew
def restore_backup(keg, formula) def restore_backup(keg, formula)
path = backup_path(keg) path = backup_path(keg)
if path.directory?
path.rename keg return unless path.directory?
keg.link unless formula.keg_only?
end path.rename keg
keg.link unless formula.keg_only?
end end
def backup_path(path) def backup_path(path)

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,11 +83,11 @@ module Homebrew
end end
end end
unless problem_count.zero? return if problem_count.zero?
problems = "problem" + plural(problem_count)
formulae = "formula" + plural(formula_count, "e") problems = "problem" + plural(problem_count)
ofail "#{problem_count} #{problems} in #{formula_count} #{formulae}" formulae = "formula" + plural(formula_count, "e")
end ofail "#{problem_count} #{problems} in #{formula_count} #{formulae}"
end end
end end
@ -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,24 +670,26 @@ class FormulaAuditor
end end
revision_map = attributes_map[:revision] revision_map = attributes_map[:revision]
if formula.revision.nonzero?
if formula.stable return if formula.revision.zero?
if revision_map[formula.stable.version].empty? # check stable spec
problem "'revision #{formula.revision}' should be removed" if formula.stable
end if revision_map[formula.stable.version].empty? # check stable spec
else # head/devel-only formula
problem "'revision #{formula.revision}' should be removed" problem "'revision #{formula.revision}' should be removed"
end end
else # head/devel-only formula
problem "'revision #{formula.revision}' should be removed"
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?
problem "Use the patch DSL instead of defining a 'patches' method" return if legacy_patches.empty?
legacy_patches.each { |p| audit_patch(p) }
end problem "Use the patch DSL instead of defining a 'patches' method"
legacy_patches.each { |p| audit_patch(p) }
end end
def audit_patch(patch) def audit_patch(patch)
@ -961,61 +965,56 @@ 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)/
bad_system = $1
unless %w[| < > & ; *].any? { |c| bad_system.include? c }
good_system = bad_system.gsub(" ", "\", \"")
problem "Use `system #{good_system}` instead of `system #{bad_system}` "
end
end
if line =~ /(require ["']formula["'])/ if line =~ /system ((["'])[^"' ]*(?:\s[^"' ]*)+\2)/
problem "`#{$1}` is now unnecessary" bad_system = $1
unless %w[| < > & ; *].any? { |c| bad_system.include? c }
good_system = bad_system.gsub(" ", "\", \"")
problem "Use `system #{good_system}` instead of `system #{bad_system}` "
end end
end
if line =~ %r{#\{share\}/#{Regexp.escape(formula.name)}[/'"]} if line =~ /(require ["']formula["'])/
problem "Use \#{pkgshare} instead of \#{share}/#{formula.name}" problem "`#{$1}` is now unnecessary"
end end
if line =~ %r{share(\s*[/+]\s*)(['"])#{Regexp.escape(formula.name)}(?:\2|/)} if line =~ %r{#\{share\}/#{Regexp.escape(formula.name)}[/'"]}
problem "Use pkgshare instead of (share#{$1}\"#{formula.name}\")" problem "Use \#{pkgshare} instead of \#{share}/#{formula.name}"
end end
if line =~ %r{share(\s*[/+]\s*)(['"])#{Regexp.escape(formula.name)}(?:\2|/)}
problem "Use pkgshare instead of (share#{$1}\"#{formula.name}\")"
end end
end end
def audit_caveats def audit_caveats
caveats = formula.caveats.to_s return unless formula.caveats.to_s.include?("setuid")
problem "Don't recommend setuid in the caveats, suggest sudo instead."
if caveats.include?("setuid")
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)
@ -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,10 +51,11 @@ module Homebrew
end end
@put_filenames ||= [] @put_filenames ||= []
unless @put_filenames.include? filename
puts "#{Tty.red}#{filename}#{Tty.reset}" return if @put_filenames.include? filename
@put_filenames << filename
end puts "#{Tty.red}#{filename}#{Tty.reset}"
@put_filenames << filename
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,34 +324,33 @@ 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" => {
"pkg_version" => f.pkg_version.to_s, "pkg_version" => f.pkg_version.to_s,
"path" => f.path.to_s.strip_prefix("#{HOMEBREW_REPOSITORY}/"), "path" => f.path.to_s.strip_prefix("#{HOMEBREW_REPOSITORY}/"),
}, },
"bottle" => { "bottle" => {
"root_url" => bottle.root_url, "root_url" => bottle.root_url,
"prefix" => bottle.prefix, "prefix" => bottle.prefix,
"cellar" => bottle.cellar.to_s, "cellar" => bottle.cellar.to_s,
"rebuild" => bottle.rebuild, "rebuild" => bottle.rebuild,
"tags" => { "tags" => {
Utils::Bottles.tag.to_s => { Utils::Bottles.tag.to_s => {
"filename" => filename.to_s, "filename" => filename.to_s,
"sha256" => sha256, "sha256" => sha256,
},
}, },
}, },
"bintray" => {
"package" => Utils::Bottles::Bintray.package(f.name),
"repository" => Utils::Bottles::Bintray.repository(tap),
},
}, },
} "bintray" => {
File.open("#{filename.prefix}.bottle.json", "w") do |file| "package" => Utils::Bottles::Bintray.package(f.name),
file.write Utils::JSON.dump json "repository" => Utils::Bottles::Bintray.repository(tap),
end },
},
}
File.open("#{filename.prefix}.bottle.json", "w") do |file|
file.write Utils::JSON.dump json
end end
end end