From 1ed51d04754d782d70398c75000d2d23877326e9 Mon Sep 17 00:00:00 2001 From: Chedli Bourguiba Date: Fri, 24 May 2024 14:09:17 +0200 Subject: [PATCH] Fix compatibility with --frozen-string-literal --- .github/workflows/ci.yml | 19 +++++++++---------- lib/spring/json.rb | 5 ++--- test/support/acceptance_test.rb | 4 ++-- test/support/application.rb | 4 ++-- test/unit/json_test.rb | 12 ++++++++++++ 5 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 test/unit/json_test.rb diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0951901a..8bfd5d47 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,21 +6,20 @@ jobs: strategy: fail-fast: false matrix: - ruby: [ '3.1', '3.2', '3.3', 'head' ] + ruby: [ '3.1', '3.2', 'head' ] rails: [ '7.1', 'edge' ] + rubyopt: [""] include: - - ruby: '2.7' - rails: '6.1' - - ruby: '3.0' - rails: '6.1' - - ruby: '3.1' - rails: '7.0' + - ruby: '3.3' + rails: '7.1' + rubyopt: "--enable-frozen-string-literal" env: RAILS_VERSION: ${{ matrix.rails }} + RUBYOPT: ${{ matrix.rubyopt }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 @@ -29,10 +28,10 @@ jobs: bundler-cache: true - name: Run unit tests - run: bundle exec rake test:unit + run: bundle exec rake test:unit RUBYOPT="${{ matrix.rubyopt }}" timeout-minutes: 3 - name: Run acceptance tests - run: bundle exec rake test:acceptance + run: bundle exec rake test:acceptance RUBYOPT="${{ matrix.rubyopt }}" timeout-minutes: 10 if: ${{ matrix.rails != 'edge' && matrix.ruby != 'head' }} # Acceptance tests use `gem install rails && rails new` diff --git a/lib/spring/json.rb b/lib/spring/json.rb index d4d71b6e..00f0dca3 100644 --- a/lib/spring/json.rb +++ b/lib/spring/json.rb @@ -1,4 +1,4 @@ -# encoding: UTF-8 +# frozen_string_literal: true # ### WHY SPRING VENDORS A JSON LIBRARY ### # @@ -13,7 +13,6 @@ module Spring module JSON def self.load(string) - string.force_encoding("utf-8") OkJson.decode(string) end @@ -364,7 +363,7 @@ def unquote(q) end end if rubydoesenc? - a[w] = '' << uchar + a[w] = +'' << uchar w += 1 else w += ucharenc(a, w, uchar) diff --git a/test/support/acceptance_test.rb b/test/support/acceptance_test.rb index af2acc31..8366fe2f 100644 --- a/test/support/acceptance_test.rb +++ b/test/support/acceptance_test.rb @@ -12,9 +12,9 @@ class AcceptanceTest < ActiveSupport::TestCase def rails_version if ENV['RAILS_VERSION'] == "edge" - ">= 7.1.0.alpha" + ">= 8.0.0.alpha" else - "~> #{ENV['RAILS_VERSION'] || "6.1"}.0" + "~> #{ENV['RAILS_VERSION'] || "7.1"}.0" end end diff --git a/test/support/application.rb b/test/support/application.rb index baeb0092..e200af66 100644 --- a/test/support/application.rb +++ b/test/support/application.rb @@ -168,7 +168,7 @@ def read_streams end def read_stream(stream) - output = "" + output = +"" while IO.select([stream], [], [], 0.5) && !stream.eof? output << stream.readpartial(10240) end @@ -176,7 +176,7 @@ def read_stream(stream) end def dump_streams(command, streams) - output = "$ #{command}\n" + output = +"$ #{command}\n" streams.each do |name, stream| unless stream.chomp.empty? diff --git a/test/unit/json_test.rb b/test/unit/json_test.rb new file mode 100644 index 00000000..b9eecc05 --- /dev/null +++ b/test/unit/json_test.rb @@ -0,0 +1,12 @@ +require_relative "../helper" +require 'spring/json' + +class JsonTest < ActiveSupport::TestCase + test 'can decode unicode characters' do + assert_equal({"unicode_example"=>"©"}, Spring::JSON.load('{"unicode_example": "\u00A9"}')) + end + + test 'can encode' do + assert_equal('{}', Spring::JSON.dump({})) + end +end