class RuboCop::Cop::Chef::Modernize::NodeInitPackage

Use node to check for systemd instead of reading the contents of '/proc/1/comm'

@example

#### incorrect
::File.open('/proc/1/comm').gets.chomp == 'systemd'
::File.open('/proc/1/comm').chomp == 'systemd'
File.open('/proc/1/comm').gets.chomp == 'systemd'
File.open('/proc/1/comm').chomp == 'systemd'
IO.read('/proc/1/comm').chomp == 'systemd'
IO.read('/proc/1/comm').gets.chomp == 'systemd'
::IO.read('/proc/1/comm').chomp == 'systemd'
::IO.read('/proc/1/comm').gets.chomp == 'systemd'
File.exist?('/proc/1/comm') && File.open('/proc/1/comm').chomp == 'systemd'
only_if 'test -f /bin/systemctl && /bin/systemctl'

#### correct
node['init_package'] == 'systemd'
only_if { node['init_package'] == 'systemd' }

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/chef/modernize/node_init_package.rb, line 69
def on_send(node)
  compare_init_system?(node) do
    # if there's a ::File.exist?('/proc/1/comm') check first we want to match that as well
    node = node.parent if node.parent&.and_type? && proc_1_comm_exists?(node.parent.conditions.first)

    add_offense(node, message: MSG, severity: :refactor) do |corrector|
      corrector.replace(node, "node['init_package'] == 'systemd'")
    end
  end

  file_systemd_conditional?(node) do |conditional|
    add_offense(node, message: MSG, severity: :refactor) do |corrector|
      corrector.replace(conditional, "{ node['init_package'] == 'systemd' }")
    end
  end
end