cookstyle 8.3.0 → 8.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/config/cookstyle.yml +10 -0
- data/lib/cookstyle/version.rb +2 -2
- data/lib/rubocop/cop/chef/correctness/empty_resource_guard.rb +104 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a0f7601383deef0fffbf9851fbdf87fa42a6fe908145bc8ce6a8bbaab4bea86
|
4
|
+
data.tar.gz: cbe17c553d5dbf697b8039d2b3fb915481e9a6cc1d7470b8fcd8a7bf70edb583
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64c98d3b8afc0ab594dd2b605b0a51ec77aab8d98abf7b10fac83240c993000c25a63138c8e374fd7d0cabedcef627363e08d1ab1fc242f9b7ad50fb81187475
|
7
|
+
data.tar.gz: 699d25ff3e3f7f3879ec6ee32901c9a2de6658be54f9499487e77a9a35e87a17e7bdb94f9645f0bc2564b602066cc769e14f55147ddfe94e831f40d1f85802ea
|
data/config/cookstyle.yml
CHANGED
@@ -391,6 +391,16 @@ Chef/Correctness/DnfPackageAllowDowngrades:
|
|
391
391
|
- '**/metadata.rb'
|
392
392
|
- '**/Berksfile'
|
393
393
|
|
394
|
+
Chef/Correctness/EmptyResourceGuard:
|
395
|
+
Description: Resource guards (not_if/only_if) should not be empty strings as empty strings will always evaluate to true.
|
396
|
+
StyleGuide: 'chef_correctness_emptyresourceguard'
|
397
|
+
Enabled: true
|
398
|
+
VersionAdded: '8.4.0'
|
399
|
+
Exclude:
|
400
|
+
- '**/attributes/*.rb'
|
401
|
+
- '**/metadata.rb'
|
402
|
+
- '**/Berksfile'
|
403
|
+
|
394
404
|
Chef/Correctness/ChefApplicationFatal:
|
395
405
|
Description: Use raise to force Chef Infra Client to fail instead of using Chef::Application.fatal
|
396
406
|
StyleGuide: 'chef_correctness_chefapplicationfatal'
|
data/lib/cookstyle/version.rb
CHANGED
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# Copyright:: 2024, Chef Software Inc.
|
4
|
+
# Author:: Sumedha (<https://github.com/sumedha-lolur>)
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module RuboCop
|
19
|
+
module Cop
|
20
|
+
module Chef
|
21
|
+
module Correctness
|
22
|
+
# Resource guards (not_if/only_if) should not be empty strings as empty strings will always evaluate to true.
|
23
|
+
# This will cause confusion in your cookbooks as the guard will always pass.
|
24
|
+
#
|
25
|
+
# Empty strings in Ruby are "truthy", which means:
|
26
|
+
# - `only_if ''` will ALWAYS execute the resource (guard always passes)
|
27
|
+
# - `not_if ''` will NEVER execute the resource (guard always blocks)
|
28
|
+
#
|
29
|
+
# This behavior is usually unintended and can lead to resources running when they shouldn't
|
30
|
+
# or never running when they should.
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
#
|
34
|
+
# ### incorrect
|
35
|
+
# template '/etc/foo' do
|
36
|
+
# mode '0644'
|
37
|
+
# source 'foo.erb'
|
38
|
+
# only_if '' # This will always be true - resource always executes
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# cookbook_file '/logs/foo/error.log' do
|
42
|
+
# source 'error.log'
|
43
|
+
# not_if { '' } # This will always be true - resource never executes
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# service 'apache2' do
|
47
|
+
# action :restart
|
48
|
+
# only_if { '' } # Block form also problematic
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# ### correct
|
52
|
+
# template '/etc/foo' do
|
53
|
+
# mode '0644'
|
54
|
+
# source 'foo.erb'
|
55
|
+
# only_if 'test -f /etc/foo' # Actual shell command
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# cookbook_file '/logs/foo/error.log' do
|
59
|
+
# source 'error.log'
|
60
|
+
# not_if { ::File.exist?('/logs/foo/error.log') } # Proper Ruby expression
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# service 'apache2' do
|
64
|
+
# action :restart
|
65
|
+
# only_if { node['platform'] == 'ubuntu' } # Meaningful condition
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# # Or simply remove the guard if no condition is needed
|
69
|
+
# package 'curl' do
|
70
|
+
# action :install
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
class EmptyResourceGuard < Base
|
74
|
+
MSG = 'Resource guards (not_if/only_if) should not be empty strings as empty strings will always evaluate to true.'
|
75
|
+
RESTRICT_ON_SEND = [:not_if, :only_if].freeze
|
76
|
+
|
77
|
+
def_node_matcher :empty_string_guard?, <<-PATTERN
|
78
|
+
(send nil? {:not_if :only_if} (str #empty_string?))
|
79
|
+
PATTERN
|
80
|
+
|
81
|
+
def_node_matcher :empty_string_block_guard?, <<-PATTERN
|
82
|
+
(block (send nil? {:not_if :only_if}) (args) (str #empty_string?))
|
83
|
+
PATTERN
|
84
|
+
|
85
|
+
def empty_string?(str)
|
86
|
+
str.empty?
|
87
|
+
end
|
88
|
+
|
89
|
+
def on_send(node)
|
90
|
+
empty_string_guard?(node) do
|
91
|
+
add_offense(node, severity: :refactor)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def on_block(node)
|
96
|
+
empty_string_block_guard?(node) do
|
97
|
+
add_offense(node, severity: :refactor)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
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: 8.
|
4
|
+
version: 8.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thom May
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.79.
|
19
|
+
version: 1.79.2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.79.
|
26
|
+
version: 1.79.2
|
27
27
|
email:
|
28
28
|
- thom@chef.io
|
29
29
|
- tsmith84@gmail.com
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- lib/rubocop/cop/chef/correctness/chef_application_fatal.rb
|
53
53
|
- lib/rubocop/cop/chef/correctness/conditional_ruby_shellout.rb
|
54
54
|
- lib/rubocop/cop/chef/correctness/dnf_package_allow_downgrades.rb
|
55
|
+
- lib/rubocop/cop/chef/correctness/empty_resource_guard.rb
|
55
56
|
- lib/rubocop/cop/chef/correctness/incorrect_library_injection.rb
|
56
57
|
- lib/rubocop/cop/chef/correctness/invalid_cookbook_name.rb
|
57
58
|
- lib/rubocop/cop/chef/correctness/invalid_default_action.rb
|