提交 beb7fba6 编写于 作者: E Edouard CHIN

Use the `file_fixture_path` for fixture_file_upload:

- We used the `fixture_path` before `file_fixture_path` was a thing,
  but now that we have the latter we should use it.

  `fixture_path` is solely used by Active Record so it seems wrong
  to be using that in ActionPack.
上级 776f2abc
* `fixture_file_upload` now uses path relative to `file_fixture_path`
Previously the path had to be relative to `fixture_path`.
You can change your existing code as follow:
```ruby
# Before
fixture_file_upload('files/dog.png')
# After
fixture_file_upload('dog.png')
```
*Edouard Chin*
* Remove deprecated `force_ssl` at the controller level.
*Rafael Mendonça França*
......
......@@ -17,8 +17,29 @@ module FixtureFile
def fixture_file_upload(path, mime_type = nil, binary = false)
if self.class.respond_to?(:fixture_path) && self.class.fixture_path &&
!File.exist?(path)
path = File.join(self.class.fixture_path, path)
original_path = path
path = Pathname.new(self.class.fixture_path).join(path)
if !self.class.file_fixture_path
ActiveSupport::Deprecation.warn(<<~EOM)
Passing a path to `fixture_file_upload` relative to `fixture_path` is deprecated.
In Rails 6.2, the path needs to be relative to `file_fixture_path` which you
haven't set yet. Set `file_fixture_path` to discard this warning.
EOM
elsif path.exist?
non_deprecated_path = path.relative_path_from(Pathname(self.class.file_fixture_path))
ActiveSupport::Deprecation.warn(<<~EOM)
Passing a path to `fixture_file_upload` relative to `fixture_path` is deprecated.
In Rails 6.2, the path needs to be relative to `file_fixture_path`.
Please modify the call from
`fixture_file_upload(#{original_path})` to `fixture_file_upload(#{non_deprecated_path})`.
EOM
end
elsif self.class.file_fixture_path && !File.exist?(path)
path = file_fixture(path)
end
Rack::Test::UploadedFile.new(path, mime_type, binary)
end
end
......
......@@ -1205,7 +1205,7 @@ def app
self.class
end
def self.fixture_path
def self.file_fixture_path
File.expand_path("../fixtures/multipart", __dir__)
end
......
......@@ -7,6 +7,7 @@
class TestCaseTest < ActionController::TestCase
def self.fixture_path; end
self.file_fixture_path = File.expand_path("../fixtures/multipart", __dir__)
class TestController < ActionController::Base
def no_op
......@@ -872,9 +873,13 @@ def test_test_uploaded_file
end
def test_fixture_path_is_accessed_from_self_instead_of_active_support_test_case
TestCaseTest.stub :fixture_path, FILES_DIR do
uploaded_file = fixture_file_upload("/ruby_on_rails.jpg", "image/png")
assert_equal File.open("#{FILES_DIR}/ruby_on_rails.jpg", READ_PLAIN).read, uploaded_file.read
TestCaseTest.stub :fixture_path, File.expand_path("../fixtures", __dir__) do
expected = "`fixture_file_upload(multipart/ruby_on_rails.jpg)` to `fixture_file_upload(ruby_on_rails.jpg)`"
assert_deprecated(expected) do
uploaded_file = fixture_file_upload("multipart/ruby_on_rails.jpg", "image/png")
assert_equal File.open("#{FILES_DIR}/ruby_on_rails.jpg", READ_PLAIN).read, uploaded_file.read
end
end
end
......@@ -915,10 +920,24 @@ def test_fixture_file_upload
assert_equal "45142", @response.body
end
def test_fixture_file_upload_output_deprecation_when_file_fixture_path_is_not_set
TestCaseTest.stub :fixture_path, File.expand_path("../fixtures", __dir__) do
TestCaseTest.stub :file_fixture_path, nil do
assert_deprecated(/In Rails 6.2, the path needs to be relative to `file_fixture_path`/) do
fixture_file_upload("multipart/ruby_on_rails.jpg", "image/jpg")
end
end
end
end
def test_fixture_file_upload_relative_to_fixture_path
TestCaseTest.stub :fixture_path, FILES_DIR do
uploaded_file = fixture_file_upload("ruby_on_rails.jpg", "image/jpg")
assert_equal File.open("#{FILES_DIR}/ruby_on_rails.jpg", READ_PLAIN).read, uploaded_file.read
TestCaseTest.stub :fixture_path, File.expand_path("../fixtures", __dir__) do
expected = "`fixture_file_upload(multipart/ruby_on_rails.jpg)` to `fixture_file_upload(ruby_on_rails.jpg)`"
assert_deprecated(expected) do
uploaded_file = fixture_file_upload("multipart/ruby_on_rails.jpg", "image/jpg")
assert_equal File.open("#{FILES_DIR}/ruby_on_rails.jpg", READ_PLAIN).read, uploaded_file.read
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册