Skip to content

Commit

Permalink
Only reset singleton where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Sep 29, 2023
1 parent 8c4f8f8 commit 185b6c5
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 26 deletions.
1 change: 0 additions & 1 deletion test/addon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def name

def teardown
Addon.addons.clear
super
end

def test_registering_an_addon_invokes_activate_on_initialized
Expand Down
2 changes: 1 addition & 1 deletion test/executor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def setup

def teardown
@message_queue.close
super
end

def test_initialize_enabled_features_with_array
Expand Down Expand Up @@ -252,5 +251,6 @@ def stub_dependencies(rubocop:, syntax_tree:)
dependencies["syntax_tree"] = "..." if syntax_tree
dependencies["rubocop"] = "..." if rubocop
Bundler.locked_gems.stubs(:dependencies).returns(dependencies)
Singleton.__init__(RubyLsp::DependencyDetector)
end
end
1 change: 0 additions & 1 deletion test/expectations/expectations_test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def setup
def teardown
@message_queue.close
super
end
def run_expectations(source)
Expand Down
1 change: 0 additions & 1 deletion test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def teardown
# Make sure the exit status is zero
assert_equal(0, @wait_thr.value)
refute_predicate(@wait_thr, :alive?)
super
end

def test_document_symbol
Expand Down
15 changes: 10 additions & 5 deletions test/requests/code_lens_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def run_expectations(source)
document = RubyLsp::Document.new(source: source, version: 1, uri: uri)

emitter = RubyLsp::EventEmitter.new
RubyLsp::DependencyDetector.instance.stubs(:detected_test_library).returns("minitest")
stub_test_library("minitest")
listener = RubyLsp::Requests::CodeLens.new(uri, emitter, @message_queue)
emitter.visit(document.tree)
listener.response
Expand All @@ -29,7 +29,7 @@ def test_bar; end
document = RubyLsp::Document.new(source: source, version: 1, uri: uri)

emitter = RubyLsp::EventEmitter.new
RubyLsp::DependencyDetector.instance.stubs(:detected_test_library).returns("test-unit")
stub_test_libary("test-unit")
listener = RubyLsp::Requests::CodeLens.new(uri, emitter, @message_queue)
emitter.visit(document.tree)
response = listener.response
Expand All @@ -56,7 +56,7 @@ def test_bar; end
document = RubyLsp::Document.new(source: source, version: 1, uri: uri)

emitter = RubyLsp::EventEmitter.new
RubyLsp::DependencyDetector.instance.stubs(:detected_test_library).returns("unknown")
stub_test_libary("unknown")
listener = RubyLsp::Requests::CodeLens.new(uri, emitter, @message_queue)
emitter.visit(document.tree)
response = listener.response
Expand All @@ -75,7 +75,7 @@ def test_bar; end
document = RubyLsp::Document.new(source: source, version: 1, uri: uri)

emitter = RubyLsp::EventEmitter.new
RubyLsp::DependencyDetector.instance.stubs(:detected_test_library).returns("rspec")
stub_test_libary("rspec")
listener = RubyLsp::Requests::CodeLens.new(uri, emitter, @message_queue)
emitter.visit(document.tree)
response = listener.response
Expand All @@ -94,7 +94,7 @@ def test_bar; end
document = RubyLsp::Document.new(source: source, version: 1, uri: uri)

emitter = RubyLsp::EventEmitter.new
RubyLsp::DependencyDetector.instance.stubs(:detected_test_library).returns("minitest")
stub_test_libary("minitest")
listener = RubyLsp::Requests::CodeLens.new(uri, emitter, @message_queue)
emitter.visit(document.tree)
response = listener.response
Expand Down Expand Up @@ -159,4 +159,9 @@ def on_class(node)
end
end
end

def stub_test_library(name)
RubyLsp::DependencyDetector.instance.stubs(:detected_test_library).returns(name)
Singleton.__init__(RubyLsp::DependencyDetector)
end
end
1 change: 0 additions & 1 deletion test/requests/completion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def setup

def teardown
T.must(@message_queue).close
super
end

def test_completion_command
Expand Down
1 change: 0 additions & 1 deletion test/requests/show_syntax_tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def setup

def teardown
@message_queue.close
super
end

def test_returns_partial_tree_if_document_has_syntax_error
Expand Down
26 changes: 16 additions & 10 deletions test/requests/support/dependency_detector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,50 @@

module RubyLsp
class DependencyDetectorTest < Minitest::Test
def teardown
end

def test_detects_no_test_library_when_there_are_no_dependencies
dependencies = {}
Bundler.locked_gems.stubs(dependencies: dependencies)
stub_dependencies({})

assert_equal("unknown", DependencyDetector.instance.detected_test_library)
end

def test_detects_minitest
dependencies = { "minitest" => "1.2.3" }
Bundler.locked_gems.stubs(dependencies: dependencies)
stub_dependencies("minitest" => "1.2.3")

assert_equal("minitest", DependencyDetector.instance.detected_test_library)
end

def test_does_not_detect_minitest_related_gems_as_minitest
dependencies = { "minitest-reporters" => "1.2.3" }
Bundler.locked_gems.stubs(dependencies: dependencies)
stub_dependencies("minitest-reporters" => "1.2.3")

assert_equal("unknown", DependencyDetector.instance.detected_test_library)
end

def test_detects_test_unit
dependencies = { "test-unit" => "1.2.3" }
Bundler.locked_gems.stubs(dependencies: dependencies)
stub_dependencies("test-unit" => "1.2.3")

assert_equal("test-unit", DependencyDetector.instance.detected_test_library)
end

def test_detects_rails_if_both_rails_and_minitest_are_present
dependencies = { "minitest" => "1.2.3", "rails" => "1.2.3" }
Bundler.locked_gems.stubs(dependencies: dependencies)
stub_dependencies("minitest" => "1.2.3", "rails" => "1.2.3")

assert_equal("rails", DependencyDetector.instance.detected_test_library)
end

def test_direct_dependency_returns_false_outside_of_bundle
File.expects(:file?).at_least_once.returns(false)
stub_dependencies({})
refute(DependencyDetector.instance.direct_dependency?(/^ruby-lsp/))
end

private

def stub_dependencies(dependencies)
Bundler.locked_gems.stubs(dependencies: dependencies)
Singleton.__init__(RubyLsp::DependencyDetector)
end
end
end
5 changes: 0 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ class Test
def stub_no_typechecker
RubyLsp::DependencyDetector.instance.stubs(:typechecker).returns(false)
end

sig { void }
def teardown
Singleton.__init__(RubyLsp::DependencyDetector)
end
end
end

Expand Down

0 comments on commit 185b6c5

Please sign in to comment.