Skip to content

Commit ea6adf3

Browse files
committed
Specs for Git class
1 parent f06ac98 commit ea6adf3

2 files changed

Lines changed: 83 additions & 9 deletions

File tree

lib/git.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
class Git
22
class << self
3+
def from_shell(cmd)
4+
`#{cmd}`
5+
end
6+
37
def show(rev)
4-
`git show #{rev.strip} -w`
8+
from_shell("git show #{rev.strip} -w")
59
end
610

711
def log(rev1, rev2)
8-
`git log #{rev1}..#{rev2}`.strip
12+
from_shell("git log #{rev1}..#{rev2}").strip
913
end
1014

1115
def branch_commits(treeish)
1216
args = Git.branch_heads - [ Git.branch_head(treeish) ]
1317
args.map! { |tree| "^#{tree}" }
1418
args << treeish
15-
lines = `git rev-list #{args.join(' ')}`
19+
lines = from_shell("git rev-list #{args.join(' ')}")
1620
lines = lines.lines if lines.respond_to?(:lines)
1721
lines.to_a.map { |commit| commit.chomp }
1822
end
1923

2024
def branch_heads
21-
lines = `git rev-parse --branches`
25+
lines = from_shell("git rev-parse --branches")
2226
lines = lines.lines if lines.respond_to?(:lines)
2327
lines.to_a.map { |head| head.chomp }
2428
end
2529

2630
def branch_head(treeish)
27-
`git rev-parse #{treeish}`.strip
31+
from_shell("git rev-parse #{treeish}").strip
2832
end
2933

3034
def repo_name
31-
git_prefix = `git config hooks.emailprefix`.strip
35+
git_prefix = from_shell("git config hooks.emailprefix").strip
3236
return git_prefix unless git_prefix.empty?
33-
dir_name = `pwd`.chomp.split("/").last.gsub(/\.git$/, '')
34-
return "#{dir_name}"
37+
Dir.pwd.split("/").last.sub(/\.git$/, '')
3538
end
3639

3740
def mailing_list_address
38-
`git config hooks.mailinglist`.strip
41+
from_shell("git config hooks.mailinglist").strip
3942
end
4043
end
4144
end

spec/lib/git_spec.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require File.expand_path('../../spec_helper', __FILE__)
2+
require 'git'
3+
4+
describe Git do
5+
SAMPLE_REV = '51b986619d88f7ba98be7d271188785cbbb541a0'.freeze
6+
7+
describe :from_shell do
8+
it "should be backtick" do
9+
Git.from_shell('pwd').should == `pwd`
10+
end
11+
end
12+
13+
describe :show do
14+
it "should get data from shell: git show" do
15+
expected = 'some data from git show'
16+
mock(Git).from_shell("git show #{SAMPLE_REV} -w") { expected }
17+
Git.show(SAMPLE_REV).should == expected
18+
end
19+
20+
it "should strip given revision" do
21+
mock(Git).from_shell("git show #{SAMPLE_REV} -w")
22+
Git.show("#{SAMPLE_REV}\n")
23+
end
24+
end
25+
26+
describe :branch_heads do
27+
before(:each) do
28+
mock(Git).from_shell("git rev-parse --branches") { "some\npopular\ntext\n" }
29+
end
30+
31+
it "should get branch heads from shell" do
32+
lambda { Git.branch_heads }.should_not raise_error
33+
end
34+
35+
it "should return array of lines" do
36+
Git.branch_heads.should == %w[ some popular text ]
37+
end
38+
end
39+
40+
41+
describe :repo_name do
42+
# this spec written because I replaced `pwd` with Dir.pwd
43+
it "Dir.pwd should be same as `pwd`.chomp" do
44+
Dir.pwd.should == `pwd`.chomp
45+
end
46+
47+
it "should return hooks.emailprefix if it's not empty" do
48+
expected = "name of repo"
49+
mock(Git).from_shell("git config hooks.emailprefix") { expected }
50+
dont_allow(Dir).pwd
51+
Git.repo_name.should == expected
52+
end
53+
54+
it "should return folder name if no emailprefix and directory not ended with .git" do
55+
mock(Git).from_shell("git config hooks.emailprefix") { " " }
56+
mock(Dir).pwd { "/home/someuser/repositories/myrepo" }
57+
Git.repo_name.should == "myrepo"
58+
end
59+
60+
it "should return folder name without extension if no emailprefix and directory ended with .git" do
61+
mock(Git).from_shell("git config hooks.emailprefix") { " " }
62+
mock(Dir).pwd { "/home/someuser/repositories/myrepo.git" }
63+
Git.repo_name.should == "myrepo"
64+
end
65+
end
66+
67+
end
68+
69+
__END__
70+
71+
vim: tabstop=2 expandtab shiftwidth=2

0 commit comments

Comments
 (0)