Class: RuboCop::Cop::Chef::Style::FileMode

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chef/style/file_mode.rb

Overview

Use strings to represent file modes to avoid confusion between octal and base 10 integer formats.

Examples:


### incorrect
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

### correct
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

Constant Summary collapse

MSG =
'Use strings to represent file modes to avoid confusion between octal and base 10 integer formats'
RESTRICT_ON_SEND =
[:mode, :files_mode].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubocop/cop/chef/style/file_mode.rb', line 63

def on_send(node)
  resource_mode?(node) do |mode_int|
    add_offense(mode_int, 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(mode_int, "\'#{replacement_mode}\'")
    end
  end
end