|
| 1 | +require 'spec_helper' |
| 2 | +require 'overcommit/hook_context/prepare_commit_msg' |
| 3 | + |
| 4 | +describe Overcommit::Hook::PrepareCommitMsg::ReplaceBranch do |
| 5 | + let(:config) { Overcommit::ConfigurationLoader.default_configuration } |
| 6 | + let(:context) do |
| 7 | + Overcommit::HookContext::PrepareCommitMsg.new( |
| 8 | + config, [prepare_commit_message_file, 'commit'], StringIO.new |
| 9 | + ) |
| 10 | + end |
| 11 | + |
| 12 | + let(:prepare_commit_message_file) { 'prepare_commit_message_file.txt' } |
| 13 | + |
| 14 | + subject(:hook) { described_class.new(config, context) } |
| 15 | + |
| 16 | + before do |
| 17 | + File.open(prepare_commit_message_file, 'w') |
| 18 | + allow(Overcommit::Utils).to receive_message_chain(:log, :debug) |
| 19 | + allow(Overcommit::GitRepo).to receive(:current_branch).and_return(new_head) |
| 20 | + end |
| 21 | + |
| 22 | + after do |
| 23 | + File.delete(prepare_commit_message_file) unless ENV['APPVEYOR'] |
| 24 | + end |
| 25 | + |
| 26 | + let(:new_head) { 'userbeforeid-12345-branch-description' } |
| 27 | + |
| 28 | + describe '#run' do |
| 29 | + context 'when the checked out branch matches the pattern' do |
| 30 | + it { is_expected.to pass } |
| 31 | + |
| 32 | + context 'template contents' do |
| 33 | + subject(:template) { hook.new_template } |
| 34 | + |
| 35 | + before do |
| 36 | + hook.stub(:replacement_text).and_return('Id is: \1') |
| 37 | + end |
| 38 | + |
| 39 | + it { is_expected.to eq('Id is: 12345') } |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context 'when the checked out branch does not match the pattern' do |
| 44 | + let(:new_head) { "this shouldn't match the default pattern" } |
| 45 | + |
| 46 | + it { is_expected.to warn } |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + describe '#replacement_text' do |
| 51 | + subject(:replacement_text) { hook.replacement_text } |
| 52 | + let(:replacement_template_file) { 'valid_filename.txt' } |
| 53 | + let(:replacement) { 'Id is: \1' } |
| 54 | + |
| 55 | + context 'when the replacement text points to a valid filename' do |
| 56 | + before do |
| 57 | + hook.stub(:replacement_text_config).and_return(replacement_template_file) |
| 58 | + File.stub(:exist?).and_return(true) |
| 59 | + File.stub(:read).with(replacement_template_file).and_return(replacement) |
| 60 | + end |
| 61 | + |
| 62 | + describe 'it reads it as the replacement template' do |
| 63 | + it { is_expected.to eq(replacement) } |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | +end |
0 commit comments