Skip to content

Commit 5517894

Browse files
committed
Inject Logger into ConfigurationLoader
In order to support displaying warnings about configuration, we need to have access to the `Logger` instance. Inject a `Logger` into the `ConfigurationLoader` constructor and update all uses of it to provide this dependency. A follow-up commit will use this to display warnings for hook configurations that don't explicitly have the `enabled` option set.
1 parent 753ddc6 commit 5517894

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

lib/overcommit/cli.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def print_version(program_name)
146146
# Prints the hooks available in the current repo and whether they're
147147
# enabled/disabled.
148148
def print_installed_hooks
149-
config = Overcommit::ConfigurationLoader.load_repo_config
149+
config = Overcommit::ConfigurationLoader.new(log).load_repo_config
150150

151151
config.all_hook_configs.each do |hook_type, hook_configs|
152152
print_hooks_for_hook_type(config, hook_configs, hook_type)
@@ -175,7 +175,7 @@ def print_hooks_for_hook_type(repo_config, hook_configs, hook_type)
175175
end
176176

177177
def sign_plugins
178-
config = Overcommit::ConfigurationLoader.load_repo_config
178+
config = Overcommit::ConfigurationLoader.new(log).load_repo_config
179179
context = Overcommit::HookContext.create(@options[:hook_to_sign],
180180
config,
181181
@arguments,
@@ -187,7 +187,7 @@ def sign_plugins
187187
end
188188

189189
def run_all
190-
config = Overcommit::ConfigurationLoader.load_repo_config
190+
config = Overcommit::ConfigurationLoader.new(log).load_repo_config
191191
context = Overcommit::HookContext.create('run-all', config, @arguments, @input)
192192
config.apply_environment!(context, ENV)
193193

lib/overcommit/configuration_loader.rb

+22-16
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,13 @@ class ConfigurationLoader
66
DEFAULT_CONFIG_PATH = File.join(Overcommit::HOME, 'config', 'default.yml')
77

88
class << self
9-
# Loads and returns the configuration for the repository we're running in.
10-
#
11-
# @return [Overcommit::Configuration]
12-
def load_repo_config
13-
overcommit_yml = File.join(Overcommit::Utils.repo_root,
14-
Overcommit::CONFIG_FILE_NAME)
15-
16-
if File.exist?(overcommit_yml)
17-
load_file(overcommit_yml)
18-
else
19-
default_configuration
20-
end
21-
end
22-
239
# Loads and returns the default configuration.
2410
#
2511
# @return [Overcommit::Configuration]
2612
def default_configuration
2713
@default_config ||= load_from_file(DEFAULT_CONFIG_PATH)
2814
end
2915

30-
private
31-
3216
# Loads a configuration, ensuring it extends the default configuration.
3317
def load_file(file)
3418
config = load_from_file(file)
@@ -40,6 +24,8 @@ def load_file(file)
4024
error.backtrace
4125
end
4226

27+
private
28+
4329
def load_from_file(file)
4430
hash =
4531
if yaml = YAML.load_file(file)
@@ -51,5 +37,25 @@ def load_from_file(file)
5137
Overcommit::Configuration.new(hash)
5238
end
5339
end
40+
41+
# Create a configuration loader which writes warnings/errors to the given
42+
# {Overcommit::Logger} instance.
43+
def initialize(logger)
44+
@log = logger
45+
end
46+
47+
# Loads and returns the configuration for the repository we're running in.
48+
#
49+
# @return [Overcommit::Configuration]
50+
def load_repo_config
51+
overcommit_yml = File.join(Overcommit::Utils.repo_root,
52+
Overcommit::CONFIG_FILE_NAME)
53+
54+
if File.exist?(overcommit_yml)
55+
self.class.load_file(overcommit_yml)
56+
else
57+
self.class.default_configuration
58+
end
59+
end
5460
end
5561
end

spec/overcommit/cli_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727

2828
context 'with the --list-hooks option specified' do
2929
let(:arguments) { ['--list-hooks'] }
30+
3031
let(:contexts) do
31-
Overcommit::ConfigurationLoader.load_repo_config.all_hook_configs.keys
32+
Overcommit::ConfigurationLoader.new(logger).load_repo_config.all_hook_configs.keys
3233
end
3334

3435
before { cli.stub(:halt) }

spec/overcommit/configuration_loader_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
require 'spec_helper'
22

33
describe Overcommit::ConfigurationLoader do
4+
let(:logger) { Overcommit::Logger.silent }
5+
46
describe '.load_repo_config' do
5-
subject { described_class.load_repo_config }
7+
subject { described_class.new(logger).load_repo_config }
68

79
context 'when repo does not contain a configuration file' do
810
around do |example|

template-dir/hooks/overcommit-hook

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ begin
4343
exec($0, *ARGV) # Execute the updated hook with all original arguments
4444
end
4545

46-
config = Overcommit::ConfigurationLoader.load_repo_config
46+
config = Overcommit::ConfigurationLoader.new(logger).load_repo_config
4747

4848
context = Overcommit::HookContext.create(hook_type, config, ARGV, STDIN)
4949
config.apply_environment!(context, ENV)

0 commit comments

Comments
 (0)