Skip to content

Commit

Permalink
Fix: space in file name will break PNGQuantOptimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
khiav reoy committed Jul 30, 2018
1 parent 5e206b2 commit 6264c53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
12 changes: 4 additions & 8 deletions lib/image_optimizer/pngquant_optimizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ class PNGQuantOptimizer < ImageOptimizerBase

private

def perform_optimizations
system("#{optimizer_bin} #{command_options.join(' ')}")
end

def command_options
flags = ['--skip-if-larger', '--speed 1',
'--force', '--verbose', '--ext .png']
flags = ['--skip-if-larger', '--speed=1',
'--force', '--verbose', '--ext=.png']

flags -= ['--verbose'] if quiet?
flags << quality
flags << path
end

def quality
return "--quality 100" unless (0..100).include?(options[:quality])
"--quality #{options[:quality]}"
return "--quality=100" unless (0..100).include?(options[:quality])
"--quality=#{options[:quality]}"
end

def extensions
Expand Down
29 changes: 20 additions & 9 deletions spec/image_optimizer/pngquant_optimizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
describe ImageOptimizer::PNGQuantOptimizer do
describe '#optimize' do
let(:options) { {} }
let(:pngquant_optimizer) { ImageOptimizer::PNGQuantOptimizer.new('/path/to/file.png', options) }
let(:path) { '/path/to/file.png' }
let(:pngquant_optimizer) { ImageOptimizer::PNGQuantOptimizer.new(path, options) }
after { ImageOptimizer::PNGQuantOptimizer.instance_variable_set(:@bin, nil) }
subject { pngquant_optimizer.optimize }

Expand All @@ -13,8 +14,8 @@
end

it 'optimizes the png' do
expected_cmd = "/usr/local/bin/pngquant --skip-if-larger --speed\ 1 --force --verbose --ext\ .png --quality\ 100 /path/to/file.png"
expect(pngquant_optimizer).to receive(:system).with(expected_cmd)
expected_cmd = %w[/usr/local/bin/pngquant --skip-if-larger --speed=1 --force --verbose --ext=.png --quality=100 /path/to/file.png]
expect(pngquant_optimizer).to receive(:system).with(*expected_cmd)
subject
end

Expand All @@ -28,8 +29,8 @@
end

it 'should optimize using the given path' do
expected_cmd = "#{image_pngquant_bin_path} --skip-if-larger --speed\ 1 --force --verbose --ext\ .png --quality\ 100 /path/to/file.png"
expect(pngquant_optimizer).to receive(:system).with(expected_cmd)
expected_cmd = %w[--skip-if-larger --speed=1 --force --verbose --ext=.png --quality=100 /path/to/file.png]
expect(pngquant_optimizer).to receive(:system).with(image_pngquant_bin_path, *expected_cmd)
subject
end
end
Expand All @@ -38,8 +39,8 @@
let(:options) { { :quality => 99 } }

it 'optimizes the png with the quality' do
expected_cmd = "/usr/local/bin/pngquant --skip-if-larger --speed\ 1 --force --verbose --ext\ .png --quality\ 99 /path/to/file.png"
expect(pngquant_optimizer).to receive(:system).with(expected_cmd)
expected_cmd = %w[/usr/local/bin/pngquant --skip-if-larger --speed=1 --force --verbose --ext=.png --quality=99 /path/to/file.png]
expect(pngquant_optimizer).to receive(:system).with(*expected_cmd)
subject
end
end
Expand All @@ -48,8 +49,18 @@
let(:options) { { :quiet => true } }

it 'optimizes the png with the quality' do
expected_cmd = "/usr/local/bin/pngquant --skip-if-larger --speed\ 1 --force --ext\ .png --quality\ 100 /path/to/file.png"
expect(pngquant_optimizer).to receive(:system).with(expected_cmd)
expected_cmd = %w[/usr/local/bin/pngquant --skip-if-larger --speed=1 --force --ext=.png --quality=100 /path/to/file.png]
expect(pngquant_optimizer).to receive(:system).with(*expected_cmd)
subject
end
end

context 'with space in file name' do
let(:path) { '/path/to/file 2.png' }

it do
expected_cmd = %w[/usr/local/bin/pngquant --skip-if-larger --speed=1 --force --verbose --ext=.png --quality=100]
expect(pngquant_optimizer).to receive(:system).with(*expected_cmd, path)
subject
end
end
Expand Down

0 comments on commit 6264c53

Please sign in to comment.