Skip to content

Commit 17358f5

Browse files
committed
(PUP-11993) Style/RescueModifier
This commit enables the Style/RescueModifier cop and fixes 143 autocorrectable offenses.
1 parent 4e573c1 commit 17358f5

File tree

14 files changed

+122
-43
lines changed

14 files changed

+122
-43
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -679,23 +679,6 @@ Style/RedundantSelfAssignment:
679679
Exclude:
680680
- 'lib/puppet/context.rb'
681681

682-
# This cop supports safe auto-correction (--auto-correct).
683-
Style/RescueModifier:
684-
Exclude:
685-
- 'lib/puppet/file_serving/metadata.rb'
686-
- 'lib/puppet/file_system/uniquefile.rb'
687-
- 'lib/puppet/file_system/windows.rb'
688-
- 'lib/puppet/module_tool/applications/application.rb'
689-
- 'lib/puppet/module_tool/applications/installer.rb'
690-
- 'lib/puppet/module_tool/applications/upgrader.rb'
691-
- 'lib/puppet/module_tool/metadata.rb'
692-
- 'lib/puppet/module_tool/shared_behaviors.rb'
693-
- 'lib/puppet/provider/file/posix.rb'
694-
- 'lib/puppet/util.rb'
695-
- 'lib/puppet/util/execution.rb'
696-
- 'lib/puppet/util/reference.rb'
697-
- 'lib/puppet/util/windows/adsi.rb'
698-
699682
# This cop supports safe auto-correction (--auto-correct).
700683
# Configuration parameters: EnforcedStyle.
701684
# SupportedStyles: implicit, explicit

lib/puppet/file_serving/metadata.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ def collect(source_permissions = nil)
117117
@checksum = "{#{@checksum_type}}" + send("#{@checksum_type}_file", path).to_s
118118
when "link"
119119
@destination = Puppet::FileSystem.readlink(real_path)
120-
@checksum = "{#{@checksum_type}}" + send("#{@checksum_type}_file", real_path).to_s rescue nil
120+
@checksum = begin
121+
"{#{@checksum_type}}" + send("#{@checksum_type}_file", real_path).to_s
122+
rescue
123+
nil
124+
end
121125
when "fifo", "socket"
122126
@checksum_type = "none"
123127
@checksum = "{#{@checksum_type}}" + send("#{@checksum_type}_file", real_path).to_s

lib/puppet/file_system/uniquefile.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,14 @@ def tmpdir
151151
tmp = '.'
152152
[ENV.fetch('TMPDIR', nil), ENV.fetch('TMP', nil), ENV.fetch('TEMP', nil), @@systmpdir, '/tmp'].each do |dir|
153153
stat = File.stat(dir) if dir
154-
if stat && stat.directory? && stat.writable?
155-
tmp = dir
156-
break
157-
end rescue nil
154+
begin
155+
if stat && stat.directory? && stat.writable?
156+
tmp = dir
157+
break
158+
end
159+
rescue
160+
nil
161+
end
158162
end
159163
File.expand_path(tmp)
160164
end

lib/puppet/file_system/windows.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,19 @@ def unlink(*file_names)
7878

7979
file_names.each do |file_name|
8080
file_name = file_name.to_s # handle PathName
81-
stat = Puppet::Util::Windows::File.stat(file_name) rescue nil
81+
stat = begin
82+
Puppet::Util::Windows::File.stat(file_name)
83+
rescue
84+
nil
85+
end
8286

8387
# sigh, Ruby + Windows :(
8488
if !stat
85-
::File.unlink(file_name) rescue Dir.rmdir(file_name)
89+
begin
90+
::File.unlink(file_name)
91+
rescue
92+
Dir.rmdir(file_name)
93+
end
8694
elsif stat.ftype == 'directory'
8795
if Puppet::Util::Windows::File.symlink?(file_name)
8896
Dir.rmdir(file_name)

lib/puppet/module_tool/applications/application.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ def discuss(response, success, failure)
2828
when Net::HTTPOK, Net::HTTPCreated
2929
Puppet.notice success
3030
else
31-
errors = Puppet::Util::Json.load(response.body)['error'] rescue "HTTP #{response.code}, #{response.body}"
31+
errors = begin
32+
Puppet::Util::Json.load(response.body)['error']
33+
rescue
34+
"HTTP #{response.code}, #{response.body}"
35+
end
3236
Puppet.warning "#{failure} (#{errors})"
3337
end
3438
end

lib/puppet/module_tool/applications/installer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ def run
7272
results[:version] = installed_module.version
7373
return results
7474
else
75-
changes = Checksummer.run(installed_modules[name].mod.path) rescue []
75+
changes = begin
76+
Checksummer.run(installed_modules[name].mod.path)
77+
rescue
78+
[]
79+
end
7680
raise AlreadyInstalledError,
7781
:module_name => name,
7882
:installed_version => installed_modules[name].version,

lib/puppet/module_tool/applications/upgrader.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ def installed_release.priority
7676
vstring = mod.version ? "v#{mod.version}" : '???'
7777
Puppet.notice _("Found '%{name}' (%{version}) in %{dir} ...") % { name: name, version: colorize(:cyan, vstring), dir: dir }
7878
unless @ignore_changes
79-
changes = Checksummer.run(mod.path) rescue []
79+
changes = begin
80+
Checksummer.run(mod.path)
81+
rescue
82+
[]
83+
end
8084
if mod.has_metadata? && !changes.empty?
8185
raise LocalChangesError,
8286
:action => :upgrade,

lib/puppet/module_tool/metadata.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ def to_json
102102
data = @data.dup.merge('dependencies' => dependencies)
103103

104104
contents = data.keys.map do |k|
105-
value = (Puppet::Util::Json.dump(data[k], :pretty => true) rescue data[k].to_json)
105+
value = begin
106+
Puppet::Util::Json.dump(data[k], :pretty => true)
107+
rescue
108+
data[k].to_json
109+
end
106110
%Q("#{k}": #{value})
107111
end
108112

lib/puppet/module_tool/shared_behaviors.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ def get_remote_constraints(forge)
3838
mod_name, releases = pair
3939
mod_name = mod_name.tr('/', '-')
4040
releases.each do |rel|
41-
semver = SemanticPuppet::Version.parse(rel['version']) rescue SemanticPuppet::Version::MIN
41+
semver = begin
42+
SemanticPuppet::Version.parse(rel['version'])
43+
rescue
44+
SemanticPuppet::Version::MIN
45+
end
4246
@versions[mod_name] << { :vstring => rel['version'], :semver => semver }
4347
@versions[mod_name].sort_by! { |a| a[:semver] }
4448
@urls["#{mod_name}@#{rel['version']}"] = rel['file']
@@ -79,10 +83,16 @@ def resolve_constraints(dependencies, source = [{ :name => :you }], seen = {}, a
7983
}
8084

8185
if forced?
82-
range = Puppet::Module.parse_range(@version) rescue Puppet::Module.parse_range('>= 0.0.0')
86+
range = begin
87+
Puppet::Module.parse_range(@version)
88+
rescue
89+
Puppet::Module.parse_range('>= 0.0.0')
90+
end
8391
else
8492
range = (@conditions[mod]).map do |r|
85-
Puppet::Module.parse_range(r[:dependency]) rescue Puppet::Module.parse_range('>= 0.0.0')
93+
Puppet::Module.parse_range(r[:dependency])
94+
rescue
95+
Puppet::Module.parse_range('>= 0.0.0')
8696
end.inject(&:&)
8797
end
8898

lib/puppet/provider/file/posix.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def uid2name(id)
3535

3636
# Determine if the user is valid, and if so, return the UID
3737
def name2uid(value)
38-
Integer(value) rescue uid(value) || false
38+
Integer(value)
39+
rescue
40+
uid(value) || false
3941
end
4042

4143
def gid2name(id)
@@ -56,7 +58,9 @@ def gid2name(id)
5658
end
5759

5860
def name2gid(value)
59-
Integer(value) rescue gid(value) || false
61+
Integer(value)
62+
rescue
63+
gid(value) || false
6064
end
6165

6266
def owner

0 commit comments

Comments
 (0)