Skip to content

Commit 2e4c1df

Browse files
committed
lint
1 parent b50b6ca commit 2e4c1df

9 files changed

Lines changed: 275 additions & 216 deletions

File tree

.rubocop_todo.yml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2023-08-17 21:26:09 UTC using RuboCop version 1.50.2.
3+
# on 2024-08-06 17:00:26 UTC using RuboCop version 1.50.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -12,6 +12,44 @@ Lint/NonAtomicFileOperation:
1212
Exclude:
1313
- 'lib/puppet/provider/archive/ruby.rb'
1414

15+
# Offense count: 1
16+
Naming/AccessorMethodName:
17+
Exclude:
18+
- 'lib/puppet/type/archive.rb'
19+
20+
# Offense count: 1
21+
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
22+
# AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to
23+
Naming/MethodParameterName:
24+
Exclude:
25+
- 'spec/lib/puppet_spec/files.rb'
26+
27+
# Offense count: 9
28+
# Configuration parameters: AssignmentOnly.
29+
RSpec/InstanceVariable:
30+
Exclude:
31+
- 'spec/unit/puppet/provider/archive/ruby_spec.rb'
32+
- 'spec/unit/puppet/type/archive_spec.rb'
33+
34+
# Offense count: 1
35+
# Configuration parameters: EnforcedStyle.
36+
# SupportedStyles: have_received, receive
37+
RSpec/MessageSpies:
38+
Exclude:
39+
- 'spec/lib/puppet_spec/compiler.rb'
40+
41+
# Offense count: 4
42+
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
43+
RSpec/VerifiedDoubles:
44+
Exclude:
45+
- 'spec/unit/puppet/type/archive_spec.rb'
46+
47+
# Offense count: 4
48+
# Configuration parameters: AllowedVariables.
49+
Style/GlobalVars:
50+
Exclude:
51+
- 'spec/lib/puppet_spec/files.rb'
52+
1553
# Offense count: 1
1654
# This cop supports unsafe autocorrection (--autocorrect-all).
1755
# Configuration parameters: AllowComments.

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ group :release do
2323
gem 'voxpupuli-release', '~> 3.0', :require => false
2424
end
2525

26-
gem 'pry'
2726
gem 'rake', :require => false
2827
gem 'facter', ENV['FACTER_GEM_VERSION'], :require => false, :groups => [:test]
2928

lib/puppet/provider/archive/ruby.rb

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -256,33 +256,32 @@ def checkexe(command)
256256
if Facter.value(:osfamily) == 'windows'
257257
if absolute_path?(exe)
258258
if !Puppet::FileSystem.exist?(exe)
259-
raise ArgumentError, _("Could not find command '%{exe}'") % { exe: exe }
259+
raise ArgumentError, format(_("Could not find command '%{exe}'"), exe: exe)
260260
elsif !File.file?(exe)
261-
raise ArgumentError, _("'%{exe}' is a %{klass}, not a file") % { exe: exe, klass: File.ftype(exe) }
261+
raise ArgumentError, format(_("'%{exe}' is a %{klass}, not a file"), exe: exe, klass: File.ftype(exe))
262262
end
263263
end
264-
else
265-
if File.expand_path(exe) == exe
266-
if !Puppet::FileSystem.exist?(exe)
267-
raise ArgumentError, _("Could not find command '%{exe}'") % { exe: exe }
268-
elsif !File.file?(exe)
269-
raise ArgumentError, _("'%{exe}' is a %{klass}, not a file") % { exe: exe, klass: File.ftype(exe) }
270-
elsif !File.executable?(exe)
271-
raise ArgumentError, _("'%{exe}' is not executable") % { exe: exe }
272-
end
264+
elsif File.expand_path(exe) == exe
265+
if !Puppet::FileSystem.exist?(exe)
266+
raise ArgumentError, format(_("Could not find command '%{exe}'"), exe: exe)
267+
elsif !File.file?(exe)
268+
raise ArgumentError, format(_("'%{exe}' is a %{klass}, not a file"), exe: exe, klass: File.ftype(exe))
269+
elsif !File.executable?(exe)
270+
raise ArgumentError, format(_("'%{exe}' is not executable"), exe: exe)
273271
end
274272
end
275273

276274
if resource[:env_path]
277-
Puppet::Util.withenv :PATH => resource[:env_path].join(File::PATH_SEPARATOR) do
275+
Puppet::Util.withenv PATH: resource[:env_path].join(File::PATH_SEPARATOR) do
278276
return if which(exe)
279277
end
280278
end
281279

282280
# 'which' will only return the command if it's executable, so we can't
283281
# distinguish not found from not executable
284-
raise ArgumentError, _("Could not find command '%{exe}'") % { exe: exe }
282+
raise ArgumentError, format(_("Could not find command '%{exe}'"), exe: exe)
285283
end
284+
286285
def environment
287286
env = {}
288287

@@ -294,19 +293,17 @@ def environment
294293

295294
envlist = [envlist] unless envlist.is_a? Array
296295
envlist.each do |setting|
297-
unless (match = /^(\w+)=((.|\n)*)$/.match(setting))
298-
warning _("Cannot understand environment setting %{setting}") % { setting: setting.inspect }
296+
unless (match = %r{^(\w+)=((.|\n)*)$}.match(setting))
297+
warning format(_('Cannot understand environment setting %{setting}'), setting: setting.inspect)
299298
next
300299
end
301300
var = match[1]
302301
value = match[2]
303302

304-
if env.include?(var) || env.include?(var.to_sym)
305-
warning _("Overriding environment setting '%{var}' with '%{value}'") % { var: var, value: value }
306-
end
303+
warning format(_("Overriding environment setting '%{var}' with '%{value}'"), var: var, value: value) if env.include?(var) || env.include?(var.to_sym)
307304

308305
if value.nil? || value.empty?
309-
msg = _("Empty environment setting '%{var}'") % { var: var }
306+
msg = format(_("Empty environment setting '%{var}'"), var: var)
310307
Puppet.warn_once('undefined_variables', "empty_env_var_#{var}", msg, resource.file, resource.line)
311308
end
312309

@@ -317,10 +314,9 @@ def environment
317314
end
318315

319316
def run(command, check = false)
320-
output = nil
321317
checkexe(command)
322318

323-
debug "Executing#{check ? " check": ""} #{command}"
319+
debug "Executing#{check ? ' check' : ''} #{command}"
324320

325321
cwd = resource[:extract] ? resource[:extract_path] : File.dirname(resource[:path])
326322
# It's ok if cwd is nil. In that case Puppet::Util::Execution.execute() simply will not attempt to
@@ -329,7 +325,7 @@ def run(command, check = false)
329325
# the working directory can fail under some circumstances, so avoiding the directory change attempt
330326
# is preferable to defaulting cwd to that directory.
331327

332-
# note that we are passing "false" for the "override_locale" parameter, which ensures that the user's
328+
# NOTE: that we are passing "false" for the "override_locale" parameter, which ensures that the user's
333329
# default/system locale will be respected. Callers may override this behavior by setting locale-related
334330
# environment variables (LANG, LC_ALL, etc.) in their 'environment' configuration.
335331
output = Puppet::Util::Execution.execute(
@@ -344,9 +340,8 @@ def run(command, check = false)
344340
sensitive: false
345341
)
346342
# The shell returns 127 if the command is missing.
347-
if output.exitstatus == 127
348-
raise ArgumentError, output
349-
end
343+
raise ArgumentError, output if output.exitstatus == 127
344+
350345
# Return output twice as processstatus was returned before, but only exitstatus was ever called.
351346
# Output has the exitstatus on it so it is returned instead. This is here twice as changing this
352347
# would result in a change to the underlying API.
@@ -357,19 +352,19 @@ def extractexe(command)
357352
if command.is_a? Array
358353
command.first
359354
else
360-
match = /^"([^"]+)"|^'([^']+)'/.match(command)
355+
match = %r{^"([^"]+)"|^'([^']+)'}.match(command)
361356
if match
362357
# extract whichever of the two sides matched the content.
363358
match[1] or match[2]
364359
else
365-
command.split(/ /)[0]
360+
command.split(%r{ })[0]
366361
end
367362
end
368363
end
369364

370365
def validatecmd(command)
371366
exe = extractexe(command)
372367
# if we're not fully qualified, require a path
373-
self.fail _("'%{exe}' is not qualified and no path was specified. Please qualify the command or specify a path.") % { exe: exe } if !absolute_path?(exe) and resource[:path].nil?
368+
self.fail format(_("'%{exe}' is not qualified and no path was specified. Please qualify the command or specify a path."), exe: exe) if !absolute_path?(exe) && resource[:path].nil?
374369
end
375370
end

lib/puppet/type/archive.rb

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
require 'uri'
55
require 'puppet/util'
66
require 'puppet/parameter/boolean'
7-
require 'pry'
87

98
Puppet::Type.newtype(:archive) do
109
@doc = 'Manage archive file download, extraction, and cleanup.'
@@ -264,9 +263,9 @@ def change_to_s(currentvalue, newvalue)
264263

265264
# Support both arrays and colon-separated fields.
266265
def value=(*values)
267-
@value = values.flatten.collect { |val|
266+
@value = values.flatten.map do |val|
268267
val.split(File::PATH_SEPARATOR)
269-
}.flatten
268+
end.flatten
270269
end
271270
end
272271

@@ -280,29 +279,27 @@ def value=(*values)
280279
validate do |values|
281280
values = [values] unless values.is_a? Array
282281
values.each do |value|
283-
unless value =~ /\w+=/
284-
raise ArgumentError, _("Invalid environment setting '%{value}'") % { value: value }
285-
end
282+
raise ArgumentError, "Invalid environment setting '#{value}'" unless value =~ %r{\w+=}
286283
end
287284
end
288285
end
289286

290-
newcheck(:creates, :parent => Puppet::Parameter::Path) do
287+
newcheck(:creates, parent: Puppet::Parameter::Path) do
291288
desc 'if file/directory exists, will not download/extract archive.'
292289

293290
accept_arrays
294291

295292
# If the file exists, return false (i.e., don't run the command),
296293
# else return true
297294
def check(value)
298-
#TRANSLATORS 'creates' is a parameter name and should not be translated
299-
debug(_("Checking that 'creates' path '%{creates_path}' exists") % { creates_path: value })
295+
# TRANSLATORS 'creates' is a parameter name and should not be translated
296+
debug("Checking that 'creates' path '#{value}' exists")
300297
Puppet::FileSystem.exist?(value)
301298
end
302299
end
303300

304301
newcheck(:unless) do
305-
desc <<-'EOT'
302+
desc <<-EOT
306303
A test command that checks the state of the target system and restricts
307304
when the `archive` can run. If present, Puppet runs this test command
308305
first, then runs the main command unless the test has an exit code of 0
@@ -333,7 +330,7 @@ def check(value)
333330
334331
This `archive` would only run if every command in the array has a
335332
non-zero exit code.
336-
EOT
333+
EOT
337334

338335
validate do |cmds|
339336
cmds = [cmds] unless cmds.is_a? Array
@@ -348,24 +345,24 @@ def check(value)
348345
begin
349346
output, status = provider.run(value, true)
350347
rescue Timeout::Error
351-
err _("Check %{value} exceeded timeout") % { value: value.inspect }
348+
err format('Check %{value} exceeded timeout', value: value.inspect)
352349
return false
353350
end
354351

355352
if sensitive
356-
self.debug("[output redacted]")
353+
debug('[output redacted]')
357354
else
358-
output.split(/\n/).each { |line|
359-
self.debug(line)
360-
}
355+
output.split(%r{\n}).each do |line|
356+
debug(line)
357+
end
361358
end
362359

363360
status.exitstatus != 0
364361
end
365362
end
366363

367364
newcheck(:onlyif) do
368-
desc <<-'EOT'
365+
desc <<-EOT
369366
A test command that checks the state of the target system and restricts
370367
when the `archive` can run. If present, Puppet runs this test command
371368
first, and only runs the main command if the test has an exit code of 0
@@ -396,7 +393,7 @@ def check(value)
396393
397394
This `archive` would only run if every command in the array has an
398395
exit code of 0 (success).
399-
EOT
396+
EOT
400397

401398
validate do |cmds|
402399
cmds = [cmds] unless cmds.is_a? Array
@@ -411,19 +408,19 @@ def check(value)
411408
begin
412409
output, status = provider.run(value, true)
413410
rescue Timeout::Error
414-
err _("Check %{value} exceeded timeout") % { value: value.inspect }
411+
err format('Check %{value} exceeded timeout', value: value.inspect)
415412
return false
416413
end
417414

418415
if sensitive
419-
self.debug("[output redacted]")
416+
debug('[output redacted]')
420417
else
421-
output.split(/\n/).each { |line|
422-
self.debug(line)
423-
}
418+
output.split(%r{\n}).each do |line|
419+
debug(line)
420+
end
424421
end
425422

426-
status.exitstatus == 0
423+
status.exitstatus.zero?
427424
end
428425
end
429426

@@ -460,9 +457,8 @@ def check(value)
460457
# Verify that we pass all of the checks. The argument determines whether
461458
# we skip the :refreshonly check, which is necessary because we now check
462459
# within refresh
463-
def check_all_attributes(refreshing = false)
464-
self.class.checks.each { |check|
465-
460+
def check_all_attributes(_refreshing = false)
461+
self.class.checks.each do |check|
466462
next unless @parameters.include?(check)
467463

468464
val = @parameters[check].value
@@ -472,13 +468,13 @@ def check_all_attributes(refreshing = false)
472468

473469
# Give a debug message so users can figure out what command would have been
474470
# but don't print sensitive commands or parameters in the clear
475-
sourcestring = @parameters[:source].sensitive ? "[command redacted]" : @parameters[:source].value
471+
sourcestring = @parameters[:source].sensitive ? '[command redacted]' : @parameters[:source].value
476472

477-
debug(_("'%{source}' won't be executed because of failed check '%{check}'") % { source: sourcestring, check: check })
473+
debug(format("'%{source}' won't be executed because of failed check '%{check}'", source: sourcestring, check: check))
478474

479475
return false
480476
end
481-
}
477+
end
482478
true
483479
end
484480

@@ -492,13 +488,13 @@ def output
492488

493489
# Run the command, or optionally run a separately-specified command.
494490
def refresh
495-
if check_all_attributes(true)
496-
cmd = self[:refresh]
497-
if cmd
498-
provider.run(cmd)
499-
else
500-
property(:returns).sync
501-
end
491+
return unless check_all_attributes(true)
492+
493+
cmd = self[:refresh]
494+
if cmd
495+
provider.run(cmd)
496+
else
497+
property(:returns).sync
502498
end
503499
end
504500

@@ -507,7 +503,7 @@ def refresh
507503
def set_sensitive_parameters(sensitive_parameters)
508504
# If any are sensitive, mark all as sensitive
509505
sensitive = false
510-
parameters_to_check = [:command, :unless, :onlyif]
506+
parameters_to_check = %i[command unless onlyif]
511507

512508
parameters_to_check.each do |p|
513509
if sensitive_parameters.include?(p)
@@ -518,9 +514,7 @@ def set_sensitive_parameters(sensitive_parameters)
518514

519515
if sensitive
520516
parameters_to_check.each do |p|
521-
if parameters.include?(p)
522-
parameter(p).sensitive = true
523-
end
517+
parameter(p).sensitive = true if parameters.include?(p)
524518
end
525519
end
526520

0 commit comments

Comments
 (0)