Skip to content

Commit

Permalink
Apply earl's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Aug 8, 2024
1 parent 41d69b5 commit e5ad0da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
10 changes: 6 additions & 4 deletions lib/ruby_lsp/setup_bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,13 @@ def correct_relative_remote_paths
# Detects if the project is a Rails app by looking if the superclass of the main class is `Rails::Application`
sig { returns(T::Boolean) }
def rails_app?
config = Pathname.new("config/application.rb").expand_path
application_contents = config.read if config.exist?
return false unless application_contents
/class .* < Rails::Application/.match?(rails_application_content)
end

/class .* < Rails::Application/.match?(application_contents)
sig { returns(T.nilable(String)) }
def rails_application_content
config = Pathname.new("config/application.rb").expand_path
config.read if config.exist?
end
end
end
50 changes: 25 additions & 25 deletions test/setup_bundler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,33 @@ def test_creates_custom_bundle
end

def test_creates_custom_bundle_for_a_rails_app
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
FileUtils.mkdir(File.join(dir, "config"))
File.write(File.join(dir, "config", "application.rb"), <<~RUBY)
module MyApp
class Application < Rails::Application
end
end
RUBY

Object.any_instance.expects(:system).with(
bundle_env(".ruby-lsp/Gemfile"),
"(bundle check || bundle install) 1>&2",
).returns(true)
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({ "rails" => true }).at_least_once
run_script
Object.any_instance.expects(:system).with(
bundle_env(".ruby-lsp/Gemfile"),
"(bundle check || bundle install) 1>&2",
).returns(true)

assert_path_exists(".ruby-lsp")
assert_path_exists(".ruby-lsp/Gemfile")
assert_path_exists(".ruby-lsp/Gemfile.lock")
assert_path_exists(".ruby-lsp/main_lockfile_hash")
gemfile_content = File.read(".ruby-lsp/Gemfile")
assert_match("ruby-lsp", gemfile_content)
assert_match("debug", gemfile_content)
assert_match("ruby-lsp-rails", gemfile_content)
# There is some unknown state leak with Bundler which prevents us from creating the
# folder structure ourselves lest we encounter flaky tests
RubyLsp::SetupBundler.any_instance.stubs(:rails_application_content).returns(<<~RUBY)
module MyApp
class Application < Rails::Application
end
end
end
RUBY

Bundler::LockfileParser.any_instance.expects(:dependencies).returns({ "rails" => true }).at_least_once

run_script

assert_path_exists(".ruby-lsp")
assert_path_exists(".ruby-lsp/Gemfile")
assert_path_exists(".ruby-lsp/Gemfile.lock")
assert_path_exists(".ruby-lsp/main_lockfile_hash")
assert_match("ruby-lsp", File.read(".ruby-lsp/Gemfile"))
assert_match("debug", File.read(".ruby-lsp/Gemfile"))
assert_match("ruby-lsp-rails", File.read(".ruby-lsp/Gemfile"))
ensure
FileUtils.rm_r(".ruby-lsp") if Dir.exist?(".ruby-lsp")
end

def test_changing_lockfile_causes_custom_bundle_to_be_rebuilt
Expand Down

0 comments on commit e5ad0da

Please sign in to comment.