Skip to content

Commit

Permalink
Include non-development gemspec dependencies in DependencyDetector ch…
Browse files Browse the repository at this point in the history
…eck (#1031)

Co-authored-by: Ufuk Kayserilioglu <[email protected]>
Co-authored-by: Vinicius Stock <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2023
1 parent 778fc07 commit eecfdef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/ruby_lsp/requests/support/dependency_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def detect_test_library

sig { params(gem_pattern: Regexp).returns(T::Boolean) }
def direct_dependency?(gem_pattern)
dependency_keys.grep(gem_pattern).any?
dependencies.any?(gem_pattern)
end

sig { returns(T::Boolean) }
Expand All @@ -65,16 +65,24 @@ def detect_typechecker
end

sig { returns(T::Array[String]) }
def dependency_keys
@dependency_keys ||= T.let(
def dependencies
# NOTE: If changing this behaviour, it's likely that the VS Code extension will also need changed.
@dependencies ||= T.let(
begin
Bundler.with_original_env { Bundler.default_gemfile }
Bundler.locked_gems.dependencies.keys
Bundler.locked_gems.dependencies.keys + gemspec_dependencies
rescue Bundler::GemfileNotFound
[]
end,
T.nilable(T::Array[String]),
)
end

sig { returns(T::Array[String]) }
def gemspec_dependencies
Bundler.locked_gems.sources
.grep(Bundler::Source::Gemspec)
.flat_map { _1.gemspec&.dependencies&.map(&:name) }
end
end
end
8 changes: 8 additions & 0 deletions test/requests/support/dependency_detector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

module RubyLsp
class DependencyDetectorTest < Minitest::Test
def teardown
Singleton.__init__(RubyLsp::DependencyDetector)
end

def test_detects_no_test_library_when_there_are_no_dependencies
stub_dependencies({})

Expand All @@ -29,6 +33,10 @@ def test_detects_test_unit
assert_equal("test-unit", DependencyDetector.instance.detected_test_library)
end

def test_detects_dependencies_in_gemspecs
assert(DependencyDetector.instance.direct_dependency?(/^yarp$/))
end

def test_detects_rails_if_both_rails_and_minitest_are_present
stub_dependencies("minitest" => "1.2.3", "rails" => "1.2.3")

Expand Down

0 comments on commit eecfdef

Please sign in to comment.