1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #41283 from tywhang/more_accurate_error_for_missing_file

Handle error when file does not exist at filepath
This commit is contained in:
Rafael França 2021-07-07 18:36:34 -04:00 committed by GitHub
commit 37fb99e2c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View file

@ -26,7 +26,11 @@ module ActionView
if File.exist?(options[:file])
Template::RawFile.new(options[:file])
else
raise ArgumentError, "`render file:` should be given the absolute path to a file. '#{options[:file]}' was given instead"
if Pathname.new(options[:file]).absolute?
raise ArgumentError, "File #{options[:file]} does not exist"
else
raise ArgumentError, "`render file:` should be given the absolute path to a file. '#{options[:file]}' was given instead"
end
end
elsif options.key?(:inline)
handler = Template.handler_for_extension(options[:type] || "erb")

View file

@ -79,7 +79,20 @@ module RenderTestCases
def test_render_file_with_full_path_no_extension
template_path = File.expand_path("../fixtures/test/hello_world", __dir__)
assert_raise(ArgumentError) { @view.render(file: template_path) }
e = assert_raise(ArgumentError) { @view.render(file: template_path) }
assert_match(/File (.+) does not exist/, e.message)
end
def test_render_file_with_invalid_full_path
template_path = File.expand_path("../fixtures/test/hello_world_invalid.erb", __dir__)
e = assert_raise(ArgumentError) { @view.render(file: template_path) }
assert_match(/File (.+) does not exist/, e.message)
end
def test_render_file_with_relative_path
template_path = "fixtures/test/hello_world.erb"
e = assert_raise(ArgumentError) { @view.render(file: template_path) }
assert_match(%r{`render file:` should be given the absolute path to a file. (.+) was given instead}, e.message)
end
# Test if :formats, :locale etc. options are passed correctly to the resolvers.