ハイパーニートプログラマーへの道

頑張ったり頑張らなかったり

【Rails】ファイルアップロードのテストにてMiniMagickのValidationがfailedするとき

RSpec + FactoryGirl (FactoryBot) でファイルアップロードが絡むテストを走らせたときに、以下のようなエラーに遭遇

Validation failed: Figures content Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: MiniMagick::Invalid

factory のほうはこんな感じ

FactoryGirl.define do
  factory :figure do
    # 中略
    factory :content_figure do
      content do
        ActionDispatch::Http::UploadedFile.new(
          filename: 'figure.png',
          type: 'image/png',
          tempfile: File.open(Rails.root.join('spec', 'files', 'figure.png'))
        )
      end
    end
  end
end

うーん、CarrierWave::MiniMagick をインクルードしているアップローダでも extension_white_listpng は入ってる

  def extension_white_list
    %w(jpg jpeg gif png)
  end

convert -list configureDELEGATES の部分を見てみる

$ convert -list configure
DELEGATES      bzlib mpeg freetype jng jpeg lzma png tiff xml zlib

んな〜、png は入っているようですぞ?

brew upgrade でimagemagick の更新

$ brew doctor

arning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  imagemagick

Warning: Some keg-only formula are linked into the Cellar.
Linking a keg-only formula, such as gettext, into the cellar with
`brew link <formula>` will cause other formulae to detect them during
the `./configure` step. This may cause problems when compiling those
other formulae.

Binaries provided by keg-only formulae may override system binaries
with other strange results.

You may wish to `brew unlink` these brews:
  imagemagick@6

あー、imagemagick@6 系にリンクしてたのが悪いのかなあ

$ brew unlink imagemagick@6
$ brew link imagemagick
$ identify spec/files/figure.png 
identify: unable to load module '/usr/local/Cellar/imagemagick/7.0.5-0/lib/ImageMagick//modules-Q16HDRI/coders/png.la': file not found @ error/module.c/OpenModule/1279.

でもまだidentifyでこけますぞ

$ brew upgrade imagemagick
$ identify spec/files/figure.png 
spec/files/figure.png PNG 64x64 64x64+0+0 8-bit sRGB 155B 0.000u 0:00.000

うむ、良さそう。これでspec が通るようになりました


参考記事:

convert -list configure で確認してたり

ruby on rails 3.2 - Carrierwave Error Msg: Failed to manipulate with MiniMagick, maybe it is not an image? - Stack Overflow

PDF 変換をできるようにする、という記事ですが
identify で確認したり、imagemagick を入れ直したりしてます

ImageMagickでPDFを変換できるようにする。 - Qiita