Skip to content

Commit 075468f

Browse files
committed
refactor ReplaceBranch specs to avoid so much mocking
This changes the specs for the ReplaceBranch hook to - test the contents of the file that `git` writes it's commit message to - explicitly set config variables instead of mocking them
1 parent 0bcc8dd commit 075468f

File tree

1 file changed

+54
-48
lines changed

1 file changed

+54
-48
lines changed

spec/overcommit/hook/prepare_commit_msg/replace_branch_spec.rb

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,83 @@
44
require 'overcommit/hook_context/prepare_commit_msg'
55

66
describe Overcommit::Hook::PrepareCommitMsg::ReplaceBranch do
7-
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
8-
let(:context) do
9-
Overcommit::HookContext::PrepareCommitMsg.new(
10-
config, [prepare_commit_message_file], StringIO.new
7+
def checkout_branch(branch)
8+
allow(Overcommit::GitRepo).to receive(:current_branch).and_return(branch)
9+
end
10+
11+
def new_config(opts = {})
12+
default = Overcommit::ConfigurationLoader.default_configuration
13+
14+
return default if opts.empty?
15+
16+
default.merge(
17+
Overcommit::Configuration.new(
18+
'PrepareCommitMsg' => {
19+
'ReplaceBranch' => opts.merge('enabled' => true)
20+
}
21+
)
1122
)
1223
end
1324

14-
let(:prepare_commit_message_file) { 'prepare_commit_message_file.txt' }
25+
def new_context(config, argv)
26+
Overcommit::HookContext::PrepareCommitMsg.new(config, argv, StringIO.new)
27+
end
1528

16-
subject(:hook) { described_class.new(config, context) }
29+
def hook_for(config, context)
30+
described_class.new(config, context)
31+
end
1732

18-
before do
19-
File.open(prepare_commit_message_file, 'w')
20-
allow(Overcommit::Utils).to receive_message_chain(:log, :debug)
21-
allow(Overcommit::GitRepo).to receive(:current_branch).and_return(new_head)
33+
def add_file(name, contents)
34+
File.open(name, 'w') { |f| f.puts contents }
2235
end
2336

24-
after do
25-
File.delete(prepare_commit_message_file) unless ENV['APPVEYOR']
37+
def remove_file(name)
38+
File.delete(name)
2639
end
2740

28-
let(:new_head) { '123-topic' }
41+
before { allow(Overcommit::Utils).to receive_message_chain(:log, :debug) }
42+
43+
let(:config) { new_config }
44+
let(:normal_context) { new_context(config, ['COMMIT_EDITMSG']) }
45+
subject(:hook) { hook_for(config, normal_context) }
2946

3047
describe '#run' do
31-
context 'when the checked out branch matches the pattern' do
32-
it { is_expected.to pass }
48+
before { add_file 'COMMIT_EDITMSG', '' }
49+
after { remove_file 'COMMIT_EDITMSG' }
3350

34-
context 'template contents' do
35-
subject(:template) { hook.new_template }
51+
context 'when the checked out branch matches the pattern' do
52+
before { checkout_branch '123-topic' }
53+
before { hook.run }
3654

37-
before do
38-
hook.stub(:replacement_text).and_return('[#\1]')
39-
end
55+
it { is_expected.to pass }
4056

41-
it { is_expected.to eq('[#123]') }
57+
it 'prepends the replacement text' do
58+
expect(File.read('COMMIT_EDITMSG')).to eq("[#123]\n")
4259
end
4360
end
4461

45-
context 'when the checked out branch does not match the pattern' do
46-
let(:new_head) { "this shouldn't match the default pattern" }
62+
context "when the checked out branch doesn't matches the pattern" do
63+
before { checkout_branch 'topic-123' }
64+
before { hook.run }
4765

48-
context 'when the commit type is in `skipped_commit_types`' do
49-
let(:context) do
50-
Overcommit::HookContext::PrepareCommitMsg.new(
51-
config, [prepare_commit_message_file, 'template'], StringIO.new
52-
)
53-
end
66+
it { is_expected.to warn }
67+
end
5468

55-
it { is_expected.to pass }
56-
end
69+
context 'when the replacement text points to a valid filename' do
70+
before { checkout_branch '123-topic' }
71+
before { add_file 'replacement_text.txt', 'FOO' }
72+
after { remove_file 'replacement_text.txt' }
5773

58-
context 'when the commit type is not in `skipped_commit_types`' do
59-
it { is_expected.to warn }
60-
end
61-
end
62-
end
74+
let(:config) { new_config('replacement_text' => 'replacement_text.txt') }
75+
let(:normal_context) { new_context(config, ['COMMIT_EDITMSG']) }
76+
subject(:hook) { hook_for(config, normal_context) }
6377

64-
describe '#replacement_text' do
65-
subject(:replacement_text) { hook.replacement_text }
66-
let(:replacement_template_file) { 'valid_filename.txt' }
67-
let(:replacement) { '[#\1]' }
78+
before { hook.run }
6879

69-
context 'when the replacement text points to a valid filename' do
70-
before do
71-
hook.stub(:replacement_text_config).and_return(replacement_template_file)
72-
File.stub(:exist?).and_return(true)
73-
File.stub(:read).with(replacement_template_file).and_return(replacement)
74-
end
80+
it { is_expected.to pass }
7581

76-
describe 'it reads it as the replacement template' do
77-
it { is_expected.to eq(replacement) }
82+
it 'uses the file contents as the replacement text' do
83+
expect(File.read('COMMIT_EDITMSG')).to eq(File.read('replacement_text.txt'))
7884
end
7985
end
8086
end

0 commit comments

Comments
 (0)