require File.expand_path('../../../spec_helper', __FILE__) require 'git_commit_notifier' describe GitCommitNotifier::CommitHook do it "should ignore merge" do # 4 commits, one email for each of them, without merge run_with_config('spec/fixtures/git-notifier-ignore-merge.yml', 4) end it "should hook with merge" do # 5 commits, one email for each of them, with merge mail run_with_config('spec/fixtures/git-notifier-with-merge.yml', 5) end it "should hook group email by push" do # 1 commit for the push, all commits in the one message run_with_config('spec/fixtures/git-notifier-group-email-by-push.yml', 1) end it "should ignore commits to non specified branches if branch limits supplied" do # 4 commits, one email for each of them, without merge run_and_reject('spec/fixtures/git-notifier-with-branch-restrictions.yml',0,'refs/heads/branchx') end it "should email for commits to branch in include_branch" do # 4 commits, one email for each of them, without merge run_with_config('spec/fixtures/git-notifier-with-branch-restrictions.yml',4,'refs/heads/branch2') end it "should email for commits to master if master set as include_branch" do # 4 commits, one email for each of them, without merge run_with_config('spec/fixtures/git-notifier-with-branch-restrictions.yml',4) end def run_with_config(config, times, branch = 'refs/heads/master') expect_repository_access emailer = mock!.send.times(times).subject mock(GitCommitNotifier::Emailer).new(anything, anything) { emailer }.times(times) mock(GitCommitNotifier::CommitHook).info(/Sending mail/) any_instance_of(GitCommitNotifier::DiffToHtml, :check_handled_commits => lambda { |commits| commits }) GitCommitNotifier::CommitHook.run config, REVISIONS.first, REVISIONS.last, branch end def run_and_reject(config,times,branch) mock(GitCommitNotifier::Git).mailing_list_address { 'recipient@test.com' } emailer = mock!.send.times(times).subject mock(GitCommitNotifier::Emailer).new(anything, anything).times(times) mock(GitCommitNotifier::CommitHook).info(/Supressing mail for branch/) GitCommitNotifier::CommitHook.run config, REVISIONS.first, REVISIONS.last, branch end def test_commit_from # 1 commit with a from: adress expect_repository_access emailer = mock!.send.subject mock(GitCommitNotifier::Emailer).new(anything, hash_including(:from_address => "max@example.com")) { emailer } GitCommitNotifier::CommitHook.run 'spec/fixtures/git-notifier-group-email-by-push.yml', REVISIONS.first, REVISIONS.last, 'refs/heads/master' end def expect_repository_access mock(GitCommitNotifier::Git).log(REVISIONS.first, REVISIONS.last) { IO.read(FIXTURES_PATH + 'git_log') } mock(GitCommitNotifier::Git).mailing_list_address { 'recipient@test.com' } REVISIONS.each do |rev| mock(GitCommitNotifier::Git).show(rev) { IO.read(FIXTURES_PATH + "git_show_#{rev}") } end end describe :logger do it "should be instance of logger" do stub(GitCommitNotifier::CommitHook).config { {} } GitCommitNotifier::CommitHook.logger.should be_kind_of(GitCommitNotifier::Logger) end end describe :show_error do it "should write error to stderr" do mock($stderr).puts("\n").times(2) mock($stderr).puts(/GIT\sNOTIFIER\sPROBLEM/).times(2) mock($stderr).puts('yes') GitCommitNotifier::CommitHook.show_error('yes') end end describe :info do it "should write to and flush stdout" do mock($stdout).puts('msg') mock($stdout).flush GitCommitNotifier::CommitHook.info('msg') end end describe :run do it "should report error when no recipients specified" do mock(File).exists?(:noconfig) { false } mock(GitCommitNotifier::Git).mailing_list_address { nil } mock(GitCommitNotifier::CommitHook).show_error(/recipient/) GitCommitNotifier::CommitHook.run(:noconfig, :rev1, :rev2, 'master') end end describe :include_branches do it "should be nil if not specified in config" do mock(GitCommitNotifier::CommitHook).config { Hash.new } GitCommitNotifier::CommitHook.include_branches.should be_nil end it "should be single item array if one branch as string specified" do mock(GitCommitNotifier::CommitHook).config { { 'include_branches' => 'staging' } } GitCommitNotifier::CommitHook.include_branches.should == %w( staging ) end it "should be array if specified as array" do mock(GitCommitNotifier::CommitHook).config { { 'include_branches' => %w(test staging gotcha) } } GitCommitNotifier::CommitHook.include_branches.should == %w(test staging gotcha) end it "should be array of items, splitted by comma if specified as comma-separated list string" do mock(GitCommitNotifier::CommitHook).config { { 'include_branches' => 'test, me, yourself' } } GitCommitNotifier::CommitHook.include_branches.should == %w(test me yourself) end end end