Class: Canopus::TestRunner::RSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/canopus/test_runner/rspec.rb

Constant Summary collapse

MAX_NODES =
100_000
MAX_GROUP_DEPTH =
32
MAX_NAME_BYTES =
4_096
GROUPS =
%i[describe context feature fdescribe xdescribe fcontext xcontext].freeze
EXAMPLES =
%i[it specify example scenario fit focus fspecify xit xspecify xexample fexample fscenario xscenario skip].freeze

Instance Method Summary collapse

Instance Method Details

#candidate?(path) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/canopus/test_runner/rspec.rb', line 12

def candidate?(path)
  TestRunner.framework_for(path) == :rspec
end

#discover(root, path:, limit:, cancelled:) ⇒ Object



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
# File 'lib/canopus/test_runner/rspec.rb', line 16

def discover(root, path:, limit:, cancelled:)
  require "prism"
  path = path.dup.freeze
  tests, visited = [], 0
  stack = [[root, [].freeze, "".freeze, false]]
  until stack.empty? || tests.length >= limit
    break if cancelled.call
    node, groups, full_description, parent_module = stack.pop
    break if (visited += 1) > MAX_NODES

    if node.is_a?(Prism::DefNode)
      next
    elsif node.is_a?(Prism::CallNode)
      if group_call?(node) && groups.length < MAX_GROUP_DEPTH && (details = group_name(node))
        group, last_module = details
        separator = !full_description.empty? && !(parent_module && group.match?(/\A(?:#|::|\.)/)) ? " " : ""
        nested_description = "#{full_description}#{separator}#{group}".freeze
        stack << [node.block.body, [*groups, group].freeze, nested_description, last_module] if node.block.body
        next
      elsif !groups.empty? && EXAMPLES.include?(node.name) && !node.receiver && (name = literal_name(node.arguments&.arguments&.first))
        add(tests, path, name, groups, full_description, node)
        next
      end
      next if node.block
    end
    node.compact_child_nodes.reverse_each do |child|
      stack << [child, groups, full_description, parent_module]
    end
  end
  tests.freeze
end