Skip to content
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

WIP: Move test_mode setting to GlobalState #1843

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/ruby_lsp/base_server.rb
Expand Up @@ -8,9 +8,8 @@ class BaseServer

abstract!

sig { params(test_mode: T::Boolean).void }
def initialize(test_mode: false)
@test_mode = T.let(test_mode, T::Boolean)
sig { void }
def initialize
@writer = T.let(Transport::Stdio::Writer.new, Transport::Stdio::Writer)
@reader = T.let(Transport::Stdio::Reader.new, Transport::Stdio::Reader)
@incoming_queue = T.let(Thread::Queue.new, Thread::Queue)
Expand All @@ -22,11 +21,11 @@ def initialize(test_mode: false)
@store = T.let(Store.new, Store)
@outgoing_dispatcher = T.let(
Thread.new do
unless test_mode
while (message = @outgoing_queue.pop)
@mutex.synchronize { @writer.write(message.to_hash) }
end
# unless @global_state.test_mode
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to figure out how we access the global state here.

while (message = @outgoing_queue.pop)
@mutex.synchronize { @writer.write(message.to_hash) }
end
# end
end,
Thread,
)
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby_lsp/global_state.rb
Expand Up @@ -11,6 +11,9 @@ class GlobalState
sig { returns(String) }
attr_accessor :formatter

sig { returns(T::Boolean) }
attr_accessor :test_mode

sig { returns(T::Boolean) }
attr_reader :typechecker

Expand All @@ -22,6 +25,7 @@ def initialize
@workspace_uri = T.let(URI::Generic.from_path(path: Dir.pwd), URI::Generic)

@formatter = T.let(detect_formatter, String)
@test_mode = T.let(false, T::Boolean)
@test_library = T.let(detect_test_library, String)
@typechecker = T.let(detect_typechecker, T::Boolean)
@index = T.let(RubyIndexer::Index.new, RubyIndexer::Index)
Expand Down
6 changes: 3 additions & 3 deletions lib/ruby_lsp/server.rb
Expand Up @@ -9,8 +9,8 @@ class Server < BaseServer
sig { returns(GlobalState) }
attr_reader :global_state

sig { params(test_mode: T::Boolean).void }
def initialize(test_mode: false)
sig { void }
def initialize
super
@global_state = T.let(GlobalState.new, GlobalState)
end
Expand All @@ -22,7 +22,7 @@ def process_message(message)
$stderr.puts("Initializing Ruby LSP v#{VERSION}...")
run_initialize(message)
when "initialized"
$stderr.puts("Finished initializing Ruby LSP!") unless @test_mode
$stderr.puts("Finished initializing Ruby LSP!") unless @global_state.test_mode
run_initialized
when "textDocument/didOpen"
text_document_did_open(message)
Expand Down
3 changes: 2 additions & 1 deletion lib/ruby_lsp/test_helper.rb
Expand Up @@ -17,7 +17,8 @@ module TestHelper
).returns(T.type_parameter(:T))
end
def with_server(source = nil, uri = Kernel.URI("file:///fake.rb"), stub_no_typechecker: false, &block)
server = RubyLsp::Server.new(test_mode: true)
server = RubyLsp::Server.new
server.global_state.test_mode = true
server.global_state.stubs(:typechecker).returns(false) if stub_no_typechecker

if source
Expand Down
15 changes: 10 additions & 5 deletions test/server_test.rb
Expand Up @@ -5,7 +5,8 @@

class ServerTest < Minitest::Test
def setup
@server = RubyLsp::Server.new(test_mode: true)
@server = RubyLsp::Server.new
@server.global_state.test_mode = true
end

def teardown
Expand Down Expand Up @@ -343,7 +344,8 @@ def test_handles_invalid_configuration
def test_detects_rubocop_if_direct_dependency
stub_dependencies(rubocop: true, syntax_tree: false)

server = RubyLsp::Server.new(test_mode: true)
server = RubyLsp::Server.new
server.global_state.test_mode = true

begin
capture_subprocess_io do
Expand All @@ -360,7 +362,8 @@ def test_detects_rubocop_if_direct_dependency

def test_detects_syntax_tree_if_direct_dependency
stub_dependencies(rubocop: false, syntax_tree: true)
server = RubyLsp::Server.new(test_mode: true)
server = RubyLsp::Server.new
server.global_state.test_mode = true

begin
capture_subprocess_io do
Expand All @@ -377,7 +380,8 @@ def test_detects_syntax_tree_if_direct_dependency

def test_gives_rubocop_precedence_if_syntax_tree_also_present
stub_dependencies(rubocop: true, syntax_tree: true)
server = RubyLsp::Server.new(test_mode: true)
server = RubyLsp::Server.new
server.global_state.test_mode = true

begin
capture_subprocess_io do
Expand All @@ -394,7 +398,8 @@ def test_gives_rubocop_precedence_if_syntax_tree_also_present

def test_sets_formatter_to_none_if_neither_rubocop_or_syntax_tree_are_present
stub_dependencies(rubocop: false, syntax_tree: false)
server = RubyLsp::Server.new(test_mode: true)
server = RubyLsp::Server.new
server.global_state.test_mode = true

begin
capture_subprocess_io do
Expand Down
3 changes: 2 additions & 1 deletion test/test_helper.rb
Expand Up @@ -58,7 +58,8 @@ def test_addon(addon_creation_method, source:, &block)
message_queue = Thread::Queue.new

uri = URI::Generic.from_path(path: "/fake.rb")
server = RubyLsp::Server.new(test_mode: true)
server = RubyLsp::Server.new
server.global_state.test_mode = true
server.global_state.stubs(:typechecker).returns(false)
server.process_message({
method: "textDocument/didOpen",
Expand Down