From b8392510f0fd0ba0a45add3a74e75a7bbe68cc90 Mon Sep 17 00:00:00 2001 From: Mark Delk Date: Fri, 7 Aug 2020 20:55:11 -0500 Subject: [PATCH] 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 --- .../prepare_commit_msg/replace_branch_spec.rb | 102 +++++++++--------- 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/spec/overcommit/hook/prepare_commit_msg/replace_branch_spec.rb b/spec/overcommit/hook/prepare_commit_msg/replace_branch_spec.rb index 7ad60d61..11e1e24e 100644 --- a/spec/overcommit/hook/prepare_commit_msg/replace_branch_spec.rb +++ b/spec/overcommit/hook/prepare_commit_msg/replace_branch_spec.rb @@ -4,77 +4,83 @@ require 'overcommit/hook_context/prepare_commit_msg' describe Overcommit::Hook::PrepareCommitMsg::ReplaceBranch do - let(:config) { Overcommit::ConfigurationLoader.default_configuration } - let(:context) do - Overcommit::HookContext::PrepareCommitMsg.new( - config, [prepare_commit_message_file], StringIO.new + def set_branch(branch) + allow(Overcommit::GitRepo).to receive(:current_branch).and_return(branch) + end + + def new_config(opts = {}) + default = Overcommit::ConfigurationLoader.default_configuration + + return default if opts.empty? + + default.merge( + Overcommit::Configuration.new( + 'PrepareCommitMsg' => { + 'ReplaceBranch' => opts.merge('enabled' => true) + } + ) ) end - let(:prepare_commit_message_file) { 'prepare_commit_message_file.txt' } + def new_context(config, argv) + Overcommit::HookContext::PrepareCommitMsg.new(config, argv, StringIO.new) + end - subject(:hook) { described_class.new(config, context) } + def hook_for(config, context) + described_class.new(config, context) + end - before do - File.open(prepare_commit_message_file, 'w') - allow(Overcommit::Utils).to receive_message_chain(:log, :debug) - allow(Overcommit::GitRepo).to receive(:current_branch).and_return(new_head) + def add_file(name, contents) + File.open(name, 'w') { |f| f.puts contents } end - after do - File.delete(prepare_commit_message_file) unless ENV['APPVEYOR'] + def remove_file(name) + File.delete(name) end - let(:new_head) { '123-topic' } + before { allow(Overcommit::Utils).to receive_message_chain(:log, :debug) } + + let(:config) { new_config } + let(:normal_context) { new_context(config, ["COMMIT_EDITMSG"]) } + subject(:hook) { hook_for(config, normal_context) } describe '#run' do - context 'when the checked out branch matches the pattern' do - it { is_expected.to pass } + before { add_file 'COMMIT_EDITMSG', '' } + after { remove_file 'COMMIT_EDITMSG' } - context 'template contents' do - subject(:template) { hook.new_template } + context 'when the checked out branch matches the pattern' do + before { set_branch '123-topic' } + before { hook.run } - before do - hook.stub(:replacement_text).and_return('[#\1]') - end + it { is_expected.to pass } - it { is_expected.to eq('[#123]') } + it 'prepends the replacement text' do + expect(File.read('COMMIT_EDITMSG')).to eq("[#123]\n") end end - context 'when the checked out branch does not match the pattern' do - let(:new_head) { "this shouldn't match the default pattern" } + context "when the checked out branch doesn't matches the pattern" do + before { set_branch 'topic-123' } + before { hook.run } - context 'when the commit type is in `skipped_commit_types`' do - let(:context) do - Overcommit::HookContext::PrepareCommitMsg.new( - config, [prepare_commit_message_file, 'template'], StringIO.new - ) - end + it { is_expected.to warn } + end - it { is_expected.to pass } - end + context 'when the replacement text points to a valid filename' do + before { set_branch '123-topic' } + before { add_file 'replacement_text.txt', 'FOO' } + after { remove_file 'replacement_text.txt' } - context 'when the commit type is not in `skipped_commit_types`' do - it { is_expected.to warn } - end - end - end + let(:config) { new_config('replacement_text' => 'replacement_text.txt') } + let(:normal_context) { new_context(config, ['COMMIT_EDITMSG']) } + subject(:hook) { hook_for(config, normal_context) } - describe '#replacement_text' do - subject(:replacement_text) { hook.replacement_text } - let(:replacement_template_file) { 'valid_filename.txt' } - let(:replacement) { '[#\1]' } + before { hook.run } - context 'when the replacement text points to a valid filename' do - before do - hook.stub(:replacement_text_config).and_return(replacement_template_file) - File.stub(:exist?).and_return(true) - File.stub(:read).with(replacement_template_file).and_return(replacement) - end + it { is_expected.to pass } - describe 'it reads it as the replacement template' do - it { is_expected.to eq(replacement) } + it 'uses the file contents as the replacement text' do + expect(File.read('COMMIT_EDITMSG')).to eq(File.read('replacement_text.txt')) end end end