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
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.
The error says that
File.extnameis being called with anilargument:This means
new_filemust benil.This means
described_class.new(png_file).to_image_type('jpg')must be returningnil.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:
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.