Skip to content

Commit 380bb64

Browse files
gsmendozaShane da Silva
authored and
Shane da Silva
committed
Add image optimization pre-commit plugin
Change-Id: I0817b9ff0b392d6e30b4c268d1a7a71e836123b8 Reviewed-on: https://gerrit.causes.com/30488 Tested-by: jenkins <jenkins@causes.com> Reviewed-by: Shane da Silva <shane@causes.com>
1 parent 811863a commit 380bb64

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'image_optim'
2+
3+
module Overcommit::GitHook
4+
class ImageOptimization < HookSpecificCheck
5+
include HookRegistry
6+
file_types :gif, :jpg, :png
7+
8+
def run_check
9+
optimized_images =
10+
begin
11+
ImageSet.new(staged.map(&:original_path)).optimize!
12+
rescue ImageOptim::BinNotFoundError => e
13+
return :stop,
14+
"#{e.message}. The image_optim gem is dependendent on this binary. " <<
15+
"See https://github.com/toy/image_optim for more info."
16+
end
17+
18+
if optimized_images.any?
19+
return :bad,
20+
"Optimized #{optimized_images.map(&:to_s).inspect}. " <<
21+
" Please add them to your commit."
22+
else
23+
return :good
24+
end
25+
end
26+
27+
class ImageSet
28+
attr_reader :image_paths
29+
30+
def initialize(image_paths)
31+
@image_paths ||= image_paths
32+
end
33+
34+
def image_optim
35+
@image_optim ||= ImageOptim.new(pngout: false)
36+
end
37+
38+
def optimize!
39+
results =
40+
image_optim.optimize_images!(image_paths) do |path, was_optimized|
41+
path if was_optimized
42+
end
43+
44+
results.compact
45+
end
46+
end
47+
end
48+
end

overcommit.gemspec

+2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ Gem::Specification.new do |s|
2121
Dir['bin/**/*'] +
2222
Dir['config/*.yml']
2323

24+
s.add_runtime_dependency "image_optim"
25+
2426
s.add_development_dependency 'rspec', '2.13.0'
2527
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::GitHook::ImageOptimization::ImageSet do
4+
describe "#initialize(image_paths)" do
5+
let(:path) { '/tmp/filename.jpg20131024-7103-s1d6n9.jpg' }
6+
7+
it "sets the image_paths of the image set" do
8+
image_set = described_class.new([path])
9+
expect(image_set.image_paths).to eq([path])
10+
end
11+
end
12+
13+
describe "#optimize!" do
14+
let(:optimized_image) { '/tmp/optimized.jpg' }
15+
let(:unoptimized_image) { '/tmp/unoptimized.jpg' }
16+
let(:image_optim) { stub(ImageOptim) }
17+
18+
it "excludes already optimized images from the results" do
19+
ImageOptim.should_receive(:new).and_return(image_optim)
20+
21+
image_optim.should_receive(:optimize_images!)
22+
.with([unoptimized_image, optimized_image])
23+
.and_yield(unoptimized_image, true)
24+
.and_yield(optimized_image, false)
25+
.and_return([unoptimized_image])
26+
27+
image_set = described_class.new([unoptimized_image, optimized_image])
28+
results = image_set.optimize!
29+
expect(results).to eq([unoptimized_image])
30+
end
31+
end
32+
33+
describe "image_optim" do
34+
it "should be an ImageOptim" do
35+
image_set = described_class.new([])
36+
expect(image_set.image_optim).to be_a(ImageOptim)
37+
end
38+
end
39+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::GitHook::ImageOptimization do
4+
describe '.filetypes' do
5+
it "includes only image filetypes" do
6+
expect(described_class.filetypes).to eq([:gif, :jpg, :png])
7+
end
8+
end
9+
10+
describe "#run_check" do
11+
let(:staged_filename) { 'filename.jpg' }
12+
13+
around do |example|
14+
repo do
15+
FileUtils.touch staged_filename
16+
`git add #{staged_filename}`
17+
example.run
18+
end
19+
end
20+
21+
context "when a dependency of image_optim is not installed" do
22+
let(:image_set) { stub(described_class::ImageSet) }
23+
24+
before do
25+
described_class::ImageSet
26+
.should_receive(:new)
27+
.with([staged_filename])
28+
.and_return(image_set)
29+
30+
image_set.should_receive(:optimize!).and_raise(ImageOptim::BinNotFoundError)
31+
end
32+
33+
it { should stop }
34+
end
35+
36+
context "when some staged files were optimized" do
37+
let(:image_set) { stub(described_class::ImageSet) }
38+
39+
before do
40+
described_class::ImageSet
41+
.should_receive(:new)
42+
.with([staged_filename])
43+
.and_return(image_set)
44+
45+
image_set.should_receive(:optimize!).and_return([staged_filename])
46+
end
47+
48+
it { should fail_check }
49+
end
50+
51+
context "when no staged files were optimized" do
52+
let(:image_set) { stub(described_class::ImageSet) }
53+
54+
before do
55+
described_class::ImageSet
56+
.should_receive(:new)
57+
.with([staged_filename])
58+
.and_return(image_set)
59+
60+
image_set.should_receive(:optimize!).and_return([])
61+
end
62+
63+
it { should pass }
64+
end
65+
end
66+
end

0 commit comments

Comments
 (0)