From a03f1f4ca179929b596c52fc6fa8e22efb6c6dbe Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Wed, 1 May 2024 14:38:33 -0400 Subject: [PATCH] Revise testing approach --- lib/ruby_lsp/ruby_lsp_rails/addon.rb | 2 +- test/ruby_lsp_rails/definition_test.rb | 2 + test/ruby_lsp_rails/hover_test.rb | 99 ++------------------------ 3 files changed, 10 insertions(+), 93 deletions(-) diff --git a/lib/ruby_lsp/ruby_lsp_rails/addon.rb b/lib/ruby_lsp/ruby_lsp_rails/addon.rb index 069b5f03..3b0bb80e 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/addon.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/addon.rb @@ -31,7 +31,7 @@ def activate(global_state, message_queue) @global_state = T.let(global_state, T.nilable(RubyLsp::GlobalState)) $stderr.puts("Activating Ruby LSP Rails addon v#{VERSION}") # Start booting the real client in a background thread. Until this completes, the client will be a NullClient - Thread.new { @client = RunnerClient.create_client }.join + Thread.new { @client = RunnerClient.create_client } end sig { override.void } diff --git a/test/ruby_lsp_rails/definition_test.rb b/test/ruby_lsp_rails/definition_test.rb index 169f3248..d3261e3e 100644 --- a/test/ruby_lsp_rails/definition_test.rb +++ b/test/ruby_lsp_rails/definition_test.rb @@ -124,6 +124,8 @@ def baz; end def generate_definitions_for_source(source, position) with_server(source) do |server, uri| + sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@client).is_a?(NullClient) + server.process_message( id: 1, method: "textDocument/definition", diff --git a/test/ruby_lsp_rails/hover_test.rb b/test/ruby_lsp_rails/hover_test.rb index bab6ad4a..b8060e41 100644 --- a/test/ruby_lsp_rails/hover_test.rb +++ b/test/ruby_lsp_rails/hover_test.rb @@ -24,21 +24,6 @@ class HoverTest < ActiveSupport::TestCase end test "hook returns model column information" do - expected_response = { - schema_file: "#{dummy_root}/db/schema.rb", - columns: [ - ["id", "integer"], - ["first_name", "string"], - ["last_name", "string"], - ["age", "integer"], - ["created_at", "datetime"], - ["updated_at", "datetime"], - ], - primary_keys: ["id"], - } - - RunnerClient.any_instance.stubs(model: expected_response) - response = hover_on_source(<<~RUBY, { line: 3, character: 0 }) class User < ApplicationRecord end @@ -70,28 +55,13 @@ class User < ApplicationRecord end test "return column information for namespaced models" do - expected_response = { - schema_file: "#{dummy_root}/db/schema.rb", - columns: [ - ["id", "integer"], - ["first_name", "string"], - ["last_name", "string"], - ["age", "integer"], - ["created_at", "datetime"], - ["updated_at", "datetime"], - ], - primary_keys: ["id"], - } - - RunnerClient.any_instance.stubs(model: expected_response) - response = hover_on_source(<<~RUBY, { line: 4, character: 6 }) module Blog - class User < ApplicationRecord + class Post < ApplicationRecord end end - Blog::User + Blog::Post RUBY assert_equal(<<~CONTENT.chomp, response.contents.value) @@ -99,11 +69,9 @@ class User < ApplicationRecord **id**: integer (PK) - **first_name**: string - - **last_name**: string + **title**: string - **age**: integer + **body**: text **created_at**: datetime @@ -112,20 +80,6 @@ class User < ApplicationRecord end test "returns column information for models with composite primary keys" do - expected_response = { - schema_file: "#{dummy_root}/db/schema.rb", - columns: [ - ["order_id", "integer"], - ["product_id", "integer"], - ["note", "string"], - ["created_at", "datetime"], - ["updated_at", "datetime"], - ], - primary_keys: ["order_id", "product_id"], - } - - RunnerClient.any_instance.stubs(model: expected_response) - response = hover_on_source(<<~RUBY, { line: 3, character: 0 }) class CompositePrimaryKey < ApplicationRecord end @@ -146,7 +100,7 @@ class CompositePrimaryKey < ApplicationRecord **product_id**: integer (PK) - **note**: string + **note**: text **created_at**: datetime @@ -154,47 +108,6 @@ class CompositePrimaryKey < ApplicationRecord CONTENT end - test "handles `db/structure.sql` instead of `db/schema.rb`" do - expected_response = { - schema_file: "#{dummy_root}/db/structure.sql", - columns: [], - primary_keys: [], - } - - RunnerClient.any_instance.stubs(model: expected_response) - - response = hover_on_source(<<~RUBY, { line: 3, character: 0 }) - class User < ApplicationRecord - end - - User - RUBY - - assert_includes( - response.contents.value, - "[Schema](file://#{dummy_root}/db/structure.sql)", - ) - end - - test "handles neither `db/structure.sql` nor `db/schema.rb` being present" do - expected_response = { - schema_file: nil, - columns: [], - primary_keys: [], - } - - RunnerClient.any_instance.stubs(model: expected_response) - - response = hover_on_source(<<~RUBY, { line: 3, character: 0 }) - class User < ApplicationRecord - end - - User - RUBY - - refute_match(/Schema/, response.contents.value) - end - test "shows documentation for routes DSLs" do value = hover_on_source("root 'projects#index'", { line: 0, character: 0 }).contents.value @@ -238,6 +151,8 @@ class ActiveRecord::Base def hover_on_source(source, position) with_server(source) do |server, uri| + sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@client).is_a?(NullClient) + server.process_message( id: 1, method: "textDocument/hover",