Class: RuboCop::Cop::InSpecStyle::UsersResourceMatchers

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/inspecstyle/users_resource_matchers.rb

Overview

Users resource deprecated matchers

Examples:

EnforcedStyle: InSpecStyle (default)


# bad
describe users('/etc/my-custom-place/users') do
  its('has_home_directory?') { should eq 'foo' }
end

# good
describe users('/etc/my-custom-place/users') do
  its('users') { should eq 'foo' }
end

Constant Summary collapse

MSG =
'Use `%<solution>s` instead of `%<violation>s` as a matcher ' \
"for the `users` resource. \nThis matcher will be removed in InSpec 5"
MAP =
{
  has_home_directory?: "its('home')",
  has_login_shell?: "its('shell')",
  has_authorized_key?: "another matcher",
  maximum_days_between_password_change: :maxdays,
  has_uid?: "another matcher",
  minimum_days_between_password_change: "mindays"
}

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubocop/cop/inspecstyle/users_resource_matchers.rb', line 72

def autocorrect(node)
  lambda do |corrector|
    # Only these two matchers are autocorrectable
    [
      'maximum_days_between_password_change',
      'minimum_days_between_password_change'
    ].map do |violation|
      if node.inspect.include?(violation)
        corrector.replace(node.loc.selector, MAP[violation.to_sym])
      end
    end
  end
end

#on_block(node) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/inspecstyle/users_resource_matchers.rb', line 55

def on_block(node)
  return unless inside_users_spec?(node)
  node.descendants.each do |descendant|
    deprecated_users_matcher?(descendant) do |violation|
      add_offense(
        descendant,
        location: offense_range(descendant),
        message: format(
          MSG,
          violation: violation,
          solution: MAP[violation]
        )
      )
    end
  end
end