Skip to content

Commit 4a53682

Browse files
author
Aiden Scandella
committed
Extract Logger class
So that we're not calling `puts`/`print` from around the codebase. This allows us to test, and lets us silence output during test runs. Change-Id: If557a490ecb9913a2bcef4a943a773711da25443 Reviewed-on: https://gerrit.causes.com/23030 Tested-by: jenkins <jenkins@causes.com> Reviewed-by: Shane da Silva <shane@causes.com>
1 parent 6de547d commit 4a53682

9 files changed

+96
-58
lines changed

lib/overcommit.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require 'overcommit/configuration'
2-
require 'overcommit/console_methods'
32
require 'overcommit/errors'
43
require 'overcommit/git_hook'
54
require 'overcommit/hook_specific_check'
65
require 'overcommit/installer'
6+
require 'overcommit/logger'
77
require 'overcommit/reporter'
88
require 'overcommit/utils'
99
require 'overcommit/version'

lib/overcommit/cli.rb

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
module Overcommit
44
class CLI
5-
include ConsoleMethods
65
attr_reader :options
76

87
def initialize(arguments = [])
@@ -19,14 +18,14 @@ def parse_arguments
1918
end
2019

2120
opts.on_tail('-v', '--version', 'Show version') do
22-
puts VERSION
21+
log.log VERSION
2322
exit 0
2423
end
2524

2625
opts.on('-l', '--list-templates', 'List built-in templates') do
2726
Overcommit.config.templates.each_pair do |name, configuration|
2827
bold name
29-
puts YAML.dump(configuration), ''
28+
log.log YAML.dump(configuration), ''
3029
end
3130
exit 0
3231
end
@@ -80,20 +79,20 @@ def parse_arguments
8079

8180
def run
8281
if @options[:targets].nil? || @options[:targets].empty?
83-
warning 'You must supply at least one directory'
84-
puts @parser.help
82+
log.warning 'You must supply at least one directory'
83+
log.log @parser.help
8584
exit 2
8685
end
8786

8887
@options[:targets].each do |target|
8988
begin
9089
Installer.new(@options, target).run
9190
rescue NotAGitRepoError => e
92-
warning "Skipping #{target}: #{e}"
91+
log.warning "Skipping #{target}: #{e}"
9392
end
9493
end
9594

96-
success "#{@options[:uninstall] ? 'Removal' : 'Installation'} complete"
95+
log.success "#{@options[:uninstall] ? 'Removal' : 'Installation'} complete"
9796

9897
rescue ArgumentError => ex
9998
error "Installation failed: #{ex}"
@@ -102,9 +101,13 @@ def run
102101

103102
private
104103

104+
def log
105+
Logger.instance
106+
end
107+
105108
def print_help(message, ex = nil)
106-
error ex.to_s + "\n" if ex
107-
puts message
109+
log.error ex.to_s + "\n" if ex
110+
log.log message
108111
exit 0
109112
end
110113
end

lib/overcommit/console_methods.rb

-23
This file was deleted.

lib/overcommit/git_hook.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
module Overcommit
22
module GitHook
33
class BaseHook
4-
include ConsoleMethods
5-
64
def initialize
75
Overcommit.config.desired_plugins.each do |plugin|
86
require plugin
97
end
108
rescue LoadError, NameError => ex
11-
error "Couldn't load plugin: #{ex}"
9+
log.error "Couldn't load plugin: #{ex}"
1210
exit 0
1311
end
1412

@@ -41,6 +39,10 @@ def run(*args)
4139

4240
private
4341

42+
def log
43+
Logger.instance
44+
end
45+
4446
# Return all loaded plugins, skipping those that are skippable and have
4547
# been asked to be skipped by the environment variable SKIP_CHECKS.
4648
#

lib/overcommit/installer.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ def run
1414
end
1515

1616
def install
17-
puts "Installing hooks into #{@target}"
17+
log.log "Installing hooks into #{@target}"
1818

1919
install_scripts
2020
install_hooks
2121
write_configuration
2222
end
2323

2424
def uninstall
25-
puts "Removing hooks from #{@target}"
25+
log.log "Removing hooks from #{@target}"
2626

2727
uninstall_scripts
2828
uninstall_hooks
@@ -31,6 +31,10 @@ def uninstall
3131

3232
private
3333

34+
def log
35+
Logger.instance
36+
end
37+
3438
def hook_path
3539
absolute_target = File.expand_path @target
3640
File.join(absolute_target, '.git/hooks')

lib/overcommit/logger.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'singleton'
2+
3+
# This class centralizes all communication to STDOUT
4+
module Overcommit
5+
class Logger
6+
include Singleton
7+
8+
def partial(*args)
9+
print *args
10+
end
11+
12+
def log(*args)
13+
puts *args
14+
end
15+
16+
def bold(str)
17+
log "\033[1;37m#{str}\033[0m"
18+
end
19+
20+
def error(str)
21+
log "\033[31m#{str}\033[0m"
22+
end
23+
24+
def success(str)
25+
log "\033[32m#{str}\033[0m"
26+
end
27+
28+
def warning(str)
29+
log "\033[33m#{str}\033[0m"
30+
end
31+
32+
def notice(str)
33+
log "\033[1;33m#{str}\033[0m"
34+
end
35+
end
36+
end

lib/overcommit/reporter.rb

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module Overcommit
22
class Reporter
3-
include ConsoleMethods
4-
53
def initialize(name, checks)
64
@name = name
75
@checks = checks
@@ -11,7 +9,7 @@ def initialize(name, checks)
119

1210
def with_status(check, &block)
1311
title = " Checking #{check.name}..."
14-
print title unless check.stealth?
12+
log.partial title unless check.stealth?
1513

1614
status, output = yield
1715

@@ -20,48 +18,53 @@ def with_status(check, &block)
2018
end
2119

2220
def print_header
23-
puts "Running #{@name} checks"
21+
log.log "Running #{@name} checks"
2422
end
2523

2624
def print_result
27-
puts
25+
log.log # Newline
26+
2827
case final_result
2928
when :good
30-
success "+++ All #{@name} checks passed"
29+
log.success "+++ All #{@name} checks passed"
3130
exit 0
3231
when :bad
33-
error "!!! One or more #{@name} checks failed"
32+
log.error "!!! One or more #{@name} checks failed"
3433
exit 1
3534
when :stop
36-
warning "*** One or more #{@name} checks needs attention"
37-
warning "*** If you really want to commit, use SKIP_CHECKS"
38-
warning "*** (takes a space-separated list of checks to skip, or 'all')"
35+
log.warning "*** One or more #{@name} checks needs attention"
36+
log.warning "*** If you really want to commit, use SKIP_CHECKS"
37+
log.warning "*** (takes a space-separated list of checks to skip, or 'all')"
3938
exit 1
4039
end
4140
end
4241

4342
private
4443

44+
def log
45+
Overcommit::Logger.instance
46+
end
47+
4548
def print_incremental_result(title, status, output, stealth = false)
4649
if stealth
4750
return if status == :good
48-
print title
51+
log.partial title
4952
end
5053

5154
print '.' * (@width - title.length)
5255
case status
5356
when :good
54-
success 'OK'
57+
log.success 'OK'
5558
when :bad
56-
error 'FAILED'
59+
log.error 'FAILED'
5760
print_report output
5861
when :warn
59-
warning output
62+
log.warning output
6063
when :stop
61-
warning 'UH OH'
64+
log.warning 'UH OH'
6265
print_report output
6366
else
64-
error '???'
67+
log.error '???'
6568
print_report "Check didn't return a status"
6669
exit 1
6770
end
@@ -74,7 +77,7 @@ def final_result
7477
end
7578

7679
def print_report(*report)
77-
puts report.flatten.map { |line| " #{line}" }.join("\n")
80+
log.log report.flatten.map { |line| " #{line}" }.join("\n")
7881
end
7982
end
8083
end

lib/overcommit/utils.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module Overcommit
22
module Utils
33
class << self
4-
include ConsoleMethods
5-
64
@@hooks = []
75

86
def register_hook(hook)
@@ -20,7 +18,7 @@ def hook_name
2018
def load_hooks
2119
require File.expand_path("../hooks/#{hook_name}", __FILE__)
2220
rescue LoadError
23-
error "No hook definition found for #{hook_name}"
21+
log.error "No hook definition found for #{hook_name}"
2422
exit 1
2523
end
2624

@@ -55,6 +53,12 @@ def modified_files
5553
@modified_files ||=
5654
`git diff --cached --name-only --diff-filter=ACM`.split "\n"
5755
end
56+
57+
private
58+
59+
def log
60+
Logger.instance
61+
end
5862
end
5963
end
6064
end

spec/spec_helper.rb

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
require 'overcommit'
22

33
def exit(*args) ; end
4+
5+
# Silence output to STDOUT
6+
class Overcommit::Logger
7+
def log(*args)
8+
end
9+
10+
def partial(*args)
11+
end
12+
end

0 commit comments

Comments
 (0)