Sha256: 511086081d43b828e272bccc950721d9bce02de6b74d8742f1b55240388791e3

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'active_support/configurable'

module Danger
  # Check that all test files in spec/ folder have `_spec` postfix.
  # Results are passed out as a string with warning.
  #
  # @example Running linter
  #
  #          # Runs a linter
  #          spec_postfix.lint
  #
  class DangerSpecPostfix < Plugin
    # Lints the test files. Will fail if any has no '_spec' postfix.
    # Generates a `string` with warning.
    #
    # @param   [String] files
    #          A globbed string which should return the files that you want to lint, defaults to nil.
    #          if nil, modified and added files from the diff will be used.
    # @return  [void]
    #
    def self.configuration
      @configuration ||= Configuration.new
    end

    def self.configure
      yield configuration
    end

    def lint
      changed_files.select { |f| f.match?(%r{^spec/.*rb$}) }
        .reject { |f| f.end_with?('_spec.rb') }
        .reject { |f| DangerSpecPostfix.configuration.exceptions.any? { |e| f.start_with?(e) } }
        .each   { |f| warn(warning_generator(f)) }
    end

    private

    def changed_files
      (git.modified_files + git.added_files)
    end

    def warning_generator(file)
      "Tests should have `_spec` postfix: #{file}"
    end
  end

  class DangerSpecPostfix::Configuration
    include ::ActiveSupport::Configurable

    config_accessor(:exceptions) { ['spec/shared_examples/', 'spec/factories/', 'spec/support/', 'spec/rails_helper.rb', 'spec/spec_helper.rb'] }
  end
end

Version data entries

3 entries across 1 versions & 1 rubygems

Version Path
danger-spec_postfix-0.0.1 lib/spec_postfix/plugin.rb
danger-spec_postfix-0.0.1 plugin.rb
danger-spec_postfix-0.0.1 spec_postfix/plugin.rb