Module: RuboCop::Chef::CookbookHelpers

Included in:
RuboCop::Cop::Chef::Correctness::ConditionalRubyShellout, RuboCop::Cop::Chef::Correctness::DnfPackageAllowDowngrades, RuboCop::Cop::Chef::Correctness::LazyEvalNodeAttributeDefaults, RuboCop::Cop::Chef::Correctness::MacosUserdefaultsInvalidType, RuboCop::Cop::Chef::Correctness::NotifiesActionNotSymbol, RuboCop::Cop::Chef::Correctness::PowershellScriptDeleteFile, RuboCop::Cop::Chef::Correctness::ResourceSetsInternalProperties, RuboCop::Cop::Chef::Correctness::ResourceSetsNameProperty, RuboCop::Cop::Chef::Correctness::ResourceWithNoneAction, RuboCop::Cop::Chef::Deprecations::ChefHandlerUsesSupports, RuboCop::Cop::Chef::Deprecations::ChefRewind, RuboCop::Cop::Chef::Deprecations::ChocolateyPackageUninstallAction, RuboCop::Cop::Chef::Deprecations::DeprecatedChefSpecPlatform, RuboCop::Cop::Chef::Deprecations::DeprecatedSudoActions, RuboCop::Cop::Chef::Deprecations::DeprecatedYumRepositoryActions, RuboCop::Cop::Chef::Deprecations::DeprecatedYumRepositoryProperties, RuboCop::Cop::Chef::Deprecations::ExecutePathProperty, RuboCop::Cop::Chef::Deprecations::ExecuteRelativeCreatesWithoutCwd, RuboCop::Cop::Chef::Deprecations::LaunchdDeprecatedHashProperty, RuboCop::Cop::Chef::Deprecations::LocaleDeprecatedLcAllProperty, RuboCop::Cop::Chef::Deprecations::LogResourceNotifications, RuboCop::Cop::Chef::Deprecations::MacosUserdefaultsGlobalProperty, RuboCop::Cop::Chef::Deprecations::PoiseArchiveUsage, RuboCop::Cop::Chef::Deprecations::ResourceUsesOnlyResourceName, RuboCop::Cop::Chef::Deprecations::RubyBlockCreateAction, RuboCop::Cop::Chef::Deprecations::UserDeprecatedSupportsProperty, RuboCop::Cop::Chef::Deprecations::VerifyPropertyUsesFileExpansion, RuboCop::Cop::Chef::Deprecations::WindowsFeatureServermanagercmd, RuboCop::Cop::Chef::Deprecations::WindowsPackageInstallerTypeString, RuboCop::Cop::Chef::Deprecations::WindowsTaskChangeAction, RuboCop::Cop::Chef::Modernize::ChefGemNokogiri, RuboCop::Cop::Chef::Modernize::CronDFileOrTemplate, RuboCop::Cop::Chef::Modernize::Definitions, RuboCop::Cop::Chef::Modernize::ExecuteScExe, RuboCop::Cop::Chef::Modernize::ExecuteSleep, RuboCop::Cop::Chef::Modernize::ExecuteSysctl, RuboCop::Cop::Chef::Modernize::ExecuteTzUtil, RuboCop::Cop::Chef::Modernize::PowerShellGuardInterpreter, RuboCop::Cop::Chef::Modernize::PowershellInstallPackage, RuboCop::Cop::Chef::Modernize::PowershellInstallWindowsFeature, RuboCop::Cop::Chef::Modernize::PowershellScriptExpandArchive, RuboCop::Cop::Chef::Modernize::RespondToCompileTime, RuboCop::Cop::Chef::Modernize::ShellOutToChocolatey, RuboCop::Cop::Chef::Modernize::SimplifyAptPpaSetup, RuboCop::Cop::Chef::Modernize::SysctlParamResource, RuboCop::Cop::Chef::Modernize::WindowsRegistryUAC, RuboCop::Cop::Chef::Modernize::ZipfileResource, RuboCop::Cop::Chef::RedundantCode::AptRepositoryDistributionDefault, RuboCop::Cop::Chef::RedundantCode::AptRepositoryNotifiesAptUpdate, RuboCop::Cop::Chef::RedundantCode::UseCreateIfMissing
Defined in:
lib/rubocop/chef/cookbook_helpers.rb

Overview

Common node helpers used for matching against Chef Infra Cookbooks

Instance Method Summary collapse

Instance Method Details

#match_property_in_resource?(resource_names, property_names, node) { ... } ⇒ Boolean

Match particular properties within a resource

Parameters:

  • resource_names (Symbol, Array<Symbol>)

    The name of the resources to match

  • property_names (String)

    The name of the property to match (or action)

  • node (RuboCop::AST::Node)

    The rubocop ast node to search

Yields:

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/chef/cookbook_helpers.rb', line 49

def match_property_in_resource?(resource_names, property_names, node)
  return unless looks_like_resource?(node)
  # bail out if we're not in the resource we care about or nil was passed (all resources)
  return unless resource_names.nil? || Array(resource_names).include?(node.children.first.method_name) # see if we're in the right resource

  resource_block = node.children[2] # the 3rd child is the actual block in the resource
  return unless resource_block # nil would be an empty block

  if resource_block.begin_type? # if begin_type we need to iterate over the children
    resource_block.children.each do |resource_blk_child|
      extract_send_types(resource_blk_child) do |p|
        yield(p) if symbolized_property_types(property_names).include?(p.method_name)
      end
    end
  else # there's only a single property to check
    extract_send_types(resource_block) do |p|
      yield(p) if symbolized_property_types(property_names).include?(p.method_name)
    end
  end
end

#match_resource_type?(resource_name, node) { ... } ⇒ Boolean

Match a particular resource

Parameters:

  • resource_name (String)

    The name of the resource to match

  • node (RuboCop::AST::Node)

    The rubocop ast node to search

Yields:

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/rubocop/chef/cookbook_helpers.rb', line 35

def match_resource_type?(resource_name, node)
  return unless looks_like_resource?(node)
  # bail out if we're not in the resource we care about or nil was passed (all resources)
  yield(node) if node.children.first.method?(resource_name.to_sym)
end

#method_arg_ast_to_string(ast) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rubocop/chef/cookbook_helpers.rb', line 70

def method_arg_ast_to_string(ast)
  # a property without a value. This is totally bogus, but they exist
  return if ast.children[2].nil?
  # https://rubular.com/r/6uzOMd6WCHewOu
  m = ast.children[2].source.match(/^("|')(.*)("|')$/)
  return m[2] unless m.nil?
end

#resource_block_name_if_string(node) ⇒ Object



22
23
24
25
26
# File 'lib/rubocop/chef/cookbook_helpers.rb', line 22

def resource_block_name_if_string(node)
  if looks_like_resource?(node) && node.children.first.arguments.first.respond_to?(:value)
    node.children.first.arguments.first.value
  end
end