Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/overcommit/hook/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ def enabled?
@config['enabled'] != false
end

def excluded?
exclude_branch_patterns.any? { |p| File.fnmatch(p, current_branch) }
end

def skip?
@config['skip']
end

def run?
enabled? &&
!excluded? &&
!(@config['requires_files'] && applicable_files.empty?)
end

Expand Down Expand Up @@ -276,5 +281,13 @@ def transform_status(status)
status
end
end

def exclude_branch_patterns
@config['exclude_branch_patterns'] || []
end

def current_branch
@current_branch ||= Overcommit::GitRepo.current_branch
end
end
end
88 changes: 88 additions & 0 deletions spec/overcommit/hook/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,92 @@
end
end
end

describe '#run?' do
let(:modified_files) { [] }
let(:hook_config) do
{
'enabled' => enabled,
'requires_files' => requires_files,
}
end

before do
config.stub(:for_hook).and_return(hook_config)
context.stub(:modified_files).and_return(modified_files)
end

subject { hook.run? }

context 'enabled is true, requires_files is false, modified_files empty' do
let(:enabled) { true }
let(:requires_files) { false }

it { subject.should == true }
end

context 'enabled is false, requires_files is false, modified_files empty' do
let(:enabled) { false }
let(:requires_files) { false }

it { subject.should == false }
end

context 'enabled is true, requires_files is true, modified_files is not empty' do
let(:enabled) { true }
let(:requires_files) { true }
let(:modified_files) { ['file1'] }

it { subject.should == true }
end

context 'enabled is true, requires_files is false, modified_files is not empty' do
let(:enabled) { true }
let(:requires_files) { false }
let(:modified_files) { ['file1'] }

it { subject.should == true }
end

context 'with exclude_branch_patterns' do
let(:current_branch) { 'tj-test-branch' }
let(:hook_config) do
{
'enabled' => true,
'requires_files' => false,
'exclude_branch_patterns' => exclude_branch_patterns
}
end

before do
allow(Overcommit::GitRepo).
to receive(:current_branch).
and_return(current_branch)
end

context 'exclude_branch_patterns is nil' do
let(:exclude_branch_patterns) { nil }

it { subject.should == true }
end

context 'exact match between exclude_branch_patterns and current_branch' do
let(:exclude_branch_patterns) { ['tj-test-branch'] }

it { subject.should == false }
end

context 'partial match between exclude_branch_patterns and current_branch' do
let(:exclude_branch_patterns) { ['tj-test-*'] }

it { subject.should == false }
end

context 'non-match between exclude_branch_patterns and current_branch' do
let(:exclude_branch_patterns) { ['test-*'] }

it { subject.should == true }
end
end
end
end