From 7f26495e163f9827e24d6d9b2b95a122ae73c11b Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Tue, 29 Aug 2023 13:31:22 -0400 Subject: [PATCH] Do not try to generate test command for temporary files (#945) --- lib/ruby_lsp/requests/code_lens.rb | 4 ++-- test/requests/code_lens_expectations_test.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/ruby_lsp/requests/code_lens.rb b/lib/ruby_lsp/requests/code_lens.rb index 2f079a36b..5dc87cae0 100644 --- a/lib/ruby_lsp/requests/code_lens.rb +++ b/lib/ruby_lsp/requests/code_lens.rb @@ -65,7 +65,7 @@ def on_class(node) class_name = node.constant.constant.value @class_stack.push(class_name) - if class_name.end_with?("Test") + if @path && class_name.end_with?("Test") add_test_code_lens( node, name: class_name, @@ -89,7 +89,7 @@ def on_def(node) visibility, _ = @visibility_stack.last if visibility == "public" method_name = node.name.value - if method_name.start_with?("test_") + if @path && method_name.start_with?("test_") add_test_code_lens( node, name: method_name, diff --git a/test/requests/code_lens_expectations_test.rb b/test/requests/code_lens_expectations_test.rb index db55ccc49..9f0e91f76 100644 --- a/test/requests/code_lens_expectations_test.rb +++ b/test/requests/code_lens_expectations_test.rb @@ -79,6 +79,24 @@ def test_bar; end assert_empty(response) end + def test_no_code_lens_for_unsaved_files + source = <<~RUBY + class FooTest < Test::Unit::TestCase + def test_bar; end + end + RUBY + uri = URI::Generic.build(scheme: "untitled", opaque: "Untitled-1") + + document = RubyLsp::Document.new(source: source, version: 1, uri: uri) + + emitter = RubyLsp::EventEmitter.new + listener = RubyLsp::Requests::CodeLens.new(uri, emitter, @message_queue, "minitest") + emitter.visit(document.tree) + response = listener.response + + assert_empty(response) + end + def test_code_lens_extensions message_queue = Thread::Queue.new create_code_lens_extension