Skip to content

Commit d2d0d4a

Browse files
committed
deduplicate bin resolving error messages
1 parent f78f075 commit d2d0d4a

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

CHANGELOG.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## unreleased
44

5+
* Deduplicate bin resolving error messages [@toy](https://github.com/toy)
6+
57
## v0.24.0 (2016-08-14)
68

79
* Rails image assets optimization is extracted into [image\_optim\_rails gem](https://github.com/toy/image_optim_rails) [#127](https://github.com/toy/image_optim/issues/127) [@toy](https://github.com/toy)

lib/image_optim/worker/class_methods.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ def create_all(image_optim, &options_proc)
6565
end
6666

6767
unless errors.empty?
68+
messages = errors.map(&:to_s).uniq
6869
if image_optim.skip_missing_workers
69-
errors.each{ |error| warn error }
70+
messages.each{ |message| warn message }
7071
else
71-
message = ['Bin resolving errors:', *errors].join("\n")
72-
fail BinResolver::Error, message
72+
joint_message = ['Bin resolving errors:', *messages].join("\n")
73+
fail BinResolver::Error, joint_message
7374
end
7475
end
7576

spec/image_optim/worker_spec.rb

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ def worker_class_doubles(workers)
160160

161161
describe 'with missing workers' do
162162
let(:workers) do
163-
Array.new(3) do |i|
163+
%w[a b c c].map do |bin|
164164
worker = worker_double
165-
unless i == 1
165+
unless bin == 'b'
166166
allow(worker).to receive(:resolve_used_bins!).
167-
and_raise(BinResolver::BinNotFound, "not found #{i}")
167+
and_raise(BinResolver::BinNotFound, "not found #{bin}")
168168
end
169169
worker
170170
end
@@ -176,32 +176,27 @@ def worker_class_doubles(workers)
176176
end
177177

178178
describe 'if skip_missing_workers is true' do
179-
define :bin_not_found do |message|
180-
match do |error|
181-
error.is_a?(BinResolver::BinNotFound) && error.message == message
182-
end
183-
end
184-
185-
it 'shows warnings and returns resolved workers ' do
179+
it 'shows deduplicated warnings and returns resolved workers ' do
186180
allow(image_optim).to receive(:skip_missing_workers).and_return(true)
187181

188-
expect(Worker).to receive(:warn).
189-
once.with(bin_not_found('not found 0'))
190-
expect(Worker).to receive(:warn).
191-
once.with(bin_not_found('not found 2'))
182+
expect(Worker).to receive(:warn).once.with('not found a')
183+
expect(Worker).to receive(:warn).once.with('not found c')
192184

193-
expect(Worker.create_all(image_optim){ {} }).
194-
to eq([workers[1]])
185+
expect(Worker.create_all(image_optim){ {} }).to eq([workers[1]])
195186
end
196187
end
197188

198189
describe 'if skip_missing_workers is false' do
199-
it 'fails with a joint exception' do
190+
it 'fails with a joint exception of deduplicated messages' do
200191
allow(image_optim).to receive(:skip_missing_workers).and_return(false)
201192

202193
expect do
203194
Worker.create_all(image_optim){ {} }
204-
end.to raise_error(BinResolver::Error, /not found 0\nnot found 2/)
195+
end.to raise_error(BinResolver::Error, [
196+
'Bin resolving errors:',
197+
'not found a',
198+
'not found c',
199+
].join("\n"))
205200
end
206201
end
207202
end

0 commit comments

Comments
 (0)