web-dev-qa-db-ja.com

Errno :: ENOENT:機能テスト後のそのようなファイルまたはディレクトリはありません@ rb_file_s_mtime

機能Rspecsを実行すると、次のエラーが表示されます(このメッセージの下部にある完全なトレース)

プーマ出力

_Rack app error handling request { GET /Rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSm5kbUZ5YVdGdWRITXZSM0IxTkRGRFRWQlFaVXRNYVUxaVpuQjZXVmw1UTNBMUx6RXhNR1V3TVRka01UWTVNalZrWXpkak5UQTNNamhqT1dNeE5UUmhOREl3TURjNVlXRTJaVFZtWTJNME16VmtZak5sTm1VNU4ySXhNemd3WldObFl6Z0dPZ1pGVkRvUVpHbHpjRzl6YVhScGIyNUpJajFwYm14cGJtVTdJR1pwYkdWdVlXMWxQU0pwYldGblpTNXdibWNpT3lCbWFXeGxibUZ0WlNvOVZWUkdMVGduSjJsdFlXZGxMbkJ1WndZN0JsUTZFV052Ym5SbGJuUmZkSGx3WlVraURtbHRZV2RsTDNCdVp3WTdCbFE9IiwiZXhwIjoiMjAxOS0wMS0yNVQxMToxNjoyOS40NDBaIiwicHVyIjoiYmxvYl9rZXkifX0=--db9451afdc95b292aa6a77c40e00ab0ceb687766/image.png }
#<Errno::ENOENT: No such file or directory @ rb_file_s_mtime - /path/to/Rails/app/tmp/storage/va/ri/variants/Gpu41CMPPeKLiMbfpzYYyCp5/110e017d16925dc7c50728c9c154a420079aa6e5fcc435db3e6e97b1380ecec8>
(followed by the full trace at the end of this message)
_

Rspec outpout

_Failure/Error: last_modified = ::File.mtime(path).httpdate

Errno::ENOENT:
No such file or directory @ rb_file_s_mtime - /path/to/Rails/app/tmp/storage/va/ri/variants/Gpu41CMPPeKLiMbfpzYYyCp5/110e017d16925dc7c50728c9c154a420079aa6e5fcc435db3e6e97b1380ecec8
(followed by the full trace at the end of this message)
_

環境

_product.image_はActiveStorageの添付ファイルです。テスト中、「products/_form」パーシャルがレンダリングされ、次のコードで添付ファイル(つまりpng画像)が表示されます:= image_tag url_for(product.image.variant(resize: "120x120"))。この行をコメントアウトすると、エラーは発生しなくなります。

テスト自体は毎回成功し、エラーはテスト後にトリガーされたようです。すべてのafterブロックを確認しましたが、すべてのafterブロックの後で発生するようです。

トレースは、_ActiveStorage::DiskController.serve_file_がエラーをトリガーすることを示しています。 ActiveStorageアプリが正常にシャットダウンしていないということですか?

_active_storage.service = :test_構成を使用していますが、_:file_でも同じエラーが発生します。

画像が壊れています

テストは失敗しますが、「リモート制御」ブラウザで画像が表示されず、代わりに壊れた画像アイコンがあることがわかります。

テストの実行

「products/_form」パーシャルのレンダリングを実行している4つの注目のテストのグループがあります。 4つのテストをランダムな順序で実行すると、エラーがランダムに発生します。エラーが2回、場合によっては3回表示されることがあります。

テストを個別に実行すると、エラーが表示されません。

画像の添付方法

これは、テストを実行する前にファイルをモデルに添付するために使用するヘルパーです。

_path = Rails.root.join 'spec', 'support', 'fixtures', 'img1.png'
raise 'Image does not exist' unless File.exist? path

product.image.attach io: File.open(path), filename: 'image.png'
_

私もこのヘルパーを使おうとしました:

_path = Rails.root.join 'spec', 'support', 'fixtures', 'img1.png'
raise 'Image does not exist' unless File.exist? path

blob =  ActiveStorage::Blob.create_after_upload! io: path.open, \
  filename: 'img1.png', content_type: 'image/png'
product.images.attach blob
_

そしてこのヘルパー:

_path = Rails.root.join 'spec', 'support', 'fixtures', 'img1.png'
raise 'Image does not exist' unless File.exist? path

byte_size = path.size
checksum = Digest::MD5.file(path).base64digest

blob = ActiveStorage::Blob.create_before_direct_upload!(filename: 'img1.png', \
   byte_size: byte_size, checksum: checksum, content_type: 'image/png').tap do |blob|
  ActiveStorage::Blob.service.upload(blob.key, path.open)
end

product.images.attach blob
_

システム構成

Railsバージョン:5.2.2

Rubyバージョン:Ruby 2.5.3p105(2018-10-18リビジョン65156)[x86_64-linux]

Rbenvバージョン:rbenv 1.1.1-39-g59785f6

Rspec関連バージョン:rspec 3.8.0-capybara 3.12.0-Selenium-webdriver 3.141.0

フルトレース

_     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/file.rb:63:in `mtime'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/file.rb:63:in `serving'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activestorage-5.2.2/app/controllers/active_storage/disk_controller.rb:42:in `serve_file'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activestorage-5.2.2/app/controllers/active_storage/disk_controller.rb:12:in `show'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/abstract_controller/base.rb:194:in `process_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/rendering.rb:30:in `process_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/abstract_controller/callbacks.rb:42:in `block in process_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:132:in `run_callbacks'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/abstract_controller/callbacks.rb:41:in `process_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/rescue.rb:22:in `process_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/notifications.rb:168:in `block in instrument'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/notifications.rb:168:in `instrument'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/railties/controller_runtime.rb:24:in `process_action'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/abstract_controller/base.rb:134:in `process'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionview-5.2.2/lib/action_view/rendering.rb:32:in `process'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal.rb:191:in `dispatch'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_controller/metal.rb:252:in `dispatch'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/routing/route_set.rb:34:in `serve'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/journey/router.rb:52:in `block in serve'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/journey/router.rb:35:in `each'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/journey/router.rb:35:in `serve'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/routing/route_set.rb:840:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/tempfile_reaper.rb:15:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/etag.rb:25:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/conditional_get.rb:25:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/head.rb:12:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/http/content_security_policy.rb:18:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/session/abstract/id.rb:232:in `context'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/session/abstract/id.rb:226:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/cookies.rb:670:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/callbacks.rb:98:in `run_callbacks'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/railties-5.2.2/lib/Rails/rack/logger.rb:38:in `call_app'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/railties-5.2.2/lib/Rails/rack/logger.rb:26:in `block in call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/tagged_logging.rb:71:in `block in tagged'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/tagged_logging.rb:28:in `tagged'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/tagged_logging.rb:71:in `tagged'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/railties-5.2.2/lib/Rails/rack/logger.rb:26:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/request_id.rb:27:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/method_override.rb:22:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/runtime.rb:22:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/activesupport-5.2.2/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/executor.rb:14:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/actionpack-5.2.2/lib/action_dispatch/middleware/static.rb:127:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/sendfile.rb:111:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/railties-5.2.2/lib/Rails/engine.rb:524:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/urlmap.rb:68:in `block in call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/urlmap.rb:53:in `each'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/rack-2.0.6/lib/rack/urlmap.rb:53:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/capybara-3.12.0/lib/capybara/server/middleware.rb:48:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/configuration.rb:225:in `call'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/server.rb:658:in `handle_request'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/server.rb:472:in `process_client'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/server.rb:332:in `block in run'
     # /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/puma-3.12.0/lib/puma/thread_pool.rb:133:in `block in spawn_thread'
     # 
     #   Showing full backtrace because every line was filtered out.
     #   See docs for RSpec::Configuration#backtrace_exclusion_patterns and
     #   RSpec::Configuration#backtrace_inclusion_patterns for more information.
     # ------------------
     # --- Caused by: ---
     # Capybara::CapybaraError:
     #   Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true
     #   /opt/rbenv/versions/2.5.3/lib/Ruby/gems/2.5.0/gems/capybara-3.12.0/lib/capybara/session.rb:147:in `raise_server_error!'
_

更新

(黙って申し訳ありませんが、緊急事態が発生しました)

ここに興味深い情報があります。 この行 の後に挿入しました、このコード:_puts "SERVING #{path} - Exist ? #{File.exist? path}"_

そして、これが出力です(各行の終わりにあるファイル名とブール値を確認してください):

_SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/Me/GK/MeGKkqgbb7Sg4RbPoCYWR6pZ - Exist ? true
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/sG/3v/sG3vd2viGWmaf69pNXSChZPa - Exist ? true
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/fy/2C/fy2C8PpaR96YgCzwt5WVBLrQ - Exist ? true
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/sG/3v/sG3vd2viGWmaf69pNXSChZPa - Exist ? true
. (green dot)
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/Me/GK/MeGKkqgbb7Sg4RbPoCYWR6pZ - Exist ? false
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/sG/3v/sG3vd2viGWmaf69pNXSChZPa - Exist ? false
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/Me/GK/MeGKkqgbb7Sg4RbPoCYWR6pZ - Exist ? false
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/fy/2C/fy2C8PpaR96YgCzwt5WVBLrQ - Exist ? false
F (red F)
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/Me/GK/MeGKkqgbb7Sg4RbPoCYWR6pZ - Exist ? false
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/sG/3v/sG3vd2viGWmaf69pNXSChZPa - Exist ? false
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/Me/GK/MeGKkqgbb7Sg4RbPoCYWR6pZ - Exist ? false
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/fy/2C/fy2C8PpaR96YgCzwt5WVBLrQ - Exist ? false
F (red F)
SERVING /tmp/active_storage_tests20190129-10797-1rebdf7/Tx/DL/TxDLPEk3ykf8ETkURY5YZPgY - Exist ? true
_

ご覧のとおり、テスト間でもファイルパスは同じです。タフなデータベースは、各テストの間にクリーンアップされます。したがって、activestorageが同じblobレコードを再利用するために、ファイルのチェックサムが保持される「場所」があります。その結果、同じファイルパスが再利用され、それがFileNotFoundエラーが発生する理由です。

何か案が ? DatabaseCleanerに加えて、次のように定義されたafterブロックがあることに注意してください。

_config.after(:each) do
  ActiveStorage::Current.reset
  ActiveStorage::Attachment.all.each(&:delete)
  ActiveStorage::Blob.all.each(&:delete)
end
_

バグが疑われる

再現するアプリケーションでPRを埋めました https://github.com/Rails/rails/issues/34989

彼らの応答を待っています

7
Benj

テスト後に例外が発生することを除いて、あなたが何を意味するのかわかりませんか?

通常、テストはアトミックで独立している必要があるため、作成するすべてのリソースは、テストの終了後すぐに削除する必要があります。また、テストはランダムな順序で実行されるため、実行順序によっては、相互に依存するテストが失敗する場合と失敗する場合があります。

失敗した出力またはテストを共有できますか?

0