diff options
author | Samuel Giddins <segiddins@segiddins.me> | 2016-01-14 16:21:54 -0600 |
---|---|---|
committer | Samuel Giddins <segiddins@segiddins.me> | 2016-01-31 22:21:13 -0600 |
commit | e85b990916f16dd9a56dbafae6503f7a48d55131 (patch) | |
tree | f9244f2ab14db6abf5c0bc38ee5508ccfc94aa30 | |
parent | 9297bcf0135a0bd8c1df29b929267582ca66217d (diff) | |
download | bundler-e85b990916f16dd9a56dbafae6503f7a48d55131.tar.gz |
[RuboCop] Address Style/GuardClause
-rw-r--r-- | .rubocop_todo.yml | 11 | ||||
-rw-r--r-- | lib/bundler.rb | 7 | ||||
-rw-r--r-- | lib/bundler/cli/config.rb | 7 | ||||
-rw-r--r-- | lib/bundler/cli/gem.rb | 10 | ||||
-rw-r--r-- | lib/bundler/cli/install.rb | 7 | ||||
-rw-r--r-- | lib/bundler/cli/update.rb | 7 | ||||
-rw-r--r-- | lib/bundler/cli/viz.rb | 9 | ||||
-rw-r--r-- | lib/bundler/dsl.rb | 6 | ||||
-rw-r--r-- | lib/bundler/endpoint_specification.rb | 7 | ||||
-rw-r--r-- | lib/bundler/fetcher.rb | 12 | ||||
-rw-r--r-- | lib/bundler/fetcher/downloader.rb | 7 | ||||
-rw-r--r-- | lib/bundler/fetcher/index.rb | 7 | ||||
-rw-r--r-- | lib/bundler/gem_helper.rb | 31 | ||||
-rw-r--r-- | lib/bundler/graph.rb | 32 | ||||
-rw-r--r-- | lib/bundler/index.rb | 9 | ||||
-rw-r--r-- | lib/bundler/installer.rb | 5 | ||||
-rw-r--r-- | lib/bundler/lockfile_parser.rb | 5 | ||||
-rw-r--r-- | lib/bundler/resolver.rb | 19 | ||||
-rw-r--r-- | lib/bundler/runtime.rb | 9 | ||||
-rw-r--r-- | lib/bundler/shared_helpers.rb | 7 | ||||
-rw-r--r-- | lib/bundler/source/path.rb | 10 | ||||
-rw-r--r-- | spec/quality_spec.rb | 15 | ||||
-rw-r--r-- | spec/support/artifice/endpoint_creds_diff_host.rb | 7 | ||||
-rw-r--r-- | spec/support/helpers.rb | 30 |
24 files changed, 117 insertions, 159 deletions
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a56603857f..c2c9e322e9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -175,17 +175,6 @@ Style/GlobalVars: - 'spec/spec_helper.rb' - 'spec/support/helpers.rb' -# Offense count: 7 -# Configuration parameters: MinBodyLength. -Style/GuardClause: - Exclude: - - 'lib/bundler/cli/config.rb' - - 'lib/bundler/cli/viz.rb' - - 'lib/bundler/dsl.rb' - - 'lib/bundler/fetcher.rb' - - 'lib/bundler/fetcher/index.rb' - - 'lib/bundler/graph.rb' - # Offense count: 2 Style/IfInsideElse: Exclude: diff --git a/lib/bundler.rb b/lib/bundler.rb index 474253aabd..ee2c78ea81 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -424,10 +424,9 @@ module Bundler def upgrade_lockfile lockfile = default_lockfile - if lockfile.exist? && lockfile.read(3) == "---" - Bundler.ui.warn "Detected Gemfile.lock generated by 0.9, deleting..." - lockfile.rmtree - end + return unless lockfile.exist? && lockfile.read(3) == "---" + Bundler.ui.warn "Detected Gemfile.lock generated by 0.9, deleting..." + lockfile.rmtree end end end diff --git a/lib/bundler/cli/config.rb b/lib/bundler/cli/config.rb index cfb8b660e8..2dc628b2dc 100644 --- a/lib/bundler/cli/config.rb +++ b/lib/bundler/cli/config.rb @@ -9,10 +9,9 @@ module Bundler @thor = thor @name = peek = args.shift @scope = "global" - if peek && peek =~ /^\-\-/ - @name = args.shift - @scope = $' - end + return unless peek && peek =~ /^\-\-/ + @name = args.shift + @scope = $' end def run diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb index 96d5a6849a..8c306d5e88 100644 --- a/lib/bundler/cli/gem.rb +++ b/lib/bundler/cli/gem.rb @@ -135,10 +135,8 @@ module Bundler `git add .` end - if options[:edit] - # Open gemspec in editor - thor.run("#{options["edit"]} \"#{target.join("#{name}.gemspec")}\"") - end + # Open gemspec in editor + open_editor(options["edit"], target.join("#{name}.gemspec")) if options[:edit] end private @@ -207,5 +205,9 @@ module Bundler exit 1 end end + + def open_editor(editor, file) + thor.run(%(#{editor} "#{file}")) + end end end diff --git a/lib/bundler/cli/install.rb b/lib/bundler/cli/install.rb index 4640ad9485..559189b314 100644 --- a/lib/bundler/cli/install.rb +++ b/lib/bundler/cli/install.rb @@ -165,10 +165,9 @@ module Bundler end def confirm_without_groups - if Bundler.settings.without.any? - require "bundler/cli/common" - Bundler.ui.confirm Bundler::CLI::Common.without_groups_message - end + return unless Bundler.settings.without.any? + require "bundler/cli/common" + Bundler.ui.confirm Bundler::CLI::Common.without_groups_message end def dependencies_count_for(definition) diff --git a/lib/bundler/cli/update.rb b/lib/bundler/cli/update.rb index 62abc13b58..0102bf0e1a 100644 --- a/lib/bundler/cli/update.rb +++ b/lib/bundler/cli/update.rb @@ -63,10 +63,9 @@ module Bundler private def without_groups_messages - if Bundler.settings.without.any? - require "bundler/cli/common" - Bundler.ui.confirm Bundler::CLI::Common.without_groups_message - end + return unless Bundler.settings.without.any? + require "bundler/cli/common" + Bundler.ui.confirm Bundler::CLI::Common.without_groups_message end end end diff --git a/lib/bundler/cli/viz.rb b/lib/bundler/cli/viz.rb index 3e1b29a513..8fbc64d5fa 100644 --- a/lib/bundler/cli/viz.rb +++ b/lib/bundler/cli/viz.rb @@ -15,12 +15,9 @@ module Bundler Bundler.ui.warn "Make sure you have the graphviz ruby gem. You can install it with:" Bundler.ui.warn "`gem install ruby-graphviz`" rescue StandardError => e - if e.message =~ /GraphViz not installed or dot not in PATH/ - Bundler.ui.error e.message - Bundler.ui.warn "Please install GraphViz. On a Mac with homebrew, you can run `brew install graphviz`." - else - raise - end + raise unless e.message =~ /GraphViz not installed or dot not in PATH/ + Bundler.ui.error e.message + Bundler.ui.warn "Please install GraphViz. On a Mac with homebrew, you can run `brew install graphviz`." end end end diff --git a/lib/bundler/dsl.rb b/lib/bundler/dsl.rb index 57be120d9f..8a3931e852 100644 --- a/lib/bundler/dsl.rb +++ b/lib/bundler/dsl.rb @@ -93,9 +93,8 @@ module Bundler if current.requirement != dep.requirement if current.type == :development @dependencies.delete current - elsif dep.type == :development - return else + return if dep.type == :development raise GemfileError, "You cannot specify the same gem twice with different version requirements.\n" \ "You specified: #{current.name} (#{current.requirement}) and #{dep.name} (#{dep.requirement})" end @@ -109,9 +108,8 @@ module Bundler if current.source != dep.source if current.type == :development @dependencies.delete current - elsif dep.type == :development - return else + return if dep.type == :development raise GemfileError, "You cannot specify the same gem twice coming from different sources.\n" \ "You specified that #{dep.name} (#{dep.requirement}) should come from " \ "#{current.source || "an unspecified source"} and #{dep.source}\n" diff --git a/lib/bundler/endpoint_specification.rb b/lib/bundler/endpoint_specification.rb index 24f1637b9e..a571e0f2f2 100644 --- a/lib/bundler/endpoint_specification.rb +++ b/lib/bundler/endpoint_specification.rb @@ -83,10 +83,9 @@ module Bundler end def _local_specification - if @loaded_from && File.exist?(local_specification_path) - eval(File.read(local_specification_path)).tap do |spec| - spec.loaded_from = @loaded_from - end + return unless @loaded_from && File.exist?(local_specification_path) + eval(File.read(local_specification_path)).tap do |spec| + spec.loaded_from = @loaded_from end end diff --git a/lib/bundler/fetcher.rb b/lib/bundler/fetcher.rb index 2511b964d6..8c9da09684 100644 --- a/lib/bundler/fetcher.rb +++ b/lib/bundler/fetcher.rb @@ -127,11 +127,10 @@ module Bundler specs.each do |name, version, platform, dependencies| next if name == "bundler" - spec = nil - if dependencies - spec = EndpointSpecification.new(name, version, platform, dependencies) + spec = if dependencies + EndpointSpecification.new(name, version, platform, dependencies) else - spec = RemoteSpecification.new(name, version, platform, self) + RemoteSpecification.new(name, version, platform, self) end spec.source = source spec.remote = @remote @@ -198,9 +197,8 @@ module Bundler end def http_proxy - if uri = connection.proxy_uri - uri.to_s - end + return unless uri = connection.proxy_uri + uri.to_s end def inspect diff --git a/lib/bundler/fetcher/downloader.rb b/lib/bundler/fetcher/downloader.rb index 68f63ed54d..9d8d783c8a 100644 --- a/lib/bundler/fetcher/downloader.rb +++ b/lib/bundler/fetcher/downloader.rb @@ -46,11 +46,8 @@ module Bundler end connection.request(uri, req) rescue NoMethodError => e - if ["undefined method", "use_ssl="].all? {|snippet| e.message.include? snippet } - raise LoadError.new("cannot load such file -- openssl") - else - raise e - end + raise unless ["undefined method", "use_ssl="].all? {|snippet| e.message.include? snippet } + raise LoadError.new("cannot load such file -- openssl") rescue OpenSSL::SSL::SSLError raise CertificateFailureError.new(uri) rescue *HTTP_ERRORS => e diff --git a/lib/bundler/fetcher/index.rb b/lib/bundler/fetcher/index.rb index 1d8aa657a8..f23811c8c5 100644 --- a/lib/bundler/fetcher/index.rb +++ b/lib/bundler/fetcher/index.rb @@ -13,11 +13,8 @@ module Bundler when /401/ raise AuthenticationRequiredError, remote_uri when /403/ - if remote_uri.userinfo - raise BadAuthenticationError, remote_uri - else - raise AuthenticationRequiredError, remote_uri - end + raise BadAuthenticationError, remote_uri if remote_uri.userinfo + raise AuthenticationRequiredError, remote_uri else Bundler.ui.trace e raise HTTPError, "Could not fetch specs from #{display_uri}" diff --git a/lib/bundler/gem_helper.rb b/lib/bundler/gem_helper.rb index 8cfbeb4df1..30822148b4 100644 --- a/lib/bundler/gem_helper.rb +++ b/lib/bundler/gem_helper.rb @@ -91,18 +91,17 @@ module Bundler protected def rubygem_push(path) - if Pathname.new("~/.gem/credentials").expand_path.exist? - allowed_push_host = nil - gem_command = "gem push '#{path}'" - if @gemspec.respond_to?(:metadata) - allowed_push_host = @gemspec.metadata["allowed_push_host"] - gem_command << " --host #{allowed_push_host}" if allowed_push_host - end - sh(gem_command) - Bundler.ui.confirm "Pushed #{name} #{version} to #{allowed_push_host ? allowed_push_host : "rubygems.org."}" - else + unless Pathname.new("~/.gem/credentials").expand_path.file? raise "Your rubygems.org credentials aren't set. Run `gem push` to set them." end + allowed_push_host = nil + gem_command = "gem push '#{path}'" + if @gemspec.respond_to?(:metadata) + allowed_push_host = @gemspec.metadata["allowed_push_host"] + gem_command << " --host #{allowed_push_host}" if allowed_push_host + end + sh(gem_command) + Bundler.ui.confirm "Pushed #{name} #{version} to #{allowed_push_host ? allowed_push_host : "rubygems.org."}" end def built_gem_path @@ -122,10 +121,9 @@ module Bundler end def already_tagged? - if sh("git tag").split(/\n/).include?(version_tag) - Bundler.ui.confirm "Tag #{version_tag} has already been created." - true - end + return false unless sh("git tag").split(/\n/).include?(version_tag) + Bundler.ui.confirm "Tag #{version_tag} has already been created." + true end def guard_clean @@ -164,11 +162,10 @@ module Bundler def sh(cmd, &block) out, code = sh_with_code(cmd, &block) - if code == 0 - out - else + unless code.zero? raise(out.empty? ? "Running `#{cmd}` failed. Run this command directly for more detailed output." : out) end + out end def sh_with_code(cmd, &block) diff --git a/lib/bundler/graph.rb b/lib/bundler/graph.rb index 414ec712fa..61c47380ac 100644 --- a/lib/bundler/graph.rb +++ b/lib/bundler/graph.rb @@ -31,25 +31,23 @@ module Bundler def _populate_relations parent_dependencies = _groups.values.to_set.flatten loop do - if parent_dependencies.empty? - break - else - tmp = Set.new - parent_dependencies.each do |dependency| - # if the dependency is a prerelease, allow to_spec to be non-nil - dependency.prerelease = true - - child_dependencies = dependency.to_spec.runtime_dependencies.to_set - @relations[dependency.name] += child_dependencies.map(&:name).to_set - tmp += child_dependencies - - @node_options[dependency.name] = _make_label(dependency, :node) - child_dependencies.each do |c_dependency| - @edge_options["#{dependency.name}_#{c_dependency.name}"] = _make_label(c_dependency, :edge) - end + break if parent_dependencies.empty? + + tmp = Set.new + parent_dependencies.each do |dependency| + # if the dependency is a prerelease, allow to_spec to be non-nil + dependency.prerelease = true + + child_dependencies = dependency.to_spec.runtime_dependencies.to_set + @relations[dependency.name] += child_dependencies.map(&:name).to_set + tmp += child_dependencies + + @node_options[dependency.name] = _make_label(dependency, :node) + child_dependencies.each do |c_dependency| + @edge_options["#{dependency.name}_#{c_dependency.name}"] = _make_label(c_dependency, :edge) end - parent_dependencies = tmp end + parent_dependencies = tmp end end diff --git a/lib/bundler/index.rb b/lib/bundler/index.rb index 1fa90e71ee..40ae97d5bd 100644 --- a/lib/bundler/index.rb +++ b/lib/bundler/index.rb @@ -139,12 +139,9 @@ module Bundler end def add_source(index) - if index.is_a?(Index) - @sources << index - @sources.uniq! # need to use uniq! here instead of checking for the item before adding - else - raise ArgumentError, "Source must be an index, not #{index.class}" - end + raise ArgumentError, "Source must be an index, not #{index.class}" unless index.is_a?(Index) + @sources << index + @sources.uniq! # need to use uniq! here instead of checking for the item before adding end private diff --git a/lib/bundler/installer.rb b/lib/bundler/installer.rb index 8257458476..4be5403607 100644 --- a/lib/bundler/installer.rb +++ b/lib/bundler/installer.rb @@ -190,9 +190,8 @@ module Bundler end end - unless local - options["local"] ? @definition.resolve_with_cache! : @definition.resolve_remotely! - end + return if local + options["local"] ? @definition.resolve_with_cache! : @definition.resolve_remotely! end end end diff --git a/lib/bundler/lockfile_parser.rb b/lib/bundler/lockfile_parser.rb index 1412cb879b..f802157cca 100644 --- a/lib/bundler/lockfile_parser.rb +++ b/lib/bundler/lockfile_parser.rb @@ -192,9 +192,8 @@ module Bundler def parse_bundled_with(line) line = line.strip - if Gem::Version.correct?(line) - @bundler_version = Gem::Version.create(line) - end + return unless Gem::Version.correct?(line) + @bundler_version = Gem::Version.create(line) end def parse_ruby(line) diff --git a/lib/bundler/resolver.rb b/lib/bundler/resolver.rb index a10c03b0f6..e0a7f186ef 100644 --- a/lib/bundler/resolver.rb +++ b/lib/bundler/resolver.rb @@ -216,11 +216,10 @@ module Bundler # @param [Integer] depth the current depth of the resolution process. # @return [void] def debug(depth = 0) - if debug? - debug_info = yield - debug_info = debug_info.inspect unless debug_info.is_a?(String) - STDERR.puts debug_info.split("\n").map {|s| " " * depth + s } - end + return unless debug? + debug_info = yield + debug_info = debug_info.inspect unless debug_info.is_a?(String) + STDERR.puts debug_info.split("\n").map {|s| " " * depth + s } end def debug? @@ -343,11 +342,11 @@ module Bundler name = requirement.name versions = @source_requirements[name][name].map(&:version) message = "Could not find gem '#{requirement}' in #{requirement.source}.\n" - if versions.any? - message << "Source contains '#{name}' at: #{versions.join(", ")}" - else - message << "Source does not contain any versions of '#{requirement}'" - end + message << if versions.any? + "Source contains '#{name}' at: #{versions.join(", ")}" + else + "Source does not contain any versions of '#{requirement}'" + end else message = "Could not find gem '#{requirement}' in any of the gem sources " \ "listed in your Gemfile or available on this machine." diff --git a/lib/bundler/runtime.rb b/lib/bundler/runtime.rb index 400a0c878d..a663026251 100644 --- a/lib/bundler/runtime.rb +++ b/lib/bundler/runtime.rb @@ -273,11 +273,10 @@ module Bundler man_subdir unless Dir[man_subdir + "/man?/"].empty? end.compact - unless manuals.empty? - ENV["MANPATH"] = manuals.concat( - ENV["MANPATH"].to_s.split(File::PATH_SEPARATOR) - ).uniq.join(File::PATH_SEPARATOR) - end + return if manuals.empty? + ENV["MANPATH"] = manuals.concat( + ENV["MANPATH"].to_s.split(File::PATH_SEPARATOR) + ).uniq.join(File::PATH_SEPARATOR) end def remove_dir(dir, dry_run) diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb index 48038ac78b..75aaf7c8f8 100644 --- a/lib/bundler/shared_helpers.rb +++ b/lib/bundler/shared_helpers.rb @@ -162,10 +162,9 @@ module Bundler def set_rubyopt rubyopt = [ENV["RUBYOPT"]].compact - if rubyopt.empty? || rubyopt.first !~ %r{-rbundler/setup} - rubyopt.unshift %(-rbundler/setup) - ENV["RUBYOPT"] = rubyopt.join(" ") - end + return unless rubyopt.empty? || rubyopt.first =~ %r{-rbundler/setup} + rubyopt.unshift %(-rbundler/setup) + ENV["RUBYOPT"] = rubyopt.join(" ") end def set_rubylib diff --git a/lib/bundler/source/path.rb b/lib/bundler/source/path.rb index 24a01b4060..605aa5ae71 100644 --- a/lib/bundler/source/path.rb +++ b/lib/bundler/source/path.rb @@ -156,10 +156,14 @@ module Bundler end end end - elsif File.exist?(expanded_path) - raise PathError, "The path `#{expanded_path}` is not a directory." else - raise PathError, "The path `#{expanded_path}` does not exist." + message = "The path `#{expanded_path}` " + message << if File.exist?(expanded_path) + "is not a directory." + else + "does not exist." + end + raise PathError, message end index diff --git a/spec/quality_spec.rb b/spec/quality_spec.rb index bbe78a4079..4b7cdba1ff 100644 --- a/spec/quality_spec.rb +++ b/spec/quality_spec.rb @@ -13,9 +13,8 @@ describe "The library itself" do failing_lines << number + 1 if line =~ /^ *(describe|it|context) {1}'{1}/ end - unless failing_lines.empty? - "#{filename} uses inconsistent single quotes on lines #{failing_lines.join(", ")}" - end + return if failing_lines.empty? + "#{filename} uses inconsistent single quotes on lines #{failing_lines.join(", ")}" end def check_for_tab_characters(filename) @@ -24,9 +23,8 @@ describe "The library itself" do failing_lines << number + 1 if line =~ /\t/ end - unless failing_lines.empty? - "#{filename} has tab characters on lines #{failing_lines.join(", ")}" - end + return if failing_lines.empty? + "#{filename} has tab characters on lines #{failing_lines.join(", ")}" end def check_for_extra_spaces(filename) @@ -37,9 +35,8 @@ describe "The library itself" do failing_lines << number + 1 if line =~ /\s+\n$/ end - unless failing_lines.empty? - "#{filename} has spaces on the EOL on lines #{failing_lines.join(", ")}" - end + return if failing_lines.empty? + "#{filename} has spaces on the EOL on lines #{failing_lines.join(", ")}" end RSpec::Matchers.define :be_well_formed do diff --git a/spec/support/artifice/endpoint_creds_diff_host.rb b/spec/support/artifice/endpoint_creds_diff_host.rb index 0c10242daf..97a81d97d8 100644 --- a/spec/support/artifice/endpoint_creds_diff_host.rb +++ b/spec/support/artifice/endpoint_creds_diff_host.rb @@ -13,10 +13,9 @@ class EndpointCredsDiffHost < Endpoint end def protected! - unless authorized? - response["WWW-Authenticate"] = %(Basic realm="Testing HTTP Auth") - throw(:halt, [401, "Not authorized\n"]) - end + return if authorized? + response["WWW-Authenticate"] = %(Basic realm="Testing HTTP Auth") + throw(:halt, [401, "Not authorized\n"]) end end diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index 182f7b9bad..8aade71af9 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -284,14 +284,13 @@ module Spec ENV["GEM_PATH"] = system_gem_path.to_s install_gems(*gems) - if block_given? - begin - yield - ensure - ENV["GEM_HOME"] = gem_home - ENV["GEM_PATH"] = gem_path - ENV["PATH"] = path - end + return unless block_given? + begin + yield + ensure + ENV["GEM_HOME"] = gem_home + ENV["GEM_PATH"] = gem_path + ENV["PATH"] = path end end @@ -312,14 +311,13 @@ module Spec gems.each do |gem| gem_command :install, "--no-rdoc --no-ri #{gem}" end - if block_given? - begin - yield - ensure - ENV["GEM_HOME"] = gem_home - ENV["GEM_PATH"] = gem_path - ENV["PATH"] = path - end + return unless block_given? + begin + yield + ensure + ENV["GEM_HOME"] = gem_home + ENV["GEM_PATH"] = gem_path + ENV["PATH"] = path end end |