Skip to content

Commit 7b1a6c3

Browse files
committed
Use Dir.glob in find_root_with_flag to return correct case
As of Ruby 2.2, Dir.glob returns matches with the correct case as opposed to in previous versions where the matches were returned with the case of the pattern. In certain scenarios on Mac OS X the app can be booted with the incorrect case, e.g. incorrect case on Pow symlink). This is because the app is built by Rack::Builder evaluating a string from the config.ru file and passing the incorrectly cased path to the eval method. As Dir.glob is the only method in Ruby 2.2 that appears to do this case correction currently we need to rewrite the find_root_with_flag method to use it so that it corrects the case for when the applications paths instance is created. Fixes rails#18660.
1 parent c99bea1 commit 7b1a6c3

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

actionpack/lib/action_controller/metal/helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def helper_attr(*attrs)
7373

7474
# Provides a proxy to access helpers methods from outside the view.
7575
def helpers
76-
@helper_proxy ||= begin
76+
@helper_proxy ||= begin
7777
proxy = ActionView::Base.new
7878
proxy.config = config.inheritable_copy
7979
proxy.extend(_helpers)

railties/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Use Dir.glob in `find_root_with_flag` so that correct case is used on Mac OS X.
2+
3+
Fixes #18660.
4+
5+
*Andrew White*
6+
7+
18
## Rails 4.2.1 (March 19, 2015) ##
29

310
* Add a new-line to the end of route method generated code.

railties/lib/rails/engine.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,16 +663,20 @@ def has_migrations? #:nodoc:
663663
end
664664

665665
def self.find_root_with_flag(flag, root_path, default=nil) #:nodoc:
666+
root_paths = [File.join(root_path, flag)]
666667

667-
while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
668-
parent = File.dirname(root_path)
669-
root_path = parent != root_path && parent
668+
while root_path != (root_path = File.dirname(root_path))
669+
root_paths << File.join(root_path, flag)
670670
end
671671

672-
root = File.exist?("#{root_path}/#{flag}") ? root_path : default
672+
matches = Dir.glob(root_paths, File::FNM_CASEFOLD)
673+
matches.sort_by!(&:size)
674+
match = matches.last
675+
676+
root = match ? File.dirname(match) : default
673677
raise "Could not find root path for #{self}" unless root
674678

675-
Pathname.new File.realpath root
679+
Pathname.new(File.realpath(root))
676680
end
677681

678682
def default_middleware_stack #:nodoc:

railties/test/application/rackup_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,38 @@ def teardown
4040
assert_equal 'UTC', Rails.application.config.time_zone
4141
end
4242
end
43+
44+
class CaseInsensitiveRackupTest < ActiveSupport::TestCase
45+
include ActiveSupport::Testing::Isolation
46+
47+
def app_path(*args)
48+
tmp_path(*%w[OtherApp] + args)
49+
end
50+
51+
def rackup
52+
require "rack"
53+
app, _ = Rack::Builder.parse_file("#{app_path.downcase}/config.ru")
54+
app
55+
end
56+
57+
def setup
58+
build_app
59+
boot_rails
60+
end
61+
62+
def teardown
63+
teardown_app
64+
end
65+
66+
test "rails app is present" do
67+
assert File.exist?(app_path("config"))
68+
end
69+
70+
test "config.ru can be racked up" do
71+
Dir.chdir app_path do
72+
@app = rackup
73+
assert_welcome get("/")
74+
end
75+
end
76+
end
4377
end

0 commit comments

Comments
 (0)