Skip to content

Commit f9bcd09

Browse files
authored
Merge pull request #9233 from tvpartytonight/PUP-11767
(PUP-11767) Enable more rubocop styles
2 parents e05d91c + 121ed91 commit f9bcd09

Some content is hidden

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

49 files changed

+127
-115
lines changed

.rubocop.yml

+39
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,42 @@ Style/ColonMethodDefinition:
232232

233233
Style/DefWithParentheses:
234234
Enabled: true
235+
236+
Style/Dir:
237+
Enabled: true
238+
239+
Style/DocumentDynamicEvalDefinition:
240+
Enabled: true
241+
242+
Style/DoubleCopDisableDirective:
243+
Enabled: true
244+
245+
Style/EachForSimpleLoop:
246+
Enabled: true
247+
248+
Style/EachWithObject:
249+
Enabled: true
250+
251+
Style/EmptyBlockParameter:
252+
Enabled: true
253+
254+
Style/EmptyCaseCondition:
255+
Enabled: true
256+
257+
Style/EmptyLambdaParameter:
258+
Enabled: true
259+
260+
Style/EmptyLiteral:
261+
Enabled: true
262+
263+
Style/EvalWithLocation:
264+
Enabled: true
265+
266+
Style/EvenOdd:
267+
Enabled: true
268+
269+
Style/ExpandPathArguments:
270+
Enabled: true
271+
272+
Style/FetchEnvVar:
273+
Enabled: true

ext/windows/service/daemon.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WindowsDaemon < Puppet::Util::Windows::Daemon
1818
@run_thread = nil
1919
@LOG_TO_FILE = false
2020
@loglevel = 0
21-
LOG_FILE = File.expand_path(File.join(ENV['ALLUSERSPROFILE'], 'PuppetLabs', 'puppet', 'var', 'log', 'windows.log'))
21+
LOG_FILE = File.expand_path(File.join(ENV.fetch('ALLUSERSPROFILE', nil), 'PuppetLabs', 'puppet', 'var', 'log', 'windows.log'))
2222
LEVELS = [:debug, :info, :notice, :warning, :err, :alert, :emerg, :crit]
2323
LEVELS.each do |level|
2424
define_method("log_#{level}") do |msg|
@@ -203,10 +203,10 @@ def load_env(base_dir)
203203
ENV['Path'] = [
204204
File.join(base_dir, 'puppet', 'bin'),
205205
File.join(base_dir, 'bin'),
206-
].join(';').tr('/', '\\') + ';' + ENV['Path']
206+
].join(';').tr('/', '\\') + ';' + ENV.fetch('Path', nil)
207207

208208
# ENV that uses forward slashes
209-
ENV['RUBYLIB'] = "#{File.join(base_dir, 'puppet', 'lib')};#{ENV['RUBYLIB']}"
209+
ENV['RUBYLIB'] = "#{File.join(base_dir, 'puppet', 'lib')};#{ENV.fetch('RUBYLIB', nil)}"
210210
rescue => e
211211
log_exception(e)
212212
end

lib/puppet/application/resource.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def handle_editing(text)
188188
tmpfile.puts text
189189

190190
# edit the content
191-
system(ENV["EDITOR"] || 'vi', tmpfile.path)
191+
system(ENV.fetch("EDITOR", nil) || 'vi', tmpfile.path)
192192

193193
# ...and, now, pass that file to puppet to apply. Because
194194
# many editors rename or replace the original file we need to

lib/puppet/confine/exists.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ def message(value)
1616
end
1717

1818
def summary
19-
result.zip(values).inject([]) { |array, args| val, f = args; array << f unless val; array }
19+
result.zip(values).each_with_object([]) { |args, array| val, f = args; array << f unless val; }
2020
end
2121
end

lib/puppet/confine/variable.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Puppet::Confine::Variable < Puppet::Confine
1212
# Only returns failed values, not all required values.
1313
def self.summarize(confines)
1414
result = Hash.new { |hash, key| hash[key] = [] }
15-
confines.inject(result) { |total, confine| total[confine.name] += confine.values unless confine.valid?; total }
15+
confines.each_with_object(result) { |confine, total| total[confine.name] += confine.values unless confine.valid?; }
1616
end
1717

1818
# This is set by ConfineCollection.

lib/puppet/defaults.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def self.default_cadir
4949
def self.default_basemodulepath
5050
if Puppet::Util::Platform.windows?
5151
path = ['$codedir/modules']
52-
installdir = ENV["FACTER_env_windows_installdir"]
52+
installdir = ENV.fetch("FACTER_env_windows_installdir", nil)
5353
if installdir
5454
path << "#{installdir}/puppet/modules"
5555
end
@@ -61,7 +61,7 @@ def self.default_basemodulepath
6161

6262
def self.default_vendormoduledir
6363
if Puppet::Util::Platform.windows?
64-
installdir = ENV["FACTER_env_windows_installdir"]
64+
installdir = ENV.fetch("FACTER_env_windows_installdir", nil)
6565
if installdir
6666
"#{installdir}\\puppet\\vendor_modules"
6767
else
@@ -373,7 +373,7 @@ def self.initialize_default_settings!(settings)
373373
Puppet::Util::Platform.default_paths.each do |path|
374374
next if paths.include?(path)
375375

376-
ENV['PATH'] = ENV['PATH'] + File::PATH_SEPARATOR + path
376+
ENV['PATH'] = ENV.fetch('PATH', nil) + File::PATH_SEPARATOR + path
377377
end
378378
value
379379
end

lib/puppet/face/epp.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,13 @@
180180
raise Puppet::Error, _("No input to parse given on command line or stdin")
181181
end
182182
else
183-
templates, missing_files = args.reduce([[], []]) do |memo, file|
183+
templates, missing_files = args.each_with_object([[], []]) do |file, memo|
184184
template_file = effective_template(file, compiler.environment)
185185
if template_file.nil?
186186
memo[1] << file
187187
else
188188
memo[0] << template_file
189189
end
190-
memo
191190
end
192191

193192
show_filename = templates.count > 1

lib/puppet/face/module/list.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ def unmet_dependencies(environment)
119119
# Prepare the unmet dependencies for display on the console.
120120
environment.modules.sort_by { |mod| mod.name }.each do |mod|
121121
unmet_grouped = Hash.new { |h, k| h[k] = [] }
122-
unmet_grouped = mod.unmet_dependencies.inject(unmet_grouped) do |acc, dep|
122+
unmet_grouped = mod.unmet_dependencies.each_with_object(unmet_grouped) do |dep, acc|
123123
acc[dep[:reason]] << dep
124-
acc
125124
end
126125
unmet_grouped.each do |type, deps|
127126
unless deps.empty?

lib/puppet/file_system/uniquefile.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def try_convert_to_hash(h)
153153

154154
def tmpdir
155155
tmp = '.'
156-
for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp']
156+
for dir in [ENV.fetch('TMPDIR', nil), ENV.fetch('TMP', nil), ENV.fetch('TEMP', nil), @@systmpdir, '/tmp']
157157
stat = File.stat(dir) if dir
158158
if stat && stat.directory? && stat.writable?
159159
tmp = dir

lib/puppet/http/dns.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,9 @@ def expired?(service_name)
146146
# @yields [[Resolv::DNS::Resource::IN::SRV]] a group of records of
147147
# the same priority
148148
def each_priority(records)
149-
pri_hash = records.inject({}) do |groups, element|
149+
pri_hash = records.each_with_object({}) do |element, groups|
150150
groups[element.priority] ||= []
151151
groups[element.priority] << element
152-
groups
153152
end
154153

155154
pri_hash.keys.sort.each do |key|

lib/puppet/http/proxy.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def self.proxy(uri)
1818

1919
def self.http_proxy_env
2020
# Returns a URI object if proxy is set, or nil
21-
proxy_env = ENV["http_proxy"] || ENV["HTTP_PROXY"]
21+
proxy_env = ENV.fetch("http_proxy", nil) || ENV.fetch("HTTP_PROXY", nil)
2222
begin
2323
return URI.parse(proxy_env) if proxy_env
2424
rescue URI::InvalidURIError
@@ -124,7 +124,7 @@ def self.http_proxy_password
124124
end
125125

126126
def self.no_proxy
127-
no_proxy_env = ENV["no_proxy"] || ENV["NO_PROXY"]
127+
no_proxy_env = ENV.fetch("no_proxy", nil) || ENV.fetch("NO_PROXY", nil)
128128

129129
if no_proxy_env
130130
return no_proxy_env

lib/puppet/indirector/node/exec.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def create_node(name, result, facts = nil)
5454

5555
# Translate the yaml string into Ruby objects.
5656
def translate(name, output)
57-
Puppet::Util::Yaml.safe_load(output, [Symbol]).inject({}) do |hash, data|
57+
Puppet::Util::Yaml.safe_load(output, [Symbol]).each_with_object({}) do |data, hash|
5858
case data[0]
5959
when String
6060
hash[data[0].intern] = data[1]
@@ -63,8 +63,6 @@ def translate(name, output)
6363
else
6464
raise Puppet::Error, _("key is a %{klass}, not a string or symbol") % { klass: data[0].class }
6565
end
66-
67-
hash
6866
end
6967
rescue => detail
7068
raise Puppet::Error, _("Could not load external node results for %{name}: %{detail}") % { name: name, detail: detail }, detail.backtrace

lib/puppet/indirector/request.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def initialize(indirection_name, method, key, instance, options = {})
6868
self.indirection_name = indirection_name
6969
self.method = method
7070

71-
options = options.inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash }
71+
options = options.each_with_object({}) { |ary, hash| hash[ary[0].to_sym] = ary[1]; }
7272

7373
set_attributes(options)
7474

lib/puppet/interface/documentation.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ def attr_doc(name, &validate)
3434
# without as methods. When we are 1.9 only (hah!) you can totally
3535
# replace this with some up-and-up define_method. --daniel 2011-04-29
3636
module_eval(<<-EOT, __FILE__, __LINE__ + 1)
37-
def #{name}(value = nil)
38-
self.#{name} = value unless value.nil?
39-
@#{name}
40-
end
41-
42-
def #{name}=(value)
43-
@#{name} = Puppet::Interface::DocGen.strip_whitespace(#{get_arg})
44-
end
37+
def #{name}(value = nil) # def attribute(value=nil)
38+
self.#{name} = value unless value.nil? # self.attribute = value unless value.nil?
39+
@#{name} # @value
40+
end # end
41+
42+
def #{name}=(value) # def attribute=(value)
43+
@#{name} = Puppet::Interface::DocGen.strip_whitespace(#{get_arg}) # @value = Puppet::Interface::DocGen.strip_whitespace(#{get_arg})
44+
end # end
4545
EOT
4646
end
4747
end

lib/puppet/network/http/handler.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,9 @@ def client_cert(request)
171171
end
172172

173173
def decode_params(params)
174-
params.select { |key, _| allowed_parameter?(key) }.inject({}) do |result, ary|
174+
params.select { |key, _| allowed_parameter?(key) }.each_with_object({}) do |ary, result|
175175
param, value = ary
176176
result[param.to_sym] = parse_parameter_value(param, value)
177-
result
178177
end
179178
end
180179

lib/puppet/node/environment.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def hash
554554
# not private so it can be called in tests
555555
def self.extralibs
556556
if ENV['PUPPETLIB']
557-
split_path(ENV['PUPPETLIB'])
557+
split_path(ENV.fetch('PUPPETLIB', nil))
558558
else
559559
[]
560560
end

lib/puppet/parser/resource.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def set_parameter(param, value = nil)
216216
alias []= set_parameter
217217

218218
def to_hash
219-
parse_title.merge(@parameters.reduce({}) do |result, (_, param)|
219+
parse_title.merge(@parameters.each_with_object({}) do |(_, param), result|
220220
value = param.value
221221
value = (:undef == value) ? nil : value
222222

@@ -231,7 +231,6 @@ def to_hash
231231
result[param.name] = value
232232
end
233233
end
234-
result
235234
end)
236235
end
237236

lib/puppet/pops/evaluator/collectors/abstract_collector.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def evaluate
6363

6464
return false if objects.empty?
6565

66-
objects.reduce(@collected) { |c, o| c[o.ref] = o; c }
66+
objects.each_with_object(@collected) { |o, c| c[o.ref] = o; }
6767

6868
objects
6969
end

lib/puppet/pops/evaluator/collectors/fixed_set_collector.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ def initialize(scope, resources)
1616
# by the realize function
1717
def collect
1818
resolved = []
19-
result = @resources.reduce([]) do |memo, ref|
19+
result = @resources.each_with_object([]) do |ref, memo|
2020
res = @scope.findresource(ref.to_s)
2121
if res
2222
res.virtual = false
2323
memo << res
2424
resolved << ref
2525
end
26-
memo
2726
end
2827

2928
@resources = @resources - resolved

lib/puppet/pops/evaluator/evaluator_impl.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def eval_LiteralList o, scope
667667
#
668668
def eval_LiteralHash o, scope
669669
# optimized
670-
o.entries.reduce({}) { |h, entry| h[evaluate(entry.key, scope)] = evaluate(entry.value, scope); h }
670+
o.entries.each_with_object({}) { |entry, h| h[evaluate(entry.key, scope)] = evaluate(entry.value, scope); }
671671
end
672672

673673
# Evaluates all statements and produces the last evaluated value
@@ -861,7 +861,7 @@ def eval_ResourceExpression(o, scope)
861861

862862
# Store evaluated parameters in a hash associated with the body, but do not yet create resource
863863
# since the entry containing :defaults may appear later
864-
body_to_params[body] = body.operations.reduce({}) do |param_memo, op|
864+
body_to_params[body] = body.operations.each_with_object({}) do |op, param_memo|
865865
params = evaluate(op, scope)
866866
params = [params] unless params.is_a?(Array)
867867
params.each do |p|
@@ -871,7 +871,6 @@ def eval_ResourceExpression(o, scope)
871871

872872
param_memo[p.name] = p
873873
end
874-
param_memo
875874
end
876875
end
877876

lib/puppet/pops/evaluator/json_strict_literal_evaluator.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,10 @@ def literal_LiteralList(o)
7474
end
7575

7676
def literal_LiteralHash(o)
77-
o.entries.reduce({}) do |result, entry|
77+
o.entries.each_with_object({}) do |entry, result|
7878
key = literal(entry.key)
7979
throw :not_literal unless key.is_a?(String)
8080
result[key] = literal(entry.value)
81-
result
8281
end
8382
end
8483
end

lib/puppet/pops/evaluator/literal_evaluator.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ def literal_LiteralList(o)
8787
end
8888

8989
def literal_LiteralHash(o)
90-
o.entries.reduce({}) do |result, entry|
90+
o.entries.each_with_object({}) do |entry, result|
9191
result[literal(entry.key)] = literal(entry.value)
92-
result
9392
end
9493
end
9594
end

lib/puppet/pops/loader/dependency_loader.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ def loaded_entry_in_dependency(typed_name, check_dependencies)
9090

9191
# An index of module_name to module loader used to speed up lookup of qualified names
9292
def index
93-
@index ||= @dependency_loaders.reduce({}) { |index, loader| index[loader.module_name] = loader; index }
93+
@index ||= @dependency_loaders.each_with_object({}) { |loader, index| index[loader.module_name] = loader; }
9494
end
9595
end

lib/puppet/pops/model/factory.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ def initialize(args, name_expr)
10071007
# expression, or expression list.
10081008
#
10091009
def self.transform_calls(expressions)
1010-
expressions.reduce([]) do |memo, expr|
1010+
expressions.each_with_object([]) do |expr, memo|
10111011
name = memo[-1]
10121012
if name.instance_of?(Factory) && name.model_class <= QualifiedName && name_is_statement?(name[KEY_VALUE])
10131013
if expr.is_a?(Array)
@@ -1035,7 +1035,6 @@ def self.transform_calls(expressions)
10351035
expr['rval_required'] = false
10361036
end
10371037
end
1038-
memo
10391038
end
10401039
end
10411040

lib/puppet/pops/parser/locator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def initialize(locator, str, leading_line_count, leading_offset, has_margin, mar
240240
# The last entry is duplicated since there will be the line "after last line" that would otherwise require
241241
# conditional logic.
242242
#
243-
@accumulated_margin = margin_per_line.reduce([0]) { |memo, val| memo << memo[-1] + val; memo }
243+
@accumulated_margin = margin_per_line.each_with_object([0]) { |val, memo| memo << memo[-1] + val; }
244244
@accumulated_margin << @accumulated_margin[-1]
245245
end
246246

0 commit comments

Comments
 (0)