forked from starcraftman/zsh-git-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gitstatus.py
980 lines (844 loc) · 29.3 KB
/
test_gitstatus.py
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
"""
Test module for gitstatus
Fixtures used to to setup git repo scenarios on the fly.
Tests are short and at the end of this file.
"""
from __future__ import absolute_import, print_function
import os
import re
import shlex
import shutil
import subprocess as sub
import tempfile
import pytest
import gitstatus
GIT_STATUS = os.path.join(os.path.dirname(__file__), 'gitstatus.py')
def run_gitstatus():
"""
Helper to simply run gitstatus in the current directory.
Returns:
The output of gitstatus.py in the CWD.
"""
return sub.check_output(['python', GIT_STATUS]).decode('utf-8', errors='ignore')
@pytest.yield_fixture(scope="function")
def empty_working_directory():
"""
Run a test inside an empty temporary directory.
"""
cwd = os.getcwd()
try:
folder = tempfile.mkdtemp()
os.chdir(folder)
yield
finally:
os.chdir(cwd)
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
@pytest.yield_fixture(scope="function")
def git_repo_initial_commit():
"""
Create a fake git repo with the following properties:
- No commits beyond initialization.
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_find_git_root():
"""
Create a fake git repo with the following properties:
- 1 commit
- nested folders called, first/second/third
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
]
try:
subs = os.path.join(folder, 'd_one', 'd_two', 'd_three')
os.makedirs(subs)
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_with_worktree():
"""
Create a fake git repo with the following properties:
- main repo has 3 commits
- upstream repo has 3 commits
- main repo has upstream set and is has diverged by 1 commit each way
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
folder_tree = folder + "_worktree"
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
"first:Second line",
"git add first",
"git commit -m 'second commit'",
"git branch tree",
"git checkout tree",
"first:third line",
"git add first",
"git commit -m 'third commit'",
"git checkout master",
"git worktree add --detach %s tree" % (folder_tree),
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
os.chdir(folder_tree)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
try:
shutil.rmtree(folder_tree)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_parse_stats():
"""
Create a fake git repo with the following properties:
- upstream set to another local git repo
- 3 staged files (1 changed, 2 additions)
- 1 changed file unstaged
- 2 untracked files
- 1 stashed change set
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
folder_up = folder + "_upstream"
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"second:A single line",
"third:A single line",
"touch untracked1 untracked2",
"git add first",
"git commit -m 'first commit'",
"first:Changes to stash",
"git stash",
"first:Changes to stage",
"git add first second third",
"first:Changes but unstaged",
"cp -R %s %s" % (folder, folder_up),
"git remote add -f up %s" % folder_up,
"git branch --set-upstream-to=up/master",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
try:
shutil.rmtree(folder_up)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_parse_stats_only_conflicts():
"""
Create a fake git repo with the following properties:
- upstream set to another local git repo
- edit the same file and create a merge conflict
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
folder_up = folder + "_upstream"
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line\nsecond line\third line",
"git add first",
"git commit -m 'first commit'",
"first:fourth line\nfifth line\n",
"git add first",
"git commit -m 'second commit'",
"cp -R %s %s" % (folder, folder_up),
"git reset --hard HEAD~1",
"first:ninth line\ntenth line\n",
"git add first",
"git commit -m 'new second commit'",
"git remote add -f up %s" % folder_up,
"git branch --set-upstream-to=up/master",
"git fetch up",
"git merge up/master",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
try:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
except sub.CalledProcessError:
pass
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
try:
shutil.rmtree(folder_up)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_branch_on_hash():
"""
Create a fake git repo with the following properties:
- 3 commits made
- yield when on checkout hash
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
"first:A second line",
"git add first",
"git commit -m 'second commit'",
"git checkout HEAD~1",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_branch_on_master():
"""
Create a fake git repo with the following properties:
- 3 commits made
- yield when on checkout hash
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
"first:A second line",
"git add first",
"git commit -m 'second commit'",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_branch_local_only():
"""
Create a fake git repo with the following properties:
- 1 commit
- no upstream copy or set value
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_remote_ahead():
"""
Create a fake git repo with the following properties:
- main repo has 3 commits
- upstream repo has 2 commits
- main repo has upstream set and is AHEAD by 1 commit
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
folder_up = folder + "_upstream"
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
"first:Second line",
"git add first",
"git commit -m 'second commit'",
"cp -R %s %s" % (folder, folder_up),
"first:third line",
"git add first",
"git commit -m 'third commit'",
"git remote add -f up %s" % folder_up,
"git branch --set-upstream-to=up/master",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
try:
shutil.rmtree(folder_up)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_remote_behind():
"""
Create a fake git repo with the following properties:
- main repo has 2 commits
- upstream repo has 3 commits
- main repo has upstream set and is BEHIND by 1 commit
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
folder_up = folder + "_upstream"
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
"first:Second line",
"git add first",
"git commit -m 'second commit'",
"first:third line",
"git add first",
"git commit -m 'third commit'",
"cp -R %s %s" % (folder, folder_up),
"git remote add -f up %s" % folder_up,
"git branch --set-upstream-to=up/master",
"git reset --hard HEAD~1",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
try:
shutil.rmtree(folder_up)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_remote_diverged():
"""
Create a fake git repo with the following properties:
- main repo has 3 commits
- upstream repo has 4 commits
- main repo has upstream set and is has diverged 2 behind, 1 ahead
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
folder_up = folder + "_upstream"
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
"first:Second line",
"git add first",
"git commit -m 'second commit'",
"first:third line",
"git add first",
"git commit -m 'third commit'",
"first:fourth line",
"git add first",
"git commit -m 'fourth commit'",
"cp -R %s %s" % (folder, folder_up),
"git remote add -f up %s" % folder_up,
"git branch --set-upstream-to=up/master",
"git reset --hard HEAD~2",
"first:different third line",
"git add first",
"git commit -m 'different third commit'",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
try:
shutil.rmtree(folder_up)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_in_merge():
"""
Create a fake git repo with the following properties:
- master branch with 2 commits
- dev branch that has 2 commits, last one differs from master
- dev branch is merging master into it
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
"git branch dev",
"first:the second master line here",
"git add first",
"git commit -m 'second master commit'",
"git checkout dev",
"first:Second line for dev",
"git add first",
"git commit -m 'second dev commit'",
"git merge master",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
proc = sub.Popen(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
proc.wait()
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_in_rebase():
"""
Create a fake git repo with the following properties:
- master branch with 3 commits
- dev branch that has 3 commits, last two differ from master
- dev is rebasing master, 2 commits need resolving
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
"git branch dev",
"first:the second master line here",
"git add first",
"git commit -m 'second master commit'",
"first:there is also a third master",
"git add first",
"git commit -m 'third master commit'",
"git checkout dev",
"first:Second line",
"git add first",
"git commit -m 'second dev commit'",
"first:Third line\nForuth line",
"git add first",
"git commit -m 'third dev commit'",
"git rebase master",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
proc = sub.Popen(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
proc.wait()
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
os.chdir(cwd)
@pytest.yield_fixture(scope="function")
def git_repo_upstream_gone():
"""
Create a fake git repo with the following properties:
- create a repo with 2 commits and a 'dev' branch
- copy repo to create an upstream
- set 'dev' branch to track 'up/dev'
- delete upstream dev
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
folder_up = folder + "_upstream"
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
"first:Second line",
"git add first",
"git commit -m 'second commit'",
"cp -R %s %s" % (folder, folder_up),
"git remote add -f up %s" % folder_up,
"git branch dev",
"git checkout dev",
"git push -u up dev",
"git fetch up",
"git push up :dev",
]
try:
os.chdir(folder)
for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
sub.check_call(shlex.split(cmd),
stdout=devnull, stderr=sub.STDOUT)
yield
finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
try:
shutil.rmtree(folder_up)
except (OSError, IOError):
pass
os.chdir(cwd)
# ----------
# Unit Tests
# ----------
def test_find_git_root(git_repo_find_git_root):
""" A unit test for gitstatus. """
expect = os.path.join(os.getcwd(), '.git')
sub_d = os.path.join(os.getcwd(), 'd_one', 'd_two', 'd_three')
assert os.path.isdir(sub_d)
os.chdir(sub_d)
assert gitstatus.find_git_root() == expect
def test_find_git_root_fail(empty_working_directory):
""" A unit test for gitstatus. """
with pytest.raises(IOError):
gitstatus.find_git_root()
def test_git_paths_in_normal_repo(git_repo_initial_commit):
""" A unit test for gitstatus. """
head_file, stash_file, merge_file, rebase_dir = gitstatus.git_paths(gitstatus.find_git_root())
assert head_file == os.path.join(os.getcwd(), '.git', 'HEAD')
assert stash_file == os.path.join(os.getcwd(), '.git', 'logs', 'refs', 'stash')
assert merge_file == os.path.join(os.getcwd(), '.git', 'MERGE_HEAD')
assert rebase_dir == os.path.join(os.getcwd(), '.git', 'rebase-apply')
def test_git_paths_in_working_tree(git_repo_with_worktree):
""" A unit test for gitstatus. """
repo_root = os.getcwd().replace('_worktree', '')
tree_root = os.path.join(repo_root, '.git', 'worktrees',
os.path.basename(repo_root) + '_worktree')
head_file, stash_file, merge_file, rebase_dir = gitstatus.git_paths(gitstatus.find_git_root())
assert head_file == os.path.join(tree_root, 'HEAD')
assert stash_file == os.path.join(repo_root, '.git', 'logs', 'refs', 'stash')
assert merge_file == os.path.join(tree_root, 'MERGE_HEAD')
assert rebase_dir == os.path.join(tree_root, 'rebase-apply')
def test_parse_stats():
""" A unit test for gitstatus. """
status_input = """?? untracked1
?? untracked2
?? untracked3
AA conflicts1
AU conflicts2
DD conflicts3
DU conflicts4
UA conflicts5
UD conflicts6
UD conflicts7
A_ staged1
C_ staged2
D_ staged3
M_ staged4
R_ staged5
_C changed1
_D changed2
_M changed3
_R changed4"""
assert gitstatus.parse_stats(status_input.splitlines()) == (5, 7, 4, 3)
def test_parse_ahead_behind_only_ahead():
""" A unit test for gitstatus. """
assert gitstatus.parse_ahead_behind("## master...up/master [ahead 2]") == (2, 0)
def test_parse_ahead_behind_only_behind():
""" A unit test for gitstatus. """
assert gitstatus.parse_ahead_behind("## master...up/master [behind 1]") == (0, 1)
def test_parse_ahead_behind_both():
""" A unit test for gitstatus. """
assert gitstatus.parse_ahead_behind("## master...up/master [ahead 2, behind 1]") == (2, 1)
def test_parse_branch_on_local_branch():
""" A unit test for gitstatus. """
branch_line = "## master"
assert gitstatus.parse_branch(branch_line, None) == ('master', '..', 1)
def test_parse_branch_has_upstream():
""" A unit test for gitstatus. """
branch_line = "## master...up/master [ahead 2, behind 1]"
assert gitstatus.parse_branch(branch_line, None) == ('master', 'up/master', 0)
def test_parse_branch_out_on_hash(git_repo_branch_on_hash):
""" A unit test for gitstatus. """
actual_hash = sub.check_output(shlex.split('git rev-parse --short HEAD'))
actual_hash = actual_hash.decode('utf-8', errors='ignore').strip()
head_file = os.path.join(os.getcwd(), '.git', 'HEAD')
branch_line = "## HEAD (no branch)"
assert gitstatus.parse_branch(branch_line, head_file) == (':' + actual_hash, '..', 0)
def test_stash_count_one_stash(git_repo_parse_stats):
""" A unit test for gitstatus. """
stash_file = os.path.join(os.getcwd(), '.git', 'logs', 'refs', 'stash')
assert gitstatus.stash_count(stash_file) == 1
def test_stash_count_no_stash(git_repo_initial_commit):
""" A unit test for gitstatus. """
stash_file = os.path.join(os.getcwd(), 'logs', 'refs', 'stash')
assert gitstatus.stash_count(stash_file) == 0
def test_rebase_progress_active_rebase(git_repo_in_rebase):
rebase_dir = os.path.join(os.getcwd(), '.git', 'rebase-apply')
assert gitstatus.rebase_progress(rebase_dir) == '1/2'
def test_rebase_progress_no_rebase(git_repo_initial_commit):
rebase_dir = os.path.join(os.getcwd(), '.git', 'rebase-apply')
assert gitstatus.rebase_progress(rebase_dir) == '0'
# ----------------
# Functional Tests
# ----------------
def test_gitstatus_no_repo(empty_working_directory):
""" A unit test for gitstatus. """
assert run_gitstatus() == ''
def test_gitstatus_initial_commit(git_repo_initial_commit):
""" A unit test for gitstatus. """
assert run_gitstatus() == 'master 0 0 0 0 0 0 0 1 {} 0 0'.format(gitstatus.SYM_NOUPSTREAM)
def test_gitstatus_local_branch(git_repo_branch_on_master):
""" A unit test for gitstatus. """
assert run_gitstatus() == 'master 0 0 0 0 0 0 0 1 {} 0 0'.format(gitstatus.SYM_NOUPSTREAM)
def test_gitstatus_on_hash(git_repo_branch_on_hash):
""" A unit test for gitstatus. """
actual_hash = sub.check_output(shlex.split('git rev-parse --short HEAD'))
actual_hash = actual_hash.decode('utf-8', errors='ignore').strip()
assert run_gitstatus() == ':{} 0 0 0 0 0 0 0 0 {} 0 0'.format(actual_hash,
gitstatus.SYM_NOUPSTREAM)
def test_gitstatus_parse_stats_no_conflicts(git_repo_parse_stats):
""" A unit test for gitstatus. """
assert run_gitstatus() == 'master 0 0 3 0 1 2 1 0 up/master 0 0'
def test_gitstatus_parse_stats_only_conflicts(git_repo_parse_stats_only_conflicts):
""" A unit test for gitstatus. """
assert run_gitstatus() == 'master 1 1 0 1 0 0 0 0 up/master 1 0'
def test_gitstatus_remote_ahead(git_repo_remote_ahead):
""" A unit test for gitstatus. """
assert run_gitstatus() == 'master 1 0 0 0 0 0 0 0 up/master 0 0'
def test_gitstatus_remote_behind(git_repo_remote_behind):
""" A unit test for gitstatus. """
assert run_gitstatus() == 'master 0 1 0 0 0 0 0 0 up/master 0 0'
def test_gitstatus_remote_diverged(git_repo_remote_diverged):
""" A unit test for gitstatus. """
assert run_gitstatus() == 'master 1 2 0 0 0 0 0 0 up/master 0 0'
def test_gitstatus_stdin(git_repo_parse_stats):
""" A unit test for gitstatus. """
std_input = sub.check_output(['git', 'status', '--branch', '--porcelain'])
with tempfile.TemporaryFile() as finput:
finput.write(std_input)
finput.seek(0)
out = sub.check_output(['python', GIT_STATUS], stdin=finput).decode('utf-8')
assert out == 'master 0 0 3 0 1 2 1 0 up/master 0 0'
def test_gitstatus_merging(git_repo_in_merge):
""" A unit test for gitstatus. """
assert run_gitstatus() == 'dev 0 0 0 1 0 0 0 1 .. 1 0'
def test_gitstatus_rebasing(git_repo_in_rebase):
""" A unit test for gitstatus. """
actual_hash = sub.check_output(shlex.split('git rev-parse --short HEAD'))
actual_hash = actual_hash.decode('utf-8', errors='ignore').strip()
assert run_gitstatus() == ':{} 0 0 0 1 0 0 0 0 .. 0 1/2'.format(actual_hash)
def test_gitstatus_upstream_gone(git_repo_upstream_gone):
""" A unit test for gitstatus. """
assert run_gitstatus() == 'dev 0 0 0 0 0 0 0 0 up/dev 0 0'