2017-10-11 07:25:40 -04:00
|
|
|
module RuboCop
|
|
|
|
module SpecHelpers
|
2018-03-29 05:49:08 -04:00
|
|
|
SPEC_HELPERS = %w[fast_spec_helper.rb rails_helper.rb spec_helper.rb].freeze
|
2019-01-16 05:35:08 -05:00
|
|
|
MIGRATION_SPEC_DIRECTORIES = ['spec/migrations', 'spec/lib/gitlab/background_migration'].freeze
|
2017-10-11 07:25:40 -04:00
|
|
|
|
|
|
|
# Returns true if the given node originated from the spec directory.
|
|
|
|
def in_spec?(node)
|
|
|
|
path = node.location.expression.source_buffer.name
|
2019-05-03 08:41:47 -04:00
|
|
|
pwd = RuboCop::PathUtil.pwd
|
2017-10-11 07:25:40 -04:00
|
|
|
|
2018-03-30 11:18:12 -04:00
|
|
|
!SPEC_HELPERS.include?(File.basename(path)) &&
|
2019-05-03 08:41:47 -04:00
|
|
|
path.start_with?(File.join(pwd, 'spec'), File.join(pwd, 'ee', 'spec'))
|
2018-03-30 11:18:12 -04:00
|
|
|
end
|
|
|
|
|
2019-01-16 05:35:08 -05:00
|
|
|
def migration_directories
|
|
|
|
@migration_directories ||= MIGRATION_SPEC_DIRECTORIES.map do |dir|
|
2019-05-03 08:41:47 -04:00
|
|
|
pwd = RuboCop::PathUtil.pwd
|
|
|
|
[File.join(pwd, dir), File.join(pwd, 'ee', dir)]
|
2019-01-16 05:35:08 -05:00
|
|
|
end.flatten
|
|
|
|
end
|
|
|
|
|
2018-03-30 11:18:12 -04:00
|
|
|
# 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) &&
|
2019-01-16 05:35:08 -05:00
|
|
|
path.start_with?(*migration_directories)
|
2017-10-11 07:25:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|