Skip to content

Commit 9e9c87f

Browse files
committed
Add some tests for ad-hoc (pre-commit) Git hooks
1 parent 7d94fa5 commit 9e9c87f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'ad-hoc pre-commit hook' do
6+
subject do
7+
hook_loader = Overcommit::HookLoader::PluginHookLoader.new(
8+
config,
9+
context,
10+
Overcommit::Logger.new(STDOUT)
11+
)
12+
hooks = hook_loader.load_hooks
13+
hooks.find { |h| h.name == hook_name }
14+
end
15+
let(:config) do
16+
config = Overcommit::Configuration.new(
17+
YAML.safe_load(config_contents, [Regexp]), {
18+
validate: false
19+
}
20+
)
21+
Overcommit::ConfigurationLoader.default_configuration.merge(config)
22+
end
23+
let(:context) do
24+
empty_stdin = File.open(File::NULL) # pre-commit hooks don't take input
25+
context = Overcommit::HookContext.create('pre-commit', config, applicable_files, empty_stdin)
26+
context
27+
end
28+
29+
around do |example|
30+
repo do
31+
example.run
32+
end
33+
end
34+
35+
describe 'if not line-aware' do
36+
let(:config_contents) do
37+
<<-'YML'
38+
PreCommit:
39+
FooGitHook:
40+
enabled: true
41+
command: "foocmd"
42+
YML
43+
end
44+
let(:hook_name) { 'FooGitHook' }
45+
let(:applicable_files) { nil }
46+
47+
before do
48+
context.stub(:execute_hook).with(%w[foocmd]).
49+
and_return(result)
50+
end
51+
52+
context 'when command succeeds' do
53+
let(:result) do
54+
double(success?: true, stdout: '')
55+
end
56+
57+
it { should pass }
58+
end
59+
60+
context 'when command fails' do
61+
let(:result) do
62+
double(success?: false, stdout: '', stderr: '')
63+
end
64+
65+
it { should fail_hook }
66+
end
67+
end
68+
end

0 commit comments

Comments
 (0)