From 7d5a220f644eff1f83ca90776935b65c1ee31c91 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Thu, 23 Sep 2021 23:57:26 -0500 Subject: [PATCH 1/8] Only do RubyVM patches if class exists This class does not exist in any implementation except CRuby. I would recommend moving this code somewhere else, like a separate file loaded only on CRuby or into CRuby itself. For now this change is sufficient to load the library on other implementations. --- lib/pp.rb | 58 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index 72480e5..04b1c43 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -530,37 +530,39 @@ def pretty_print(q) # :nodoc: end end -class RubyVM::AbstractSyntaxTree::Node - def pretty_print_children(q, names = []) - children.zip(names) do |c, n| - if n - q.breakable - q.text "#{n}:" - end - q.group(2) do - q.breakable - q.pp c +if defined?(RubyVM::AbstractSyntaxTree) + class RubyVM::AbstractSyntaxTree::Node + def pretty_print_children(q, names = []) + children.zip(names) do |c, n| + if n + q.breakable + q.text "#{n}:" + end + q.group(2) do + q.breakable + q.pp c + end end end - end - def pretty_print(q) - q.group(1, "(#{type}@#{first_lineno}:#{first_column}-#{last_lineno}:#{last_column}", ")") { - case type - when :SCOPE - pretty_print_children(q, %w"tbl args body") - when :ARGS - pretty_print_children(q, %w[pre_num pre_init opt first_post post_num post_init rest kw kwrest block]) - when :DEFN - pretty_print_children(q, %w[mid body]) - when :ARYPTN - pretty_print_children(q, %w[const pre rest post]) - when :HSHPTN - pretty_print_children(q, %w[const kw kwrest]) - else - pretty_print_children(q) - end - } + def pretty_print(q) + q.group(1, "(#{type}@#{first_lineno}:#{first_column}-#{last_lineno}:#{last_column}", ")") { + case type + when :SCOPE + pretty_print_children(q, %w"tbl args body") + when :ARGS + pretty_print_children(q, %w[pre_num pre_init opt first_post post_num post_init rest kw kwrest block]) + when :DEFN + pretty_print_children(q, %w[mid body]) + when :ARYPTN + pretty_print_children(q, %w[const pre rest post]) + when :HSHPTN + pretty_print_children(q, %w[const kw kwrest]) + else + pretty_print_children(q) + end + } + end end end From 60117f1728806f0346d7d95815a0e9f7ead2db7e Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Fri, 24 Sep 2021 00:02:07 -0500 Subject: [PATCH 2/8] Add CI job for JRuby --- .github/workflows/jruby.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/jruby.yml diff --git a/.github/workflows/jruby.yml b/.github/workflows/jruby.yml new file mode 100644 index 0000000..0e8cb1d --- /dev/null +++ b/.github/workflows/jruby.yml @@ -0,0 +1,30 @@ +name: build + +on: + push: + pull_request: + schedule: + - cron: '12 3 * * *' + +jobs: + build: + name: build (${{ matrix.ruby }} / ${{ matrix.os }}) + strategy: + matrix: + ruby: [ 'jruby-9.3' ] + os: [ ubuntu-latest ] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v2 + - name: Set up Ruby + uses: ruby/setup-ruby@v1.81.0 + with: + ruby-version: ${{ matrix.ruby }} + - name: Install dependencies + run: bundle install + - name: Build + run: rake build + - name: Run test + run: rake test + - name: Installation test + run: gem install pkg/*.gem From 316587a85f7cbcf38ff3a786d00cd59cb163cc60 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 28 Sep 2021 06:06:42 -0700 Subject: [PATCH 3/8] Enable bundler cache for JRuby job Co-authored-by: Olle Jonsson --- .github/workflows/jruby.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/jruby.yml b/.github/workflows/jruby.yml index 0e8cb1d..569a256 100644 --- a/.github/workflows/jruby.yml +++ b/.github/workflows/jruby.yml @@ -20,6 +20,7 @@ jobs: uses: ruby/setup-ruby@v1.81.0 with: ruby-version: ${{ matrix.ruby }} + bundler-cache: true # 'bundle install' and enable caching - name: Install dependencies run: bundle install - name: Build From 157b11e51890346e5fc77b9be5809d521bafbe30 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 28 Sep 2021 09:26:50 -0500 Subject: [PATCH 4/8] Additional changes from review comments * Remove separate install of bundler dependencies. * Use broader setup-ruby version to pick up JRuby 9.3 * Use `bundle exec` for rake tasks --- .github/workflows/jruby.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/jruby.yml b/.github/workflows/jruby.yml index 569a256..5eeb652 100644 --- a/.github/workflows/jruby.yml +++ b/.github/workflows/jruby.yml @@ -17,15 +17,13 @@ jobs: steps: - uses: actions/checkout@v2 - name: Set up Ruby - uses: ruby/setup-ruby@v1.81.0 + uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} bundler-cache: true # 'bundle install' and enable caching - - name: Install dependencies - run: bundle install - name: Build - run: rake build + run: bundle exec rake build - name: Run test - run: rake test + run: bundle exec rake test - name: Installation test run: gem install pkg/*.gem From 2061f994e024d1d2980e7ee4962090897aeca62f Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 28 Sep 2021 09:37:07 -0500 Subject: [PATCH 5/8] Use etc instead of .so for broader compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The use of `etc.so` here requires that etc is always implemented as a C extension on-disk. However at least one impl – JRuby – currently implements it as an internal extension, loaded via a Ruby script. This require should simply use the base name of the library, `etc`, to allow Ruby-based implementations to load as well. --- lib/pp.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pp.rb b/lib/pp.rb index 04b1c43..253aecb 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -424,7 +424,7 @@ def pretty_print(q) # :nodoc: class File < IO # :nodoc: class Stat # :nodoc: def pretty_print(q) # :nodoc: - require 'etc.so' + require 'etc' q.object_group(self) { q.breakable q.text sprintf("dev=0x%x", self.dev); q.comma_breakable From 721d8cbd103768bf743487bc5bd1c25a31bd2ca7 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 28 Sep 2021 09:53:03 -0500 Subject: [PATCH 6/8] Incorporate 2.6 versions of 2.7 methods needed * UnboundMethod#bind_call * ruby2_keywords gem for testing --- Gemfile | 1 + test/test_pp.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/Gemfile b/Gemfile index eb86192..0ab3dc1 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,4 @@ source "https://rubygems.org" gem "rake" gem "test-unit" +gem "ruby2_keywords", group: :test \ No newline at end of file diff --git a/test/test_pp.rb b/test/test_pp.rb index 4aa3195..ab8f959 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -3,6 +3,16 @@ require 'pp' require 'delegate' require 'test/unit' +require 'ruby2_keywords' + +# Define bind_call for Ruby 2.6 and earlier +class UnboundMethod + unless public_method_defined?(:bind_call) + def bind_call(obj, *args, &block) + bind(obj).call(*args, &block) + end + end +end module PPTestModule From 892c61295875771d41b86693670c2af48679dff0 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 7 Dec 2021 15:10:16 -0600 Subject: [PATCH 7/8] Add context to bind_call and skip 2.7+ test The bind_call definition here is added primarily to support running the tests on JRuby 9.3, which only supports Ruby 2.6 features. The excluded test appears to depend on inspect behavior that changed after Ruby 2.6. With these two changes the test suite runs green on Ruby 2.6 and JRuby 9.3. --- test/test_pp.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_pp.rb b/test/test_pp.rb index ab8f959..9cef555 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -5,7 +5,7 @@ require 'test/unit' require 'ruby2_keywords' -# Define bind_call for Ruby 2.6 and earlier +# Define bind_call for Ruby 2.6 and earlier, to allow testing on JRuby 9.3 class UnboundMethod unless public_method_defined?(:bind_call) def bind_call(obj, *args, &block) @@ -168,7 +168,7 @@ def test_withinspect a << HasInspect.new(a) assert_equal("[]\n", PP.pp(a, ''.dup)) assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) - end + end unless RUBY_VERSION < "2.7" # temporary mask to test on JRuby 9.3 (2.6 equivalent) def test_share_nil begin From d27e9da09c8f33e32a728434a6bd836cf7dacecb Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 7 Dec 2021 15:18:11 -0600 Subject: [PATCH 8/8] Skip installation test on JRuby for now THe latest released JRuby supports Ruby 2.6, but the installation test requires Ruby 2.7. Skip this test for now. --- .github/workflows/jruby.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/jruby.yml b/.github/workflows/jruby.yml index 5eeb652..2c51c22 100644 --- a/.github/workflows/jruby.yml +++ b/.github/workflows/jruby.yml @@ -25,5 +25,6 @@ jobs: run: bundle exec rake build - name: Run test run: bundle exec rake test - - name: Installation test - run: gem install pkg/*.gem +# Temporarily skipped until a released version of JRuby support 2.7+ +# - name: Installation test +# run: gem install pkg/*.gem