Skip to content

Commit c419562

Browse files
avargitster
authored andcommitted
checkout tests: don't ignore "git <cmd>" exit code
Change a fragile pattern introduced in 696acf4 (checkout: implement "-" abbreviation, add docs and tests, 2009-01-17) to check the exit code of both "git symbolic-ref" and "git rev-parse". Without this change this test will become flaky e.g. under SANITIZE=leak if some (but not all) memory leaks revealed by these commands are fixed. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e19b319 commit c419562

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

t/t2012-checkout-last.sh

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@ test_expect_success 'first branch switch' '
2121
git checkout other
2222
'
2323

24+
test_cmp_symbolic_HEAD_ref () {
25+
echo refs/heads/"$1" >expect &&
26+
git symbolic-ref HEAD >actual &&
27+
test_cmp expect actual
28+
}
29+
2430
test_expect_success '"checkout -" switches back' '
2531
git checkout - &&
26-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/main"
32+
test_cmp_symbolic_HEAD_ref main
2733
'
2834

2935
test_expect_success '"checkout -" switches forth' '
3036
git checkout - &&
31-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/other"
37+
test_cmp_symbolic_HEAD_ref other
3238
'
3339

3440
test_expect_success 'detach HEAD' '
@@ -37,12 +43,16 @@ test_expect_success 'detach HEAD' '
3743

3844
test_expect_success '"checkout -" attaches again' '
3945
git checkout - &&
40-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/other"
46+
test_cmp_symbolic_HEAD_ref other
4147
'
4248

4349
test_expect_success '"checkout -" detaches again' '
4450
git checkout - &&
45-
test "z$(git rev-parse HEAD)" = "z$(git rev-parse other)" &&
51+
52+
git rev-parse other >expect &&
53+
git rev-parse HEAD >actual &&
54+
test_cmp expect actual &&
55+
4656
test_must_fail git symbolic-ref HEAD
4757
'
4858

@@ -63,31 +73,31 @@ more_switches () {
6373
test_expect_success 'switch to the last' '
6474
more_switches &&
6575
git checkout @{-1} &&
66-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch2"
76+
test_cmp_symbolic_HEAD_ref branch2
6777
'
6878

6979
test_expect_success 'switch to second from the last' '
7080
more_switches &&
7181
git checkout @{-2} &&
72-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch3"
82+
test_cmp_symbolic_HEAD_ref branch3
7383
'
7484

7585
test_expect_success 'switch to third from the last' '
7686
more_switches &&
7787
git checkout @{-3} &&
78-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch4"
88+
test_cmp_symbolic_HEAD_ref branch4
7989
'
8090

8191
test_expect_success 'switch to fourth from the last' '
8292
more_switches &&
8393
git checkout @{-4} &&
84-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch5"
94+
test_cmp_symbolic_HEAD_ref branch5
8595
'
8696

8797
test_expect_success 'switch to twelfth from the last' '
8898
more_switches &&
8999
git checkout @{-12} &&
90-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch13"
100+
test_cmp_symbolic_HEAD_ref branch13
91101
'
92102

93103
test_expect_success 'merge base test setup' '
@@ -98,27 +108,36 @@ test_expect_success 'merge base test setup' '
98108
test_expect_success 'another...main' '
99109
git checkout another &&
100110
git checkout another...main &&
101-
test "z$(git rev-parse --verify HEAD)" = "z$(git rev-parse --verify main^)"
111+
112+
git rev-parse --verify main^ >expect &&
113+
git rev-parse --verify HEAD >actual &&
114+
test_cmp expect actual
102115
'
103116

104117
test_expect_success '...main' '
105118
git checkout another &&
106119
git checkout ...main &&
107-
test "z$(git rev-parse --verify HEAD)" = "z$(git rev-parse --verify main^)"
120+
121+
git rev-parse --verify main^ >expect &&
122+
git rev-parse --verify HEAD >actual &&
123+
test_cmp expect actual
108124
'
109125

110126
test_expect_success 'main...' '
111127
git checkout another &&
112128
git checkout main... &&
113-
test "z$(git rev-parse --verify HEAD)" = "z$(git rev-parse --verify main^)"
129+
130+
git rev-parse --verify main^ >expect &&
131+
git rev-parse --verify HEAD >actual &&
132+
test_cmp expect actual
114133
'
115134

116135
test_expect_success '"checkout -" works after a rebase A' '
117136
git checkout main &&
118137
git checkout other &&
119138
git rebase main &&
120139
git checkout - &&
121-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/main"
140+
test_cmp_symbolic_HEAD_ref main
122141
'
123142

124143
test_expect_success '"checkout -" works after a rebase A B' '
@@ -127,15 +146,15 @@ test_expect_success '"checkout -" works after a rebase A B' '
127146
git checkout other &&
128147
git rebase main moodle &&
129148
git checkout - &&
130-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/main"
149+
test_cmp_symbolic_HEAD_ref main
131150
'
132151

133152
test_expect_success '"checkout -" works after a rebase -i A' '
134153
git checkout main &&
135154
git checkout other &&
136155
git rebase -i main &&
137156
git checkout - &&
138-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/main"
157+
test_cmp_symbolic_HEAD_ref main
139158
'
140159

141160
test_expect_success '"checkout -" works after a rebase -i A B' '
@@ -144,7 +163,7 @@ test_expect_success '"checkout -" works after a rebase -i A B' '
144163
git checkout other &&
145164
git rebase main foodle &&
146165
git checkout - &&
147-
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/main"
166+
test_cmp_symbolic_HEAD_ref main
148167
'
149168

150169
test_done

0 commit comments

Comments
 (0)