class RuboCop::Cop::Chef::Style::FileMode

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

@example

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

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# 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, 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(mode_int, "\'#{replacement_mode}\'")
    end
  end
end

Private Instance Methods

octal?(node) click to toggle source
# File lib/rubocop/cop/chef/style/file_mode.rb, line 79
def octal?(node)
  node.source =~ /^0o?\d+/i
end