Skip to content

Commit a940a32

Browse files
sdsShane da Silva
authored and
Shane da Silva
committed
Add integration spec for committing
Change-Id: I78e35d2c9c6dd178d4f4d3773cae03499ae342ab Reviewed-on: http://gerrit.causes.com/35584 Reviewed-by: Shane da Silva <shane@causes.com> Tested-by: Shane da Silva <shane@causes.com>
1 parent 54058b0 commit a940a32

File tree

7 files changed

+68
-6
lines changed

7 files changed

+68
-6
lines changed

config/default.yml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pre_commit:
3434
quiet: false
3535

3636
AuthorName:
37+
description: 'Checking for author name'
3738
requires_files: false
3839
required: true
3940
quiet: true

lib/overcommit/configuration.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ def ==(other)
1515
# Returns absolute path to the directory that external hook plugins should
1616
# be loaded from.
1717
def plugin_directory
18-
File.join(Overcommit::Utils.repo_root, @hash['plugin_directory'])
18+
File.join(Overcommit::Utils.repo_root, @hash['plugin_directory'] || '.githooks')
1919
end
2020

2121
# Returns the built-in hooks that have been enabled for a hook type.
2222
def enabled_builtin_hooks(hook_type)
2323
@hash[hook_type].keys.
2424
select { |hook_name| hook_name != 'ALL' }.
25-
select { |hook_name| @hash[hook_type][hook_name]['enabled'] != false }
25+
select { |hook_name| hook_enabled?(hook_type, hook_name) }
2626
end
2727

2828
# Returns a non-modifiable configuration for a hook.
@@ -76,6 +76,16 @@ def hook_exists?(hook_type, hook_name)
7676
hook_type, "#{hook_name}.rb"))
7777
end
7878

79+
def hook_enabled?(hook_type, hook_name)
80+
individual_enabled = @hash[hook_type][hook_name]['enabled']
81+
return individual_enabled unless individual_enabled.nil?
82+
83+
all_enabled = @hash[hook_type]['ALL']['enabled']
84+
return all_enabled unless all_enabled.nil?
85+
86+
true
87+
end
88+
7989
# Validates the configuration for any invalid options, normalizing it where
8090
# possible.
8191
def validate

lib/overcommit/hook/pre_commit/author_name.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ def run
66
name = result.stdout.chomp
77

88
unless name.split(' ').count >= 2
9-
return :bad, 'Author must have at least first and last name; ' <<
10-
"was: '#{name}'.\n Set your name with " <<
9+
return :bad, 'Author must have at least first and last name, but ' <<
10+
"was: '#{name}'.\nSet your name with " <<
1111
"`git config --global user.name 'Your Name'`"
1212
end
1313

lib/overcommit/reporter.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize(context, hooks, config, log)
88
@config = config
99
@name = Overcommit::Utils.underscorize(context.hook_class_name).gsub('_', '-')
1010
@hooks = hooks
11-
@width = [@hooks.map { |s| s.description.length }.max + 3, 60].max
11+
@width = [(@hooks.map { |s| s.description.length }.max || 57) + 3, 60].max
1212
@results = []
1313
end
1414

@@ -20,7 +20,7 @@ def with_status(hook, &block)
2020
title = " #{hook.description}"
2121
unless hook.quiet?
2222
log.partial title
23-
print '.' * (@width - title.length)
23+
log.partial '.' * (@width - title.length)
2424
end
2525

2626
status, output = yield
@@ -51,6 +51,7 @@ def print_hook_result(hook, title, status, output)
5151
if hook.quiet?
5252
return if status == :good
5353
log.partial title
54+
log.partial '.' * (@width - title.length)
5455
end
5556

5657
case status

spec/integration/committing_spec.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'spec_helper'
2+
3+
describe 'commiting' do
4+
subject { shell('git commit --allow-empty -m "Test"') }
5+
6+
let(:config) { <<-YML }
7+
commit_msg:
8+
ALL:
9+
enabled: false
10+
pre_commit:
11+
ALL:
12+
enabled: false
13+
AuthorName:
14+
enabled: true
15+
YML
16+
17+
around do |example|
18+
repo do
19+
File.open('.overcommit.yml', 'w') { |f| f.write(config) }
20+
Overcommit::Installer.new(Overcommit::Logger.silent).
21+
run('.', :action => :install)
22+
example.run
23+
end
24+
end
25+
26+
context 'when a hook fails' do
27+
before do
28+
`git config --local user.name ''`
29+
end
30+
31+
its(:status) { should_not be_zero }
32+
end
33+
34+
context 'when no hooks fail' do
35+
before do
36+
`git config --local user.name 'John Doe'`
37+
end
38+
39+
its(:status) { should be_zero }
40+
end
41+
end

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
RSpec.configure do |config|
1919
config.include GitSpecHelpers
2020
config.include OutputHelpers
21+
config.include ShellHelpers
2122
end

spec/support/shell_helpers.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'wopen3'
2+
3+
# Helpers for executing shell commands in tests.
4+
module ShellHelpers
5+
def shell(command)
6+
Wopen3.system(command)
7+
end
8+
end

0 commit comments

Comments
 (0)