Sha256: c741986a76e67d64702bb59672a34b9f7aa6e94af4a8f7858ff649d998f09475

Contents?: true

Size: 1.99 KB

Versions: 25

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Chef
    # Mixin for cops that skips non-cookbook files
    #
    # The criteria for whether cookstyle analyzes a certain ruby file
    # is configured via `AllCops/Chef`. For example, if you want to
    # customize your project to scan all files within a `test/` directory
    # then you could add this to your configuration:
    #
    # @example configuring analyzed paths
    #
    #   AllCops:
    #     Chef:
    #       Patterns:
    #       - '_spec.rb$'
    #       - '(?:^|/)spec/'
    #
    module CookbookOnly
      DEFAULT_CONFIGURATION = CONFIG.fetch('AllCops')
      COOKBOOK_SEGMENTS = %w(attributes definitions libraries metadata providers recipes resources).freeze

      def relevant_file?(file)
        cookbook_pattern =~ file && super
      end

      private

      def cookbook_pattern
        patterns = []
        COOKBOOK_SEGMENTS.each do |segment|
          next unless self.class.cookbook_only_segments[segment.to_sym]

          cookbook_pattern_config(segment).each do |pattern|
            patterns << Regexp.new(pattern)
          end
        end
        Regexp.union(patterns)
      end

      def cookbook_pattern_config(segment)
        config_key = "Chef#{segment.capitalize}"
        config
          .for_all_cops
          .fetch(config_key, DEFAULT_CONFIGURATION.fetch(config_key))
          .fetch('Patterns')
      end

      module ClassMethods
        attr_writer :cookbook_only_segments

        def cookbook_only_segments
          @cookbook_only_segments || Hash.new(true)
        end

        def included(klass)
          super
          klass.extend(ClassMethods)
        end
      end

      extend ClassMethods
    end

    def self.CookbookOnly(segments) # rubocop: disable Naming/MethodName
      Module.new do |mod|
        mod.define_singleton_method(:included) do |klass|
          super(klass)
          klass.include(CookbookOnly)
          klass.cookbook_only_segments = segments
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
cookstyle-5.21.9 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.20.0 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.19.9 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.18.4 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.17.4 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.16.11 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.16.10 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.15.7 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.14.1 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.13.7 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.12.13 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.12.12 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.11.0 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.10.13 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.10.11 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.9.3 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.8.1 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.7.0 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.6.5 lib/rubocop/chef/cookbook_only.rb
cookstyle-5.6.2 lib/rubocop/chef/cookbook_only.rb