Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rspec_order.rb « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58f1fe2935743a179972fbbbe410c07033ec5021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module Support
  module RspecOrder
    TODO_YAML = File.join(__dir__, 'rspec_order_todo.yml')

    module_function

    def order_for(example_group)
      order_from_env || random_order(example_group)
    end

    def order_from_env
      return @order_from_env if defined?(@order_from_env)

      # Passing custom defined order via `--order NAME` is not supported.
      # For example, `--order reverse` does not work so we are passing it via
      # environment variable RSPEC_ORDER.
      @order_from_env = ENV['RSPEC_ORDER']
    end

    def random_order(example_group)
      path = example_group.metadata.fetch(:file_path)

      :random unless potential_order_dependent?(path)
    end

    def potential_order_dependent?(path)
      @todo ||= YAML.load_file(TODO_YAML).to_set # rubocop:disable Gitlab/PredicateMemoization

      @todo.include?(path)
    end
  end
end

RSpec.configure do |config|
  # Useful to find order-dependent specs.
  config.register_ordering(:reverse, &:reverse)

  # Randomization can be reproduced across test runs.
  Kernel.srand config.seed

  config.on_example_group_definition do |example_group|
    order = Support::RspecOrder.order_for(example_group)

    if order
      example_group.metadata[:order] = order.to_sym
      example_group.metadata[:description] += " (order #{order})"
    end
  end
end