Skip to content

Commit

Permalink
Handle US_ASCII encoding when parsing rails application config
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlopain committed Aug 20, 2024
1 parent 88b85b8 commit 26bdc81
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
ruby: ["3.1", "3.2", "3.3"]
rubyopt: [""]
include:
- os: ubuntu-latest
ruby: "3.3"
rubyopt: "--encoding US-ASCII"
runs-on: ${{ matrix.os }}
timeout-minutes: 30
name: Ruby ${{ matrix.ruby }} on ${{ matrix.os }}
name: Ruby ${{ matrix.ruby }} on ${{ matrix.os }} ${{ matrix.rubyopt }}
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -45,6 +50,8 @@ jobs:
cache-version: 1

- name: Run tests
env:
RUBYOPT: ${{ matrix.rubyopt }}
run: bundle exec rake

- name: Run index troubleshooting tool
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_lsp/setup_bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def rails_app?
application_contents = config.read if config.exist?
return false unless application_contents

application_contents = application_contents.encode(Encoding::UTF_8, Encoding::UTF_8, invalid: :replace)
/class .* < (::)?Rails::Application/.match?(application_contents)
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/rails_application_utf8_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module MyApp
class Application < Rails::Application
# あ
end
end
28 changes: 19 additions & 9 deletions test/requests/support/expectations_test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,25 @@ def default_args
end

if expectation_path && File.file?(expectation_path)
class_eval(<<~RB, __FILE__, __LINE__ + 1)
def test_#{expectation_suffix}__#{test_name}
@_path = "#{path}"
source = File.read(@_path)
expected = File.read("#{expectation_path}")
initialize_params(expected)
assert_expectations(source, expected)
end
RB
# We run tests on CI with US_ASCII encoding, which would differ in expectations
# for UTF8 characters. Just skip these.
if expectation_path.include?("multibyte") && Encoding.default_external == Encoding::UTF_8
class_eval(<<~RB, __FILE__, __LINE__ + 1)
def test_#{expectation_suffix}__#{test_name}
@_path = "#{path}"
source = File.read(@_path)
expected = File.read("#{expectation_path}")
initialize_params(expected)
assert_expectations(source, expected)
end
RB
else
class_eval(<<~RB, __FILE__, __LINE__ + 1)
def test_#{expectation_suffix}__#{test_name}
skip "Running with non-utf8 encoding"
end
RB
end
else
class_eval(<<~RB, __FILE__, __LINE__ + 1)
def test_#{expectation_suffix}__#{test_name}__does_not_raise
Expand Down
31 changes: 31 additions & 0 deletions test/setup_bundler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,37 @@ def test_ruby_lsp_rails_is_automatically_included_in_rails_apps
end
end

def test_ruby_lsp_rails_is_automatically_included_in_rails_apps_utf8
Dir.mktmpdir do |dir|
FileUtils.mkdir("#{dir}/config")
FileUtils.cp("test/fixtures/rails_application_utf8_comment.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(".ruby-lsp/Gemfile"),
"(bundle check || bundle install) 1>&2",
).returns(true)
Bundler.with_unbundled_env do
run_script
end

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

def test_recovers_from_stale_lockfiles
Dir.mktmpdir do |dir|
custom_dir = File.join(dir, ".ruby-lsp")
Expand Down

0 comments on commit 26bdc81

Please sign in to comment.