cookstyle 5.20.0 → 5.21.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/cookstyle.yml +38 -3
- data/lib/cookstyle.rb +1 -0
- data/lib/cookstyle/version.rb +1 -1
- data/lib/rubocop/chef/autocorrect_helpers.rb +32 -0
- data/lib/rubocop/cop/chef/deprecation/windows_task_change_action.rb +3 -1
- data/lib/rubocop/cop/chef/deprecation/yum_dnf_compat_recipe.rb +1 -0
- data/lib/rubocop/cop/chef/modernize/simplify_apt_ppa_setup.rb +67 -0
- data/lib/rubocop/cop/chef/redundant/attribute_metadata.rb +2 -1
- data/lib/rubocop/cop/chef/redundant/grouping_metadata.rb +2 -1
- data/lib/rubocop/cop/chef/redundant/long_description_metadata.rb +2 -6
- data/lib/rubocop/cop/chef/redundant/property_splat_regex.rb +58 -0
- data/lib/rubocop/cop/chef/redundant/recipe_metadata.rb +2 -1
- data/lib/rubocop/cop/chef/redundant/string_property_with_nil_default.rb +65 -0
- data/lib/rubocop/cop/chef/sharing/invalid_license_string.rb +1 -0
- data/lib/rubocop/cop/chef/style/unnecessary_os_check.rb +113 -0
- data/lib/rubocop/cop/chef/style/use_platform_helpers.rb +14 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 415ddd30149c8277cf255cd81ceb26835eb969da515e97e748372dc72ba40840
|
4
|
+
data.tar.gz: cf2a3e2be30fff68aabef795aecdb6e489a2ede68f09f21cd3c781c35979846c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 911b2afb60dbf89849e4a3e349ef784d7e2b29a25ed00a9eca07222a4fd002fd7486d9d1e2baac4c94cf486c8423ba53f2d0bd83d85cfa2ba668daf05056cb11
|
7
|
+
data.tar.gz: 8f81cdfd43904b11c7651d29dc4f26e5bd930af8d68c2be477cf3c59f9f5d5a37a0f20c5b41640054d5c2ec817d4a4c87ea17be6cda9a486a5c1ffd4f4dbfeb1
|
data/config/cookstyle.yml
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
AllCops:
|
2
|
-
TargetRubyVersion: 2.
|
2
|
+
TargetRubyVersion: 2.3
|
3
3
|
TargetChefVersion: ~
|
4
4
|
Exclude:
|
5
5
|
- '**/files/**/*'
|
@@ -127,6 +127,11 @@ ChefStyle/ChefWhaaat:
|
|
127
127
|
Enabled: false
|
128
128
|
VersionAdded: '5.20.0'
|
129
129
|
|
130
|
+
ChefStyle/UnnecessaryOSCheck:
|
131
|
+
Description: Use the platform_family?() helpers instead of node['os] == 'foo' for platform_families that match 1:1 with OS values. These helpers are easier to read and can accept multiple platform arguments, which greatly simplifies complex platform logic.
|
132
|
+
Enabled: false
|
133
|
+
VersionAdded: '5.21.0'
|
134
|
+
|
130
135
|
###############################
|
131
136
|
# ChefCorrectness: Avoiding potential problems
|
132
137
|
###############################
|
@@ -1101,6 +1106,15 @@ ChefModernize/ExecuteSysctl:
|
|
1101
1106
|
- '**/attributes/*.rb'
|
1102
1107
|
- '**/Berksfile'
|
1103
1108
|
|
1109
|
+
ChefModernize/SimplifyAptPpaSetup:
|
1110
|
+
Description: The apt_repository resource allows setting up PPAs without using the full URL to ppa.launchpad.net.
|
1111
|
+
Enabled: true
|
1112
|
+
VersionAdded: '5.21.0'
|
1113
|
+
Exclude:
|
1114
|
+
- '**/metadata.rb'
|
1115
|
+
- '**/attributes/*.rb'
|
1116
|
+
- '**/Berksfile'
|
1117
|
+
|
1104
1118
|
###############################
|
1105
1119
|
# ChefRedundantCode: Cleanup unnecessary code in your cookbooks regardless of Chef Infra Client release
|
1106
1120
|
###############################
|
@@ -1247,6 +1261,22 @@ ChefRedundantCode/GroupingMetadata:
|
|
1247
1261
|
Include:
|
1248
1262
|
- '**/metadata.rb'
|
1249
1263
|
|
1264
|
+
ChefRedundantCode/StringPropertyWithNilDefault:
|
1265
|
+
Description: 'Properties have a nil value by default so there is no need to set the default value to nil.'
|
1266
|
+
Enabled: true
|
1267
|
+
VersionAdded: '5.21.0'
|
1268
|
+
Include:
|
1269
|
+
- '**/resources/*.rb'
|
1270
|
+
- '**/libraries/*.rb'
|
1271
|
+
|
1272
|
+
ChefRedundantCode/PropertySplatRegex:
|
1273
|
+
Description: 'There is no need to validate the input of properties in resources using a regex value that will always pass.'
|
1274
|
+
Enabled: true
|
1275
|
+
VersionAdded: '5.21.0'
|
1276
|
+
Include:
|
1277
|
+
- '**/resources/*.rb'
|
1278
|
+
- '**/libraries/*.rb'
|
1279
|
+
|
1250
1280
|
###############################
|
1251
1281
|
# Migrating to new patterns
|
1252
1282
|
###############################
|
@@ -1444,8 +1474,6 @@ Style/MethodDefParentheses:
|
|
1444
1474
|
Enabled: true
|
1445
1475
|
Naming/MethodName:
|
1446
1476
|
Enabled: true
|
1447
|
-
Style/ModuleFunction:
|
1448
|
-
Enabled: true
|
1449
1477
|
Style/MultilineBlockChain:
|
1450
1478
|
Enabled: true
|
1451
1479
|
Layout/MultilineBlockLayout:
|
@@ -1625,6 +1653,7 @@ Layout/ConditionPosition:
|
|
1625
1653
|
Lint/Debugger:
|
1626
1654
|
Enabled: true
|
1627
1655
|
Layout/DefEndAlignment:
|
1656
|
+
AutoCorrect: true
|
1628
1657
|
Enabled: true
|
1629
1658
|
Lint/DeprecatedClassMethods:
|
1630
1659
|
Enabled: true
|
@@ -1642,6 +1671,7 @@ Lint/EmptyInterpolation:
|
|
1642
1671
|
Enabled: true
|
1643
1672
|
Layout/EndAlignment:
|
1644
1673
|
Enabled: true
|
1674
|
+
AutoCorrect: true
|
1645
1675
|
Lint/EndInMethod:
|
1646
1676
|
Enabled: true
|
1647
1677
|
Lint/EnsureReturn:
|
@@ -1958,3 +1988,8 @@ Style/IfUnlessModifier:
|
|
1958
1988
|
# in general cookbooks shouldn't include a gemfile, but if they do they can do with it as they wish
|
1959
1989
|
Bundler/OrderedGems:
|
1960
1990
|
Enabled: false
|
1991
|
+
|
1992
|
+
# extend self and module_function are not the same. module_function makes all the methods private
|
1993
|
+
# and this rule will break any library that worked with extend self.
|
1994
|
+
Style/ModuleFunction:
|
1995
|
+
Enabled: false
|
data/lib/cookstyle.rb
CHANGED
@@ -38,6 +38,7 @@ module Cookstyle
|
|
38
38
|
end
|
39
39
|
|
40
40
|
require_relative 'rubocop/chef'
|
41
|
+
require_relative 'rubocop/chef/autocorrect_helpers'
|
41
42
|
require_relative 'rubocop/chef/cookbook_helpers'
|
42
43
|
require_relative 'rubocop/chef/platform_helpers'
|
43
44
|
require_relative 'rubocop/chef/cookbook_only'
|
data/lib/cookstyle/version.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright 2020, Chef Software Inc.
|
3
|
+
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
module RuboCop
|
18
|
+
module Chef
|
19
|
+
# Helpers for use in autocorrection
|
20
|
+
module AutocorrectHelpers
|
21
|
+
# if the node has a heredoc as an argument you'll only get the start of the heredoc and removing
|
22
|
+
# the node will result in broken ruby. This way we match the node and the entire heredoc for removal
|
23
|
+
def expression_including_heredocs(node)
|
24
|
+
if node.arguments.last.respond_to?(:heredoc?) && node.arguments.last.heredoc?
|
25
|
+
node.loc.expression.join(node.arguments.last.loc.heredoc_end)
|
26
|
+
else
|
27
|
+
node.loc.expression
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -73,7 +73,9 @@ module RuboCop
|
|
73
73
|
private
|
74
74
|
|
75
75
|
def check_action(ast_obj)
|
76
|
-
|
76
|
+
if ast_obj.respond_to?(:value) && ast_obj.value == :change
|
77
|
+
add_offense(ast_obj, location: :expression, message: MSG, severity: :refactor)
|
78
|
+
end
|
77
79
|
end
|
78
80
|
end
|
79
81
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: 2020, Chef Software Inc.
|
3
|
+
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
module RuboCop
|
19
|
+
module Cop
|
20
|
+
module Chef
|
21
|
+
module ChefModernize
|
22
|
+
# The apt_repository resource allows setting up PPAs without using the full URL to ppa.launchpad.net, which should be used to simplify the resource code in your cookbooks.
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
#
|
26
|
+
# # bad
|
27
|
+
# apt_repository 'atom-ppa' do
|
28
|
+
# uri 'http://ppa.launchpad.net/webupd8team/atom/ubuntu'
|
29
|
+
# components ['main']
|
30
|
+
# keyserver 'keyserver.ubuntu.com'
|
31
|
+
# key 'C2518248EEA14886'
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
# apt_repository 'atom-ppa' do
|
36
|
+
# uri 'ppa:webupd8team/atom'
|
37
|
+
# components ['main']
|
38
|
+
# keyserver 'keyserver.ubuntu.com'
|
39
|
+
# key 'C2518248EEA14886'
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
class SimplifyAptPpaSetup < Cop
|
43
|
+
include RangeHelp
|
44
|
+
include RuboCop::Chef::CookbookHelpers
|
45
|
+
|
46
|
+
MSG = 'The apt_repository resource allows setting up PPAs without using the full URL to ppa.launchpad.net.'.freeze
|
47
|
+
|
48
|
+
def on_block(node)
|
49
|
+
match_property_in_resource?(:apt_repository, 'uri', node) do |uri|
|
50
|
+
if %r{http(s)*://ppa.launchpad.net/(.*)/ubuntu$}.match?(uri.arguments&.first&.str_content)
|
51
|
+
add_offense(uri, location: :expression, message: MSG, severity: :refactor)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def autocorrect(node)
|
57
|
+
if (replacement_val = %r{http(s)*://ppa.launchpad.net/(.*)/ubuntu}.match(node.source)[2])
|
58
|
+
lambda do |corrector|
|
59
|
+
corrector.replace(node.loc.expression, "uri 'ppa:#{replacement_val}'")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -34,6 +34,7 @@ module RuboCop
|
|
34
34
|
#
|
35
35
|
class AttributeMetadata < Cop
|
36
36
|
include RangeHelp
|
37
|
+
include RuboCop::Chef::AutocorrectHelpers
|
37
38
|
|
38
39
|
MSG = 'The attribute metadata.rb method is not used and is unnecessary in cookbooks.'.freeze
|
39
40
|
|
@@ -43,7 +44,7 @@ module RuboCop
|
|
43
44
|
|
44
45
|
def autocorrect(node)
|
45
46
|
lambda do |corrector|
|
46
|
-
corrector.remove(range_with_surrounding_space(range: node
|
47
|
+
corrector.remove(range_with_surrounding_space(range: expression_including_heredocs(node), side: :left))
|
47
48
|
end
|
48
49
|
end
|
49
50
|
end
|
@@ -29,6 +29,7 @@ module RuboCop
|
|
29
29
|
#
|
30
30
|
class GroupingMetadata < Cop
|
31
31
|
include RangeHelp
|
32
|
+
include RuboCop::Chef::AutocorrectHelpers
|
32
33
|
|
33
34
|
MSG = 'The grouping metadata.rb method is not used and is unnecessary in cookbooks.'.freeze
|
34
35
|
|
@@ -38,7 +39,7 @@ module RuboCop
|
|
38
39
|
|
39
40
|
def autocorrect(node)
|
40
41
|
lambda do |corrector|
|
41
|
-
corrector.remove(range_with_surrounding_space(range: node
|
42
|
+
corrector.remove(range_with_surrounding_space(range: expression_including_heredocs(node), side: :left))
|
42
43
|
end
|
43
44
|
end
|
44
45
|
end
|
@@ -28,6 +28,7 @@ module RuboCop
|
|
28
28
|
#
|
29
29
|
class LongDescriptionMetadata < Cop
|
30
30
|
include RangeHelp
|
31
|
+
include RuboCop::Chef::AutocorrectHelpers
|
31
32
|
|
32
33
|
MSG = 'The long_description metadata.rb method is not used and is unnecessary in cookbooks.'.freeze
|
33
34
|
|
@@ -37,12 +38,7 @@ module RuboCop
|
|
37
38
|
|
38
39
|
def autocorrect(node)
|
39
40
|
lambda do |corrector|
|
40
|
-
|
41
|
-
total_range = range_with_surrounding_space(range: node.loc.expression.join(node.arguments.first.loc.heredoc_end), side: :left)
|
42
|
-
corrector.remove(total_range)
|
43
|
-
else
|
44
|
-
corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left))
|
45
|
-
end
|
41
|
+
corrector.remove(range_with_surrounding_space(range: expression_including_heredocs(node), side: :left))
|
46
42
|
end
|
47
43
|
end
|
48
44
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright 2020, Chef Software Inc.
|
3
|
+
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
module RuboCop
|
18
|
+
module Cop
|
19
|
+
module Chef
|
20
|
+
module ChefRedundantCode
|
21
|
+
# When a property has a type of String it can accept any string. There is no need to also validate string inputs against a regex that accept all values.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# property :config_file, String, regex: /.*/
|
27
|
+
# attribute :config_file, String, regex: /.*/
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# property :config_file, String
|
31
|
+
# attribute :config_file, String
|
32
|
+
#
|
33
|
+
class PropertySplatRegex < Cop
|
34
|
+
include RangeHelp
|
35
|
+
|
36
|
+
MSG = 'There is no need to validate the input of properties in resources using a regex value that will always pass.'.freeze
|
37
|
+
|
38
|
+
def_node_matcher :property_with_regex_splat?, <<-PATTERN
|
39
|
+
(send nil? {:property :attribute} (sym _) ... (hash <$(pair (sym :regex) (regexp (str ".*") (regopt))) ...>))
|
40
|
+
PATTERN
|
41
|
+
|
42
|
+
def on_send(node)
|
43
|
+
property_with_regex_splat?(node) do |splat|
|
44
|
+
add_offense(splat, location: :expression, message: MSG, severity: :refactor)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def autocorrect(node)
|
49
|
+
lambda do |corrector|
|
50
|
+
range = range_with_surrounding_comma(range_with_surrounding_space(range: node.loc.expression, side: :left), :left)
|
51
|
+
corrector.remove(range)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -29,6 +29,7 @@ module RuboCop
|
|
29
29
|
#
|
30
30
|
class RecipeMetadata < Cop
|
31
31
|
include RangeHelp
|
32
|
+
include RuboCop::Chef::AutocorrectHelpers
|
32
33
|
|
33
34
|
MSG = "The recipe metadata.rb method is not used and is unnecessary in cookbooks. Recipes should be documented in the cookbook's README.md file instead.".freeze
|
34
35
|
|
@@ -38,7 +39,7 @@ module RuboCop
|
|
38
39
|
|
39
40
|
def autocorrect(node)
|
40
41
|
lambda do |corrector|
|
41
|
-
corrector.remove(range_with_surrounding_space(range: node
|
42
|
+
corrector.remove(range_with_surrounding_space(range: expression_including_heredocs(node), side: :left))
|
42
43
|
end
|
43
44
|
end
|
44
45
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright 2019-2020, Chef Software Inc.
|
3
|
+
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
module RuboCop
|
18
|
+
module Cop
|
19
|
+
module Chef
|
20
|
+
module ChefRedundantCode
|
21
|
+
# Properties have a nil value by default so there is no need to set the default value to nil.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# property :config_file, String, default: nil
|
27
|
+
# property :config_file, [String, NilClass], default: nil
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# property :config_file, String
|
31
|
+
# property :config_file, [String, NilClass]
|
32
|
+
#
|
33
|
+
class StringPropertyWithNilDefault < Cop
|
34
|
+
include RangeHelp
|
35
|
+
|
36
|
+
MSG = 'Properties have a nil value by default so there is no need to set the default value to nil.'.freeze
|
37
|
+
|
38
|
+
def_node_matcher :string_property_with_nil_default?, <<-PATTERN
|
39
|
+
(send nil? :property (sym _)
|
40
|
+
{(const nil? :String) #string_and_nil_like?}
|
41
|
+
(hash <$(pair (sym :default) (nil)) ...>))
|
42
|
+
PATTERN
|
43
|
+
|
44
|
+
# An array of types that includes String & either NilClass or nil
|
45
|
+
def_node_matcher :string_and_nil_like?, <<-PATTERN
|
46
|
+
(array <(const nil? :String) {(const nil? :NilClass) (nil)}>)
|
47
|
+
PATTERN
|
48
|
+
|
49
|
+
def on_send(node)
|
50
|
+
string_property_with_nil_default?(node) do |nil_default|
|
51
|
+
add_offense(nil_default, location: :expression, message: MSG, severity: :refactor)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def autocorrect(node)
|
56
|
+
lambda do |corrector|
|
57
|
+
range = range_with_surrounding_comma(range_with_surrounding_space(range: node.loc.expression, side: :left), :left)
|
58
|
+
corrector.remove(range)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: 2020, Chef Software, Inc.
|
3
|
+
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
module RuboCop
|
18
|
+
module Cop
|
19
|
+
module Chef
|
20
|
+
module ChefStyle
|
21
|
+
# Use the platform_family?() helpers instead of node['os] == 'foo' for platform_families that match 1:1 with OS values. These helpers are easier to read and can accept multiple platform arguments, which greatly simplifies complex platform logic.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# node['os'] == 'darwin'
|
27
|
+
# node['os'] == 'windows'
|
28
|
+
# node['os'].eql?('aix')
|
29
|
+
# %w(netbsd openbsd freebsd).include?(node['os'])
|
30
|
+
#
|
31
|
+
# # good
|
32
|
+
# platform_family?('mac_os_x')
|
33
|
+
# platform_family?('windows')
|
34
|
+
# platform_family?('aix')
|
35
|
+
# platform_family?('netbsd', 'openbsd', 'freebsd)
|
36
|
+
#
|
37
|
+
class UnnecessaryOSCheck < Cop
|
38
|
+
MSG = "Use the platform_family?() helpers instead of node['os] == 'foo' for platform_families that match 1:1 with OS values.".freeze
|
39
|
+
|
40
|
+
# sorted list of all the os values that match 1:1 with a platform_family
|
41
|
+
UNNECESSARY_OS_VALUES = %w(aix darwin dragonflybsd freebsd netbsd openbsd solaris2 windows).freeze
|
42
|
+
|
43
|
+
def_node_matcher :os_equals?, <<-PATTERN
|
44
|
+
(send (send (send nil? :node) :[] (str "os") ) ${:== :!=} $str )
|
45
|
+
PATTERN
|
46
|
+
|
47
|
+
def_node_matcher :os_eql?, <<-PATTERN
|
48
|
+
(send (send (send nil? :node) :[] (str "os") ) :eql? $str )
|
49
|
+
PATTERN
|
50
|
+
|
51
|
+
def_node_matcher :os_include?, <<-PATTERN
|
52
|
+
(send $(array ...) :include? (send (send nil? :node) :[] (str "os")))
|
53
|
+
PATTERN
|
54
|
+
|
55
|
+
def on_send(node)
|
56
|
+
os_equals?(node) do |_operator, val|
|
57
|
+
if UNNECESSARY_OS_VALUES.include?(val.value)
|
58
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
os_eql?(node) do |val|
|
63
|
+
if UNNECESSARY_OS_VALUES.include?(val.value)
|
64
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
os_include?(node) do |val|
|
69
|
+
array_of_plats = array_from_ast(val)
|
70
|
+
# see if all the values in the .include? usage are in our list of 1:1 platform family to os values
|
71
|
+
if (UNNECESSARY_OS_VALUES & array_of_plats) == array_of_plats
|
72
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# return the passed value unless the value is darwin and then return mac_os_x
|
78
|
+
def sanitized_platform(plat)
|
79
|
+
plat == 'darwin' ? 'mac_os_x' : plat
|
80
|
+
end
|
81
|
+
|
82
|
+
# given an ast array spit out a ruby array
|
83
|
+
def array_from_ast(ast)
|
84
|
+
vals = []
|
85
|
+
ast.each_child_node { |x| vals << x.value }
|
86
|
+
vals.sort
|
87
|
+
end
|
88
|
+
|
89
|
+
def autocorrect(node)
|
90
|
+
lambda do |corrector|
|
91
|
+
os_equals?(node) do |operator, plat|
|
92
|
+
corrected_string = operator == :!= ? '!' : ''
|
93
|
+
corrected_string << "platform_family?('#{sanitized_platform(plat.value)}')"
|
94
|
+
corrector.replace(node.loc.expression, corrected_string)
|
95
|
+
end
|
96
|
+
|
97
|
+
os_include?(node) do |plats|
|
98
|
+
platforms = plats.values.map { |x| x.str_type? ? "'#{sanitized_platform(x.value)}'" : x.source }
|
99
|
+
corrected_string = "platform_family?(#{platforms.join(', ')})"
|
100
|
+
corrector.replace(node.loc.expression, corrected_string)
|
101
|
+
end
|
102
|
+
|
103
|
+
os_eql?(node) do |plat|
|
104
|
+
corrected_string = "platform_family?('#{sanitized_platform(plat.value)}')"
|
105
|
+
corrector.replace(node.loc.expression, corrected_string)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -28,6 +28,7 @@ module RuboCop
|
|
28
28
|
# node['platform'] != 'ubuntu'
|
29
29
|
# node['platform_family'] != 'debian'
|
30
30
|
# %w(rhel suse).include?(node['platform_family'])
|
31
|
+
# node['platform'].eql?('ubuntu')
|
31
32
|
#
|
32
33
|
# # good
|
33
34
|
# platform?('ubuntu')
|
@@ -47,6 +48,10 @@ module RuboCop
|
|
47
48
|
(send $(array ...) :include? (send (send nil? :node) :[] $(str {"platform" "platform_family"})))
|
48
49
|
PATTERN
|
49
50
|
|
51
|
+
def_node_matcher :platform_eql?, <<-PATTERN
|
52
|
+
(send (send (send nil? :node) :[] $(str {"platform" "platform_family"}) ) :eql? $str )
|
53
|
+
PATTERN
|
54
|
+
|
50
55
|
def on_send(node)
|
51
56
|
platform_equals?(node) do
|
52
57
|
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
@@ -55,6 +60,10 @@ module RuboCop
|
|
55
60
|
platform_include?(node) do
|
56
61
|
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
57
62
|
end
|
63
|
+
|
64
|
+
platform_eql?(node) do
|
65
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
66
|
+
end
|
58
67
|
end
|
59
68
|
|
60
69
|
def autocorrect(node)
|
@@ -70,6 +79,11 @@ module RuboCop
|
|
70
79
|
corrected_string = "#{type.value}?(#{platforms.join(', ')})"
|
71
80
|
corrector.replace(node.loc.expression, corrected_string)
|
72
81
|
end
|
82
|
+
|
83
|
+
platform_eql?(node) do |type, plat|
|
84
|
+
corrected_string = "#{type.value}?('#{plat.value}')"
|
85
|
+
corrector.replace(node.loc.expression, corrected_string)
|
86
|
+
end
|
73
87
|
end
|
74
88
|
end
|
75
89
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cookstyle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.21.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thom May
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-
|
12
|
+
date: 2020-02-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rubocop
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- lib/cookstyle.rb
|
46
46
|
- lib/cookstyle/version.rb
|
47
47
|
- lib/rubocop/chef.rb
|
48
|
+
- lib/rubocop/chef/autocorrect_helpers.rb
|
48
49
|
- lib/rubocop/chef/cookbook_helpers.rb
|
49
50
|
- lib/rubocop/chef/cookbook_only.rb
|
50
51
|
- lib/rubocop/chef/platform_helpers.rb
|
@@ -167,6 +168,7 @@ files:
|
|
167
168
|
- lib/rubocop/cop/chef/modernize/sc_windows_resource.rb
|
168
169
|
- lib/rubocop/cop/chef/modernize/seven_zip_archive.rb
|
169
170
|
- lib/rubocop/cop/chef/modernize/shellouts_to_chocolatey.rb
|
171
|
+
- lib/rubocop/cop/chef/modernize/simplify_apt_ppa_setup.rb
|
170
172
|
- lib/rubocop/cop/chef/modernize/systctl_param_resource.rb
|
171
173
|
- lib/rubocop/cop/chef/modernize/unnecessary_mixlib_shellout_require.rb
|
172
174
|
- lib/rubocop/cop/chef/modernize/whyrun_supported_true.rb
|
@@ -183,12 +185,14 @@ files:
|
|
183
185
|
- lib/rubocop/cop/chef/redundant/grouping_metadata.rb
|
184
186
|
- lib/rubocop/cop/chef/redundant/long_description_metadata.rb
|
185
187
|
- lib/rubocop/cop/chef/redundant/name_property_and_required.rb
|
188
|
+
- lib/rubocop/cop/chef/redundant/property_splat_regex.rb
|
186
189
|
- lib/rubocop/cop/chef/redundant/property_with_default_and_required.rb
|
187
190
|
- lib/rubocop/cop/chef/redundant/provides_metadata.rb
|
188
191
|
- lib/rubocop/cop/chef/redundant/recipe_metadata.rb
|
189
192
|
- lib/rubocop/cop/chef/redundant/replaces_metadata.rb
|
190
193
|
- lib/rubocop/cop/chef/redundant/resource_with_nothing_action.rb
|
191
194
|
- lib/rubocop/cop/chef/redundant/sensitive_property_in_resource.rb
|
195
|
+
- lib/rubocop/cop/chef/redundant/string_property_with_nil_default.rb
|
192
196
|
- lib/rubocop/cop/chef/redundant/suggests_metadata.rb
|
193
197
|
- lib/rubocop/cop/chef/redundant/unnecessary_desired_state.rb
|
194
198
|
- lib/rubocop/cop/chef/redundant/unnecessary_name_property.rb
|
@@ -207,6 +211,7 @@ files:
|
|
207
211
|
- lib/rubocop/cop/chef/style/overly_complex_supports_depends_metadata.rb
|
208
212
|
- lib/rubocop/cop/chef/style/simplify_platform_major_version_check.rb
|
209
213
|
- lib/rubocop/cop/chef/style/true_false_resource_properties.rb
|
214
|
+
- lib/rubocop/cop/chef/style/unnecessary_os_check.rb
|
210
215
|
- lib/rubocop/cop/chef/style/unnecessary_platform_case_statement.rb
|
211
216
|
- lib/rubocop/cop/chef/style/use_platform_helpers.rb
|
212
217
|
- lib/rubocop/cop/target_chef_version.rb
|