From 6afdde88a891faebf624d22c54715bc271de03b7 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 21 Apr 2022 17:28:55 +0200 Subject: [PATCH 1/8] Only add absolute paths in $LOAD_PATH, not relative paths --- Rakefile | 2 +- run-test.rb | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Rakefile b/Rakefile index b886d9a78d..115344311f 100644 --- a/Rakefile +++ b/Rakefile @@ -30,7 +30,7 @@ end desc "Run test" task :test do - ENV["RUBYOPT"] = "-Ilib -Itest/lib -rbundler/setup -rhelper" + ENV["RUBYOPT"] = "-rbundler/setup" ruby("run-test.rb") end diff --git a/run-test.rb b/run-test.rb index 4ccb024a6b..037d5d836d 100755 --- a/run-test.rb +++ b/run-test.rb @@ -1,8 +1,10 @@ #!/usr/bin/env ruby -$LOAD_PATH.unshift("test") -$LOAD_PATH.unshift("test/lib") -$LOAD_PATH.unshift("lib") +$LOAD_PATH.unshift("#{__dir__}/test") +$LOAD_PATH.unshift("#{__dir__}/test/lib") +$LOAD_PATH.unshift("#{__dir__}/lib") + +require_relative 'test/lib/helper' Dir.glob("test/strscan/**/*test_*.rb") do |test_rb| require File.expand_path(test_rb) From 3efd592ece043f4e8913096073539718e74c3cbb Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 21 Apr 2022 17:29:15 +0200 Subject: [PATCH 2/8] Add support for TruffleRuby in the gem * This is necessary since lib/strscan.rb was added (#25), as the TruffleRuby stdlib strscan.rb is no longer found first by `require "strscan"`. * Write conditions in a way that any other Ruby implementation would simply use its stdlib until it is added explicit support in this gem. --- .github/workflows/ci.yml | 4 ++++ Rakefile | 4 +++- ext/strscan/extconf.rb | 10 +++++++--- lib/strscan.rb | 5 ++++- strscan.gemspec | 2 +- test/strscan/test_stringscanner.rb | 6 ++++++ 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 016b288c19..e427450464 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,11 +26,15 @@ jobs: - '3.1' - debug - jruby-head + - truffleruby + - truffleruby-head include: - { os: windows-latest , ruby: mingw } - { os: windows-latest , ruby: mswin } exclude: - { os: windows-latest , ruby: debug } + - { os: windows-latest , ruby: truffleruby } + - { os: windows-latest , ruby: truffleruby-head } steps: - uses: actions/checkout@v2 diff --git a/Rakefile b/Rakefile index 115344311f..7158c50a18 100644 --- a/Rakefile +++ b/Rakefile @@ -23,9 +23,11 @@ if RUBY_ENGINE == "jruby" ext.target_version = '1.8' ext.ext_dir = 'ext/java' end -else +elsif RUBY_ENGINE == "ruby" require 'rake/extensiontask' Rake::ExtensionTask.new("strscan") +else + task :compile end desc "Run test" diff --git a/ext/strscan/extconf.rb b/ext/strscan/extconf.rb index f0ecbf85d8..b53b63e455 100644 --- a/ext/strscan/extconf.rb +++ b/ext/strscan/extconf.rb @@ -1,5 +1,9 @@ # frozen_string_literal: true require 'mkmf' -$INCFLAGS << " -I$(top_srcdir)" if $extmk -have_func("onig_region_memsize", "ruby.h") -create_makefile 'strscan' +if RUBY_ENGINE == 'ruby' + $INCFLAGS << " -I$(top_srcdir)" if $extmk + have_func("onig_region_memsize", "ruby.h") + create_makefile 'strscan' +else + File.write('Makefile', dummy_makefile("").join) +end diff --git a/lib/strscan.rb b/lib/strscan.rb index 19baccff26..362ea3d078 100644 --- a/lib/strscan.rb +++ b/lib/strscan.rb @@ -1,6 +1,9 @@ if RUBY_ENGINE == 'jruby' require 'strscan.jar' JRuby::Util.load_ext("org.jruby.ext.strscan.StringScannerLibrary") -else +elsif RUBY_ENGINE == 'ruby' require 'strscan.so' +else + $LOAD_PATH.delete(__dir__) + require 'strscan' # load from stdlib end diff --git a/strscan.gemspec b/strscan.gemspec index 871636d86c..f31b14d3cb 100644 --- a/strscan.gemspec +++ b/strscan.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |s| s.files = %w{lib/strscan.jar lib/strscan.rb} s.platform = "java" else - s.files = %w{ext/strscan/extconf.rb ext/strscan/strscan.c} + s.files = %w{ext/strscan/extconf.rb ext/strscan/strscan.c lib/strscan.rb} s.extensions = %w{ext/strscan/extconf.rb} end s.required_ruby_version = ">= 2.4.0" diff --git a/test/strscan/test_stringscanner.rb b/test/strscan/test_stringscanner.rb index 6e30be1f7d..8f05f7ebbc 100644 --- a/test/strscan/test_stringscanner.rb +++ b/test/strscan/test_stringscanner.rb @@ -207,6 +207,8 @@ def test_pos_unicode end def test_charpos_not_use_string_methods + pend "not supported on TruffleRuby" if RUBY_ENGINE == "truffleruby" + string = +'abcädeföghi' scanner = create_string_scanner(string) @@ -567,6 +569,8 @@ def test_encoding_string end def test_invalid_encoding_string + pend "no encoding check on TruffleRuby for scan(String)" if RUBY_ENGINE == "truffleruby" + str = "\xA1\xA2".dup.force_encoding("euc-jp") ss = create_string_scanner(str) assert_raise(Encoding::CompatibilityError) do @@ -712,6 +716,8 @@ def test_inspect2 end def test_aref_without_regex + pend "#[:missing] always raises on TruffleRuby if matched" if RUBY_ENGINE == "truffleruby" + s = create_string_scanner('abc') s.get_byte assert_nil(s[:c]) From 9e5c4d174b9d1fe33c10e9e81db954e9263aa5e0 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 21 Apr 2022 18:06:44 +0200 Subject: [PATCH 3/8] Improve CI speed by testing one version per OS --- .github/workflows/ci.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e427450464..c2f61044cf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,10 +12,8 @@ jobs: fail-fast: false matrix: os: - - ubuntu-20.04 - - ubuntu-18.04 - - macos-11.0 - - macos-10.15 + - ubuntu-latest + - macos-latest - windows-latest ruby: - '2.4' From c1646cd921acea27ce2551cde0f41ae0f057a078 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Fri, 22 Apr 2022 12:07:59 +0200 Subject: [PATCH 4/8] Prefer omit to pend * It is less noisy in the test output, and such skipped tests are not issues of this gem. --- test/strscan/test_ractor.rb | 2 +- test/strscan/test_stringscanner.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/strscan/test_ractor.rb b/test/strscan/test_ractor.rb index 480c1ae8a6..a5de7e56ed 100644 --- a/test/strscan/test_ractor.rb +++ b/test/strscan/test_ractor.rb @@ -3,7 +3,7 @@ class TestStringScannerRactor < Test::Unit::TestCase def setup - pend unless defined? Ractor + omit "Ractor not defined" unless defined? Ractor end def test_ractor diff --git a/test/strscan/test_stringscanner.rb b/test/strscan/test_stringscanner.rb index 8f05f7ebbc..b46b47bedd 100644 --- a/test/strscan/test_stringscanner.rb +++ b/test/strscan/test_stringscanner.rb @@ -207,7 +207,7 @@ def test_pos_unicode end def test_charpos_not_use_string_methods - pend "not supported on TruffleRuby" if RUBY_ENGINE == "truffleruby" + omit "not supported on TruffleRuby" if RUBY_ENGINE == "truffleruby" string = +'abcädeföghi' scanner = create_string_scanner(string) @@ -569,7 +569,7 @@ def test_encoding_string end def test_invalid_encoding_string - pend "no encoding check on TruffleRuby for scan(String)" if RUBY_ENGINE == "truffleruby" + omit "no encoding check on TruffleRuby for scan(String)" if RUBY_ENGINE == "truffleruby" str = "\xA1\xA2".dup.force_encoding("euc-jp") ss = create_string_scanner(str) @@ -716,7 +716,7 @@ def test_inspect2 end def test_aref_without_regex - pend "#[:missing] always raises on TruffleRuby if matched" if RUBY_ENGINE == "truffleruby" + omit "#[:missing] always raises on TruffleRuby if matched" if RUBY_ENGINE == "truffleruby" s = create_string_scanner('abc') s.get_byte From 34e02628d905c46577cddc739a0a3a7e180cefa2 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Fri, 22 Apr 2022 12:12:02 +0200 Subject: [PATCH 5/8] Remove unnecessary $LOAD_PATH changes --- run-test.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/run-test.rb b/run-test.rb index 037d5d836d..c864e52148 100755 --- a/run-test.rb +++ b/run-test.rb @@ -1,7 +1,5 @@ #!/usr/bin/env ruby -$LOAD_PATH.unshift("#{__dir__}/test") -$LOAD_PATH.unshift("#{__dir__}/test/lib") $LOAD_PATH.unshift("#{__dir__}/lib") require_relative 'test/lib/helper' From 16397dc16e12108869f6ce786f2e77bacc0e15aa Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Fri, 22 Apr 2022 12:27:38 +0200 Subject: [PATCH 6/8] Run tests on the installed gem too in CI --- .github/workflows/ci.yml | 4 ++++ Rakefile | 2 +- run-test.rb | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c2f61044cf..d6340e489d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,3 +75,7 @@ jobs: - run: bundle exec rake test - run: gem install --verbose --backtrace pkg/*.gem + + - name: Run tests on the installed gem + run: ruby run-test.rb + if: matrix.ruby != '2.4' # strscan is a default gem from 2.5 diff --git a/Rakefile b/Rakefile index 7158c50a18..4b9050e5d1 100644 --- a/Rakefile +++ b/Rakefile @@ -32,7 +32,7 @@ end desc "Run test" task :test do - ENV["RUBYOPT"] = "-rbundler/setup" + ENV["RUBYOPT"] = "-Ilib -rbundler/setup" ruby("run-test.rb") end diff --git a/run-test.rb b/run-test.rb index c864e52148..247926c7d4 100755 --- a/run-test.rb +++ b/run-test.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby -$LOAD_PATH.unshift("#{__dir__}/lib") +require 'strscan' +puts "Loaded strscan from:", *$".grep(/\/strscan\./) require_relative 'test/lib/helper' From d374f962e0e137d5d4abaa2d63ebeab6beede6a0 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Sat, 23 Apr 2022 10:29:29 +0900 Subject: [PATCH 7/8] Use strscan.rb only with JRuby --- Rakefile | 3 ++- lib/jruby/strscan.rb | 2 ++ lib/strscan.rb | 9 --------- strscan.gemspec | 9 ++++----- 4 files changed, 8 insertions(+), 15 deletions(-) create mode 100644 lib/jruby/strscan.rb delete mode 100644 lib/strscan.rb diff --git a/Rakefile b/Rakefile index 4b9050e5d1..dfd1a008ad 100644 --- a/Rakefile +++ b/Rakefile @@ -32,7 +32,8 @@ end desc "Run test" task :test do - ENV["RUBYOPT"] = "-Ilib -rbundler/setup" + require_path = RUBY_ENGINE == 'jruby' ? "lib/jruby" : "lib" + ENV["RUBYOPT"] = "-I#{require_path} -rbundler/setup" ruby("run-test.rb") end diff --git a/lib/jruby/strscan.rb b/lib/jruby/strscan.rb new file mode 100644 index 0000000000..4f796c25cd --- /dev/null +++ b/lib/jruby/strscan.rb @@ -0,0 +1,2 @@ +require 'strscan.jar' +JRuby::Util.load_ext("org.jruby.ext.strscan.StringScannerLibrary") diff --git a/lib/strscan.rb b/lib/strscan.rb deleted file mode 100644 index 362ea3d078..0000000000 --- a/lib/strscan.rb +++ /dev/null @@ -1,9 +0,0 @@ -if RUBY_ENGINE == 'jruby' - require 'strscan.jar' - JRuby::Util.load_ext("org.jruby.ext.strscan.StringScannerLibrary") -elsif RUBY_ENGINE == 'ruby' - require 'strscan.so' -else - $LOAD_PATH.delete(__dir__) - require 'strscan' # load from stdlib -end diff --git a/strscan.gemspec b/strscan.gemspec index f31b14d3cb..d59e645695 100644 --- a/strscan.gemspec +++ b/strscan.gemspec @@ -16,15 +16,14 @@ Gem::Specification.new do |s| s.summary = "Provides lexical scanning operations on a String." s.description = "Provides lexical scanning operations on a String." - s.require_path = %w{lib} - jruby = true if Gem::Platform.new('java') =~ s.platform or RUBY_ENGINE == 'jruby' - if jruby - s.files = %w{lib/strscan.jar lib/strscan.rb} + s.require_paths = %w{lib/jruby lib} + s.files = %w{lib/strscan.jar lib/jruby/strscan.rb} s.platform = "java" else - s.files = %w{ext/strscan/extconf.rb ext/strscan/strscan.c lib/strscan.rb} + s.require_paths = %w{lib} + s.files = %w{ext/strscan/extconf.rb ext/strscan/strscan.c} s.extensions = %w{ext/strscan/extconf.rb} end s.required_ruby_version = ">= 2.4.0" From 441332e5c19a3b40e75a179d30de42c204d8118f Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 30 Apr 2022 13:53:23 +0200 Subject: [PATCH 8/8] Show better where strscan comes from --- run-test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/run-test.rb b/run-test.rb index 247926c7d4..b4390272ad 100755 --- a/run-test.rb +++ b/run-test.rb @@ -1,7 +1,8 @@ #!/usr/bin/env ruby require 'strscan' -puts "Loaded strscan from:", *$".grep(/\/strscan\./) +puts "Loaded strscan from #{$".grep(/\/strscan\./).join(', ')}" +puts "Gem from #{Gem.loaded_specs["strscan"]&.full_gem_path}" require_relative 'test/lib/helper'