@@ -506,6 +506,39 @@ def test_ruby_lsp_rails_is_automatically_included_in_rails_apps
506506 end
507507 end
508508
509+ def test_ruby_lsp_rails_detection_handles_lang_from_environment
510+ with_default_external_encoding ( "us-ascii" ) do
511+ Dir . mktmpdir do |dir |
512+ FileUtils . mkdir ( "#{ dir } /config" )
513+ FileUtils . cp ( "test/fixtures/rails_application.rb" , "#{ dir } /config/application.rb" )
514+ Dir . chdir ( dir ) do
515+ File . write ( File . join ( dir , "Gemfile" ) , <<~GEMFILE )
516+ source "https://rubygems.org"
517+ gem "rails"
518+ GEMFILE
519+
520+ capture_subprocess_io do
521+ Bundler . with_unbundled_env do
522+ # Run bundle install to generate the lockfile
523+ system ( "bundle install" )
524+ end
525+ end
526+
527+ Object . any_instance . expects ( :system ) . with (
528+ bundle_env ( dir , ".ruby-lsp/Gemfile" ) ,
529+ "(bundle check || bundle install) 1>&2" ,
530+ ) . returns ( true )
531+ Bundler . with_unbundled_env do
532+ run_script ( dir )
533+ end
534+
535+ assert_path_exists ( ".ruby-lsp/Gemfile" )
536+ assert_match ( 'gem "ruby-lsp-rails"' , File . read ( ".ruby-lsp/Gemfile" ) )
537+ end
538+ end
539+ end
540+ end
541+
509542 def test_recovers_from_stale_lockfiles
510543 Dir . mktmpdir do |dir |
511544 custom_dir = File . join ( dir , ".ruby-lsp" )
@@ -575,6 +608,30 @@ def test_recovers_from_stale_lockfiles
575608
576609 private
577610
611+ def with_default_external_encoding ( encoding , &block )
612+ ignore_warnings do
613+ original_encoding = Encoding . default_external
614+ begin
615+ Encoding . default_external = encoding
616+ block . call
617+ ensure
618+ Encoding . default_external = original_encoding
619+ end
620+ end
621+ end
622+
623+ def ignore_warnings ( &block )
624+ # Since overwriting the encoding emits a warning
625+ previous = $VERBOSE
626+ $VERBOSE = nil
627+
628+ begin
629+ block . call
630+ ensure
631+ $VERBOSE = previous
632+ end
633+ end
634+
578635 # This method runs the script and then immediately unloads it. This allows us to make assertions against the effects
579636 # of running the script multiple times
580637 def run_script ( path = Dir . pwd , expected_path : nil , **options )
0 commit comments