Skip to content
This repository was archived by the owner on Mar 31, 2022. It is now read-only.

Commit 91e7475

Browse files
author
Michael Grosser
committed
add tests for git pair
1 parent 42a847d commit 91e7475

File tree

5 files changed

+155
-5
lines changed

5 files changed

+155
-5
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ gemspec
66
gem "rake"
77
gem "bundler"
88
gem "rspec"
9+
gem "unindent"

Gemfile.lock

+12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ PATH
66
GEM
77
remote: http://rubygems.org/
88
specs:
9+
diff-lcs (1.1.3)
910
rake (0.9.2.2)
11+
rspec (2.8.0)
12+
rspec-core (~> 2.8.0)
13+
rspec-expectations (~> 2.8.0)
14+
rspec-mocks (~> 2.8.0)
15+
rspec-core (2.8.0)
16+
rspec-expectations (2.8.0)
17+
diff-lcs (~> 1.1.2)
18+
rspec-mocks (2.8.0)
19+
unindent (1.0)
1020

1121
PLATFORMS
1222
ruby
@@ -15,3 +25,5 @@ DEPENDENCIES
1525
bundler
1626
pivotal_git_scripts!
1727
rake
28+
rspec
29+
unindent

bin/git-about

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env ruby
22
user_name = `git config --get user.name`
3-
user_name = "NONE" if user_name.empty?
3+
user_name = "NONE" if user_name.strip.empty?
44

55
user_email = `git config --get user.email`
6-
user_email = "NONE" if user_email.empty?
6+
user_email = "NONE" if user_email.strip.empty?
77

88
begin
99
key_file = File.readlink(File.expand_path("~/.ssh/id_github_current"))

pivotal_git_scripts.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
1717
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
1818
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
1919
s.require_paths = ["lib"]
20-
s.licence = "MIT"
20+
s.license = "MIT"
2121

2222
# specify any dependencies here; for example:
2323
# s.add_development_dependency "rspec"

spec/cli_spec.rb

+139-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
require "unindent"
2+
13
describe "CLI" do
24
before :all do
35
# use local scripts
46
ENV["PATH"] = "#{File.join(File.dirname(__FILE__),"..","bin")}:#{ENV["PATH"]}"
57
end
68

7-
def run(command)
9+
def run(command, options={})
810
result = `#{command}`
9-
raise "FAILED #{command} : #{result}" unless $?.success?
11+
raise "FAILED #{command} : #{result}" if $?.success? == !!options[:fail]
1012
result
1113
end
1214

15+
def write(file, content)
16+
File.open(file, 'w'){|f| f.write content }
17+
end
18+
1319
around do |example|
1420
dir = "spec/tmp"
1521
run "rm -rf #{dir}"
@@ -62,4 +68,135 @@ def run(command)
6268
end
6369
end
6470
end
71+
72+
describe "pair" do
73+
def expect_config(result, name, initials, email, options={})
74+
global = "cd /tmp && " if options[:global]
75+
run("#{global}git config user.name").should == "#{name}\n"
76+
run("#{global}git config user.initials").should == "#{initials}\n"
77+
run("#{global}git config user.email").should == "#{email}\n"
78+
79+
prefix = (options[:global] ? "global: " : "local: ")
80+
result.should include "#{prefix}user.name #{name}"
81+
#result.should include "#{prefix}user.initials #{initials}" # TODO
82+
result.should include "#{prefix}user.email #{email}"
83+
end
84+
85+
context "with .pairs file" do
86+
before do
87+
write ".pairs", <<-YAML.unindent
88+
pairs:
89+
ab: Aa Bb
90+
bc: Bb Cc
91+
cd: Cc Dd
92+
93+
email:
94+
prefix: the-pair
95+
domain: the-host.com
96+
YAML
97+
end
98+
99+
describe "global" do
100+
it "sets pairs globally when global: true is set" do
101+
write ".pairs", File.read(".pairs") + "\nglobal: true"
102+
result = run "git pair ab"
103+
expect_config result, "Aa Bb", "ab", "[email protected]", :global => true
104+
end
105+
106+
it "sets pairs globally when --global is given" do
107+
result = run "git pair ab --global"
108+
result.should include "global: user.name Aa Bb"
109+
expect_config result, "Aa Bb", "ab", "[email protected]", :global => true
110+
end
111+
112+
it "unsets global config when no argument is passed" do
113+
run "git pair ab --global"
114+
run "git pair ab"
115+
result = run "git pair --global"
116+
#result.should include "Unset --global user.name, user.email and user.initials"
117+
expect_config result, "Aa Bb", "ab", "[email protected]"
118+
result.should_not include("global:")
119+
end
120+
end
121+
122+
it "can set a single user as pair" do
123+
result = run "git pair ab"
124+
expect_config result, "Aa Bb", "ab", "[email protected]"
125+
end
126+
127+
it "can set a 2 users as pair" do
128+
result = run "git pair ab bc"
129+
expect_config result, "Aa Bb & Bb Cc", "ab bc", "[email protected]"
130+
end
131+
132+
it "can set a n users as pair" do
133+
result = run "git pair ab bc cd"
134+
expect_config result, "Aa Bb, Bb Cc & Cc Dd", "ab bc cd", "[email protected]"
135+
end
136+
137+
it "fails when there is no .git" do
138+
run "rm -rf .git"
139+
run "git pair ab", :fail => true
140+
end
141+
142+
it "finds .pairs file in lower parent folder" do
143+
run "mkdir foo"
144+
Dir.chdir "foo" do
145+
run "git init"
146+
result = run "git pair ab"
147+
expect_config result, "Aa Bb", "ab", "[email protected]"
148+
end
149+
end
150+
151+
it "unsets local config when no argument is passed" do
152+
run "git pair ab --global"
153+
run "git pair bc"
154+
result = run "git pair"
155+
#result.should include "Unset user.name, user.email and user.initials" #TODO
156+
expect_config result, "Aa Bb", "ab", "[email protected]", :global => true
157+
result.should_not include("local:")
158+
end
159+
160+
it "uses hard email when given" do
161+
write ".pairs", File.read(".pairs").sub(/email:.*/m, "email: [email protected]")
162+
result = run "git pair ab"
163+
expect_config result, "Aa Bb", "ab", "[email protected]"
164+
end
165+
166+
it "fails with unknown initials" do
167+
result = run "git pair xx", :fail => true
168+
result.should include("Couldn't find author name for initials: xx")
169+
end
170+
171+
it "uses alternate email prefix" do
172+
write ".pairs", File.read(".pairs").sub(/ab:.*/, "ab: Aa Bb; blob")
173+
result = run "git pair ab"
174+
expect_config result, "Aa Bb", "ab", "[email protected]"
175+
end
176+
end
177+
178+
context "without a .pairs file in the tree" do
179+
around do |example|
180+
Dir.chdir "/tmp" do
181+
dir = "git_stats_test"
182+
run "rm -rf #{dir}"
183+
run "mkdir #{dir}"
184+
Dir.chdir dir do
185+
run "git init"
186+
example.run
187+
end
188+
run "rm -rf #{dir}"
189+
end
190+
end
191+
192+
it "fails if it cannot find a pairs file" do
193+
run "git pair ab", :fail => true
194+
end
195+
196+
it "prints instructions" do
197+
result = run "git pair ab", :fail => true
198+
result.should include("Could not find a .pairs file. Create a YAML file in your project or home directory.")
199+
end
200+
end
201+
end
65202
end

0 commit comments

Comments
 (0)