Skip to content

Commit 18a8332

Browse files
committed
(CONT-801) Autocorrect unsafe group 1
1 parent 8840b86 commit 18a8332

File tree

6 files changed

+90
-128
lines changed

6 files changed

+90
-128
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,6 @@ Lint/MissingCopEnableDirective:
3434
- 'spec/functions/merge_spec.rb'
3535
- 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb'
3636

37-
# Offense count: 2
38-
# This cop supports unsafe autocorrection (--autocorrect-all).
39-
# Configuration parameters: AllowedMethods.
40-
# AllowedMethods: instance_of?, kind_of?, is_a?, eql?, respond_to?, equal?
41-
Lint/RedundantSafeNavigation:
42-
Exclude:
43-
- 'lib/puppet/parser/functions/assert_private.rb'
44-
- 'lib/puppet/parser/functions/getparam.rb'
45-
4637
# Offense count: 3
4738
# Configuration parameters: AllowComments, AllowNil.
4839
Lint/SuppressedException:
@@ -105,19 +96,6 @@ Performance/CollectionLiteralInLoop:
10596
Exclude:
10697
- 'lib/puppet/functions/ensure_packages.rb'
10798

108-
# Offense count: 1
109-
# This cop supports unsafe autocorrection (--autocorrect-all).
110-
# Configuration parameters: AllowRegexpMatch.
111-
Performance/RedundantEqualityComparisonBlock:
112-
Exclude:
113-
- 'lib/puppet/parser/functions/bool2str.rb'
114-
115-
# Offense count: 1
116-
# This cop supports unsafe autocorrection (--autocorrect-all).
117-
Performance/UnfreezeString:
118-
Exclude:
119-
- 'lib/puppet/parser/functions/pw_hash.rb'
120-
12199
# Offense count: 95
122100
# This cop supports unsafe autocorrection (--autocorrect-all).
123101
RSpec/BeEq:
@@ -202,21 +180,6 @@ RSpec/StubbedMock:
202180
- 'spec/functions/reverse_spec.rb'
203181
- 'spec/functions/squeeze_spec.rb'
204182

205-
# Offense count: 1
206-
# This cop supports unsafe autocorrection (--autocorrect-all).
207-
# Configuration parameters: MinBranchesCount.
208-
Style/CaseLikeIf:
209-
Exclude:
210-
- 'lib/puppet_x/stdlib/toml_dumper.rb'
211-
212-
# Offense count: 1
213-
# This cop supports unsafe autocorrection (--autocorrect-all).
214-
# Configuration parameters: EnforcedStyle.
215-
# SupportedStyles: nested, compact
216-
Style/ClassAndModuleChildren:
217-
Exclude:
218-
- 'lib/puppet_x/stdlib/toml_dumper.rb'
219-
220183
# Offense count: 2
221184
# This cop supports unsafe autocorrection (--autocorrect-all).
222185
Style/CollectionCompact:

lib/puppet/parser/functions/assert_private.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Puppet::Parser::Functions
1919
scope = self
2020
if scope.lookupvar('module_name') != scope.lookupvar('caller_module_name')
2121
message = nil
22-
if args[0]&.is_a?(String)
22+
if args[0].is_a?(String)
2323
message = args[0]
2424
else
2525
manifest_name = scope.source.name

lib/puppet/parser/functions/bool2str.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module Puppet::Parser::Functions
4949
# We can have either true or false, and nothing else
5050
raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with') unless [FalseClass, TrueClass].include?(klass)
5151

52-
raise(Puppet::ParseError, 'bool2str(): Requires strings to convert to') unless [true_string, false_string].all? { |x| x.is_a?(String) }
52+
raise(Puppet::ParseError, 'bool2str(): Requires strings to convert to') unless [true_string, false_string].all?(String)
5353

5454
return value ? true_string : false_string
5555
end

lib/puppet/parser/functions/getparam.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
) do |vals|
4747
reference, param = vals
4848
raise(ArgumentError, 'Must specify a reference') unless reference
49-
raise(ArgumentError, 'Must specify name of a parameter') unless param&.instance_of?(String)
49+
raise(ArgumentError, 'Must specify name of a parameter') unless param.instance_of?(String)
5050

5151
return '' if param.empty?
5252

lib/puppet/parser/functions/pw_hash.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676

7777
# handle weak implementations of String#crypt
7878
# dup the string to get rid of frozen status for testing
79-
if 'test'.dup.crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.'
79+
if (+'test').crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.'
8080
password.crypt(salt)
8181
else
8282
# JRuby < 1.7.17

lib/puppet_x/stdlib/toml_dumper.rb

Lines changed: 86 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -21,116 +21,115 @@
2121
require_relative '../../puppet_x/stdlib'
2222
require 'date'
2323

24-
module PuppetX::Stdlib
25-
# The Dumper class was blindly copied from https://github.com/emancu/toml-rb/blob/v2.0.1/lib/toml-rb/dumper.rb
26-
# This allows us to use the `to_toml` function as a `Deferred` function because the `toml-rb` gem is usually
27-
# installed on the agent and the `Deferred` function gets evaluated before the catalog gets applied. This
28-
# makes it in most scenarios impossible to install the gem before it is used.
29-
class TomlDumper
30-
attr_reader :toml_str
31-
32-
def initialize(hash)
33-
@toml_str = ''
34-
35-
visit(hash, [])
36-
end
24+
# The Dumper class was blindly copied from https://github.com/emancu/toml-rb/blob/v2.0.1/lib/toml-rb/dumper.rb
25+
# This allows us to use the `to_toml` function as a `Deferred` function because the `toml-rb` gem is usually
26+
# installed on the agent and the `Deferred` function gets evaluated before the catalog gets applied. This
27+
# makes it in most scenarios impossible to install the gem before it is used.
28+
class PuppetX::Stdlib::TomlDumper
29+
attr_reader :toml_str
3730

38-
private
31+
def initialize(hash)
32+
@toml_str = ''
3933

40-
def visit(hash, prefix, extra_brackets = false)
41-
simple_pairs, nested_pairs, table_array_pairs = sort_pairs hash
34+
visit(hash, [])
35+
end
4236

43-
print_prefix prefix, extra_brackets if prefix.any? && (simple_pairs.any? || hash.empty?)
37+
private
4438

45-
dump_pairs simple_pairs, nested_pairs, table_array_pairs, prefix
46-
end
39+
def visit(hash, prefix, extra_brackets = false)
40+
simple_pairs, nested_pairs, table_array_pairs = sort_pairs hash
4741

48-
def sort_pairs(hash)
49-
nested_pairs = []
50-
simple_pairs = []
51-
table_array_pairs = []
52-
53-
hash.keys.sort.each do |key|
54-
val = hash[key]
55-
element = [key, val]
56-
57-
if val.is_a? Hash
58-
nested_pairs << element
59-
elsif val.is_a?(Array) && val.first.is_a?(Hash)
60-
table_array_pairs << element
61-
else
62-
simple_pairs << element
63-
end
64-
end
42+
print_prefix prefix, extra_brackets if prefix.any? && (simple_pairs.any? || hash.empty?)
6543

66-
[simple_pairs, nested_pairs, table_array_pairs]
67-
end
68-
69-
def dump_pairs(simple, nested, table_array, prefix = [])
70-
# First add simple pairs, under the prefix
71-
dump_simple_pairs simple
72-
dump_nested_pairs nested, prefix
73-
dump_table_array_pairs table_array, prefix
74-
end
44+
dump_pairs simple_pairs, nested_pairs, table_array_pairs, prefix
45+
end
7546

76-
def dump_simple_pairs(simple_pairs)
77-
simple_pairs.each do |key, val|
78-
key = quote_key(key) unless bare_key? key
79-
@toml_str << "#{key} = #{to_toml(val)}\n"
80-
end
81-
end
47+
def sort_pairs(hash)
48+
nested_pairs = []
49+
simple_pairs = []
50+
table_array_pairs = []
8251

83-
def dump_nested_pairs(nested_pairs, prefix)
84-
nested_pairs.each do |key, val|
85-
key = quote_key(key) unless bare_key? key
52+
hash.keys.sort.each do |key|
53+
val = hash[key]
54+
element = [key, val]
8655

87-
visit val, prefix + [key], false
56+
if val.is_a? Hash
57+
nested_pairs << element
58+
elsif val.is_a?(Array) && val.first.is_a?(Hash)
59+
table_array_pairs << element
60+
else
61+
simple_pairs << element
8862
end
8963
end
9064

91-
def dump_table_array_pairs(table_array_pairs, prefix)
92-
table_array_pairs.each do |key, val|
93-
key = quote_key(key) unless bare_key? key
94-
aux_prefix = prefix + [key]
65+
[simple_pairs, nested_pairs, table_array_pairs]
66+
end
9567

96-
val.each do |child|
97-
print_prefix aux_prefix, true
98-
args = sort_pairs(child) << aux_prefix
68+
def dump_pairs(simple, nested, table_array, prefix = [])
69+
# First add simple pairs, under the prefix
70+
dump_simple_pairs simple
71+
dump_nested_pairs nested, prefix
72+
dump_table_array_pairs table_array, prefix
73+
end
9974

100-
dump_pairs(*args)
101-
end
102-
end
75+
def dump_simple_pairs(simple_pairs)
76+
simple_pairs.each do |key, val|
77+
key = quote_key(key) unless bare_key? key
78+
@toml_str << "#{key} = #{to_toml(val)}\n"
10379
end
80+
end
10481

105-
def print_prefix(prefix, extra_brackets = false)
106-
new_prefix = prefix.join('.')
107-
new_prefix = '[' + new_prefix + ']' if extra_brackets
82+
def dump_nested_pairs(nested_pairs, prefix)
83+
nested_pairs.each do |key, val|
84+
key = quote_key(key) unless bare_key? key
10885

109-
@toml_str += "[" + new_prefix + "]\n" # rubocop:disable Style/StringLiterals
86+
visit val, prefix + [key], false
11087
end
88+
end
11189

112-
def to_toml(obj)
113-
if obj.is_a?(Time) || obj.is_a?(DateTime)
114-
obj.strftime('%Y-%m-%dT%H:%M:%SZ')
115-
elsif obj.is_a?(Date)
116-
obj.strftime('%Y-%m-%d')
117-
elsif obj.is_a? Regexp
118-
obj.inspect.inspect
119-
elsif obj.is_a? String
120-
obj.inspect.gsub(/\\(#[$@{])/, '\1') # rubocop:disable Style/RegexpLiteral
121-
else
122-
obj.inspect
90+
def dump_table_array_pairs(table_array_pairs, prefix)
91+
table_array_pairs.each do |key, val|
92+
key = quote_key(key) unless bare_key? key
93+
aux_prefix = prefix + [key]
94+
95+
val.each do |child|
96+
print_prefix aux_prefix, true
97+
args = sort_pairs(child) << aux_prefix
98+
99+
dump_pairs(*args)
123100
end
124101
end
102+
end
125103

126-
def bare_key?(key)
127-
# rubocop:disable Style/RegexpLiteral
128-
!!key.to_s.match(/^[a-zA-Z0-9_-]*$/)
129-
# rubocop:enable Style/RegexpLiteral
130-
end
104+
def print_prefix(prefix, extra_brackets = false)
105+
new_prefix = prefix.join('.')
106+
new_prefix = '[' + new_prefix + ']' if extra_brackets
131107

132-
def quote_key(key)
133-
'"' + key.gsub('"', '\\"') + '"'
108+
@toml_str += "[" + new_prefix + "]\n" # rubocop:disable Style/StringLiterals
109+
end
110+
111+
def to_toml(obj)
112+
case obj
113+
when Time, DateTime
114+
obj.strftime('%Y-%m-%dT%H:%M:%SZ')
115+
when Date
116+
obj.strftime('%Y-%m-%d')
117+
when Regexp
118+
obj.inspect.inspect
119+
when String
120+
obj.inspect.gsub(/\\(#[$@{])/, '\1') # rubocop:disable Style/RegexpLiteral
121+
else
122+
obj.inspect
134123
end
135124
end
125+
126+
def bare_key?(key)
127+
# rubocop:disable Style/RegexpLiteral
128+
!!key.to_s.match(/^[a-zA-Z0-9_-]*$/)
129+
# rubocop:enable Style/RegexpLiteral
130+
end
131+
132+
def quote_key(key)
133+
'"' + key.gsub('"', '\\"') + '"'
134+
end
136135
end

0 commit comments

Comments
 (0)