Skip to content

Commit 55c4aea

Browse files
committed
(CONT-801) Autocorrect unsafe group 3
1 parent 0e3e5b1 commit 55c4aea

File tree

15 files changed

+28
-90
lines changed

15 files changed

+28
-90
lines changed

.rubocop_todo.yml

Lines changed: 5 additions & 61 deletions
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-05-18 13:42:01 UTC using RuboCop version 1.48.1.
3+
# on 2023-05-18 14:27:28 UTC using RuboCop version 1.48.1.
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
@@ -20,12 +20,11 @@ Lint/ConstantDefinitionInBlock:
2020
- 'spec/functions/get_module_path_spec.rb'
2121
- 'spec/unit/facter/util/puppet_settings_spec.rb'
2222

23-
# Offense count: 3
23+
# Offense count: 2
2424
# Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches.
2525
Lint/DuplicateBranch:
2626
Exclude:
2727
- 'lib/puppet/parser/functions/str2bool.rb'
28-
- 'lib/puppet/provider/file_line/ruby.rb'
2928

3029
# Offense count: 2
3130
# Configuration parameters: MaximumRangeSize.
@@ -55,17 +54,17 @@ Metrics/BlockLength:
5554
# Offense count: 4
5655
# Configuration parameters: AllowedMethods, AllowedPatterns.
5756
Metrics/CyclomaticComplexity:
58-
Max: 15
57+
Max: 14
5958

6059
# Offense count: 8
6160
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
6261
Metrics/MethodLength:
63-
Max: 35
62+
Max: 33
6463

6564
# Offense count: 3
6665
# Configuration parameters: AllowedMethods, AllowedPatterns.
6766
Metrics/PerceivedComplexity:
68-
Max: 19
67+
Max: 18
6968

7069
# Offense count: 2
7170
# Configuration parameters: ForbiddenDelimiters.
@@ -183,70 +182,15 @@ Style/FrozenStringLiteralComment:
183182
Exclude:
184183
- 'lib/puppet_x/stdlib/toml_dumper.rb'
185184

186-
# Offense count: 4
187-
# This cop supports unsafe autocorrection (--autocorrect-all).
188-
# Configuration parameters: AllowedMethods.
189-
# AllowedMethods: nonzero?
190-
Style/IfWithBooleanLiteralBranches:
191-
Exclude:
192-
- 'lib/facter/pe_version.rb'
193-
- 'lib/puppet/parser/functions/fqdn_rotate.rb'
194-
- 'lib/puppet/parser/functions/shuffle.rb'
195-
- 'lib/puppet/provider/file_line/ruby.rb'
196-
197-
# Offense count: 2
198-
# This cop supports unsafe autocorrection (--autocorrect-all).
199-
Style/MapToHash:
200-
Exclude:
201-
- 'lib/puppet/parser/functions/prefix.rb'
202-
- 'lib/puppet/parser/functions/suffix.rb'
203-
204185
# Offense count: 1
205186
Style/MixinUsage:
206187
Exclude:
207188
- 'spec/spec_helper.rb'
208189

209-
# Offense count: 3
210-
# This cop supports unsafe autocorrection (--autocorrect-all).
211-
# Configuration parameters: EnforcedStyle, AllowedMethods, AllowedPatterns.
212-
# SupportedStyles: predicate, comparison
213-
Style/NumericPredicate:
214-
Exclude:
215-
- 'spec/**/*'
216-
- 'lib/puppet/parser/functions/num2bool.rb'
217-
- 'lib/puppet/provider/file_line/ruby.rb'
218-
219190
# Offense count: 3
220191
# Configuration parameters: AllowedMethods.
221192
# AllowedMethods: respond_to_missing?
222193
Style/OptionalBooleanParameter:
223194
Exclude:
224195
- 'lib/puppet/functions/to_json_pretty.rb'
225196
- 'lib/puppet_x/stdlib/toml_dumper.rb'
226-
227-
# Offense count: 2
228-
# This cop supports unsafe autocorrection (--autocorrect-all).
229-
Style/StringChars:
230-
Exclude:
231-
- 'lib/puppet/parser/functions/fqdn_rotate.rb'
232-
- 'lib/puppet/parser/functions/shuffle.rb'
233-
234-
# Offense count: 5
235-
# This cop supports unsafe autocorrection (--autocorrect-all).
236-
# Configuration parameters: Mode.
237-
Style/StringConcatenation:
238-
Exclude:
239-
- 'lib/puppet_x/stdlib/toml_dumper.rb'
240-
- 'spec/functions/get_module_path_spec.rb'
241-
242-
# Offense count: 5
243-
# This cop supports unsafe autocorrection (--autocorrect-all).
244-
# Configuration parameters: AllowMethodsWithArguments, AllowedMethods, AllowedPatterns, AllowComments.
245-
# AllowedMethods: define_method
246-
Style/SymbolProc:
247-
Exclude:
248-
- 'lib/puppet/functions/to_json_pretty.rb'
249-
- 'lib/puppet/parser/functions/clamp.rb'
250-
- 'lib/puppet/parser/functions/fqdn_uuid.rb'
251-
- 'lib/puppet/parser/functions/shell_join.rb'
252-
- 'lib/puppet/parser/functions/squeeze.rb'

lib/facter/pe_version.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030
# Fact: is_pe
3131
Facter.add('is_pe') do
3232
setcode do
33-
if Facter.value(:pe_version).to_s.empty?
34-
false
35-
else
36-
true
37-
end
33+
!Facter.value(:pe_version).to_s.empty?
3834
end
3935
end
4036

lib/puppet/functions/to_json_pretty.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def to_json_pretty(data, skip_undef = false, opts = nil)
6161
# It's not possible to make an abstract type that can be either a boolean
6262
# false or an integer, so we use -1 as the falsey value
6363
if opts
64-
opts = opts.transform_keys { |k| k.to_sym }
64+
opts = opts.transform_keys(&:to_sym)
6565

6666
opts[:max_nesting] = false if opts[:max_nesting] == -1
6767
end

lib/puppet/parser/functions/clamp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ module Puppet::Parser::Functions
4141

4242
# convert to numeric each element
4343
# then sort them and get a middle value
44-
args.map { |n| n.to_i }.sort[1]
44+
args.map(&:to_i).sort[1]
4545
end
4646
end

lib/puppet/parser/functions/fqdn_rotate.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626

2727
result = value.clone
2828

29-
string = value.is_a?(String) ? true : false
29+
string = value.is_a?(String)
3030

3131
# Check whether it makes sense to rotate ...
3232
return result if result.size <= 1
3333

3434
# We turn any string value into an array to be able to rotate ...
35-
result = string ? result.split('') : result
35+
result = string ? result.chars : result
3636

3737
elements = result.size
3838

lib/puppet/parser/functions/fqdn_uuid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module Puppet::Parser::Functions
4545
0x4f,
4646
0xd4,
4747
0x30,
48-
0xc8].map { |b| b.chr }.join
48+
0xc8].map(&:chr).join
4949

5050
sha1 = Digest::SHA1.new
5151
sha1.update(uuid_name_space_dns)

lib/puppet/parser/functions/num2bool.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Puppet::Parser::Functions
4242
number = number.to_i
4343

4444
# Return true for any positive number and false otherwise
45-
return number > 0
45+
return number.positive?
4646
end
4747
end
4848

lib/puppet/parser/functions/prefix.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ module Puppet::Parser::Functions
3838
prefix ? prefix + i : i
3939
end
4040
else
41-
enumerable.map { |k, v|
41+
enumerable.to_h do |k, v|
4242
k = k.to_s
4343
[prefix ? prefix + k : k, v]
44-
}.to_h
44+
end
4545
end
4646

4747
return result

lib/puppet/parser/functions/shell_join.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Puppet::Parser::Functions
2222
raise Puppet::ParseError, "First argument is not an Array: #{array.inspect}" unless array.is_a?(Array)
2323

2424
# explicit conversion to string is required for ruby 1.9
25-
array = array.map { |item| item.to_s }
25+
array = array.map(&:to_s)
2626
result = Shellwords.shelljoin(array)
2727

2828
return result

lib/puppet/parser/functions/shuffle.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ module Puppet::Parser::Functions
2020

2121
result = value.clone
2222

23-
string = value.is_a?(String) ? true : false
23+
string = value.is_a?(String)
2424

2525
# Check whether it makes sense to shuffle ...
2626
return result if result.size <= 1
2727

2828
# We turn any string value into an array to be able to shuffle ...
29-
result = string ? result.split('') : result
29+
result = string ? result.chars : result
3030

3131
elements = result.size
3232

lib/puppet/parser/functions/squeeze.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Puppet::Parser::Functions
2121
if squeezeval
2222
item.map { |i| i.squeeze(squeezeval) }
2323
else
24-
item.map { |i| i.squeeze }
24+
item.map(&:squeeze)
2525
end
2626
elsif squeezeval
2727
item.squeeze(squeezeval)

lib/puppet/parser/functions/suffix.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ module Puppet::Parser::Functions
4242
suffix ? i + suffix : i
4343
end
4444
else
45-
enumerable.map { |k, v|
45+
enumerable.to_h do |k, v|
4646
k = k.to_s
4747
[suffix ? k + suffix : k, v]
48-
}.to_h
48+
end
4949
end
5050

5151
return result

lib/puppet/provider/file_line/ruby.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ def exists?
1717
found = line.chomp == resource[:line]
1818
lines_count += 1 if found
1919
end
20-
return found = lines_count > 0 if resource[:match].nil?
20+
return found = lines_count.positive? if resource[:match].nil?
2121

2222
match_count = count_matches(new_match_regex)
2323
found = if resource[:ensure] == :present
2424
if match_count.zero?
2525
if lines_count.zero? && resource[:append_on_no_match].to_s == 'false'
2626
true # lies, but gets the job done
27-
elsif lines_count.zero? && resource[:append_on_no_match].to_s != 'false'
28-
false
2927
else
30-
true
28+
!(lines_count.zero? && resource[:append_on_no_match].to_s != 'false')
3129
end
3230
elsif resource[:replace_all_matches_not_matching_line].to_s == 'true'
3331
false # maybe lies, but knows there's still work to do
@@ -50,7 +48,7 @@ def exists?
5048
end
5149

5250
def create
53-
return if resource[:replace].to_s != 'true' && count_matches(new_match_regex) > 0
51+
return if resource[:replace].to_s != 'true' && count_matches(new_match_regex).positive?
5452

5553
if resource[:match]
5654
handle_create_with_match

lib/puppet_x/stdlib/toml_dumper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ def dump_table_array_pairs(table_array_pairs, prefix)
103103

104104
def print_prefix(prefix, extra_brackets = false)
105105
new_prefix = prefix.join('.')
106-
new_prefix = '[' + new_prefix + ']' if extra_brackets
106+
new_prefix = "[#{new_prefix}]" if extra_brackets
107107

108-
@toml_str += "[" + new_prefix + "]\n" # rubocop:disable Style/StringLiterals
108+
@toml_str += "[#{new_prefix}]\n"
109109
end
110110

111111
def to_toml(obj)
@@ -130,6 +130,6 @@ def bare_key?(key)
130130
end
131131

132132
def quote_key(key)
133-
'"' + key.gsub('"', '\\"') + '"'
133+
"\"#{key.gsub('"', '\\"')}\""
134134
end
135135
end

spec/functions/get_module_path_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def initialize(path)
3636
end
3737

3838
it 'when the modulepath is a list' do
39-
Puppet[:modulepath] = modulepath + 'tmp/something_else'
39+
Puppet[:modulepath] = "#{modulepath}tmp/something_else"
4040

4141
expect(subject).to run.with_params('foo').and_return(path_of_module_foo.path)
4242
end
@@ -54,7 +54,7 @@ def initialize(path)
5454
end
5555

5656
it 'when the modulepath is a list' do
57-
Puppet[:modulepath] = modulepath + 'tmp/something_else'
57+
Puppet[:modulepath] = "#{modulepath}tmp/something_else"
5858
expect(subject).to run.with_params('foo').and_return(path_of_module_foo.path)
5959
end
6060
end

0 commit comments

Comments
 (0)