Skip to content

Commit

Permalink
Add skip_if hook option (#715)
Browse files Browse the repository at this point in the history
Skip hook with `skip_if` configuration if defined.
  • Loading branch information
Kevin Deisz authored Apr 24, 2020
1 parent a15edd2 commit 9a4028e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ Option | Description
`processors` | The number of processing units to reserve for this hook. This does not reserve CPUs, but indicates that out of the total number of possible concurrent hooks allowed by the global `concurrency` option, this hook requires the specified number. Thus in the typical case where `concurrency` is set to the number of available cores (default), and you have a hook that executes an application which itself creates 2 threads (or is otherwise scheduled on 2 cores), you can indicate that Overcommit should allocate 2 `processors` to the hook. Ideally this means your hooks won't put undue load on your available cores.
`install_command` | Command the user can run to install the `required_executable` (or alternately the specified `required_libraries`). This is intended for documentation purposes, as Overcommit does not install software on your behalf since there are too many edge cases where such behavior would result in incorrectly configured installations (e.g. installing a Python package in the global package space instead of in a virtual environment).
`skip_file_checkout` | Whether to skip this hook for file checkouts (e.g. `git checkout some-ref -- file`). Only applicable to `PostCheckout` hooks.
`skip_if` | Array of arguments to be executed to determine whether or not the hook should run. For example, setting this to a value of `['bash', '-c', '! which my-executable']` would allow you to skip running this hook if `my-executable` was not in the bin path.

In addition to the built-in configuration options, each hook can expose its
own unique configuration options. The `AuthorEmail` hook, for example, allows
Expand Down
3 changes: 2 additions & 1 deletion lib/overcommit/hook/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def excluded?
end

def skip?
@config['skip']
@config['skip'] ||
(@config['skip_if'] ? execute(@config['skip_if']).success? : false)
end

def run?
Expand Down
65 changes: 65 additions & 0 deletions spec/overcommit/hook/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,69 @@
end
end
end

context '#skip?' do
before do
config.stub(:for_hook).and_return(hook_config)
end

subject { hook.skip? }

context 'with skip_if not specified' do
let(:hook_config) do
{ 'skip' => skip }
end

context 'with skip true' do
let(:skip) { true }

it { subject.should == true }
end

context 'with skip false' do
let(:skip) { false }

it { subject.should == false }
end
end

context 'with skip_if specified' do
before do
result = Overcommit::Subprocess::Result.new(success ? 0 : 1, '', '')
allow(Overcommit::Utils).to receive(:execute).and_return(result)
end

let(:hook_config) do
{ 'skip' => skip, 'skip_if' => ['bash', '-c', '! which my-executable'] }
end

context 'with skip true and skip_if returning true' do
let(:skip) { true }
let(:success) { true }

it { subject.should == true }
end

context 'with skip true and skip_if returning false' do
let(:skip) { true }
let(:success) { false }

it { subject.should == true }
end

context 'with skip false and skip_if returning true' do
let(:skip) { false }
let(:success) { true }

it { subject.should == true }
end

context 'with skip false and skip_if returning false' do
let(:skip) { false }
let(:success) { false }

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

0 comments on commit 9a4028e

Please sign in to comment.