Skip to content

Commit 068871f

Browse files
Merge pull request #9227 from tvpartytonight/PUP-11767
(PUP-11767) Add Style Cops
2 parents e8ff4c1 + 22bf7ab commit 068871f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+203
-143
lines changed

.rubocop.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,12 @@ RSpec/FactoryBot/SyntaxMethods: # new in 2.7
199199

200200
RSpec/Rails/AvoidSetupHook: # new in 2.4
201201
Enabled: true
202+
203+
Style/AutoResourceCleanup:
204+
Enabled: true
205+
206+
Style/CaseEquality:
207+
Enabled: true
208+
209+
Style/CaseLikeIf:
210+
Enabled: true

lib/hiera/scope.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ def initialize(real)
2020
end
2121

2222
def [](key)
23-
if key == CALLING_CLASS
23+
case key
24+
when CALLING_CLASS
2425
ans = find_hostclass(@real)
25-
elsif key == CALLING_CLASS_PATH
26+
when CALLING_CLASS_PATH
2627
ans = find_hostclass(@real).gsub(/::/, '/')
27-
elsif key == CALLING_MODULE
28+
when CALLING_MODULE
2829
ans = safe_lookupvar(MODULE_NAME)
2930
else
3031
ans = safe_lookupvar(key)

lib/puppet/etc.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ def override_field_values_to_utf8(struct)
165165

166166
new_struct = struct.is_a?(Etc::Passwd) ? puppet_etc_passwd_class.new : puppet_etc_group_class.new
167167
struct.each_pair do |member, value|
168-
if value.is_a?(String)
168+
case value
169+
when String
169170
new_struct["canonical_#{member}".to_sym] = value.dup
170171
new_struct[member] = Puppet::Util::CharacterEncoding.override_encoding_to_utf_8(value).scrub
171-
elsif value.is_a?(Array)
172+
when Array
172173
new_struct["canonical_#{member}".to_sym] = value.inject([]) { |acc, elem| acc << elem.dup }
173174
new_struct[member] = value.inject([]) do |acc, elem|
174175
acc << Puppet::Util::CharacterEncoding.override_encoding_to_utf_8(elem).scrub

lib/puppet/face/epp.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,10 @@ def get_values(compiler, options)
406406
values_file = options[:values_file]
407407
if values_file
408408
begin
409-
if values_file =~ /\.yaml$/
409+
case values_file
410+
when /\.yaml$/
410411
template_values = Puppet::Util::Yaml.safe_load_file(values_file, [Symbol])
411-
elsif values_file =~ /\.pp$/
412+
when /\.pp$/
412413
evaluating_parser = Puppet::Pops::Parser::EvaluatingParser.new
413414
template_values = evaluating_parser.evaluate_file(compiler.topscope, values_file)
414415
else

lib/puppet/face/module/install.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@
131131
end
132132

133133
when_rendering :console do |return_value, name, _options|
134-
if return_value[:result] == :noop
134+
case return_value[:result]
135+
when :noop
135136
Puppet.notice _("Module %{name} %{version} is already installed.") % { name: name, version: return_value[:version] }
136137
exit 0
137-
elsif return_value[:result] == :failure
138+
when :failure
138139
Puppet.err(return_value[:error][:multiline])
139140
exit 1
140141
else

lib/puppet/face/module/upgrade.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@
7171
end
7272

7373
when_rendering :console do |return_value|
74-
if return_value[:result] == :noop
74+
case return_value[:result]
75+
when :noop
7576
Puppet.notice return_value[:error][:multiline]
7677
exit 0
77-
elsif return_value[:result] == :failure
78+
when :failure
7879
Puppet.err(return_value[:error][:multiline])
7980
exit 1
8081
else

lib/puppet/functions/abs.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ def on_string(x)
5252
# These patterns for conversion are backwards compatible with the stdlib
5353
# version of this function.
5454
#
55-
if x =~ %r{^-?(?:\d+)(?:\.\d+){1}$}
55+
case x
56+
when %r{^-?(?:\d+)(?:\.\d+){1}$}
5657
x.to_f.abs
57-
elsif x =~ %r{^-?\d+$}
58+
when %r{^-?\d+$}
5859
x.to_i.abs
5960
else
6061
raise(ArgumentError, 'abs(): Requires float or integer to work with - was given non decimal string')

lib/puppet/indirector/catalog/compiler.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,12 @@ def require_environment?
116116
# @return [Puppet::Node::Facts] facts object deserialized from supplied string
117117
# @api private
118118
def convert_wire_facts(facts, format)
119-
if format == 'pson'
119+
case format
120+
when 'pson'
120121
# We unescape here because the corresponding code in Puppet::Configurer::FactHandler encodes with Puppet::Util.uri_query_encode
121122
# PSON is deprecated, but continue to accept from older agents
122123
return Puppet::Node::Facts.convert_from('pson', CGI.unescape(facts))
123-
elsif format == 'application/json'
124+
when 'application/json'
124125
return Puppet::Node::Facts.convert_from('json', CGI.unescape(facts))
125126
else
126127
raise ArgumentError, _("Unsupported facts format")

lib/puppet/network/formats.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,12 @@ def render_multiple(data)
193193
:weight => 0) do
194194
def flatten_hash(hash)
195195
hash.each_with_object({}) do |(k, v), h|
196-
if v.is_a? Hash
196+
case v
197+
when Hash
197198
flatten_hash(v).map do |h_k, h_v|
198199
h["#{k}.#{h_k}"] = h_v
199200
end
200-
elsif v.is_a? Array
201+
when Array
201202
v.each_with_index do |el, i|
202203
if el.is_a? Hash
203204
flatten_hash(el).map do |el_k, el_v|
@@ -240,10 +241,11 @@ def render(datum)
240241
return datum if datum.is_a?(String) || datum.is_a?(Numeric)
241242

242243
# Simple hash
243-
if datum.is_a?(Hash)
244+
case datum
245+
when Hash
244246
data = flatten_hash(datum)
245247
return construct_output(data)
246-
elsif datum.is_a?(Array)
248+
when Array
247249
data = flatten_array(datum)
248250
return construct_output(data)
249251
end

lib/puppet/node/facts.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,14 @@ def quote_special_strings(fact_hash)
138138
end
139139

140140
def sanitize_fact(fact)
141-
if fact.is_a? Hash then
141+
case fact
142+
when Hash
142143
ret = {}
143144
fact.each_pair { |k, v| ret[sanitize_fact k] = sanitize_fact v }
144145
ret
145-
elsif fact.is_a? Array then
146+
when Array
146147
fact.collect { |i| sanitize_fact i }
147-
elsif fact.is_a? Numeric \
148-
or fact.is_a? TrueClass \
149-
or fact.is_a? FalseClass \
150-
or fact.is_a? String
148+
when Numeric, TrueClass, FalseClass, String
151149
fact
152150
else
153151
result = fact.to_s

0 commit comments

Comments
 (0)