Skip to content

Commit

Permalink
Assume UTF-8 when detecting Rails
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Aug 20, 2024
1 parent bec458c commit 0d9dcec
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/setup_bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def correct_relative_remote_paths
sig { returns(T::Boolean) }
def rails_app?
config = Pathname.new("config/application.rb").expand_path
application_contents = config.read if config.exist?
application_contents = config.read(external_encoding: Encoding::UTF_8) if config.exist?
return false unless application_contents

/class .* < (::)?Rails::Application/.match?(application_contents)
Expand Down
7 changes: 7 additions & 0 deletions sorbet/rbi/shims/encoding.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# typed: true

class Encoding < Object
sig {returns(Encoding)}
sig {returns(String)}
def self.default_external; end
end
16 changes: 16 additions & 0 deletions sorbet/rbi/shims/pathname.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# typed: false

class Pathname
sig do
params(
external_encoding: T.any(String, Encoding),
internal_encoding: T.any(String, Encoding),
encoding: T.any(String, Encoding),
textmode: BasicObject,
binmode: BasicObject,
autoclose: BasicObject,
mode: String,
).returns(String)
end
def read(external_encoding: T.unsafe(nil), internal_encoding: T.unsafe(nil), encoding: T.unsafe(nil), textmode: T.unsafe(nil), binmode: T.unsafe(nil), autoclose: T.unsafe(nil), mode: T.unsafe(nil)); end
end
1 change: 1 addition & 0 deletions test/fixtures/rails_application.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module MyApp
class Application < Rails::Application
# あ
end
end
57 changes: 57 additions & 0 deletions test/setup_bundler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,39 @@ def test_ruby_lsp_rails_is_automatically_included_in_rails_apps
end
end

def test_ruby_lsp_rails_detection_handles_lang_from_environment
with_default_external_encoding("us-ascii") do
Dir.mktmpdir do |dir|
FileUtils.mkdir("#{dir}/config")
FileUtils.cp("test/fixtures/rails_application.rb", "#{dir}/config/application.rb")
Dir.chdir(dir) do
File.write(File.join(dir, "Gemfile"), <<~GEMFILE)
source "https://rubygems.org"
gem "rails"
GEMFILE

capture_subprocess_io do
Bundler.with_unbundled_env do
# Run bundle install to generate the lockfile
system("bundle install")
end
end

Object.any_instance.expects(:system).with(
bundle_env(dir, ".ruby-lsp/Gemfile"),
"(bundle check || bundle install) 1>&2",
).returns(true)
Bundler.with_unbundled_env do
run_script(dir)
end

assert_path_exists(".ruby-lsp/Gemfile")
assert_match('gem "ruby-lsp-rails"', File.read(".ruby-lsp/Gemfile"))
end
end
end
end

def test_recovers_from_stale_lockfiles
Dir.mktmpdir do |dir|
custom_dir = File.join(dir, ".ruby-lsp")
Expand Down Expand Up @@ -575,6 +608,30 @@ def test_recovers_from_stale_lockfiles

private

def with_default_external_encoding(encoding, &block)
ignore_warnings do
original_encoding = Encoding.default_external
begin
Encoding.default_external = encoding
block.call
ensure
Encoding.default_external = original_encoding
end
end
end

def ignore_warnings(&block)
# Since overwriting the encoding emits a warning
previous = $VERBOSE
$VERBOSE = nil

begin
block.call
ensure
$VERBOSE = previous
end
end

# This method runs the script and then immediately unloads it. This allows us to make assertions against the effects
# of running the script multiple times
def run_script(path = Dir.pwd, expected_path: nil, **options)
Expand Down

0 comments on commit 0d9dcec

Please sign in to comment.