Sha256: 5ad7b280d813d3940495d70d64d94de968c5b34ba327bee623dfa2d3f6ed648b

Contents?: true

Size: 1.95 KB

Versions: 93

Compression:

Stored size: 1.95 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)
      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

93 entries across 93 versions & 2 rubygems

Version Path
cookstyle-7.32.8 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.32.7 lib/rubocop/chef/cookbook_only.rb
cookstyle-ng-8.0.0 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.32.2 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.32.1 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.32.0 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.31.9 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.31.7 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.31.3 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.31.1 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.31.0 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.30.4 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.30.3 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.30.1 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.28.2 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.27.0 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.26.1 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.25.10 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.25.9 lib/rubocop/chef/cookbook_only.rb
cookstyle-7.25.8 lib/rubocop/chef/cookbook_only.rb