From 07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b Mon Sep 17 00:00:00 2001 From: Markus Roberts Date: Fri, 9 Jul 2010 18:06:37 -0700 Subject: Code smell: Avoid unneeded blocks Replaced 45 occurances of (DEF) begin (LINES) rescue(.*) (LINES) end end with 3 Examples: The code: def find(name) begin self.const_get(name.to_s.capitalize) rescue puts "Unable to find application '#{name.to_s}'." Kernel::exit(1) end end becomes: def find(name) self.const_get(name.to_s.capitalize) rescue puts "Unable to find application '#{name.to_s}'." Kernel::exit(1) end The code: def exit_on_fail(message, code = 1) begin yield rescue RuntimeError, NotImplementedError => detail puts detail.backtrace if Puppet[:trace] $stderr.puts "Could not #{message}: #{detail}" exit(code) end end becomes: def exit_on_fail(message, code = 1) yield rescue RuntimeError, NotImplementedError => detail puts detail.backtrace if Puppet[:trace] $stderr.puts "Could not #{message}: #{detail}" exit(code) end The code: def start begin case ssl when :tls @connection = LDAP::SSLConn.new(host, port, true) when true @connection = LDAP::SSLConn.new(host, port) else @connection = LDAP::Conn.new(host, port) end @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) @connection.simple_bind(user, password) rescue => detail raise Puppet::Error, "Could not connect to LDAP: #{detail}" end end becomes: def start case ssl when :tls @connection = LDAP::SSLConn.new(host, port, true) when true @connection = LDAP::SSLConn.new(host, port) else @connection = LDAP::Conn.new(host, port) end @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) @connection.simple_bind(user, password) rescue => detail raise Puppet::Error, "Could not connect to LDAP: #{detail}" end --- test/ral/providers/group.rb | 4 +--- test/ral/providers/user.rb | 4 +--- test/ral/type/filesources.rb | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) (limited to 'test') diff --git a/test/ral/providers/group.rb b/test/ral/providers/group.rb index 48120f332..ceba65ac4 100755 --- a/test/ral/providers/group.rb +++ b/test/ral/providers/group.rb @@ -71,12 +71,10 @@ class TestGroupProvider < Test::Unit::TestCase end else def missing?(group) - begin obj = Etc.getgrnam(group) return false - rescue ArgumentError + rescue ArgumentError return true - end end def gid(name) diff --git a/test/ral/providers/user.rb b/test/ral/providers/user.rb index 033632894..7769e3a26 100755 --- a/test/ral/providers/user.rb +++ b/test/ral/providers/user.rb @@ -65,12 +65,10 @@ class TestUserProvider < Test::Unit::TestCase end else def missing?(user) - begin obj = Etc.getpwnam(user) return false - rescue ArgumentError + rescue ArgumentError return true - end end def current?(param, user) diff --git a/test/ral/type/filesources.rb b/test/ral/type/filesources.rb index d3eb537c1..2b43424ac 100755 --- a/test/ral/type/filesources.rb +++ b/test/ral/type/filesources.rb @@ -30,11 +30,9 @@ class TestFileSources < Test::Unit::TestCase end def use_storage - begin initstorage - rescue + rescue system("rm -rf #{Puppet[:statefile]}") - end end def initstorage -- cgit