RSpec wrong with MiniMagick convert file only gitlab ci/cd (not in env development)

210 views Asked by At
class **ConverterFile**
  attr_accessor :old_file

  SUPPORT_IMAGE_FORMAT = %w[png jpg jpeg pdf].freeze

  def initialize(old_file)
    @old_file = old_file
  end

  def to_image_type(file_format)
    raise 'format not support' if file_format.blank? || SUPPORT_IMAGE_FORMAT.exclude?(file_format)

    return old_file if File.extname(@old_file) == ".#{file_format}"

    converted_file = MiniMagick::Image.open(@old_file.path)
    converted_file.format(file_format)
    File.open(converted_file.path)
  rescue StandardError => e
    nil
  end
end
 describe '#to_image_type' do
    let(:png_file)    { File.open(Rails.root.join('spec/support/files/logo.png')) }
    
    context 'when convert to jpg file' do
      it 'convert png file to jpg successfuly' do
        new_file = described_class.new(png_file).to_image_type('jpg')
        expect(File.extname(new_file)).to eq('.jpg')
      end
    end
 end

error-in-gitlab-ci-cd

I'am confusing because i' try run spec in env dev local is pass but gitlab ci/cd error every times. I think error is about reference file missing not sure.

1

There are 1 answers

0
Tom Lord On

The error says that File.extname is being called with a nil argument:

File.extname(nil)
  #=> TypeError: no implicit conversion of nil into String

This means new_file must be nil.

This means described_class.new(png_file).to_image_type('jpg') must be returning nil.

Why is this happening? Well, it's hard for me to say for sure without taking a closer look at the CI setup, but I am highly suspicious of this part of the code:

rescue StandardError => e
  nil
end

Your method is silently swallowing errors, and returning nil. That doesn't seem like a good idea here in general, and it's probably hiding the true cause of the test failure!

My suspicion is that you have not committed the file 'spec/support/files/logo.png' into source control. So the test passes locally, but fails on CI where this file does not exist.

I think an exception is being raised when the non-existed file gets opened, but you're capturing the exception and returning nil -- which results in this more confusing test failure message.