-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support loading Overcommit hooks supplied by Gems in the same fashion as hooks that can be provided in Repo-Specific hooks.
- Loading branch information
Showing
9 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Overcommit::HookLoader | ||
# Responsible for loading custom hooks that users install via Gems | ||
class GemHookLoader < Base | ||
def load_hooks | ||
@config.enabled_gem_hooks(@context).map do |hook_name| | ||
underscored_hook_name = Overcommit::Utils.snake_case(hook_name) | ||
require "overcommit/hook/#{@context.hook_type_name}/#{underscored_hook_name}" | ||
create_hook(hook_name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe Overcommit::HookLoader::GemHookLoader do | ||
let(:hash) { {} } | ||
let(:config) { Overcommit::Configuration.new(hash) } | ||
let(:logger) { double('logger') } | ||
let(:context) { double('context') } | ||
let(:loader) { described_class.new(config, context, logger) } | ||
|
||
describe '#load_hooks' do | ||
subject(:load_hooks) { loader.send(:load_hooks) } | ||
|
||
before do | ||
context.stub(hook_class_name: 'PreCommit', | ||
hook_type_name: 'pre_commit') | ||
end | ||
|
||
it 'loads enabled gem hooks' do | ||
allow(config).to receive(:enabled_gem_hooks).with(context).and_return(['MyCustomHook']) | ||
|
||
allow(loader).to receive(:require). | ||
with('overcommit/hook/pre_commit/my_custom_hook'). | ||
and_return(true) | ||
allow(loader).to receive(:create_hook).with('MyCustomHook') | ||
expect(loader).to receive(:require).with('overcommit/hook/pre_commit/my_custom_hook') | ||
load_hooks | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe Overcommit::HookRunner do | ||
let(:hash) { {} } | ||
let(:config) { Overcommit::Configuration.new(hash) } | ||
let(:logger) { double('logger') } | ||
let(:context) { double('context') } | ||
let(:printer) { double('printer') } | ||
let(:runner) { described_class.new(config, logger, context, printer) } | ||
|
||
describe '#load_hooks' do | ||
subject(:load_hooks) { runner.send(:load_hooks) } | ||
|
||
before do | ||
context.stub(hook_class_name: 'PreCommit', | ||
hook_type_name: 'pre_commit') | ||
allow_any_instance_of(Overcommit::HookLoader::BuiltInHookLoader). | ||
to receive(:load_hooks).and_return([]) | ||
allow_any_instance_of(Overcommit::HookLoader::PluginHookLoader). | ||
to receive(:load_hooks).and_return([]) | ||
end | ||
|
||
context 'when gem_plugins is disabled' do | ||
let(:hash) do | ||
{ | ||
'gem_plugins' => false | ||
} | ||
end | ||
|
||
it 'expects not to load Gem hooks' do | ||
expect_any_instance_of(Overcommit::HookLoader::GemHookLoader). | ||
not_to receive(:load_hooks) | ||
load_hooks | ||
end | ||
end | ||
|
||
context 'when gem_plugins is enabled' do | ||
let(:hash) do | ||
{ | ||
'gem_plugins' => true | ||
} | ||
end | ||
let(:gemhookloader) { Overcommit::HookLoader::GemHookLoader.new(config, context, logger) } | ||
|
||
it 'expects to load Gem hooks' do | ||
expect_any_instance_of(Overcommit::HookLoader::GemHookLoader). | ||
to receive(:load_hooks).and_call_original | ||
load_hooks | ||
end | ||
end | ||
end | ||
end |