Skip to content

Commit 1d58640

Browse files
committed
Allow running tests without a bundle
1 parent af955d8 commit 1d58640

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

lib/ruby_lsp/listeners/test_discovery.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,26 @@ def calc_fully_qualified_name(name)
6161

6262
#: (Prism::ClassNode node, String fully_qualified_name) -> Array[String]
6363
def calc_attached_ancestors(node, fully_qualified_name)
64-
@index.linearized_ancestors_of(fully_qualified_name)
65-
rescue RubyIndexer::Index::NonExistingNamespaceError
66-
# When there are dynamic parts in the constant path, we will not have indexed the namespace. We can still
67-
# provide test functionality if the class inherits directly from Test::Unit::TestCase or Minitest::Test
68-
[node.superclass&.slice].compact
64+
superclass = node.superclass
65+
66+
begin
67+
ancestors = @index.linearized_ancestors_of(fully_qualified_name)
68+
# If the project has no bundle and a test class inherits from `Minitest::Test`, the linearized ancestors will
69+
# not include the parent class because we never indexed it in the first place. Here we add the superclass
70+
# directly, so that we can support running tests in projects without a bundle
71+
return ancestors if ancestors.length > 1
72+
73+
# If all we found is the class itself, then manually include the parent class
74+
if ancestors.first == fully_qualified_name && superclass
75+
return [*ancestors, superclass.slice]
76+
end
77+
78+
ancestors
79+
rescue RubyIndexer::Index::NonExistingNamespaceError
80+
# When there are dynamic parts in the constant path, we will not have indexed the namespace. We can still
81+
# provide test functionality if the class inherits directly from Test::Unit::TestCase or Minitest::Test
82+
[superclass&.slice].compact
83+
end
6984
end
7085

7186
#: (Prism::ConstantPathNode | Prism::ConstantReadNode | Prism::ConstantPathTargetNode | Prism::CallNode | Prism::MissingNode node) -> String

lib/ruby_lsp/listeners/test_style.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def handle_test_unit_groups(file_path, groups_and_examples)
145145
MINITEST_REPORTER_PATH = File.expand_path("../test_reporters/minitest_reporter.rb", __dir__) #: String
146146
TEST_UNIT_REPORTER_PATH = File.expand_path("../test_reporters/test_unit_reporter.rb", __dir__) #: String
147147
BASE_COMMAND = begin
148-
Bundler.with_original_env { Bundler.default_lockfile }
148+
Bundler.with_unbundled_env { Bundler.default_lockfile }
149149
"bundle exec ruby"
150150
rescue Bundler::GemfileNotFound
151151
"ruby"

test/requests/discover_tests_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,29 @@ def test_other_thing; end
745745
end
746746
end
747747

748+
def test_can_discover_tests_even_if_parent_class_was_not_indexed
749+
source = <<~RUBY
750+
class MyTest < Minitest::Test
751+
def test_something; end
752+
end
753+
RUBY
754+
755+
with_server(source) do |server, uri|
756+
server.process_message(id: 1, method: "rubyLsp/discoverTests", params: {
757+
textDocument: { uri: uri },
758+
})
759+
760+
items = get_response(server)
761+
762+
assert_equal(
763+
["MyTest"],
764+
items.map { |i| i[:id] },
765+
)
766+
assert_equal(["MyTest#test_something"], items[0][:children].map { |i| i[:id] })
767+
assert_all_items_tagged_with(items, :minitest)
768+
end
769+
end
770+
748771
private
749772

750773
def create_test_discovery_addon

0 commit comments

Comments
 (0)