Skip to content

Commit 2fd826b

Browse files
committed
start ajusting Gem.path directly instead of mangling with ENV['GEM_PATH']
1 parent 2e647e9 commit 2fd826b

File tree

2 files changed

+92
-38
lines changed

2 files changed

+92
-38
lines changed

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

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ def layout
7777
end
7878
attr_writer :layout
7979

80-
%w( app_path gem_path public_path ).each do |path|
81-
# def app_path; layout.app_path; end
82-
# def app_path=(path); layout.app_path = path; end
83-
class_eval "def #{path}; layout.#{path}; end"
84-
class_eval "def #{path}=(path); layout.#{path} = path; end"
85-
end
80+
def app_path; layout.app_path end
81+
def app_path=(path); layout.app_path = path end
82+
def gem_path; layout.gem_path end
83+
def gem_path=(path); layout.gem_path = path end
84+
def public_path; layout.public_path end
85+
def public_path=(path); layout.public_path = path end
8686

8787
# @deprecated use {JRuby::Rack#logger} instead
8888
# @return [Logger]
@@ -91,16 +91,8 @@ def logger; JRuby::Rack.logger; end
9191
# Boot-up this booter, preparing the environment for the application.
9292
def boot!
9393
adjust_load_path
94+
adjust_gem_path
9495
ENV['RACK_ENV'] = rack_env
95-
gem_path = layout.gem_path
96-
if env_gem_path = ENV['GEM_PATH']
97-
if gem_path.nil? || gem_path.empty?
98-
gem_path = env_gem_path # keep ENV['GEM_PATH'] as is
99-
elsif env_gem_path != gem_path
100-
gem_path = "#{gem_path}#{File::PATH_SEPARATOR}#{env_gem_path}"
101-
end
102-
end
103-
ENV['GEM_PATH'] = gem_path
10496
export_global_settings
10597
change_working_directory
10698
load_settings_from_init_rb
@@ -111,6 +103,36 @@ def boot!
111103

112104
protected
113105

106+
def adjust_gem_path
107+
case gem_path?
108+
when false then return false
109+
when true then
110+
if env_gem_path = ENV['GEM_PATH']
111+
if gem_path.nil? || gem_path.empty?
112+
return # keep ENV['GEM_PATH'] as is
113+
elsif env_gem_path != gem_path
114+
separator = File::PATH_SEPARATOR
115+
unless env_gem_path.split(separator).include?(gem_path)
116+
ENV['GEM_PATH'] = "#{gem_path}#{separator}#{env_gem_path}"
117+
end
118+
end
119+
else
120+
ENV['GEM_PATH'] = gem_path
121+
end
122+
else # default
123+
begin
124+
require 'rubygems'
125+
rescue LoadError
126+
else
127+
Gem.path.unshift(gem_path) unless Gem.path.include?(gem_path)
128+
end
129+
end
130+
end
131+
132+
def gem_path? # TODO
133+
@rack_context.getInitParameter('jruby.rack.env.gem_path') || nil
134+
end
135+
114136
# @note called during {#boot!}
115137
def export_global_settings
116138
JRuby::Rack.send(:instance_variable_set, :@booter, self) # TODO
@@ -233,6 +255,9 @@ def run_boot_hooks
233255
self.class.run_boot_hooks!
234256
end
235257

258+
def real_path(path); layout.real_path(path) end
259+
def expand_path(path); layout.expand_path(path) end
260+
236261
private
237262

238263
def silence_warnings(&block)

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

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
after(:all) { JRuby::Rack.context = nil }
1818

1919
@@rack_env = ENV['RACK_ENV']
20+
@@gem_path = Gem.path.dup
2021

2122
after do
2223
@@rack_env.nil? ? ENV.delete('RACK_ENV') : ENV['RACK_ENV'] = @@rack_env
24+
Gem.path.replace(@@gem_path)
2325
end
2426

2527
it "should determine the public html root from the 'public.root' init parameter" do
@@ -50,22 +52,23 @@
5052

5153
it "should determine the gem path from the gem.path init parameter" do
5254
@rack_context.should_receive(:getInitParameter).with("gem.path").and_return "/blah"
53-
@rack_context.should_receive(:getRealPath).with("/blah").and_return "."
55+
@rack_context.should_receive(:getRealPath).with("/blah").and_return "./blah"
5456
booter.boot!
55-
booter.gem_path.should == "."
57+
booter.gem_path.should == "./blah"
5658
end
5759

5860
it "should also be able to determine the gem path from the gem.home init parameter" do
5961
@rack_context.should_receive(:getInitParameter).with("gem.home").and_return "/blah"
60-
@rack_context.should_receive(:getRealPath).with("/blah").and_return "."
62+
@rack_context.should_receive(:getRealPath).with("/blah").and_return "/home/kares/blah"
6163
booter.boot!
62-
booter.gem_path.should == "."
64+
booter.gem_path.should == "/home/kares/blah"
6365
end
6466

6567
it "defaults gem path to '/WEB-INF/gems'" do
66-
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "."
68+
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "file:/home/kares/WEB-INF"
69+
@rack_context.should_receive(:getRealPath).with("/WEB-INF/gems").and_return "file:/home/kares/WEB-INF/gems"
6770
booter.boot!
68-
booter.gem_path.should == "./gems"
71+
booter.gem_path.should == "file:/home/kares/WEB-INF/gems"
6972
end
7073

7174
it "gets rack environment from rack.env" do
@@ -82,30 +85,56 @@
8285
booter.rack_env.should == 'production'
8386
end
8487

85-
it "prepends gem_path to ENV['GEM_PATH']" do
86-
ENV['GEM_PATH'] = '/other/gems'
87-
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/blah"
88+
it "prepends gem_path to Gem.path" do
89+
Gem.path.replace [ '/opt/gems' ]
90+
booter.gem_path = "wsjar:file:/opt/deploy/sample.war!/WEB-INF/gems"
8891
booter.boot!
89-
ENV['GEM_PATH'].should == "/blah/gems#{File::PATH_SEPARATOR}/other/gems"
90-
end
9192

92-
it "keeps ENV['GEM_PATH'] when gem_path is nil" do
93-
ENV['GEM_PATH'] = '/some/other/gems'
94-
booter.layout = layout = double('layout')
95-
layout.stub(:app_path).and_return '.'
96-
layout.stub(:public_path).and_return nil
97-
layout.should_receive(:gem_path).and_return nil
98-
booter.boot!
99-
ENV['GEM_PATH'].should == "/some/other/gems"
93+
expect( Gem.path ).to eql [ 'wsjar:file:/opt/deploy/sample.war!/WEB-INF/gems', '/opt/gems' ]
10094
end
10195

102-
it "sets ENV['GEM_PATH'] to the value of gem_path if ENV['GEM_PATH'] is not present" do
103-
ENV['GEM_PATH'] = nil
104-
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/blah"
96+
it "prepends gem_path to Gem.path if not already present" do
97+
Gem.path.replace [ "file:/home/gems", "/usr/local/gems" ]
98+
booter.gem_path = '/usr/local/gems'
10599
booter.boot!
106-
ENV['GEM_PATH'].should == "/blah/gems"
100+
101+
expect( Gem.path ).to eql [ "file:/home/gems", "/usr/local/gems" ]
107102
end
108103

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
112+
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
120+
121+
# it "keeps ENV['GEM_PATH'] when gem_path is nil" do
122+
# ENV['GEM_PATH'] = '/usr/local/gems'
123+
# booter.layout = layout = double('layout')
124+
# layout.stub(:app_path).and_return '.'
125+
# layout.stub(:public_path).and_return nil
126+
# layout.should_receive(:gem_path).and_return nil
127+
# booter.boot!
128+
# ENV['GEM_PATH'].should == "/usr/local/gems"
129+
# end
130+
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
137+
109138
it "creates a logger that writes messages to the servlet context" do
110139
booter.boot!
111140
@rack_context.should_receive(:log).with(/hello/)

0 commit comments

Comments
 (0)