Skip to content

Allow running tests without a bundle #3689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/ruby_lsp/listeners/test_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,26 @@ def calc_fully_qualified_name(name)

#: (Prism::ClassNode node, String fully_qualified_name) -> Array[String]
def calc_attached_ancestors(node, fully_qualified_name)
@index.linearized_ancestors_of(fully_qualified_name)
rescue RubyIndexer::Index::NonExistingNamespaceError
# When there are dynamic parts in the constant path, we will not have indexed the namespace. We can still
# provide test functionality if the class inherits directly from Test::Unit::TestCase or Minitest::Test
[node.superclass&.slice].compact
superclass = node.superclass

begin
ancestors = @index.linearized_ancestors_of(fully_qualified_name)
# If the project has no bundle and a test class inherits from `Minitest::Test`, the linearized ancestors will
# not include the parent class because we never indexed it in the first place. Here we add the superclass
# directly, so that we can support running tests in projects without a bundle
return ancestors if ancestors.length > 1

# If all we found is the class itself, then manually include the parent class
if ancestors.first == fully_qualified_name && superclass
return [*ancestors, superclass.slice]
end

ancestors
rescue RubyIndexer::Index::NonExistingNamespaceError
# When there are dynamic parts in the constant path, we will not have indexed the namespace. We can still
# provide test functionality if the class inherits directly from Test::Unit::TestCase or Minitest::Test
[superclass&.slice].compact
end
end

#: (Prism::ConstantPathNode | Prism::ConstantReadNode | Prism::ConstantPathTargetNode | Prism::CallNode | Prism::MissingNode node) -> String
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/listeners/test_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def handle_test_unit_groups(file_path, groups_and_examples)
MINITEST_REPORTER_PATH = File.expand_path("../test_reporters/minitest_reporter.rb", __dir__) #: String
TEST_UNIT_REPORTER_PATH = File.expand_path("../test_reporters/test_unit_reporter.rb", __dir__) #: String
BASE_COMMAND = begin
Bundler.with_original_env { Bundler.default_lockfile }
Bundler.with_unbundled_env { Bundler.default_lockfile }
"bundle exec ruby"
rescue Bundler::GemfileNotFound
"ruby"
Expand Down
23 changes: 23 additions & 0 deletions test/requests/discover_tests_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,29 @@ def test_spec_using_describe_with_additional_descriptions
end
end

def test_can_discover_tests_even_if_parent_class_was_not_indexed
source = <<~RUBY
class MyTest < Minitest::Test
def test_something; end
end
RUBY

with_server(source) do |server, uri|
server.process_message(id: 1, method: "rubyLsp/discoverTests", params: {
textDocument: { uri: uri },
})

items = get_response(server)

assert_equal(
["MyTest"],
items.map { |i| i[:id] },
)
assert_equal(["MyTest#test_something"], items[0][:children].map { |i| i[:id] })
assert_all_items_tagged_with(items, :minitest)
end
end

private

def create_test_discovery_addon
Expand Down
Loading