OptimizedImage 规格在 Ruby 3.0.2 下失败

我正在支持 Ruby 3,但遇到了这个测试失败。

复现步骤

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

预期结果

它应成功完成,如下所示:

1 example, 0 failures

实际结果

它总是失败,如下所示。

$ 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

$

诊断信息

此失败与以下行相关。

在 Ruby 2.7.4 上运行时正常,但在 Ruby 3.0.2 上会抛出 “can’t convert ImageOptim::Timer into Float (TypeError)”。以下是复现所需的最小步骤。

$ 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)
  • 在 Ruby 3.0.2 上,显示 “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
$
  • 在 Ruby 2.7.4 上运行时没有异常。
$ 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 个赞

看起来那里有一个简单的修复方法……只需添加 .to_f

if waiter.join(timeout.to_f)

你能提交一个 PR 吗?

2 个赞

感谢您的建议。好的,我将为 GitHub - toy/image_optim: Optimize images using multiple utilities · GitHub 提交一个拉取请求。

3 个赞

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

1 个赞