OptimizedImage specs are failing with Ruby 3.0.2

I’m working on supporting Ruby 3 and got this spec failure.

Steps to reproduce

  1. Run Ruby 3.0.2
$ cd discourse
$ bundle exec rspec spec/models/optimized_image_spec.rb:10

Expected result

It should finish successfully as follows

1 example, 0 failures

Actual result

It always get failure as follows.

$ bundle exec rspec spec/models/optimized_image_spec.rb:10
Run options: include {:locations=>{"./spec/models/optimized_image_spec.rb"=>[10]}}

Randomized with seed 44957
F

Failures:

  1) OptimizedImage.crop should produce cropped images (requires ImageMagick 7)
     Failure/Error: expect(cropped_size).to be < 120

       expected: < 120
            got:   9476
     # ./spec/models/optimized_image_spec.rb:27:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:279:in `block (2 levels) in <top (required)>'

Finished in 0.22966 seconds (files took 2.89 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/optimized_image_spec.rb:10 # OptimizedImage.crop should produce cropped images (requires ImageMagick 7)

Randomized with seed 44957

$

Diag info

This failure is related to this line.

When it runs with Ruby 2.7.4 it works but with Ruby 3.0.2 it raises “can’t convert ImageOptim::Timer into Float (TypeError)”. Here are the minimum steps to reproduce.

$ more foo.rb
gem 'image_optim'
require 'image_optim'

timeout = ImageOptim::Timer.new(15.0)

args = [{"PATH"=>
   "/usr/bin"},
   "date"
]

pid = Process.spawn(*args)
waiter = Process.detach(pid)

waiter.join(timeout)
  • With Ruby 3.0.2, it shows “can’t convert ImageOptim::Timer into Float (TypeError)”
$ ruby -v
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
$ ruby foo.rb
foo.rb:14:in `join': can't convert ImageOptim::Timer into Float (TypeError)
	from foo.rb:14:in `<main>'
Fri Oct  1 12:42:51 PM JST 2021
$
  • With Ruby 2.7.4 it works with no exception.
$ ruby -v
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux]
$ ruby foo.rb
Fri Oct  1 12:44:00 PM JST 2021
$
4 Likes

Looks like there is a trivial fix there … just add a .to_f

if waiter.join(timeout.to_f)

Can you send a PR through?

2 Likes

Thanks for the suggestion. Sure. Will open a pull request to GitHub - toy/image_optim: Optimize images using multiple utilities .

3 Likes

Opened Fix: TypeError: can't convert ImageOptim::Timer into Float by yahonda · Pull Request #194 · toy/image_optim · GitHub

1 Like