From e5ad0da4534ee253e59b2a338c4dc7411432b104 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Thu, 8 Aug 2024 11:42:40 -0400 Subject: [PATCH] Apply earl's suggestion --- lib/ruby_lsp/setup_bundler.rb | 10 ++++--- test/setup_bundler_test.rb | 50 +++++++++++++++++------------------ 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/lib/ruby_lsp/setup_bundler.rb b/lib/ruby_lsp/setup_bundler.rb index 903f43a3d..af354f9a2 100644 --- a/lib/ruby_lsp/setup_bundler.rb +++ b/lib/ruby_lsp/setup_bundler.rb @@ -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 diff --git a/test/setup_bundler_test.rb b/test/setup_bundler_test.rb index 77f3b6059..a65d7b3ca 100644 --- a/test/setup_bundler_test.rb +++ b/test/setup_bundler_test.rb @@ -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