forked from vmware-archive/git_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_spec.rb
445 lines (368 loc) · 13 KB
/
cli_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
require "unindent"
describe "CLI" do
before :all do
# use local scripts
ENV["PATH"] = "#{File.join(File.dirname(__FILE__),"..","bin")}:#{ENV["PATH"]}"
end
def run(command, options={})
output = `#{command}`
return output if $?.success?
return output if options[:fail]
message = "Unable to run #{command.inspect} in #{Dir.pwd}.\n#{output}"
warn "ERROR: #{message}"
raise message
end
def write(file, content)
File.open(file, 'w'){|f| f.write content }
end
around do |example|
dir = "spec/tmp"
run "rm -rf #{dir}"
run "mkdir #{dir}"
# use fake home for .ssh hacks
run "mkdir #{dir}/home"
ENV["HOME"] = File.absolute_path("#{dir}/home")
Dir.chdir dir do
run "touch a"
run "git init"
run "git add ."
run "git config user.email '[email protected]'"
run "git config user.name 'rspec test suite'"
run "git commit -am 'initial'"
run "git config --unset user.email"
run "git config --unset user.name"
example.run
end
end
describe "about" do
it "lists the user" do
run "git config user.name NAME"
run("git about").should =~ /git user:\s+NAME/
end
it "lists the user as NONE if there is none" do
run "git config user.name ''"
run("git about").should =~ /git user:\s+NONE/
end
it "lists the email" do
run "git config user.email EMAIL"
run("git about").should =~ /git email:\s+EMAIL/
end
it "lists the email as NONE if there is none" do
run "git config user.email ''"
run("git about").should =~ /git email:\s+NONE/
end
it "does not find a project" do
run("git about").should =~ /GitHub project:\s+NONE/
end
context "with github project" do
before do
run "mkdir home/.ssh"
run "touch home/.ssh/id_github_foo"
run "ln -s home/.ssh/id_github_foo home/.ssh/id_github_current"
end
it "finds a project" do
run("git about").should =~ /GitHub project:\s+foo/
end
end
end
describe "pair" do
def expect_config(result, name, initials, email, options={})
global = "cd /tmp && " if options[:global]
run("#{global}git config user.name").should == "#{name}\n"
run("#{global}git config user.initials").should == "#{initials}\n"
run("#{global}git config user.email").should == "#{email}\n"
prefix = (options[:global] ? "global: " : "local: ")
result.should include "#{prefix}user.name #{name}"
result.should include "#{prefix}user.initials #{initials}"
result.should include "#{prefix}user.email #{email}"
end
def git_config_value(name, global = false)
global_prefix = "cd /tmp && " if global
`#{global_prefix}git config user.#{name}`
end
it "prints help" do
result = run "git-pair --help"
result.should include("Configures git authors when pair programming")
end
it "prints version" do
result = run "git pair --version"
result.should =~ /\d+\.\d+\.\d+/
end
context "with .pairs file" do
before do
write ".pairs", <<-YAML.unindent
pairs:
ab: Aa Bb
bc: Bb Cc
cd: Cc Dd
email:
prefix: the-pair
domain: the-host.com
YAML
end
describe "global" do
it "sets pairs globally when global: true is set" do
write ".pairs", File.read(".pairs") + "\nglobal: true"
result = run "git pair ab"
expect_config result, "Aa Bb", "ab", "[email protected]", :global => true
end
it "sets pairs globally when --global is given" do
result = run "git pair ab --global"
result.should include "global: user.name Aa Bb"
expect_config result, "Aa Bb", "ab", "[email protected]", :global => true
end
it "unsets global config when no argument is passed" do
run "git pair ab --global"
run "git pair ab"
result = run "git pair --global"
#result.should include "Unset --global user.name, user.email and user.initials"
expect_config result, "Aa Bb", "ab", "[email protected]"
result.should_not include("global:")
end
end
it "can set a single user as pair" do
result = run "git pair ab"
expect_config result, "Aa Bb", "ab", "[email protected]"
end
it "can set a 2 users as pair" do
result = run "git pair ab bc"
expect_config result, "Aa Bb and Bb Cc", "ab bc", "[email protected]"
end
it "can set n users as pair" do
result = run "git pair ab bc cd"
expect_config result, "Aa Bb, Bb Cc and Cc Dd", "ab bc cd", "[email protected]"
end
it "prints names, email addresses, and initials in alphabetical order" do
result = run "git pair ab cd bc"
expect_config result, "Aa Bb, Bb Cc and Cc Dd", "ab bc cd", "[email protected]"
end
it "can set a user with apostrophes as pair" do
write ".pairs", File.read(".pairs").sub("Aa Bb", "Pete O'Connor")
result = run "git pair ab"
expect_config result, "Pete O'Connor", "ab", "[email protected]"
end
it "fails when there is no .git in the tree" do
run "rm -f /tmp/pairs"
run "cp .pairs /tmp"
Dir.chdir "/tmp" do
result = run "git pair ab 2>&1", :fail => true
result.should include("Not a git repository (or any of the parent directories)")
end
run "rm -f /tmp/pairs"
end
it "finds .pairs file in lower parent folder" do
run "mkdir foo"
Dir.chdir "foo" do
result = run "git pair ab"
expect_config result, "Aa Bb", "ab", "[email protected]"
end
end
it "unsets local config when no argument is passed" do
run "git pair ab --global"
run "git pair bc"
result = run "git pair"
result.should include "Unset user.name, user.email, user.initials"
expect_config result, "Aa Bb", "ab", "[email protected]", :global => true
result.should_not include("local:")
end
it "uses hard email when given" do
write ".pairs", File.read(".pairs").sub(/email:.*/m, "email: [email protected]")
result = run "git pair ab"
expect_config result, "Aa Bb", "ab", "[email protected]"
end
context "when no email config is present" do
before do
write ".pairs", File.read(".pairs").sub(/email:.*/m, "")
end
it "doesn't set email" do
run "git pair ab"
git_config_value('email').should be_empty
end
it "doesn't report about email" do
result = run "git pair ab"
result.should_not include "email"
end
end
it "uses no email prefix when only host is given" do
write ".pairs", File.read(".pairs").sub(/email:.*/m, "email:\n domain: foo.com")
result = run "git pair ab"
expect_config result, "Aa Bb", "ab", "[email protected]"
end
context "when no no_solo_prefix is given" do
before do
write ".pairs", File.read(".pairs").sub(/email:.*/m, "email:\n prefix: pairs\n no_solo_prefix: true\n domain: foo.com")
end
it "uses no email prefix for single developers" do
result = run "git pair ab"
expect_config result, "Aa Bb", "ab", "[email protected]"
end
it "uses email prefix for multiple developers" do
result = run "git pair ab bc"
expect_config result, "Aa Bb and Bb Cc", "ab bc", "[email protected]"
end
end
it "fails with unknown initials" do
result = run "git pair xx", :fail => true
result.should include("Couldn't find author name for initials: xx")
end
it "uses alternate email prefix" do
write ".pairs", File.read(".pairs").sub(/ab:.*/, "ab: Aa Bb; blob")
result = run "git pair ab"
expect_config result, "Aa Bb", "ab", "[email protected]"
end
end
context "without a .pairs file in the tree" do
around do |example|
Dir.chdir "/tmp" do
run "rm -f .pairs"
dir = "git_stats_test"
run "rm -rf #{dir}"
run "mkdir #{dir}"
Dir.chdir dir do
run "git init"
example.run
end
run "rm -rf #{dir}"
end
end
context "and without a .pairs file in the home directory" do
it "fails if it cannot find a pairs file" do
run "git pair ab", :fail => true
end
it "prints instructions" do
result = run "git pair ab", :fail => true
result.should include("Could not find a .pairs file. Create a YAML file in your project or home directory.")
end
end
context "but a .pairs file in the home directory" do
around do |example|
file = File.join(ENV["HOME"], ".pairs")
write file, <<-YAML.unindent
pairs:
ab: Aa Bb
bc: Bb Cc
cd: Cc Dd
email:
prefix: the-pair
domain: the-host.com
YAML
example.run
FileUtils.rm file
end
it "loads the file" do
result = run "git pair ab"
expect_config result, "Aa Bb", "ab", "[email protected]"
end
end
end
end
describe 'pair-commit' do
before do
write ".pairs", <<-YAML.unindent
pairs:
ab: Aa Bb; abb
bc: Bb Cc; bcc
cd: Cc Dd; cdd
email:
prefix: the-pair
domain: the-host.com
email_addresses:
YAML
end
context 'when a pair has been set' do
before do
run "git pair ab cd"
end
def author_name_of_last_commit
(run "git log -1 --pretty=%an").strip
end
def author_email_of_last_commit
(run "git log -1 --pretty=%ae").strip
end
def committer_name_of_last_commit
(run "git log -1 --pretty=%cn").strip
end
def committer_email_of_last_commit
(run "git log -1 --pretty=%ce").strip
end
it "makes a commit" do
git_pair_commit
output = run "git log -1"
output.should include("Pair pare pear")
end
it "sets the author name to the pair's names" do
git_pair_commit
output = run "git log -1 --pretty=%an"
output.strip.should eq("Aa Bb and Cc Dd")
end
it "randomly chooses from pair and sets user.email" do
emails = 6.times.map do
git_pair_commit
author_email_of_last_commit
end.uniq
emails.should =~ ['[email protected]', '[email protected]']
end
context 'when git options are passed' do
it 'forwards those options to git' do
git_pair_commit
run 'git pair ab bc'
run 'git pair-commit --amend -C HEAD --reset-author'
output = run "git log -1 --pretty=%an"
output.strip.should eq("Aa Bb and Bb Cc")
end
end
context 'when the pair is set globally and the local repo has custom user name and email' do
before do
run 'git pair --global ab cd'
run "git config user.name 'Betty White'"
run "git config user.email '[email protected]'"
end
it 'still makes the commit with the correct user name' do
git_pair_commit
author_name_of_last_commit.should eq("Aa Bb and Cc Dd")
end
it 'still makes the commit with the correct user email' do
git_pair_commit
%w([email protected] [email protected]).should include(author_email_of_last_commit)
end
it 'still makes the commit with the correct committer name' do
git_pair_commit
committer_name_of_last_commit.should eq("Aa Bb and Cc Dd")
end
it 'still makes the commit with the correct committer email' do
git_pair_commit
%w([email protected] [email protected]).should include(committer_email_of_last_commit)
end
end
context 'when one of the pair has a custom email address' do
before do
run 'git pair ab bc'
end
it 'uses that email address' do
emails = 6.times.map do
git_pair_commit
author_email_of_last_commit
end.uniq
emails.should =~ ['[email protected]', '[email protected]']
end
end
end
context 'when no pair has been set' do
it 'raises an exception' do
git_pair_commit.should include('Error: No pair set')
end
end
context 'when -h flag is passed' do
it 'shows the help message' do
results = run 'git pair-commit -h'
results.gsub(/\s+/, ' ').should include('randomly chooses the author email from the members of the pair')
end
end
def git_pair_commit
run "echo #{rand(100)} > b"
run 'git add b'
run 'git pair-commit -m "Pair pare pear"', :fail => true
end
end
end