Skip to content

Commit e9f201b

Browse files
sdsShane da Silva
authored and
Shane da Silva
committed
Add specs for commit-msg hooks
Change-Id: I469b1c6fc51ade36e41a4d38f4844f9b3df05584 Reviewed-on: http://gerrit.causes.com/35564 Reviewed-by: Shane da Silva <shane@causes.com> Tested-by: Shane da Silva <shane@causes.com>
1 parent 3ea3fcb commit e9f201b

10 files changed

+199
-16
lines changed

lib/overcommit/hook/commit_msg/gerrit_change_id.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ module Overcommit::Hook::CommitMsg
66
# you need to do it in a commit-msg hook. This is because the user could still
77
# edit the message after a prepare-commit-msg hook was run.
88
class GerritChangeId < Base
9-
SCRIPT_LOCATION = Overcommit::Utils.script_path('gerrit-change-id')
10-
119
def run
12-
command "#{SCRIPT_LOCATION} #{commit_message_file}"
13-
:good
10+
result = command("#{SCRIPT_LOCATION} #{commit_message_file}")
11+
return (result.success? ? :good : :bad), result.stdout
1412
end
13+
14+
private
15+
16+
SCRIPT_LOCATION = Overcommit::Utils.script_path('gerrit-change-id')
1517
end
1618
end

lib/overcommit/hook/commit_msg/russian_novel.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class RussianNovel < Base
44
RUSSIAN_NOVEL_LENGTH = 30
55

66
def run
7-
if commit_message.length > RUSSIAN_NOVEL_LENGTH
7+
if commit_message.length >= RUSSIAN_NOVEL_LENGTH
88
return :warn, 'You seem to have authored a Russian novel; congratulations!'
99
end
1010

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
module Overcommit::HookContext
22
class CommitMsg < Base
3-
# User commit message stripped of comments and diff (from verbose output)
3+
# User commit message stripped of comments and diff (from verbose output).
44
def commit_message
5-
@commit_message ||= raw_commit_message.
5+
raw_commit_message.
66
reject { |line| line =~ /^#/ }.
77
take_while { |line| !line.start_with?('diff --git') }
88
end
99

10-
private
11-
12-
def raw_commit_message
13-
@raw_commit_message ||= ::IO.readlines(commit_message_file)
10+
def commit_message_file
11+
@args[0]
1412
end
1513

16-
def commit_message_file
17-
unless @args[0] && ::File.exist?(@args[0])
18-
fail 'Not running in the context of a commit message'
19-
end
14+
private
2015

21-
@args[0]
16+
def raw_commit_message
17+
::IO.readlines(commit_message_file)
2218
end
2319
end
2420
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::CommitMsg::GerritChangeId do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
let(:subject) { described_class.new(config, context) }
7+
let(:commit_msg_file) { Tempfile.new('commit-msg') }
8+
9+
before do
10+
commit_msg_file.write(commit_msg)
11+
commit_msg_file.close
12+
subject.stub(:commit_message_file).and_return(commit_msg_file.path)
13+
end
14+
15+
context 'when the commit message contains no Change-Id' do
16+
let(:commit_msg) { 'Add code to repo' }
17+
18+
it { should pass }
19+
20+
it 'adds a Change-Id to the commit message' do
21+
subject.run
22+
File.open(commit_msg_file.path, 'r').read.should =~ /Change-Id/
23+
end
24+
end
25+
26+
context 'when the commit message already contains a Change-Id' do
27+
let(:commit_msg) { "Add code to repo\n\nChange-Id: I9f2b5528fa20ac91a55bbe9371e76a12dd1cce11" }
28+
29+
it { should pass }
30+
31+
it 'does nothing' do
32+
before = File.open(commit_msg_file.path, 'r').read
33+
subject.run
34+
after = File.open(commit_msg_file.path, 'r').read
35+
before.should == after
36+
end
37+
end
38+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::CommitMsg::HardTabs do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
let(:subject) { described_class.new(config, context) }
7+
8+
before do
9+
subject.stub(:commit_message).and_return(commit_msg)
10+
end
11+
12+
context 'when message contains hard tabs' do
13+
let(:commit_msg) { ["This is a hard-tab\tcommit message"] }
14+
15+
it { should warn }
16+
end
17+
18+
context 'when message does not contain hard tabs' do
19+
let(:commit_msg) { ['No hard tabs to be found'] }
20+
21+
it { should pass }
22+
end
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::CommitMsg::RussianNovel do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
let(:subject) { described_class.new(config, context) }
7+
8+
before do
9+
subject.stub(:commit_message).and_return(commit_msg)
10+
end
11+
12+
context 'when message contains fewer than 30 lines' do
13+
let(:commit_msg) { ['A single line'] * 10 }
14+
15+
it { should pass }
16+
end
17+
18+
context 'when message contains at least 30 lines' do
19+
let(:commit_msg) { ['A single line'] * 30 }
20+
21+
it { should warn }
22+
end
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::CommitMsg::SingleLineSubject do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
let(:subject) { described_class.new(config, context) }
7+
8+
before do
9+
subject.stub(:commit_message).and_return(commit_msg.split("\n"))
10+
end
11+
12+
context 'when subject is separated from body by a blank line' do
13+
let(:commit_msg) { <<-MSG }
14+
Initial commit
15+
16+
Mostly cats so far.
17+
MSG
18+
19+
it { should pass }
20+
end
21+
22+
context 'when subject is not kept to one line' do
23+
let(:commit_msg) { <<-MSG }
24+
Initial commit where I forget about commit message
25+
standards and decide to hard-wrap my subject
26+
27+
Still mostly cats so far.
28+
MSG
29+
30+
it { should warn }
31+
end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::CommitMsg::TextWidth do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
let(:subject) { described_class.new(config, context) }
7+
8+
before do
9+
subject.stub(:commit_message).and_return(commit_msg.split("\n"))
10+
end
11+
12+
context 'when subject is longer than 60 characters' do
13+
let(:commit_msg) { 'A' * 61 }
14+
15+
it { should warn /subject/ }
16+
end
17+
18+
context 'when subject is 60 characters or fewer' do
19+
let(:commit_msg) { 'A' * 60 }
20+
21+
it { should pass }
22+
end
23+
24+
context 'when a line in the message is longer than 72 characters' do
25+
let(:commit_msg) { <<-MSG }
26+
Some summary
27+
28+
This line is longer than 72 characters which is clearly be seen by count.
29+
MSG
30+
31+
it { should warn /72 char/ }
32+
end
33+
34+
context 'when all lines in the message are fewer than 72 characters' do
35+
let(:commit_msg) { <<-MSG }
36+
Some summary
37+
38+
A reasonable line.
39+
40+
Another reasonable line.
41+
MSG
42+
43+
it { should pass }
44+
end
45+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::CommitMsg::TrailingPeriod do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
let(:subject) { described_class.new(config, context) }
7+
8+
before do
9+
subject.stub(:commit_message).and_return(commit_msg.split("\n"))
10+
end
11+
12+
context 'when subject contains a trailing period' do
13+
let(:commit_msg) { 'This subject has a period.' }
14+
15+
it { should warn }
16+
end
17+
18+
context 'when subject does not contain a trailing period' do
19+
let(:commit_msg) { 'This subject has no period' }
20+
21+
it { should pass }
22+
end
23+
end

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'overcommit'
2+
require 'tempfile'
23

34
hook_types = Dir[File.join(Overcommit::OVERCOMMIT_HOME, 'lib/overcommit/hook/*')].
45
select { |f| File.directory?(f) }.

0 commit comments

Comments
 (0)