spec_helpers.rb 969 Bytes
Newer Older
1 2
module RuboCop
  module SpecHelpers
3
    SPEC_HELPERS = %w[fast_spec_helper.rb rails_helper.rb spec_helper.rb].freeze
4
    MIGRATION_SPEC_DIRECTORIES = ['spec/migrations', 'spec/lib/gitlab/background_migration'].freeze
5 6 7 8 9

    # Returns true if the given node originated from the spec directory.
    def in_spec?(node)
      path = node.location.expression.source_buffer.name

10 11 12 13
      !SPEC_HELPERS.include?(File.basename(path)) &&
        path.start_with?(File.join(Dir.pwd, 'spec'), File.join(Dir.pwd, 'ee', 'spec'))
    end

14 15 16 17 18 19
    def migration_directories
      @migration_directories ||= MIGRATION_SPEC_DIRECTORIES.map do |dir|
        [File.join(Dir.pwd, dir), File.join(Dir.pwd, 'ee', dir)]
      end.flatten
    end

20 21 22 23 24
    # Returns true if the given node originated from a migration spec.
    def in_migration_spec?(node)
      path = node.location.expression.source_buffer.name

      in_spec?(node) &&
25
        path.start_with?(*migration_directories)
26 27 28
    end
  end
end