Skip to content

Commit 7f14be6

Browse files
committed
Add integration test for gemfile config option
Since this is likely going to be an important feature, add an integration test that ensures it is working as intended. The main reason this is difficult is because we are already running the tests within a Bundler context, so we need to construct an artificial environment that still exposes Overcommit as a gem without having to have it installed ahead of time.
1 parent afe54a6 commit 7f14be6

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
require 'spec_helper'
2+
3+
describe 'specifying `gemfile` option in Overcommit configuration' do
4+
let(:repo_root) { File.expand_path(File.join('..', '..'), File.dirname(__FILE__)) }
5+
let(:fake_gem_path) { File.join('lib', 'my_fake_gem') }
6+
7+
# We point the overcommit gem back to this repo since we can't assume the gem
8+
# has already been installed in a test environment
9+
let(:gemfile) { normalize_indent(<<-RUBY) }
10+
gem 'overcommit', path: '#{repo_root}'
11+
gem 'my_fake_gem', path: '#{fake_gem_path}'
12+
RUBY
13+
14+
let(:gemspec) { normalize_indent(<<-RUBY) }
15+
Gem::Specification.new do |s|
16+
s.name = 'my_fake_gem'
17+
s.version = '1.0.0'
18+
s.author = 'John Doe'
19+
s.email = 'john.doe@example.com'
20+
s.summary = 'A fake gem'
21+
s.files = [File.join('lib', 'my_fake_gem.rb')]
22+
end
23+
RUBY
24+
25+
# Specify a hook that depends on an external gem to test Gemfile loading
26+
let(:hook) { normalize_indent(<<-RUBY) }
27+
module Overcommit::Hook::PreCommit
28+
class FakeHook < Base
29+
def run
30+
require 'my_fake_gem'
31+
:pass
32+
end
33+
end
34+
end
35+
RUBY
36+
37+
let(:config) { normalize_indent(<<-YAML) }
38+
verify_plugin_signatures: false
39+
40+
CommitMsg:
41+
ALL:
42+
enabled: false
43+
44+
PreCommit:
45+
ALL:
46+
enabled: false
47+
FakeHook:
48+
enabled: true
49+
requires_files: false
50+
YAML
51+
52+
around do |example|
53+
repo do
54+
# Since RSpec is being run within a Bundler context we need to clear it
55+
# in order to not taint the test
56+
Bundler.with_clean_env do
57+
FileUtils.mkdir_p(File.join(fake_gem_path, 'lib'))
58+
echo(gemspec, File.join(fake_gem_path, 'my_fake_gem.gemspec'))
59+
FileUtils.touch(File.join(fake_gem_path, 'lib', 'my_fake_gem.rb'))
60+
61+
echo(gemfile, '.overcommit_gems.rb')
62+
`bundle install --gemfile=.overcommit_gems.rb`
63+
64+
echo(config, '.overcommit.yml')
65+
66+
# Set BUNDLE_GEMFILE so we load Overcommit from the current repo
67+
`BUNDLE_GEMFILE=.overcommit_gems.rb bundle exec overcommit --install > #{File::NULL}`
68+
FileUtils.mkdir_p(File.join('.git-hooks', 'pre_commit'))
69+
echo(hook, File.join('.git-hooks', 'pre_commit', 'fake_hook.rb'))
70+
71+
example.run
72+
end
73+
end
74+
end
75+
76+
subject { shell(%w[git commit --allow-empty -m Test]) }
77+
78+
context 'when configuration specifies the gemfile' do
79+
let(:config) { "gemfile: .overcommit_gems.rb\n" + super() }
80+
81+
it 'runs the hook successfully' do
82+
subject.status.should == 0
83+
end
84+
end
85+
86+
context 'when configuration does not specify the gemfile' do
87+
it 'fails to run the hook' do
88+
subject.status.should_not == 0
89+
end
90+
end
91+
end

spec/support/normalize_indent.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Strips off excess leading indentation from each line so we can use Heredocs
2+
# for writing code without having the leading indentation count.
3+
module IndentNormalizer
4+
def normalize_indent(code)
5+
leading_indent = code[/^(\s*)/, 1]
6+
code.lstrip.gsub(/\n#{leading_indent}/, "\n")
7+
end
8+
end
9+
10+
RSpec.configure do |_config|
11+
include IndentNormalizer
12+
end

0 commit comments

Comments
 (0)