cookstyle 5.14.1 → 5.15.7
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 +191 -120
- data/cookstyle.gemspec +8 -1
- data/lib/cookstyle.rb +1 -0
- data/lib/cookstyle/version.rb +1 -1
- data/lib/rubocop/chef/cookbook_helpers.rb +16 -0
- data/lib/rubocop/chef/platform_helpers.rb +70 -0
- data/lib/rubocop/cop/chef/correctness/invalid_platform_family_helper.rb +53 -0
- data/lib/rubocop/cop/chef/correctness/invalid_platform_helper.rb +54 -0
- data/lib/rubocop/cop/chef/correctness/invalid_platform_metadata.rb +5 -30
- data/lib/rubocop/cop/chef/correctness/invalid_value_for_platform_family_helper.rb +67 -0
- data/lib/rubocop/cop/chef/correctness/invalid_value_for_platform_helper.rb +66 -0
- data/lib/rubocop/cop/chef/correctness/scoped_file_exist.rb +53 -0
- data/lib/rubocop/cop/chef/modernize/allowed_actions_initializer.rb +76 -0
- data/lib/rubocop/cop/chef/modernize/berksfile_source.rb +9 -0
- data/lib/rubocop/cop/chef/modernize/default_action_initializer.rb +7 -1
- data/lib/rubocop/cop/chef/{correctness → modernize}/ohai_default_recipe.rb +1 -1
- data/lib/rubocop/cop/chef/{correctness → modernize}/property_with_name_attribute.rb +2 -3
- data/lib/rubocop/cop/chef/modernize/respond_to_resource_name.rb +1 -3
- data/lib/rubocop/cop/chef/modernize/unnecessary_mixlib_shellout_require.rb +2 -2
- data/lib/rubocop/cop/chef/{deprecation → redundant}/attribute_metadata.rb +3 -3
- data/lib/rubocop/cop/chef/{deprecation → redundant}/conflicts_metadata.rb +3 -3
- data/lib/rubocop/cop/chef/{modernize/resource_with_allowed_actions.rb → redundant/custom_resource_with_allowed_actions.rb} +13 -29
- data/lib/rubocop/cop/chef/{deprecation → redundant}/long_description_metadata.rb +3 -3
- data/lib/rubocop/cop/chef/{correctness → redundant}/name_property_and_required.rb +1 -1
- data/lib/rubocop/cop/chef/{correctness → redundant}/property_with_default_and_required.rb +1 -1
- data/lib/rubocop/cop/chef/{deprecation → redundant}/provides_metadata.rb +3 -3
- data/lib/rubocop/cop/chef/{deprecation → redundant}/recipe_metadata.rb +3 -4
- data/lib/rubocop/cop/chef/{deprecation → redundant}/replaces_metadata.rb +3 -3
- data/lib/rubocop/cop/chef/{correctness → redundant}/resource_with_nothing_action.rb +1 -1
- data/lib/rubocop/cop/chef/{deprecation → redundant}/suggests_metadata.rb +3 -3
- data/lib/rubocop/cop/chef/{correctness → redundant}/unnecessary_name_property.rb +1 -1
- data/lib/rubocop/cop/chef/{correctness → sharing}/default_maintainer_metadata.rb +1 -1
- data/lib/rubocop/cop/chef/{correctness → sharing}/empty_metadata_field.rb +1 -1
- data/lib/rubocop/cop/chef/{correctness → sharing}/insecure_cookbook_url.rb +1 -1
- data/lib/rubocop/cop/chef/{correctness → sharing}/invalid_license_string.rb +1 -1
- metadata +34 -22
@@ -0,0 +1,70 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright 2019, 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
|
+
# common helpers for Platforms in Chef Infra Cookbooks
|
20
|
+
module PlatformHelpers
|
21
|
+
# a mapping of invalid platform family values to valid platform family
|
22
|
+
INVALID_PLATFORM_FAMILIES = {
|
23
|
+
'archlinux' => 'arch',
|
24
|
+
'centos' => 'rhel',
|
25
|
+
'darwin' => 'mac_os_x',
|
26
|
+
'debuan' => 'debian',
|
27
|
+
'linux' => nil,
|
28
|
+
'mac_os_x_server' => 'mac_os_x',
|
29
|
+
'macos' => 'mac_os_x',
|
30
|
+
'macosx' => 'mac_os_x',
|
31
|
+
'mingw32' => 'windows',
|
32
|
+
'mswin' => 'windows',
|
33
|
+
'opensuse' => 'suse',
|
34
|
+
'opensuseleap' => 'suse',
|
35
|
+
'redhat' => 'rhel',
|
36
|
+
'scientific' => 'rhel',
|
37
|
+
'sles' => 'suse',
|
38
|
+
'ubuntu' => 'debian',
|
39
|
+
}.freeze
|
40
|
+
|
41
|
+
# a mapping of invalid platforms values to valid platforms
|
42
|
+
INVALID_PLATFORMS = {
|
43
|
+
'aws' => nil,
|
44
|
+
'archlinux' => 'arch',
|
45
|
+
'amazonlinux' => 'amazon',
|
46
|
+
'darwin' => 'mac_os_x',
|
47
|
+
'debuan' => 'debian',
|
48
|
+
'mingw32' => 'windows',
|
49
|
+
'mswin' => 'windows',
|
50
|
+
'macos' => 'mac_os_x',
|
51
|
+
'macosx' => 'mac_os_x',
|
52
|
+
'mac_os_x_server' => 'mac_os_x',
|
53
|
+
'mint' => 'linuxmint',
|
54
|
+
'linux' => nil,
|
55
|
+
'oel' => 'oracle',
|
56
|
+
'oraclelinux' => 'oracle',
|
57
|
+
'rhel' => 'redhat',
|
58
|
+
'schientific' => 'scientific',
|
59
|
+
'scientificlinux' => 'scientific',
|
60
|
+
'sles' => 'suse',
|
61
|
+
'solaris' => 'solaris2',
|
62
|
+
'ubundu' => 'ubuntu',
|
63
|
+
'ubunth' => 'ubuntu',
|
64
|
+
'ubunutu' => 'ubuntu',
|
65
|
+
'windwos' => 'windows',
|
66
|
+
'xcp' => nil,
|
67
|
+
}.freeze
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright 2019, 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 ChefCorrectness
|
21
|
+
# Pass valid platform families to the platform_family? helper.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# platform_family?('redhat')
|
27
|
+
# platform_family?('sles')
|
28
|
+
#
|
29
|
+
# # bad
|
30
|
+
# platform_family?('rhel')
|
31
|
+
# platform_family?('suse')
|
32
|
+
#
|
33
|
+
class InvalidPlatformFamilyHelper < Cop
|
34
|
+
include ::RuboCop::Chef::PlatformHelpers
|
35
|
+
|
36
|
+
MSG = 'Pass valid platform families to the platform_family? helper.'.freeze
|
37
|
+
|
38
|
+
def_node_matcher :platform_family_helper?, <<-PATTERN
|
39
|
+
(send nil? :platform_family? $str*)
|
40
|
+
PATTERN
|
41
|
+
|
42
|
+
def on_send(node)
|
43
|
+
platform_family_helper?(node) do |plat|
|
44
|
+
plat.to_a.each do |p|
|
45
|
+
add_offense(p, location: :expression, message: MSG, severity: :refactor) if INVALID_PLATFORM_FAMILIES.key?(p.value)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright 2019, 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 ChefCorrectness
|
21
|
+
# Pass valid platforms to the platform? helper.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# platform?('darwin')
|
27
|
+
# platform?('rhel)
|
28
|
+
# platform?('sles')
|
29
|
+
#
|
30
|
+
# # good
|
31
|
+
# platform?('mac_os_x')
|
32
|
+
# platform?('redhat)
|
33
|
+
# platform?('suse')
|
34
|
+
class InvalidPlatformHelper < Cop
|
35
|
+
include ::RuboCop::Chef::PlatformHelpers
|
36
|
+
|
37
|
+
MSG = 'Pass valid platforms to the platform? helper.'.freeze
|
38
|
+
|
39
|
+
def_node_matcher :platform_helper?, <<-PATTERN
|
40
|
+
(send nil? :platform? $str*)
|
41
|
+
PATTERN
|
42
|
+
|
43
|
+
def on_send(node)
|
44
|
+
platform_helper?(node) do |plat|
|
45
|
+
plat.to_a.each do |p|
|
46
|
+
add_offense(p, location: :expression, message: MSG, severity: :refactor) if INVALID_PLATFORMS.key?(p.value)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -32,32 +32,7 @@ module RuboCop
|
|
32
32
|
# supports 'windows'
|
33
33
|
#
|
34
34
|
class InvalidPlatformMetadata < Cop
|
35
|
-
|
36
|
-
"aws": nil,
|
37
|
-
"archlinux": 'arch',
|
38
|
-
"amazonlinux": 'amazon',
|
39
|
-
"darwin": 'mac_os_x',
|
40
|
-
"debuan": nil,
|
41
|
-
"mingw32": 'windows',
|
42
|
-
"mswin": 'windows',
|
43
|
-
"macos": 'mac_os_x',
|
44
|
-
"macosx": 'mac_os_x',
|
45
|
-
"mac_os_x_server": 'mac_os_x',
|
46
|
-
"mint": 'linuxmint',
|
47
|
-
"linux": nil,
|
48
|
-
"oel": 'oracle',
|
49
|
-
"oraclelinux": 'oracle',
|
50
|
-
"rhel": 'redhat',
|
51
|
-
"schientific": 'scientific',
|
52
|
-
"scientificlinux": 'scientific',
|
53
|
-
"sles": 'suse',
|
54
|
-
"solaris": 'solaris2',
|
55
|
-
"ubundu": 'ubuntu',
|
56
|
-
"ubunth": 'ubuntu',
|
57
|
-
"ubunutu": 'ubuntu',
|
58
|
-
"windwos": 'windows',
|
59
|
-
"xcp": nil,
|
60
|
-
}.freeze
|
35
|
+
include ::RuboCop::Chef::PlatformHelpers
|
61
36
|
|
62
37
|
MSG = 'metadata.rb "supports" platform is invalid'.freeze
|
63
38
|
|
@@ -65,14 +40,14 @@ module RuboCop
|
|
65
40
|
|
66
41
|
def on_send(node)
|
67
42
|
supports?(node) do |plat|
|
68
|
-
if
|
43
|
+
if INVALID_PLATFORMS[plat.str_content]
|
69
44
|
add_offense(plat, location: :expression, message: MSG, severity: :refactor)
|
70
45
|
end
|
71
46
|
end
|
72
47
|
end
|
73
48
|
|
74
49
|
def autocorrect(node)
|
75
|
-
correct_string =
|
50
|
+
correct_string = autocorrect_platform_string(node.str_content)
|
76
51
|
if correct_string
|
77
52
|
lambda do |corrector|
|
78
53
|
corrector.replace(node.loc.expression, "'#{correct_string}'")
|
@@ -82,8 +57,8 @@ module RuboCop
|
|
82
57
|
|
83
58
|
# private
|
84
59
|
|
85
|
-
def
|
86
|
-
|
60
|
+
def autocorrect_platform_string(bad_string)
|
61
|
+
INVALID_PLATFORMS[bad_string.delete(',').downcase]
|
87
62
|
end
|
88
63
|
end
|
89
64
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright 2019, 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 ChefCorrectness
|
21
|
+
# Pass valid platforms families to the value_for_platform_family helper.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# value_for_platform_family(
|
27
|
+
# %w(rhel sles) => 'foo',
|
28
|
+
# %w(mac) => 'foo'
|
29
|
+
# )
|
30
|
+
#
|
31
|
+
# # good
|
32
|
+
# value_for_platform_family(
|
33
|
+
# %w(rhel suse) => 'foo',
|
34
|
+
# %w(mac_os_x) => 'foo'
|
35
|
+
# )
|
36
|
+
#
|
37
|
+
class InvalidPlatformValueForPlatformFamilyHelper < Cop
|
38
|
+
include ::RuboCop::Chef::PlatformHelpers
|
39
|
+
|
40
|
+
MSG = 'Pass valid platform families to the value_for_platform_family helper.'.freeze
|
41
|
+
|
42
|
+
def_node_matcher :value_for_platform_family?, <<-PATTERN
|
43
|
+
(send nil? :value_for_platform_family
|
44
|
+
(hash
|
45
|
+
$...
|
46
|
+
)
|
47
|
+
)
|
48
|
+
PATTERN
|
49
|
+
|
50
|
+
def on_send(node)
|
51
|
+
value_for_platform_family?(node) do |plats|
|
52
|
+
plats.each do |p_hash|
|
53
|
+
if p_hash.key.array_type?
|
54
|
+
p_hash.key.values.each do |plat|
|
55
|
+
add_offense(plat, location: :expression, message: MSG, severity: :refactor) if INVALID_PLATFORM_FAMILIES.key?(plat.value)
|
56
|
+
end
|
57
|
+
elsif INVALID_PLATFORM_FAMILIES.key?(p_hash.key.value)
|
58
|
+
add_offense(p_hash.key, location: :expression, message: MSG, severity: :refactor)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright 2019, 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 ChefCorrectness
|
21
|
+
# Pass valid platforms to the value_for_platform helper.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# value_for_platform(
|
27
|
+
# %w(rhel mac_os_x_server) => { 'default' => 'foo' },
|
28
|
+
# %w(sles) => { 'default' => 'bar' }
|
29
|
+
# )
|
30
|
+
# # good
|
31
|
+
# value_for_platform(
|
32
|
+
# %w(redhat mac_os_x) => { 'default' => 'foo' },
|
33
|
+
# %w(opensuseleap) => { 'default' => 'bar' }
|
34
|
+
# )
|
35
|
+
#
|
36
|
+
class InvalidPlatformValueForPlatformHelper < Cop
|
37
|
+
include ::RuboCop::Chef::PlatformHelpers
|
38
|
+
|
39
|
+
MSG = 'Pass valid platforms to the value_for_platform helper.'.freeze
|
40
|
+
|
41
|
+
def_node_matcher :value_for_platform?, <<-PATTERN
|
42
|
+
(send nil? :value_for_platform
|
43
|
+
(hash
|
44
|
+
$...
|
45
|
+
)
|
46
|
+
)
|
47
|
+
PATTERN
|
48
|
+
|
49
|
+
def on_send(node)
|
50
|
+
value_for_platform?(node) do |plats|
|
51
|
+
plats.each do |p_hash|
|
52
|
+
if p_hash.key.array_type?
|
53
|
+
p_hash.key.values.each do |plat|
|
54
|
+
add_offense(plat, location: :expression, message: MSG, severity: :refactor) if INVALID_PLATFORMS.key?(plat.value)
|
55
|
+
end
|
56
|
+
elsif INVALID_PLATFORMS.key?(p_hash.key.value)
|
57
|
+
add_offense(p_hash.key, location: :expression, message: MSG, severity: :refactor)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright 2019, 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 ChefCorrectness
|
21
|
+
# Scope file exist to access the correct File class by using ::File.exist? not File.exist?.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# not_if { File.exist?('/etc/foo/bar') }
|
27
|
+
#
|
28
|
+
# # good
|
29
|
+
# not_if { ::File.exist?('/etc/foo/bar') }
|
30
|
+
#
|
31
|
+
class ScopedFileExist < Cop
|
32
|
+
MSG = 'Scope file exist to access the correct File class by using ::File.exist? not File.exist?.'.freeze
|
33
|
+
|
34
|
+
def_node_matcher :unscoped_file_exist?, <<-PATTERN
|
35
|
+
(block (send nil? {:not_if :only_if}) (args) (send $(const nil? :File) {:exist? :exists?} (...)))
|
36
|
+
PATTERN
|
37
|
+
|
38
|
+
def on_block(node)
|
39
|
+
unscoped_file_exist?(node) do |m|
|
40
|
+
add_offense(m, location: :expression, message: MSG, severity: :refactor)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def autocorrect(node)
|
45
|
+
lambda do |corrector|
|
46
|
+
corrector.replace(node.loc.expression, '::File')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: 2019, 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 ChefModernize
|
21
|
+
# The allowed actions can now be specified using the `allowed_actions` helper instead of using the @actions or @allowed_actions variables in the resource's initialize method. In general we recommend against writing HWRPs, but if HWRPs are necessary you should utilize as much of the resource DSL as possible.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# def initialize(*args)
|
27
|
+
# super
|
28
|
+
# @actions = [ :create, :add ]
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# # also bad
|
32
|
+
# def initialize(*args)
|
33
|
+
# super
|
34
|
+
# @allowed_actions = [ :create, :add ]
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# # good
|
38
|
+
# allowed_actions [ :create, :add ]
|
39
|
+
#
|
40
|
+
class AllowedActionsFromInitialize < Cop
|
41
|
+
include RangeHelp
|
42
|
+
|
43
|
+
MSG = 'The allowed actions of a resource can be set with the "allowed_actions" helper instead of using the initialize method.'.freeze
|
44
|
+
|
45
|
+
def on_def(node)
|
46
|
+
return unless node.method_name == :initialize
|
47
|
+
return if node.body.nil? # nil body is an empty initialize method
|
48
|
+
|
49
|
+
node.body.each_node do |x|
|
50
|
+
if x.assignment? && !x.node_parts.empty? && %i(@actions @allowed_actions).include?(x.node_parts.first)
|
51
|
+
add_offense(x, location: :expression, message: MSG, severity: :refactor)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def_node_search :action_methods?, '(send nil? {:actions :allowed_actions} ... )'
|
57
|
+
|
58
|
+
def_node_search :intialize_method, '(def :initialize ... )'
|
59
|
+
|
60
|
+
def autocorrect(node)
|
61
|
+
lambda do |corrector|
|
62
|
+
# insert the new allowed_actions call above the initialize method, but not if one already exists (this is sadly common)
|
63
|
+
unless action_methods?(processed_source.ast)
|
64
|
+
initialize_node = intialize_method(processed_source.ast).first
|
65
|
+
corrector.insert_before(initialize_node.source_range, "allowed_actions #{node.descendants.first.source}\n\n")
|
66
|
+
end
|
67
|
+
|
68
|
+
# remove the variable from the initialize method
|
69
|
+
corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|