cookstyle 5.17.4 → 5.18.4
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 +22 -4
- data/lib/cookstyle/version.rb +1 -1
- data/lib/rubocop/cop/chef/modernize/compile_time_resources.rb +51 -0
- data/lib/rubocop/cop/chef/modernize/execute_sysctl.rb +73 -0
- data/lib/rubocop/cop/chef/redundant/name_property_and_required.rb +9 -26
- data/lib/rubocop/cop/chef/redundant/property_with_default_and_required.rb +17 -23
- data/lib/rubocop/cop/chef/style/true_false_resource_properties.rb +17 -6
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56fb1dda6ccaeeb4a3f521c52407e12becf9f1b1625a89b4c893f415820e40c4
|
4
|
+
data.tar.gz: 46c9bf893653173d77d7282056dc81cb385ef2b6f7828cc5f76e0e4e8e51a3f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3e1c5acbb968e895c0a8190c28635baa023574e797d0b56ba54bba776f97f24b7d0f718bb02b92080175f46f475500a02254db3bd6f92090fd89f5b2239ec4e
|
7
|
+
data.tar.gz: 44d2c68635670c76ac81c4941dd70a3fd99fa53cdf40654576584784fd88b256f6c94066716cdff8c8cb7b63b29f68b17524b654e7de4cba514d89803993941b
|
data/config/cookstyle.yml
CHANGED
@@ -2,8 +2,8 @@ AllCops:
|
|
2
2
|
TargetRubyVersion: 2.4
|
3
3
|
TargetChefVersion: ~
|
4
4
|
Exclude:
|
5
|
-
- files/**/*
|
6
|
-
- vendor/**/*
|
5
|
+
- '**/files/**/*'
|
6
|
+
- '**/vendor/**/*'
|
7
7
|
- Guardfile
|
8
8
|
ChefAttributes:
|
9
9
|
Patterns:
|
@@ -1064,8 +1064,26 @@ ChefModernize/DslIncludeInResource:
|
|
1064
1064
|
- '**/resources/*.rb'
|
1065
1065
|
- '**/providers/*.rb'
|
1066
1066
|
|
1067
|
+
ChefModernize/ResourceForcingCompileTime:
|
1068
|
+
Description: The hostname, build_essential, chef_gem, and ohai_hint resources include 'compile_time' properties, which should be used to force the resources to run at compile time by setting `compile_time true`.
|
1069
|
+
Enabled: true
|
1070
|
+
VersionAdded: '5.18.0'
|
1071
|
+
Exclude:
|
1072
|
+
- '**/metadata.rb'
|
1073
|
+
- '**/attributes/*.rb'
|
1074
|
+
- '**/Berksfile'
|
1075
|
+
|
1076
|
+
ChefModernize/ExecuteSysctl:
|
1077
|
+
Description: Chef Infra Client 14.0 and later includes a sysctl resource that should be used to idempotently load sysctl values instead of templating files and using execute to load them.
|
1078
|
+
Enabled: true
|
1079
|
+
VersionAdded: '5.18.0'
|
1080
|
+
Exclude:
|
1081
|
+
- '**/metadata.rb'
|
1082
|
+
- '**/attributes/*.rb'
|
1083
|
+
- '**/Berksfile'
|
1084
|
+
|
1067
1085
|
###############################
|
1068
|
-
# ChefRedundantCode: Cleanup
|
1086
|
+
# ChefRedundantCode: Cleanup unnecessary code in your cookbooks regardless of Chef Infra Client release
|
1069
1087
|
###############################
|
1070
1088
|
|
1071
1089
|
ChefRedundantCode/ConflictsMetadata:
|
@@ -1892,7 +1910,7 @@ Lint/BigDecimalNew:
|
|
1892
1910
|
Lint/UnneededCopEnableDirective:
|
1893
1911
|
Enabled: true
|
1894
1912
|
|
1895
|
-
# get people on a much simpler ruby 2.4 way of doing things
|
1913
|
+
# get people on a much simpler ruby 2.4+ way of doing things
|
1896
1914
|
Style/UnpackFirst:
|
1897
1915
|
Enabled: true
|
1898
1916
|
|
data/lib/cookstyle/version.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
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 ChefModernize
|
21
|
+
# The hostname, build_essential, chef_gem, and ohai_hint resources include 'compile_time' properties, which should be used to force the resources to run at compile time by setting `compile_time true`.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# build_essential 'install build tools' do
|
27
|
+
# action :nothing
|
28
|
+
# end.run_action(:install)
|
29
|
+
#
|
30
|
+
# # good
|
31
|
+
# build_essential 'install build tools' do
|
32
|
+
# compile_time true
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
class ResourceForcingCompileTime < Cop
|
36
|
+
MSG = "Set 'compile_time true' in resources when available instead of forcing resources to run at compile time by setting an action on the block.".freeze
|
37
|
+
|
38
|
+
def_node_matcher :compile_time_resource?, <<-PATTERN
|
39
|
+
(send (block (send nil? {:build_essential :chef_gem :hostname :ohai_hint} (...)) (args) (...)) $:run_action (sym ...))
|
40
|
+
PATTERN
|
41
|
+
|
42
|
+
def on_send(node)
|
43
|
+
compile_time_resource?(node) do
|
44
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,73 @@
|
|
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 ChefModernize
|
21
|
+
# Chef Infra Client 14.0 and later includes a sysctl resource that should be used to idempotently load sysctl values instead of templating files and using execute to load them.
|
22
|
+
#
|
23
|
+
# # bad
|
24
|
+
# file '/etc/sysctl.d/ipv4.conf' do
|
25
|
+
# owner 'root'
|
26
|
+
# group 'root'
|
27
|
+
# mode '0755'
|
28
|
+
# notifies :run, 'service[sysctl -p /etc/sysctl.d/ipv4.conf]', :immediately
|
29
|
+
# content '9000 65500'
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# execute 'sysctl -p /etc/sysctl.d/ipv4.conf' do
|
33
|
+
# action :nothing
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# # good
|
37
|
+
# sysctl 'net.ipv4.ip_local_port_range' do
|
38
|
+
# value '9000 65500'
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
class ExecuteSysctl < Cop
|
42
|
+
include RuboCop::Chef::CookbookHelpers
|
43
|
+
extend TargetChefVersion
|
44
|
+
|
45
|
+
minimum_target_chef_version '14.0'
|
46
|
+
|
47
|
+
MSG = 'Chef Infra Client 14.0 and later includes a sysctl resource that should be used to idempotently load sysctl values instead of templating files and using execute to load them.'.freeze
|
48
|
+
|
49
|
+
# non block execute resources
|
50
|
+
def on_send(node)
|
51
|
+
return unless node.method_name == :execute
|
52
|
+
|
53
|
+
# use a regex on source instead of .value in case there's string interpolation which adds a complex dstr type
|
54
|
+
# with a nested string and a begin. Source allows us to avoid a lot of defensive programming here
|
55
|
+
if node&.arguments.first&.source&.match?(/^("|')sysctl -p/)
|
56
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# block execute resources
|
61
|
+
def on_block(node)
|
62
|
+
match_property_in_resource?(:execute, 'command', node) do |code_property|
|
63
|
+
property_data = method_arg_ast_to_string(code_property)
|
64
|
+
if property_data && property_data.match?(/^sysctl -p/i)
|
65
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# Copyright:: Copyright 2019, Chef Software Inc.
|
2
|
+
# Copyright:: Copyright 2019-2020, Chef Software Inc.
|
3
3
|
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
4
|
#
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -61,35 +61,18 @@ module RuboCop
|
|
61
61
|
class NamePropertyIsRequired < Cop
|
62
62
|
MSG = 'Resource properties marked as name properties should not also be required properties'.freeze
|
63
63
|
|
64
|
+
# match on a property or attribute that has any name and any type and a hash that
|
65
|
+
# contains name_property: true and required: true. These are wrapped in <> which means
|
66
|
+
# the order doesn't matter in the hash.
|
67
|
+
def_node_matcher :name_property_and_required?, <<-PATTERN
|
68
|
+
(send nil? {:property :attribute} (sym _) ... (hash <(pair (sym {:name_property :name_attribute}) (true)) $(pair (sym :required) (true) ) ...>))
|
69
|
+
PATTERN
|
70
|
+
|
64
71
|
def on_send(node)
|
65
|
-
|
72
|
+
name_property_and_required?(node) do
|
66
73
|
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
67
74
|
end
|
68
75
|
end
|
69
|
-
|
70
|
-
private
|
71
|
-
|
72
|
-
def property_is_name_property?(node)
|
73
|
-
if %i(property attribute).include?(node.method_name)
|
74
|
-
node.arguments.each do |arg|
|
75
|
-
if arg.type == :hash
|
76
|
-
return true if arg.source.match?(/name_(property|attribute):\s*true/)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
false # no required: true found
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def required_property?(node)
|
84
|
-
if %i(property attribute).include?(node.method_name)
|
85
|
-
node.arguments.each do |arg|
|
86
|
-
if arg.type == :hash
|
87
|
-
return true if arg.source.match?(/required:\s*true/)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
false # no default: found
|
91
|
-
end
|
92
|
-
end
|
93
76
|
end
|
94
77
|
end
|
95
78
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# Copyright:: Copyright 2019, Chef Software Inc.
|
2
|
+
# Copyright:: Copyright 2019-2020, Chef Software Inc.
|
3
3
|
# Author:: Tim Smith (<tsmith@chef.io>)
|
4
4
|
#
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -32,35 +32,29 @@ module RuboCop
|
|
32
32
|
# property :bob, String, required: true
|
33
33
|
#
|
34
34
|
class PropertyWithRequiredAndDefault < Cop
|
35
|
-
|
35
|
+
include RangeHelp
|
36
36
|
|
37
|
-
|
38
|
-
if required_property?(node) && property_has_default?(node)
|
39
|
-
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
40
|
-
end
|
41
|
-
end
|
37
|
+
MSG = 'Resource properties should not be both required and have a default value. This will fail on Chef Infra Client 13+'.freeze
|
42
38
|
|
43
|
-
|
39
|
+
# match on a property or attribute that has any name and any type and a hash that
|
40
|
+
# contains default: true and required: true. These are wrapped in <> which means
|
41
|
+
# the order doesn't matter in the hash.
|
42
|
+
def_node_matcher :required_and_default?, <<-PATTERN
|
43
|
+
(send nil? {:property :attribute} (sym _) ... (hash <$(pair (sym :default) ...) (pair (sym :required) true) ...>))
|
44
|
+
PATTERN
|
44
45
|
|
45
|
-
def
|
46
|
-
|
47
|
-
node
|
48
|
-
if arg.type == :hash
|
49
|
-
return true if arg.source.match?(/required:\s*true/)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
false # no required: true found
|
46
|
+
def on_send(node)
|
47
|
+
required_and_default?(node) do
|
48
|
+
add_offense(node, location: :expression, message: MSG, severity: :refactor)
|
53
49
|
end
|
54
50
|
end
|
55
51
|
|
56
|
-
def
|
57
|
-
|
58
|
-
node
|
59
|
-
|
60
|
-
|
61
|
-
end
|
52
|
+
def autocorrect(node)
|
53
|
+
lambda do |corrector|
|
54
|
+
required_and_default?(node) do |default|
|
55
|
+
range = range_with_surrounding_comma(range_with_surrounding_space(range: default.loc.expression, side: :left), :left)
|
56
|
+
corrector.remove(range)
|
62
57
|
end
|
63
|
-
false # no default: found
|
64
58
|
end
|
65
59
|
end
|
66
60
|
end
|
@@ -24,28 +24,39 @@ module RuboCop
|
|
24
24
|
#
|
25
25
|
# # bad
|
26
26
|
# property :foo, [TrueClass, FalseClass]
|
27
|
+
# attribute :foo, kind_of: [TrueClass, FalseClass]
|
27
28
|
#
|
28
29
|
# # good
|
29
30
|
# property :foo, [true, false]
|
31
|
+
# attribute :foo, kind_of: [true, false]
|
30
32
|
#
|
31
33
|
class TrueClassFalseClassResourceProperties < Cop
|
32
34
|
MSG = "When setting the allowed types for a resource to accept either true or false values it's much simpler to use true and false instead of TrueClass and FalseClass.".freeze
|
33
35
|
|
36
|
+
def_node_matcher :trueclass_falseclass_array?, <<-PATTERN
|
37
|
+
(array (const nil? :TrueClass) (const nil? :FalseClass))
|
38
|
+
PATTERN
|
39
|
+
|
40
|
+
def_node_matcher :tf_in_kind_of_hash?, <<-PATTERN
|
41
|
+
(hash (pair (sym :kind_of) #trueclass_falseclass_array? ))
|
42
|
+
PATTERN
|
43
|
+
|
34
44
|
def_node_matcher :trueclass_falseclass_property?, <<-PATTERN
|
35
|
-
(send nil? {:property :attribute} (sym _)
|
45
|
+
(send nil? {:property :attribute} (sym _)
|
46
|
+
|
47
|
+
${#tf_in_kind_of_hash? #trueclass_falseclass_array? } ... )
|
36
48
|
PATTERN
|
37
49
|
|
38
50
|
def on_send(node)
|
39
|
-
trueclass_falseclass_property?(node) do
|
40
|
-
add_offense(
|
51
|
+
trueclass_falseclass_property?(node) do |tf_match|
|
52
|
+
add_offense(tf_match, location: :expression, message: MSG, severity: :refactor)
|
41
53
|
end
|
42
54
|
end
|
43
55
|
|
44
56
|
def autocorrect(node)
|
45
57
|
lambda do |corrector|
|
46
|
-
|
47
|
-
|
48
|
-
end
|
58
|
+
replacement_text = node.hash_type? ? 'kind_of: [true, false]' : '[true, false]'
|
59
|
+
corrector.replace(node.loc.expression, replacement_text)
|
49
60
|
end
|
50
61
|
end
|
51
62
|
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.18.4
|
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:
|
12
|
+
date: 2020-01-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rubocop
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- lib/rubocop/cop/chef/modernize/build_essential.rb
|
130
130
|
- lib/rubocop/cop/chef/modernize/chef_14_resources.rb
|
131
131
|
- lib/rubocop/cop/chef/modernize/chef_gem_nokogiri.rb
|
132
|
+
- lib/rubocop/cop/chef/modernize/compile_time_resources.rb
|
132
133
|
- lib/rubocop/cop/chef/modernize/cron_manage_resource.rb
|
133
134
|
- lib/rubocop/cop/chef/modernize/default_action_initializer.rb
|
134
135
|
- lib/rubocop/cop/chef/modernize/defines_chefspec_matchers.rb
|
@@ -139,6 +140,7 @@ files:
|
|
139
140
|
- lib/rubocop/cop/chef/modernize/execute_apt_update.rb
|
140
141
|
- lib/rubocop/cop/chef/modernize/execute_sc_exe.rb
|
141
142
|
- lib/rubocop/cop/chef/modernize/execute_sleep.rb
|
143
|
+
- lib/rubocop/cop/chef/modernize/execute_sysctl.rb
|
142
144
|
- lib/rubocop/cop/chef/modernize/execute_tzutil.rb
|
143
145
|
- lib/rubocop/cop/chef/modernize/foodcritic_comments.rb
|
144
146
|
- lib/rubocop/cop/chef/modernize/if_provides_default_action.rb
|