Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ChefStyle/FileMode to support files_mode as well #702

Merged
merged 1 commit into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ChefStyle/CommentFormat:
- '**/Berksfile'

ChefStyle/FileMode:
Description: Use strings to represent file modes in Chef resources
Description: Use strings to represent file modes to avoid confusion between octal and base 10 integer formats.
StyleGuide: '#chefstylefilemode'
Enabled: true
VersionAdded: '5.0.0'
Expand Down
57 changes: 38 additions & 19 deletions lib/rubocop/cop/chef/style/file_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,58 @@ module RuboCop
module Cop
module Chef
module ChefStyle
# Check the file modes are given as strings instead of integers.
# Use strings to represent file modes to avoid confusion between octal and base 10 integer formats.
#
# @example
#
# # bad
# mode 644
# mode 0644
# remote_directory '/etc/my.conf' do
# content 'some content'
# mode 0600
# action :create
# end
#
# remote_directory 'handler' do
# source 'handlers'
# recursive true
# files_mode 644
# action :create
# end
#
# # good
# mode '644'
# remote_directory '/etc/my.conf' do
# content 'some content'
# mode '600'
# action :create
# end
#
# remote_directory 'handler' do
# source 'handlers'
# recursive true
# files_mode '644'
# action :create
# end
#
class FileMode < Cop
MSG = 'Use strings for file modes'
class FileMode < Base
extend RuboCop::Cop::AutoCorrector

MSG = 'Use strings to represent file modes to avoid confusion between octal and base 10 integer formats'

def_node_matcher :resource_mode?, <<-PATTERN
(send nil? :mode $int)
(send nil? {:mode :files_mode} $int)
PATTERN

def on_send(node)
resource_mode?(node) do |mode_int|
add_offense(mode_int, location: :expression, message: MSG, severity: :refactor)
end
end

def autocorrect(node)
lambda do |corrector|
# If it was an octal literal, make sure we write out the right number.
replacement_base = octal?(node) ? 8 : 10
replacement_mode = node.children.first.to_s(replacement_base)
add_offense(mode_int.loc.expression, message: MSG, severity: :refactor) do |corrector|
# If it was an octal literal, make sure we write out the right number.
replacement_base = octal?(mode_int) ? 8 : 10
replacement_mode = mode_int.children.first.to_s(replacement_base)

# we build our own escaped string instead of using .inspect because that way
# we can use single quotes instead of the double quotes that .inspect adds
corrector.replace(node.loc.expression, "\'#{replacement_mode}\'")
# we build our own escaped string instead of using .inspect because that way
# we can use single quotes instead of the double quotes that .inspect adds
corrector.replace(mode_int.loc.expression, "\'#{replacement_mode}\'")
end
end
end

Expand Down
21 changes: 19 additions & 2 deletions spec/rubocop/cop/chef/style/file_mode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
file '/foo' do
owner 'root'
mode 644
^^^ Use strings for file modes
^^^ Use strings to represent file modes to avoid confusion between octal and base 10 integer formats
end
RUBY

Expand All @@ -42,7 +42,7 @@
file '/foo' do
owner 'root'
mode 0644
^^^^ Use strings for file modes
^^^^ Use strings to represent file modes to avoid confusion between octal and base 10 integer formats
end
RUBY

Expand Down Expand Up @@ -72,6 +72,23 @@
RUBY
end

it 'registers an offense when setting a files_mode as well' do
expect_offense(<<~RUBY)
file '/foo' do
owner 'root'
files_mode 644
^^^ Use strings to represent file modes to avoid confusion between octal and base 10 integer formats
end
RUBY

expect_correction(<<~RUBY)
file '/foo' do
owner 'root'
files_mode '644'
end
RUBY
end

include_examples 'autocorrect',
'mode 644',
"mode '644'"
Expand Down