Skip to content

Commit 71b31bd

Browse files
committed
Switch ImageOptim pre-commit hook to use executable
The `image_optim` gem now has a friendly CLI we can use instead of integrating with the Ruby API directly. It is better to use the CLI since it allows us to further decouple ourselves from the tool. Change-Id: Icb4df4cc29016f0b79561eaa3ceb8abc0c8135fb Reviewed-on: http://gerrit.causes.com/47233 Tested-by: jenkins <jenkins@brigade.com> Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
1 parent 87f7c87 commit 71b31bd

File tree

5 files changed

+44
-36
lines changed

5 files changed

+44
-36
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
a list of paths a hook should load with `Kernel.require` before running
88
* Disable `ShellCheck` pre-commit hook by default
99
* Fix `CssLint` pre-commit hook to ignore blank lines in `csslint` output
10+
* Switch `ImageOptim` hook to use executable instead of Ruby API
1011

1112
## 0.23.0
1213

config/default.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ PreCommit:
163163

164164
ImageOptim:
165165
description: 'Checking for optimizable images'
166-
required_library: 'image_optim'
166+
required_executable: 'image_optim'
167167
install_command: 'gem install image_optim'
168168
include:
169169
- '**/*.gif'

lib/overcommit/hook/pre_commit/image_optim.rb

+11-22
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,23 @@ module Overcommit::Hook::PreCommit
22
# Checks for images that can be optimized with `image_optim`.
33
class ImageOptim < Base
44
def run
5-
optimized_images =
6-
begin
7-
optimize_images(applicable_files)
8-
rescue ::ImageOptim::BinResolver::BinNotFound => e
9-
return :fail, "#{e.message}. The image_optim gem is dependendent on this binary."
10-
end
5+
result = execute(command + applicable_files)
6+
return [:fail, result.stdout + result.stderr] unless result.success?
117

12-
if optimized_images.any?
13-
return :fail,
14-
"The following images are optimizable:\n#{optimized_images.join("\n")}" \
15-
"\n\nOptimize them by running:\n" \
16-
" image_optim --skip-missing-workers #{optimized_images.join(' ')}"
17-
end
8+
optimized_files = extract_optimized_files(result.stdout)
9+
return :pass if optimized_files.empty?
1810

19-
:pass
11+
output = "The following images are optimizable:\n#{optimized_files.join("\n")}"
12+
output += "\n\nOptimize them by running `#{command.join(' ')} #{optimized_files.join(' ')}`"
13+
[:fail, output]
2014
end
2115

2216
private
2317

24-
def optimize_images(image_paths)
25-
image_optim = ::ImageOptim.new(skip_missing_workers: true)
26-
27-
optimized_images =
28-
image_optim.optimize_images(image_paths) do |path, optimized|
29-
path if optimized
30-
end
31-
32-
optimized_images.compact
18+
def extract_optimized_files(output)
19+
output.split("\n").
20+
select { |line| line =~ /^\d+/ }.
21+
map { |line| line.split(/\s+/).last }
3322
end
3423
end
3524
end

overcommit.gemspec

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Gem::Specification.new do |s|
2828

2929
s.add_dependency 'childprocess', '~> 0.5.0'
3030

31-
s.add_development_dependency 'image_optim', '~> 0.20.0'
3231
s.add_development_dependency 'rspec', '~> 3.0'
3332
s.add_development_dependency 'travis', '~> 1.7'
3433
s.add_development_dependency 'w3c_validators', '~> 1.2'
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'spec_helper'
2-
require 'image_optim'
32

43
describe Overcommit::Hook::PreCommit::ImageOptim do
54
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
@@ -10,27 +9,47 @@
109
subject.stub(:applicable_files).and_return(%w[file1.jpg file2.png])
1110
end
1211

13-
context 'when a dependency of image_optim is not installed' do
12+
context 'when image_optim exits successfully' do
13+
let(:result) { double('result') }
14+
1415
before do
15-
subject.stub(:optimize_images).and_raise(::ImageOptim::BinResolver::BinNotFound)
16+
result.stub(:success?).and_return(true)
17+
subject.stub(:execute).and_return(result)
1618
end
1719

18-
it { should fail_hook }
19-
end
20+
context 'and images were optimized' do
21+
before do
22+
result.stub(:stdout).and_return([
23+
'32.30% 1.2K file1.jpg',
24+
'Total: 32.30% 1.2K',
25+
].join("\n"))
26+
end
2027

21-
context 'when an image was optimized' do
22-
before do
23-
subject.stub(:optimize_images).and_return(['file1.jpg'])
28+
it { should fail_hook }
2429
end
2530

26-
it { should fail_hook }
31+
context 'and no images were optimized' do
32+
before do
33+
result.stub(:stdout).and_return([
34+
'------ app/assets/images/favicons/favicon-96x96.png',
35+
'Total: ------',
36+
].join("\n"))
37+
end
38+
39+
it { should pass }
40+
end
2741
end
2842

29-
context 'when no images were optimized' do
43+
context 'when image_optim exits unsuccessfully' do
44+
let(:result) { double('result') }
45+
3046
before do
31-
subject.stub(:optimize_images).and_return([])
47+
result.stub(:success?).and_return(false)
48+
result.stub(:stdout).and_return('An error occurred')
49+
result.stub(:stderr).and_return('')
50+
subject.stub(:execute).and_return(result)
3251
end
3352

34-
it { should pass }
53+
it { should fail_hook }
3554
end
3655
end

0 commit comments

Comments
 (0)