|
4 | 4 | require 'overcommit/hook_context/prepare_commit_msg' |
5 | 5 |
|
6 | 6 | 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 | + ) |
11 | 22 | ) |
12 | 23 | end |
13 | 24 |
|
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 |
15 | 28 |
|
16 | | - subject(:hook) { described_class.new(config, context) } |
| 29 | + def hook_for(config, context) |
| 30 | + described_class.new(config, context) |
| 31 | + end |
17 | 32 |
|
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 } |
22 | 35 | end |
23 | 36 |
|
24 | | - after do |
25 | | - File.delete(prepare_commit_message_file) unless ENV['APPVEYOR'] |
| 37 | + def remove_file(name) |
| 38 | + File.delete(name) |
26 | 39 | end |
27 | 40 |
|
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) } |
29 | 46 |
|
30 | 47 | 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' } |
33 | 50 |
|
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 } |
36 | 54 |
|
37 | | - before do |
38 | | - hook.stub(:replacement_text).and_return('[#\1]') |
39 | | - end |
| 55 | + it { is_expected.to pass } |
40 | 56 |
|
41 | | - it { is_expected.to eq('[#123]') } |
| 57 | + it 'prepends the replacement text' do |
| 58 | + expect(File.read('COMMIT_EDITMSG')).to eq("[#123]\n") |
42 | 59 | end |
43 | 60 | end |
44 | 61 |
|
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 } |
47 | 65 |
|
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 |
54 | 68 |
|
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' } |
57 | 73 |
|
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) } |
63 | 77 |
|
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 } |
68 | 79 |
|
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 } |
75 | 81 |
|
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')) |
78 | 84 | end |
79 | 85 | end |
80 | 86 | end |
|
0 commit comments