Class: RuboCop::Cop::Chef::Modernize::NodeInitPackage

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

Overview

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

Examples:


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

Constant Summary collapse

MSG =
"Use node['init_package'] to check for systemd instead of reading the contents of '/proc/1/comm'"
RESTRICT_ON_SEND =
[:open, :read, :exist?, :==, :not_if, :only_if].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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, severity: :refactor) do |corrector|
      corrector.replace(node, "node['init_package'] == 'systemd'")
    end
  end

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