Class: RubyLsp::Rails::RailsTestStyle

Inherits:
Listeners::TestDiscovery
  • Object
show all
Defined in:
lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb

Constant Summary collapse

BASE_COMMAND =

: String

"bundle exec ruby -r#{Listeners::TestStyle::MINITEST_REPORTER_PATH} bin/rails test"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_builder, global_state, dispatcher, uri) ⇒ RailsTestStyle

: (ResponseBuilders::TestCollection response_builder, GlobalState global_state, Prism::Dispatcher dispatcher, URI::Generic uri) -> void



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb', line 56

def initialize(response_builder, global_state, dispatcher, uri)
  @parent_stack = [response_builder] #: Array[(Requests::Support::TestItem | ResponseBuilders::TestCollection)?]
  super(response_builder, global_state, uri)

  register_events(
    dispatcher,
    :on_class_node_enter,
    :on_call_node_enter,
    :on_def_node_enter,
  )
end

Class Method Details

.resolve_test_commands(items) ⇒ Object

: (Array[Hash[Symbol, untyped]]) -> Array



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
52
# File 'lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb', line 11

def resolve_test_commands(items)
  commands = []
  queue = items.dup

  full_files = []

  until queue.empty?
    item = queue.shift #: as !nil
    tags = Set.new(item[:tags])
    next unless tags.include?("framework:rails")

    children = item[:children]
    uri = URI(item[:uri])
    path = uri.full_path
    next unless path

    if tags.include?("test_dir")
      if children.empty?
        full_files.concat(
          Dir.glob(
            "#{path}/**/{*_test,test_*}.rb",
            File::Constants::FNM_EXTGLOB | File::Constants::FNM_PATHNAME,
          ).map! { |f| Shellwords.escape(f) },
        )
      end
    elsif tags.include?("test_file")
      full_files << Shellwords.escape(path) if children.empty?
    elsif tags.include?("test_group")
      commands << "#{BASE_COMMAND} #{Shellwords.escape(path)} --name \"/#{Shellwords.escape(item[:id])}(#|::)/\""
    else
      full_files << "#{Shellwords.escape(path)}:#{item.dig(:range, :start, :line) + 1}"
    end

    queue.concat(children)
  end

  unless full_files.empty?
    commands << "#{BASE_COMMAND} #{full_files.join(" ")}"
  end

  commands
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object

: (Prism::CallNode node) -> void



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb', line 110

def on_call_node_enter(node)
  return unless node.name == :test
  return unless node.block

  arguments = node.arguments&.arguments
  first_arg = arguments&.first
  return unless first_arg.is_a?(Prism::StringNode)

  test_name = first_arg.unescaped
  test_name = "<empty test name>" if test_name.empty?

  # Rails' `test "foo bar"` helper defines a method `def test_foo_bar`. We normalize test names
  # the same way (spaces to underscores, prefix with `test_`) to match the actual method names
  # Rails uses at runtime, ensuring proper test discovery and execution.
  rails_normalized_name = "test_#{test_name.gsub(/\s+/, "_")}"

  add_test_item(node, rails_normalized_name, test_name)
end

#on_class_node_enter(node) ⇒ Object

: (Prism::ClassNode node) -> void



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb', line 69

def on_class_node_enter(node)
  with_test_ancestor_tracking(node) do |name, ancestors|
    if declarative_minitest?(ancestors, name)
      label = constant_name(node.constant_path) || name_with_dynamic_reference(node.constant_path)

      test_item = Requests::Support::TestItem.new(
        name,
        label,
        @uri,
        range_from_node(node),
        framework: :rails,
      )

      last_test_group.add(test_item)
      @response_builder.add_code_lens(test_item)
      @parent_stack << test_item
    else
      @parent_stack << nil
    end
  end
end

#on_class_node_leave(node) ⇒ Object

: (Prism::ClassNode node) -> void



92
93
94
95
# File 'lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb', line 92

def on_class_node_leave(node)
  @parent_stack.pop
  super
end

#on_def_node_enter(node) ⇒ Object

: (Prism::DefNode node) -> void



130
131
132
133
134
135
136
137
# File 'lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb', line 130

def on_def_node_enter(node)
  return if @visibility_stack.last != :public

  name = node.name.to_s
  return unless name.start_with?("test_")

  add_test_item(node, name, name)
end

#on_module_node_enter(node) ⇒ Object

: (Prism::ModuleNode node) -> void



98
99
100
101
# File 'lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb', line 98

def on_module_node_enter(node)
  @parent_stack << nil
  super
end

#on_module_node_leave(node) ⇒ Object

: (Prism::ModuleNode node) -> void



104
105
106
107
# File 'lib/ruby_lsp/ruby_lsp_rails/rails_test_style.rb', line 104

def on_module_node_leave(node)
  @parent_stack.pop
  super
end