[rubygems/rubygems] util/rubocop -A --only Style/RedundantBegin

https://github.com/rubygems/rubygems/commit/b595d3cf0f
This commit is contained in:
Hiroshi SHIBATA 2023-03-16 14:10:30 +09:00
parent a532e9dc37
commit 72d09a568f
16 changed files with 148 additions and 188 deletions

View File

@ -1090,13 +1090,11 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
require "bundler" require "bundler"
begin begin
Gem::DefaultUserInteraction.use_ui(ui) do Gem::DefaultUserInteraction.use_ui(ui) do
begin Bundler.ui.silence do
Bundler.ui.silence do @gemdeps = Bundler.setup
@gemdeps = Bundler.setup
end
ensure
Gem::DefaultUserInteraction.ui.close
end end
ensure
Gem::DefaultUserInteraction.ui.close
end end
rescue Bundler::BundlerError => e rescue Bundler::BundlerError => e
warn e.message warn e.message

View File

@ -429,12 +429,10 @@ class Gem::Command
# True if the command handles the given argument list. # True if the command handles the given argument list.
def handles?(args) def handles?(args)
begin parser.parse!(args.dup)
parser.parse!(args.dup) return true
return true rescue StandardError
rescue StandardError return false
return false
end
end end
## ##

View File

@ -93,14 +93,12 @@ permission to.
def manage_owners(method, name, owners) def manage_owners(method, name, owners)
owners.each do |owner| owners.each do |owner|
begin response = send_owner_request(method, name, owner)
response = send_owner_request(method, name, owner) action = method == :delete ? "Removing" : "Adding"
action = method == :delete ? "Removing" : "Adding"
with_response response, "#{action} #{owner}" with_response response, "#{action} #{owner}"
rescue StandardError rescue StandardError
# ignore # ignore
end
end end
end end

View File

@ -95,12 +95,10 @@ command help for an example.
FileUtils.mkdir_p @options[:target] if @options[:target] FileUtils.mkdir_p @options[:target] if @options[:target]
destination = begin destination = if @options[:target]
if @options[:target] File.join @options[:target], spec_file
File.join @options[:target], spec_file else
else spec_file
spec_file
end
end end
File.open destination, "w" do |io| File.open destination, "w" do |io|

View File

@ -162,13 +162,11 @@ class Gem::DependencyInstaller
specs = [] specs = []
tuples.each do |tup, source| tuples.each do |tup, source|
begin spec = source.fetch_spec(tup)
spec = source.fetch_spec(tup) rescue Gem::RemoteFetcher::FetchError => e
rescue Gem::RemoteFetcher::FetchError => e errors << Gem::SourceFetchProblem.new(source, e)
errors << Gem::SourceFetchProblem.new(source, e) else
else specs << [spec, source]
specs << [spec, source]
end
end end
if @errors if @errors
@ -294,11 +292,9 @@ class Gem::DependencyInstaller
version = src.spec.version if version == Gem::Requirement.default version = src.spec.version if version == Gem::Requirement.default
elsif dep_or_name =~ /\.gem$/ elsif dep_or_name =~ /\.gem$/
Dir[dep_or_name].each do |name| Dir[dep_or_name].each do |name|
begin src = Gem::Source::SpecificFile.new name
src = Gem::Source::SpecificFile.new name installer_set.add_local dep_or_name, src.spec, src
installer_set.add_local dep_or_name, src.spec, src rescue Gem::Package::FormatError
rescue Gem::Package::FormatError
end
end end
# else This is a dependency. InstallerSet handles this case # else This is a dependency. InstallerSet handles this case
end end

View File

@ -58,10 +58,8 @@ class Gem::Resolver::ActivationRequest
if @spec.respond_to? :sources if @spec.respond_to? :sources
exception = nil exception = nil
path = @spec.sources.find do |source| path = @spec.sources.find do |source|
begin source.download full_spec, path
source.download full_spec, path rescue exception
rescue exception
end
end end
return path if path return path if path
raise exception if exception raise exception if exception

View File

@ -44,13 +44,11 @@ class Gem::Security::TrustDir
glob = File.join @dir, "*.pem" glob = File.join @dir, "*.pem"
Dir[glob].each do |certificate_file| Dir[glob].each do |certificate_file|
begin certificate = load_certificate certificate_file
certificate = load_certificate certificate_file
yield certificate, certificate_file yield certificate, certificate_file
rescue OpenSSL::X509::CertificateError rescue OpenSSL::X509::CertificateError
next # HACK: warn next # HACK: warn
end
end end
end end

View File

@ -38,36 +38,34 @@ class Gem::Source::Local < Gem::Source
@specs = {} @specs = {}
Dir["*.gem"].each do |file| Dir["*.gem"].each do |file|
begin pkg = Gem::Package.new(file)
pkg = Gem::Package.new(file) rescue SystemCallError, Gem::Package::FormatError
rescue SystemCallError, Gem::Package::FormatError # ignore
# ignore else
else tup = pkg.spec.name_tuple
tup = pkg.spec.name_tuple @specs[tup] = [File.expand_path(file), pkg]
@specs[tup] = [File.expand_path(file), pkg]
case type case type
when :released when :released
unless pkg.spec.version.prerelease? unless pkg.spec.version.prerelease?
names << pkg.spec.name_tuple
end
when :prerelease
if pkg.spec.version.prerelease?
names << pkg.spec.name_tuple
end
when :latest
tup = pkg.spec.name_tuple
cur = names.find {|x| x.name == tup.name }
if !cur
names << tup
elsif cur.version < tup.version
names.delete cur
names << tup
end
else
names << pkg.spec.name_tuple names << pkg.spec.name_tuple
end end
when :prerelease
if pkg.spec.version.prerelease?
names << pkg.spec.name_tuple
end
when :latest
tup = pkg.spec.name_tuple
cur = names.find {|x| x.name == tup.name }
if !cur
names << tup
elsif cur.version < tup.version
names.delete cur
names << tup
end
else
names << pkg.spec.name_tuple
end end
end end

View File

@ -154,13 +154,11 @@ class Gem::SpecFetcher
specs = [] specs = []
tuples.each do |tup, source| tuples.each do |tup, source|
begin spec = source.fetch_spec(tup)
spec = source.fetch_spec(tup) rescue Gem::RemoteFetcher::FetchError => e
rescue Gem::RemoteFetcher::FetchError => e errors << Gem::SourceFetchProblem.new(source, e)
errors << Gem::SourceFetchProblem.new(source, e) else
else specs << [spec, source]
specs << [spec, source]
end
end end
return [specs, errors] return [specs, errors]
@ -214,34 +212,32 @@ class Gem::SpecFetcher
list = {} list = {}
@sources.each_source do |source| @sources.each_source do |source|
begin names = case type
names = case type when :latest
when :latest tuples_for source, :latest
tuples_for source, :latest when :released
when :released tuples_for source, :released
tuples_for source, :released when :complete
when :complete names =
names = tuples_for(source, :prerelease, true) +
tuples_for(source, :prerelease, true) + tuples_for(source, :released)
tuples_for(source, :released)
names.sort names.sort
when :abs_latest when :abs_latest
names = names =
tuples_for(source, :prerelease, true) + tuples_for(source, :prerelease, true) +
tuples_for(source, :latest) tuples_for(source, :latest)
names.sort names.sort
when :prerelease when :prerelease
tuples_for(source, :prerelease) tuples_for(source, :prerelease)
else else
raise Gem::Exception, "Unknown type - :#{type}" raise Gem::Exception, "Unknown type - :#{type}"
end
rescue Gem::RemoteFetcher::FetchError => e
errors << Gem::SourceFetchProblem.new(source, e)
else
list[source] = names
end end
rescue Gem::RemoteFetcher::FetchError => e
errors << Gem::SourceFetchProblem.new(source, e)
else
list[source] = names
end end
[list, errors] [list, errors]

View File

@ -111,20 +111,18 @@ class Gem::StubSpecification < Gem::BasicSpecification
saved_lineno = $. saved_lineno = $.
Gem.open_file loaded_from, OPEN_MODE do |file| Gem.open_file loaded_from, OPEN_MODE do |file|
begin file.readline # discard encoding line
file.readline # discard encoding line stubline = file.readline.chomp
stubline = file.readline.chomp if stubline.start_with?(PREFIX)
if stubline.start_with?(PREFIX) extensions = if /\A#{PREFIX}/ =~ file.readline.chomp
extensions = if /\A#{PREFIX}/ =~ file.readline.chomp $'.split "\0"
$'.split "\0" else
else StubLine::NO_EXTENSIONS
StubLine::NO_EXTENSIONS
end
@data = StubLine.new stubline, extensions
end end
rescue EOFError
@data = StubLine.new stubline, extensions
end end
rescue EOFError
end end
ensure ensure
$. = saved_lineno $. = saved_lineno

View File

@ -270,7 +270,7 @@ class Gem::StreamUI
when /^y/i then true when /^y/i then true
when /^n/i then false when /^n/i then false
when /^$/ then default when /^$/ then default
end end
end end
return result return result

View File

@ -109,15 +109,13 @@ class Gem::Validator
end end
good.each do |entry, data| good.each do |entry, data|
begin next unless data # HACK: `gem check -a mkrf`
next unless data # HACK: `gem check -a mkrf`
source = File.join gem_directory, entry["path"] source = File.join gem_directory, entry["path"]
File.open source, Gem.binary_mode do |f| File.open source, Gem.binary_mode do |f|
unless f.read == data unless f.read == data
errors[gem_name][entry["path"]] = "Modified from original" errors[gem_name][entry["path"]] = "Modified from original"
end
end end
end end
end end

View File

@ -117,32 +117,30 @@ class Gem::TestCase < Test::Unit::TestCase
# https://github.com/seattlerb/minitest/blob/21d9e804b63c619f602f3f4ece6c71b48974707a/lib/minitest/assertions.rb#L546 # https://github.com/seattlerb/minitest/blob/21d9e804b63c619f602f3f4ece6c71b48974707a/lib/minitest/assertions.rb#L546
def capture_subprocess_io def capture_subprocess_io
_synchronize do _synchronize do
begin require "tempfile"
require "tempfile"
captured_stdout = Tempfile.new("out") captured_stdout = Tempfile.new("out")
captured_stderr = Tempfile.new("err") captured_stderr = Tempfile.new("err")
orig_stdout = $stdout.dup orig_stdout = $stdout.dup
orig_stderr = $stderr.dup orig_stderr = $stderr.dup
$stdout.reopen captured_stdout $stdout.reopen captured_stdout
$stderr.reopen captured_stderr $stderr.reopen captured_stderr
yield yield
$stdout.rewind $stdout.rewind
$stderr.rewind $stderr.rewind
return captured_stdout.read, captured_stderr.read return captured_stdout.read, captured_stderr.read
ensure ensure
$stdout.reopen orig_stdout $stdout.reopen orig_stdout
$stderr.reopen orig_stderr $stderr.reopen orig_stderr
orig_stdout.close orig_stdout.close
orig_stderr.close orig_stderr.close
captured_stdout.close! captured_stdout.close!
captured_stderr.close! captured_stderr.close!
end
end end
end end

View File

@ -1159,14 +1159,12 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg==
end end
server.ssl_context.tmp_dh_callback = proc { TEST_KEY_DH2048 } server.ssl_context.tmp_dh_callback = proc { TEST_KEY_DH2048 }
t = Thread.new do t = Thread.new do
begin server.start
server.start rescue Exception => ex
rescue Exception => ex puts "ERROR during server thread: #{ex.message}"
puts "ERROR during server thread: #{ex.message}" raise
raise ensure
ensure server.shutdown
server.shutdown
end
end end
while server.status != :Running while server.status != :Running
sleep 0.1 sleep 0.1
@ -1213,13 +1211,11 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg==
end end
end end
th = Thread.new do th = Thread.new do
begin s.start
s.start rescue Exception => ex
rescue Exception => ex abort "ERROR during server thread: #{ex.message}"
abort "ERROR during server thread: #{ex.message}" ensure
ensure s.shutdown
s.shutdown
end
end end
th[:server] = s th[:server] = s
th th

View File

@ -145,25 +145,21 @@ class TestUpdateSuggestion < Gem::TestCase
def test_eglible_for_update_prevent_config def test_eglible_for_update_prevent_config
with_eglible_environment(cmd: @cmd) do with_eglible_environment(cmd: @cmd) do
begin original_config = Gem.configuration[:prevent_update_suggestion]
original_config = Gem.configuration[:prevent_update_suggestion] Gem.configuration[:prevent_update_suggestion] = true
Gem.configuration[:prevent_update_suggestion] = true refute @cmd.eglible_for_update?
refute @cmd.eglible_for_update? ensure
ensure Gem.configuration[:prevent_update_suggestion] = original_config
Gem.configuration[:prevent_update_suggestion] = original_config
end
end end
end end
def test_eglible_for_update_prevent_env def test_eglible_for_update_prevent_env
with_eglible_environment(cmd: @cmd) do with_eglible_environment(cmd: @cmd) do
begin original_env = ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"]
original_env = ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = "yes"
ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = "yes" refute @cmd.eglible_for_update?
refute @cmd.eglible_for_update? ensure
ensure ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = original_env
ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = original_env
end
end end
end end
@ -181,13 +177,11 @@ class TestUpdateSuggestion < Gem::TestCase
def test_eglible_for_update_disabled_update def test_eglible_for_update_disabled_update
with_eglible_environment(cmd: @cmd) do with_eglible_environment(cmd: @cmd) do
begin original_disable = Gem.disable_system_update_message
original_disable = Gem.disable_system_update_message Gem.disable_system_update_message = "disabled"
Gem.disable_system_update_message = "disabled" refute @cmd.eglible_for_update?
refute @cmd.eglible_for_update? ensure
ensure Gem.disable_system_update_message = original_disable
Gem.disable_system_update_message = original_disable
end
end end
end end

View File

@ -91,12 +91,10 @@ class TestGemRequire < Gem::TestCase
def create_sync_thread def create_sync_thread
Thread.new do Thread.new do
begin yield
yield ensure
ensure FILE_ENTERED_LATCH.release
FILE_ENTERED_LATCH.release FILE_EXIT_LATCH.await
FILE_EXIT_LATCH.await
end
end end
end end