Skip to content

Commit e14bd08

Browse files
committed
t: add test for git refs exists subcommand
The new `git refs exists` subcommand must have identical behavior to its predecessor, `git show-ref --exists`. To avoid duplicating the entire test suite, refactor the existing tests into a shareable helper script. Extract the tests for the `--exists` flag from `t1403-show-ref.sh` into a new `show-ref-exists-tests.sh` helper. The command under test is parameterized using the `$git_show_ref_exists` variable. Source new helper to both `t1403-show-ref.sh` and the new test file, `t1462-refs-exists.sh`, ensuring both commands are verified against the same comprehensive test suite. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
1 parent 5643b59 commit e14bd08

File tree

4 files changed

+91
-66
lines changed

4 files changed

+91
-66
lines changed

t/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ integration_tests = [
211211
't1451-fsck-buffer.sh',
212212
't1460-refs-migrate.sh',
213213
't1461-refs-list.sh',
214+
't1462-refs-exists.sh',
214215
't1500-rev-parse.sh',
215216
't1501-work-tree.sh',
216217
't1502-rev-parse-parseopt.sh',
@@ -1219,4 +1220,4 @@ if perl.found() and time.found()
12191220
timeout: 0,
12201221
)
12211222
endforeach
1222-
endif
1223+
endif

t/show-ref-exists-tests.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
git_show_ref_exists=${git_show_ref_exists:-git show-ref --exists}
2+
3+
test_expect_success '--exists with existing reference' '
4+
${git_show_ref_exists} refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
5+
'
6+
7+
test_expect_success '--exists with missing reference' '
8+
test_expect_code 2 ${git_show_ref_exists} refs/heads/does-not-exist
9+
'
10+
11+
test_expect_success '--exists does not use DWIM' '
12+
test_expect_code 2 ${git_show_ref_exists} $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 2>err &&
13+
grep "reference does not exist" err
14+
'
15+
16+
test_expect_success '--exists with HEAD' '
17+
${git_show_ref_exists} HEAD
18+
'
19+
20+
test_expect_success '--exists with bad reference name' '
21+
test_when_finished "git update-ref -d refs/heads/bad...name" &&
22+
new_oid=$(git rev-parse HEAD) &&
23+
test-tool ref-store main update-ref msg refs/heads/bad...name $new_oid $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
24+
${git_show_ref_exists} refs/heads/bad...name
25+
'
26+
27+
test_expect_success '--exists with arbitrary symref' '
28+
test_when_finished "git symbolic-ref -d refs/symref" &&
29+
git symbolic-ref refs/symref refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
30+
${git_show_ref_exists} refs/symref
31+
'
32+
33+
test_expect_success '--exists with dangling symref' '
34+
test_when_finished "git symbolic-ref -d refs/heads/dangling" &&
35+
git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
36+
${git_show_ref_exists} refs/heads/dangling
37+
'
38+
39+
test_expect_success '--exists with nonexistent object ID' '
40+
test-tool ref-store main update-ref msg refs/heads/missing-oid $(test_oid 001) $ZERO_OID REF_SKIP_OID_VERIFICATION &&
41+
${git_show_ref_exists} refs/heads/missing-oid
42+
'
43+
44+
test_expect_success '--exists with non-commit object' '
45+
tree_oid=$(git rev-parse HEAD^{tree}) &&
46+
test-tool ref-store main update-ref msg refs/heads/tree ${tree_oid} $ZERO_OID REF_SKIP_OID_VERIFICATION &&
47+
${git_show_ref_exists} refs/heads/tree
48+
'
49+
50+
test_expect_success '--exists with directory fails with generic error' '
51+
cat >expect <<-EOF &&
52+
error: reference does not exist
53+
EOF
54+
test_expect_code 2 ${git_show_ref_exists} refs/heads 2>err &&
55+
test_cmp expect err
56+
'
57+
58+
test_expect_success '--exists with non-existent special ref' '
59+
test_expect_code 2 ${git_show_ref_exists} FETCH_HEAD
60+
'
61+
62+
test_expect_success '--exists with existing special ref' '
63+
test_when_finished "rm .git/FETCH_HEAD" &&
64+
git rev-parse HEAD >.git/FETCH_HEAD &&
65+
${git_show_ref_exists} FETCH_HEAD
66+
'

t/t1403-show-ref.sh

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -228,69 +228,5 @@ test_expect_success 'show-ref sub-modes are mutually exclusive' '
228228
grep "cannot be used together" err
229229
'
230230

231-
test_expect_success '--exists with existing reference' '
232-
git show-ref --exists refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
233-
'
234-
235-
test_expect_success '--exists with missing reference' '
236-
test_expect_code 2 git show-ref --exists refs/heads/does-not-exist
237-
'
238-
239-
test_expect_success '--exists does not use DWIM' '
240-
test_expect_code 2 git show-ref --exists $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 2>err &&
241-
grep "reference does not exist" err
242-
'
243-
244-
test_expect_success '--exists with HEAD' '
245-
git show-ref --exists HEAD
246-
'
247-
248-
test_expect_success '--exists with bad reference name' '
249-
test_when_finished "git update-ref -d refs/heads/bad...name" &&
250-
new_oid=$(git rev-parse HEAD) &&
251-
test-tool ref-store main update-ref msg refs/heads/bad...name $new_oid $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
252-
git show-ref --exists refs/heads/bad...name
253-
'
254-
255-
test_expect_success '--exists with arbitrary symref' '
256-
test_when_finished "git symbolic-ref -d refs/symref" &&
257-
git symbolic-ref refs/symref refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
258-
git show-ref --exists refs/symref
259-
'
260-
261-
test_expect_success '--exists with dangling symref' '
262-
test_when_finished "git symbolic-ref -d refs/heads/dangling" &&
263-
git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
264-
git show-ref --exists refs/heads/dangling
265-
'
266-
267-
test_expect_success '--exists with nonexistent object ID' '
268-
test-tool ref-store main update-ref msg refs/heads/missing-oid $(test_oid 001) $ZERO_OID REF_SKIP_OID_VERIFICATION &&
269-
git show-ref --exists refs/heads/missing-oid
270-
'
271-
272-
test_expect_success '--exists with non-commit object' '
273-
tree_oid=$(git rev-parse HEAD^{tree}) &&
274-
test-tool ref-store main update-ref msg refs/heads/tree ${tree_oid} $ZERO_OID REF_SKIP_OID_VERIFICATION &&
275-
git show-ref --exists refs/heads/tree
276-
'
277-
278-
test_expect_success '--exists with directory fails with generic error' '
279-
cat >expect <<-EOF &&
280-
error: reference does not exist
281-
EOF
282-
test_expect_code 2 git show-ref --exists refs/heads 2>err &&
283-
test_cmp expect err
284-
'
285-
286-
test_expect_success '--exists with non-existent special ref' '
287-
test_expect_code 2 git show-ref --exists FETCH_HEAD
288-
'
289-
290-
test_expect_success '--exists with existing special ref' '
291-
test_when_finished "rm .git/FETCH_HEAD" &&
292-
git rev-parse HEAD >.git/FETCH_HEAD &&
293-
git show-ref --exists FETCH_HEAD
294-
'
295-
231+
. "$TEST_DIRECTORY"/show-ref-exists-tests.sh
296232
test_done

t/t1462-refs-exists.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
test_description='refs exists'
4+
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5+
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6+
7+
. ./test-lib.sh
8+
9+
git_show_ref_exists='git refs exists'
10+
11+
test_expect_success setup '
12+
test_commit --annotate A &&
13+
git checkout -b side &&
14+
test_commit --annotate B &&
15+
git checkout main &&
16+
test_commit C &&
17+
git branch B A^0
18+
'
19+
20+
. "$TEST_DIRECTORY"/show-ref-exists-tests.sh
21+
22+
test_done

0 commit comments

Comments
 (0)