cookstyle 5.1.19 → 5.2.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
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
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ # metadata.rb supports methods should contain valid platforms
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # supports 'darwin'
27
+ # supports 'mswin'
28
+ #
29
+ #
30
+ # # good
31
+ # supports 'mac_os_x'
32
+ # supports 'windows'
33
+ #
34
+
35
+ class InvalidPlatformMetadata < Cop
36
+ COMMON_TYPOS = {
37
+ "aws": nil,
38
+ "archlinux": 'arch',
39
+ "amazonlinux": 'amazon',
40
+ "darwin": 'mac_os_x',
41
+ "debuan": nil,
42
+ "mingw32": 'windows',
43
+ "mswin": 'windows',
44
+ "macos": 'mac_os_x',
45
+ "macosx": 'mac_os_x',
46
+ "mac_os_x_server": 'mac_os_x',
47
+ "mint": 'linuxmint',
48
+ "linux": nil,
49
+ "oel": 'oracle',
50
+ "oraclelinux": 'oracle',
51
+ "rhel": 'redhat',
52
+ "schientific": 'scientific',
53
+ "scientificlinux": 'scientific',
54
+ "sles": 'suse',
55
+ "solaris": 'solaris2',
56
+ "ubundu": 'ubuntu',
57
+ "ubunth": 'ubuntu',
58
+ "ubunutu": 'ubuntu',
59
+ "windwos": 'windows',
60
+ "xcp": nil,
61
+ }.freeze
62
+
63
+ MSG = 'metadata.rb "supports" platform is invalid'.freeze
64
+
65
+ def_node_matcher :supports?, '(send nil? :supports $str ...)'
66
+
67
+ def on_send(node)
68
+ supports?(node) do |plat|
69
+ if COMMON_TYPOS[plat.str_content.to_sym]
70
+ add_offense(plat, location: :expression, message: MSG, severity: :refactor)
71
+ end
72
+ end
73
+ end
74
+
75
+ def autocorrect(node)
76
+ correct_string = autocorrect_license_string(node.str_content)
77
+ if correct_string
78
+ lambda do |corrector|
79
+ corrector.replace(node.loc.expression, "'#{correct_string}'")
80
+ end
81
+ end
82
+ end
83
+
84
+ # private
85
+
86
+ def autocorrect_license_string(bad_string)
87
+ COMMON_TYPOS[bad_string.delete(',').downcase.to_sym]
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,46 @@
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
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ # metadata.rb needs to include the name method
22
+ #
23
+ # @example
24
+ #
25
+ # # good
26
+ # name 'foo'
27
+ #
28
+ class MetadataMissingName < Cop
29
+ include RangeHelp
30
+
31
+ MSG = 'metadata.rb needs to include the name method'.freeze
32
+
33
+ def investigate(processed_source)
34
+ return if processed_source.blank?
35
+
36
+ # Using range similar to RuboCop::Cop::Naming::Filename (file_name.rb)
37
+ range = source_range(processed_source.buffer, 1, 0)
38
+
39
+ add_offense(nil, location: range, message: MSG, severity: :refactor) unless cb_name(processed_source.ast).any?
40
+ end
41
+
42
+ def_node_search :cb_name, '(send nil? :name str ...)'
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,44 @@
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
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ # The long_description metadata.rb method is not used and is unnecessary in cookbooks
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # long_description 'this is my cookbook and this description will never be seen'
27
+ #
28
+
29
+ class LongDescriptionMetadata < Cop
30
+ MSG = 'The long_description metadata.rb method is not used and is unnecessary in cookbooks'.freeze
31
+
32
+ def on_send(node)
33
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.method_name == :long_description
34
+ end
35
+
36
+ def autocorrect(node)
37
+ lambda do |corrector|
38
+ corrector.remove(node.loc.expression)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright 2019, Chef Software Inc.
2
+ # Copyright:: Copyright 2019-2019, Chef Software Inc.
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -33,18 +33,18 @@ module RuboCop
33
33
  MSG = 'Do not use node.set. Replace with node.normal to keep identical behavior.'.freeze
34
34
 
35
35
  def_node_matcher :node_set?, <<-PATTERN
36
- (send (send _ :node) :set)
36
+ (send (send _ :node) $:set)
37
37
  PATTERN
38
38
 
39
39
  def on_send(node)
40
40
  node_set?(node) do
41
- add_offense(node, location: :expression, message: MSG, severity: :refactor)
41
+ add_offense(node, location: :selector, message: MSG, severity: :refactor)
42
42
  end
43
43
  end
44
44
 
45
45
  def autocorrect(node)
46
46
  lambda do |corrector|
47
- corrector.replace(node.loc.expression, 'node.normal')
47
+ corrector.replace(node.loc.selector, 'normal')
48
48
  end
49
49
  end
50
50
  end
@@ -33,18 +33,18 @@ module RuboCop
33
33
  MSG = 'Do not use node.set_unless. Replace with node.normal_unless to keep identical behavior.'.freeze
34
34
 
35
35
  def_node_matcher :node_set_unless?, <<-PATTERN
36
- (send (send _ :node) :set_unless)
36
+ (send (send _ :node) $:set_unless)
37
37
  PATTERN
38
38
 
39
39
  def on_send(node)
40
40
  node_set_unless?(node) do
41
- add_offense(node, location: :expression, message: MSG, severity: :refactor)
41
+ add_offense(node, location: :selector, message: MSG, severity: :refactor)
42
42
  end
43
43
  end
44
44
 
45
45
  def autocorrect(node)
46
46
  lambda do |corrector|
47
- corrector.replace(node.loc.expression, 'node.normal_unless')
47
+ corrector.replace(node.loc.selector, 'normal_unless')
48
48
  end
49
49
  end
50
50
  end
@@ -0,0 +1,50 @@
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
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ # Make sure to use include_recipe instead of require_recipe
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # require_recipe 'foo'
27
+ #
28
+ # # good
29
+ # include_recipe 'foo'
30
+ #
31
+ class RequireRecipe < Cop
32
+ MSG = 'Use include_recipe instead of the require_recipe method'.freeze
33
+
34
+ def_node_matcher :require_recipe?, <<-PATTERN
35
+ (send nil? :require_recipe $str)
36
+ PATTERN
37
+
38
+ def on_send(node)
39
+ require_recipe?(node) { add_offense(node, location: :selector, message: MSG, severity: :refactor) }
40
+ end
41
+
42
+ def autocorrect(node)
43
+ lambda do |corrector|
44
+ corrector.replace(node.loc.selector, 'include_recipe')
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,53 @@
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
+ # It is not longer necessary respond_to?(:foo) in metadata. This was used to support new metadata
21
+ # methods in Chef 11 and early versions of Chef 12.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # chef_version '>= 13' if respond_to?(:chef_version)
27
+ #
28
+ # # good
29
+ # chef_version '>= 13'
30
+ #
31
+
32
+ class RespondToInMetadata < Cop
33
+ MSG = 'It is no longer necessary to use respond_to? in metadata.rb in Chef 12.15 and later'.freeze
34
+
35
+ def on_if(node)
36
+ if_respond_to?(node) do
37
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
38
+ end
39
+ end
40
+
41
+ def_node_matcher :if_respond_to?, <<~PATTERN
42
+ (if (send nil? :respond_to? _ ) ... )
43
+ PATTERN
44
+
45
+ def autocorrect(node)
46
+ lambda do |corrector|
47
+ corrector.replace(node.loc.expression, node.children[1].source)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -29,7 +29,7 @@ module RuboCop
29
29
  # # good
30
30
  # build_essential 'install compilation tools'
31
31
  class UseBuildEssentialResource < Cop
32
- MSG = 'Use the build_essential resource built into Chef 14+ instead of the legacy build-essential recipe'.freeze
32
+ MSG = 'Use the build_essential resource instead of the legacy build-essential recipe. This resource ships in the build-essential cookbook v5.0+ and is built into Chef Infra Client 14+'.freeze
33
33
 
34
34
  def_node_matcher :build_essential_recipe_usage?, <<-PATTERN
35
35
  (send nil? :include_recipe (str {"build-essential" "build-essential::default"}))
@@ -0,0 +1,50 @@
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
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ # set_or_return within a method should not be used to define property in a resource. Instead use
22
+ # the property method which properly validates and defines properties in a way that works with
23
+ # reporting and documentation functionality in Chef Infra Client
24
+ #
25
+ # @example
26
+ #
27
+ # # bad
28
+ # def severity(arg = nil)
29
+ # set_or_return(
30
+ # :severity, arg,
31
+ # :kind_of => String,
32
+ # :default => nil
33
+ # )
34
+ # end
35
+ #
36
+ # # good
37
+ # property :severity, String
38
+ #
39
+ class SetOrReturnInResources < Cop
40
+ MSG = 'Do not use set_or_return within a method to define a property for a resource. Use the property method instead, which supports validation, reporting, and documentation functionality'.freeze
41
+
42
+ def on_send(node)
43
+ if node.method_name == :set_or_return
44
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,75 @@
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
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ # In HWRPs and LWRPs it was necessary to define the allowed actions within the resource.
22
+ # In custom resources this is no longer necessary as Chef will determine it based on the
23
+ # actions defined in the resource.
24
+ #
25
+ # @example
26
+ #
27
+ # # bad
28
+ # property :something, String
29
+ #
30
+ # allowed_actions [:create, :remove]
31
+ # action :create do
32
+ # # some action code because we're in a custom resource
33
+ # end
34
+ #
35
+ # # also bad
36
+ # property :something, String
37
+ #
38
+ # actions [:create, :remove]
39
+ # action :create do
40
+ # # some action code because we're in a custom resource
41
+ # end
42
+ #
43
+ # # good
44
+ # property :something, String
45
+ #
46
+ # action :create do
47
+ # # some action code because we're in a custom resource
48
+ # end
49
+ #
50
+ class CustomResourceWithAllowedActions < Cop
51
+ MSG = "Custom Resources don't need to define the allowed actions with allowed_actions or actions methods".freeze
52
+
53
+ def_node_matcher :allowed_actions?, <<-PATTERN
54
+ (send nil? {:allowed_actions :actions} ... )
55
+ PATTERN
56
+
57
+ def_node_search :resource_actions?, <<-PATTERN
58
+ (block (send nil? :action ... ) ... )
59
+ PATTERN
60
+
61
+ def on_send(node)
62
+ allowed_actions?(node) do
63
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if resource_actions?(processed_source.ast)
64
+ end
65
+ end
66
+
67
+ def autocorrect(node)
68
+ lambda do |corrector|
69
+ corrector.remove(node.loc.expression)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end