Skip to content

Commit

Permalink
add tests and external file backtrace for Routing::Mapper#draw
Browse files Browse the repository at this point in the history
  • Loading branch information
korny committed May 15, 2012
1 parent 2c0add7 commit 0d3172c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
7 changes: 4 additions & 3 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1336,10 +1336,11 @@ def draw(name)
msg = "Your router tried to #draw the external file #{name}.rb,\n" \
"but the file was not found in:\n\n"
msg += @draw_paths.map { |_path| " * #{_path}" }.join("\n")
raise msg
raise ArgumentError, msg
end

instance_eval(path.join("#{name}.rb").read)

route_path = path.join("#{name}.rb")
instance_eval(route_path.read, route_path.to_s)
end

# match 'path' => 'controller#action'
Expand Down
49 changes: 49 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,55 @@ def test_controller_options
end
end

class TestDrawExternalFile < ActionDispatch::IntegrationTest
class ExternalController < ActionController::Base
def index
render :text => "external#index"
end
end

DRAW_PATH = Pathname.new(File.expand_path('../../fixtures/routes', __FILE__))

DefaultScopeRoutes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw_paths << DRAW_PATH
end

def app
DefaultScopeRoutes
end

def test_draw_external_file
DefaultScopeRoutes.draw do
scope :module => 'test_draw_external_file' do
draw :external
end
end

get '/external'
assert_equal "external#index", @response.body
end

def test_draw_nonexistent_file
exception = assert_raise ArgumentError do
DefaultScopeRoutes.draw do
draw :nonexistent
end
end
assert_match 'Your router tried to #draw the external file nonexistent.rb', exception.message
assert_match DRAW_PATH.to_s, exception.message
end

def test_draw_bogus_file
exception = assert_raise NoMethodError do
DefaultScopeRoutes.draw do
draw :bogus
end
end
assert_match "undefined method `wrong'", exception.message
assert_match 'test/fixtures/routes/bogus.rb:1', exception.backtrace.first
end
end

class TestDefaultScope < ActionDispatch::IntegrationTest
module ::Blog
class PostsController < ActionController::Base
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/fixtures/routes/bogus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wrong :route
1 change: 1 addition & 0 deletions actionpack/test/fixtures/routes/external.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
get '/external' => 'external#index'

0 comments on commit 0d3172c

Please sign in to comment.