Skip to content

Commit 30e450e

Browse files
committed
allow to mangle ENV['GEM_PATH'] like we did before using jruby.rack.gem_path = 'env'
1 parent 4f7e0b0 commit 30e450e

File tree

2 files changed

+69
-27
lines changed

2 files changed

+69
-27
lines changed

src/main/ruby/jruby/rack/booter.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,15 @@ def boot!
104104
protected
105105

106106
def adjust_gem_path
107+
gem_path = self.gem_path
107108
case gem_path?
108-
when false then return false
109-
when true then
109+
when false then # org.jruby.rack.RackLogger::DEBUG
110+
if gem_path && ! gem_path.empty? &&
111+
( ! defined?(Gem.path) || ! Gem.path.include?(gem_path) )
112+
@rack_context.log("Gem.path won't be updated although seems configured: #{gem_path}")
113+
end
114+
return false
115+
when true then
110116
if env_gem_path = ENV['GEM_PATH']
111117
if gem_path.nil? || gem_path.empty?
112118
return # keep ENV['GEM_PATH'] as is
@@ -119,18 +125,27 @@ def adjust_gem_path
119125
else
120126
ENV['GEM_PATH'] = gem_path
121127
end
122-
else # default
128+
else # nil (default)
123129
begin
124-
require 'rubygems'
130+
require 'rubygems' unless defined? Gem.path
125131
rescue LoadError
126132
else
133+
return if gem_path.nil? || gem_path.empty?
127134
Gem.path.unshift(gem_path) unless Gem.path.include?(gem_path)
128135
end
129136
end
130137
end
131138

132-
def gem_path? # TODO
133-
@rack_context.getInitParameter('jruby.rack.env.gem_path') || nil
139+
# @return whether to update Gem.path and/or the environment GEM_PATH
140+
# - true/'env' forces ENV['GEM_PATH'] to be updated
141+
# - false disabled Gem.path mangling for good (leaves all as is)
142+
# - if not specified Gem.path will be updated based on setting
143+
def gem_path?
144+
return @_gem_path if defined? @_gem_path
145+
gem_path = @rack_context.getInitParameter('jruby.rack.gem_path')
146+
return @_gem_path = nil if gem_path.nil?
147+
return @_gem_path = false if gem_path.empty? || gem_path == 'false'
148+
@_gem_path = true # true / 'env'
134149
end
135150

136151
# @note called during {#boot!}

src/spec/ruby/jruby/rack/booter_spec.rb

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
@@rack_env = ENV['RACK_ENV']
2020
@@gem_path = Gem.path.dup
21+
@@env_gem_path = ENV['GEM_PATH']
2122

2223
after do
2324
@@rack_env.nil? ? ENV.delete('RACK_ENV') : ENV['RACK_ENV'] = @@rack_env
2425
Gem.path.replace(@@gem_path)
26+
@@env_gem_path.nil? ? ENV.delete('GEM_PATH') : ENV['GEM_PATH'] = @@env_gem_path
2527
end
2628

2729
it "should determine the public html root from the 'public.root' init parameter" do
@@ -101,22 +103,43 @@
101103
expect( Gem.path ).to eql [ "file:/home/gems", "/usr/local/gems" ]
102104
end
103105

104-
# it "prepends gem_path to ENV['GEM_PATH']" do
105-
# ENV['GEM_PATH'] = '/opt/gems'
106-
# @rack_context.should_receive(:getRealPath).
107-
# with("/WEB-INF").and_return "/opt/deploy/sample.war!/WEB-INF"
108-
# booter.boot!
109-
#
110-
# ENV['GEM_PATH'].should == "/opt/deploy/sample.war!/WEB-INF/gems#{File::PATH_SEPARATOR}/opt/gems"
111-
# end
106+
it "does not change Gem.path if gem_path empty" do
107+
Gem.path.replace [ '/opt/gems' ]
108+
booter.gem_path = ""
109+
booter.boot!
112110

113-
# it "prepends gem_path to ENV['GEM_PATH'] if not already present" do
114-
# ENV['GEM_PATH'] = "/home/gems#{File::PATH_SEPARATOR}/usr/local/gems"
115-
# booter.gem_path = '/usr/local/gems'
116-
# booter.boot!
117-
#
118-
# ENV['GEM_PATH'].should == "/home/gems#{File::PATH_SEPARATOR}/usr/local/gems"
119-
# end
111+
expect( Gem.path ).to eql [ '/opt/gems' ]
112+
end
113+
114+
it "prepends gem_path to ENV['GEM_PATH'] if jruby.rack.gem_path set to env" do
115+
@rack_context.should_receive(:getInitParameter).with("jruby.rack.gem_path").and_return "env"
116+
ENV['GEM_PATH'] = '/opt/gems'
117+
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/opt/deploy/sample.war!/WEB-INF"
118+
@rack_context.should_receive(:getRealPath).with("/WEB-INF/gems").and_return "/opt/deploy/sample.war!/WEB-INF/gems"
119+
120+
booter.boot!
121+
122+
ENV['GEM_PATH'].should == "/opt/deploy/sample.war!/WEB-INF/gems#{File::PATH_SEPARATOR}/opt/gems"
123+
end
124+
125+
it "does not prepend gem_path to ENV['GEM_PATH'] if jruby.rack.gem_path set not set" do
126+
ENV['GEM_PATH'] = '/opt/gems'
127+
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/opt/deploy/sample.war!/WEB-INF"
128+
@rack_context.should_receive(:getRealPath).with("/WEB-INF/gems").and_return "/opt/deploy/sample.war!/WEB-INF/gems"
129+
130+
booter.boot!
131+
132+
ENV['GEM_PATH'].should == "/opt/gems"
133+
end
134+
135+
it "prepends gem_path to ENV['GEM_PATH'] if not already present" do
136+
@rack_context.should_receive(:getInitParameter).with("jruby.rack.gem_path").and_return "env"
137+
ENV['GEM_PATH'] = "/home/gems#{File::PATH_SEPARATOR}/usr/local/gems"
138+
booter.gem_path = '/usr/local/gems'
139+
booter.boot!
140+
141+
ENV['GEM_PATH'].should == "/home/gems#{File::PATH_SEPARATOR}/usr/local/gems"
142+
end
120143

121144
# it "keeps ENV['GEM_PATH'] when gem_path is nil" do
122145
# ENV['GEM_PATH'] = '/usr/local/gems'
@@ -128,12 +151,16 @@
128151
# ENV['GEM_PATH'].should == "/usr/local/gems"
129152
# end
130153

131-
# it "sets ENV['GEM_PATH'] to the value of gem_path if ENV['GEM_PATH'] is not present" do
132-
# ENV['GEM_PATH'] = nil
133-
# @rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/blah"
134-
# booter.boot!
135-
# ENV['GEM_PATH'].should == "/blah/gems"
136-
# end
154+
it "sets ENV['GEM_PATH'] to the value of gem_path if ENV['GEM_PATH'] is not present" do
155+
@rack_context.should_receive(:getInitParameter).with("jruby.rack.gem_path").and_return 'true'
156+
ENV.delete('GEM_PATH')
157+
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/blah"
158+
@rack_context.should_receive(:getRealPath).with("/WEB-INF/gems").and_return "/blah/gems"
159+
160+
booter.boot!
161+
162+
ENV['GEM_PATH'].should == "/blah/gems"
163+
end
137164

138165
it "creates a logger that writes messages to the servlet context" do
139166
booter.boot!

0 commit comments

Comments
 (0)