From b374b3981f06eabb029901b3b0b66914191e6e6a Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 3 May 2019 20:18:51 +0200 Subject: [PATCH] Make JRuby before command hook work on Aruba environment --- lib/aruba/config/jruby.rb | 16 ++++++--- spec/aruba/jruby_spec.rb | 73 +++++++++++++++++++++++++-------------- 2 files changed, 59 insertions(+), 30 deletions(-) diff --git a/lib/aruba/config/jruby.rb b/lib/aruba/config/jruby.rb index 743e77a4f..3c734e0cd 100644 --- a/lib/aruba/config/jruby.rb +++ b/lib/aruba/config/jruby.rb @@ -5,13 +5,21 @@ config.before :command do next unless RUBY_PLATFORM == 'java' + jruby_opts = aruba.environment['JRUBY_OPTS'] || '' + # disable JIT since these processes are so short lived - ENV['JRUBY_OPTS'] = "-X-C #{ENV['JRUBY_OPTS']}" unless (ENV['JRUBY_OPTS'] || '') .include? '-X-C' + jruby_opts = "-X-C #{jruby_opts}" unless jruby_opts.include? '-X-C' # Faster startup for jruby - ENV['JRUBY_OPTS'] = "--dev #{ENV['JRUBY_OPTS']}" unless (ENV['JRUBY_OPTS'] || '').include? '--dev' + jruby_opts = "--dev #{jruby_opts}" unless jruby_opts.include? '--dev' + + set_environment_variable('JRUBY_OPTS', jruby_opts) + + if RbConfig::CONFIG['host_os'] =~ /solaris|sunos/i + java_opts = aruba.environment['JAVA_OPTS'] || '' - # force jRuby to use client JVM for faster startup times - ENV['JAVA_OPTS'] = "-d32 #{ENV['JAVA_OPTS']}" if RbConfig::CONFIG['host_os'] =~ /solaris|sunos/i && !(ENV['JAVA_OPTS'] || '').include?('-d32') + # force jRuby to use client JVM for faster startup times + set_environment_variable('JAVA_OPTS', "-d32 #{java_opts}") unless java_opts.include?('-d32') + end end end diff --git a/spec/aruba/jruby_spec.rb b/spec/aruba/jruby_spec.rb index e079cb432..4e41ae14d 100644 --- a/spec/aruba/jruby_spec.rb +++ b/spec/aruba/jruby_spec.rb @@ -2,54 +2,75 @@ require 'aruba/api' describe "Aruba JRuby Startup Helper" do - before(:all) do - @fake_env = ENV.clone - end + include Aruba::Api + include_context 'uses aruba API' + + let(:rb_config) { double('RbConfig::CONFIG') } - before :each do + before do Aruba.config.reset - # Define before_cmd-hook + # Define before :command hook load 'aruba/config/jruby.rb' end - before(:each) do - @fake_env['JRUBY_OPTS'] = "--1.9" - @fake_env['JAVA_OPTS'] = "-Xdebug" - - stub_const('ENV', @fake_env) + around do |example| + with_environment('JRUBY_OPTS' => '--1.9', 'JAVA_OPTS' => '-Xdebug') do + example.run + end end - context 'when some mri ruby' do - before :each do + context 'when running under some MRI Ruby' do + before do stub_const('RUBY_PLATFORM', 'x86_64-chocolate') end - before :each do + it 'keeps the existing JRuby and Java option values' do + # Run defined before :command hook Aruba.config.before :command, self - end - it { expect(ENV['JRUBY_OPTS']).to eq '--1.9' } - it { expect(ENV['JAVA_OPTS']).to eq '-Xdebug' } + with_environment do + expect(ENV['JRUBY_OPTS']).to eq '--1.9' + expect(ENV['JAVA_OPTS']).to eq '-Xdebug' + end + end end - context 'when jruby ruby' do - before :each do - stub_const('RUBY_PLATFORM', 'java') + context 'when running under JRuby but not on Solaris' do + before do + stub_const 'RUBY_PLATFORM', 'java' + stub_const 'RbConfig::CONFIG', rb_config + + allow(rb_config).to receive(:[]).with('host_os').and_return('foo-os') end - before :each do - rb_config = double('rb_config') - allow(rb_config).to receive(:[]).and_return('solaris') + it 'updates the existing JRuby but not Java option values' do + # Run defined before :command hook + Aruba.config.before :command, self - stub_const 'RbConfig::CONFIG', rb_config + with_environment do + expect(ENV['JRUBY_OPTS']).to eq '--dev -X-C --1.9' + expect(ENV['JAVA_OPTS']).to eq '-Xdebug' + end end + end + context 'when running under JRuby on Solaris' do before :each do - Aruba.config.before :command, self + stub_const 'RUBY_PLATFORM', 'java' + stub_const 'RbConfig::CONFIG', rb_config + + allow(rb_config).to receive(:[]).with('host_os').and_return('solaris') end - it { expect(ENV['JRUBY_OPTS']).to eq '--dev -X-C --1.9' } - it { expect(ENV['JAVA_OPTS']).to eq '-d32 -Xdebug' } + it 'keeps the existing JRuby and Java option values' do + # Run defined before :command hook + Aruba.config.before :command, self + + with_environment do + expect(ENV['JRUBY_OPTS']).to eq '--dev -X-C --1.9' + expect(ENV['JAVA_OPTS']).to eq '-d32 -Xdebug' + end + end end end