Skip to content

Commit 01d429c

Browse files
inosmeetgitster
authored andcommitted
t1422: refactor tests to be shareable
In preparation for adding tests for the `git refs exists` command, refactor the existing t1422 test suite to make its logic shareable. Move the core test logic from `t1422-show-ref-exists.sh` to `show-ref-exists-tests.sh` file. Inside this script, replace hardcoded calls to "git show-ref --exists" with the `$git_show_ref_exists` variable. The original `t1422-show-ref-exists.sh` script now becomes a simple "driver". It is responsible for setting the default value of the variable and then sourcing the test library. This structure follows an established pattern for sharing tests and prepares the test suite for the `refs exists` tests to be added in a subsequent commit. Mentored-by: Patrick Steinhardt <[email protected]> Mentored-by: shejialuo <[email protected]> Signed-off-by: Meet Soni <[email protected]> Acked-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0749b93 commit 01d429c

File tree

2 files changed

+78
-75
lines changed

2 files changed

+78
-75
lines changed

t/show-ref-exists-tests.sh

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

t/t1422-show-ref-exists.sh

100644100755
Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,4 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
66

77
. ./test-lib.sh
88

9-
test_expect_success setup '
10-
test_commit --annotate A &&
11-
git checkout -b side &&
12-
test_commit --annotate B &&
13-
git checkout main &&
14-
test_commit C &&
15-
git branch B A^0
16-
'
17-
18-
test_expect_success '--exists with existing reference' '
19-
git show-ref --exists refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
20-
'
21-
22-
test_expect_success '--exists with missing reference' '
23-
test_expect_code 2 git show-ref --exists refs/heads/does-not-exist
24-
'
25-
26-
test_expect_success '--exists does not use DWIM' '
27-
test_expect_code 2 git show-ref --exists $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 2>err &&
28-
grep "reference does not exist" err
29-
'
30-
31-
test_expect_success '--exists with HEAD' '
32-
git show-ref --exists HEAD
33-
'
34-
35-
test_expect_success '--exists with bad reference name' '
36-
test_when_finished "git update-ref -d refs/heads/bad...name" &&
37-
new_oid=$(git rev-parse HEAD) &&
38-
test-tool ref-store main update-ref msg refs/heads/bad...name $new_oid $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
39-
git show-ref --exists refs/heads/bad...name
40-
'
41-
42-
test_expect_success '--exists with arbitrary symref' '
43-
test_when_finished "git symbolic-ref -d refs/symref" &&
44-
git symbolic-ref refs/symref refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
45-
git show-ref --exists refs/symref
46-
'
47-
48-
test_expect_success '--exists with dangling symref' '
49-
test_when_finished "git symbolic-ref -d refs/heads/dangling" &&
50-
git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
51-
git show-ref --exists refs/heads/dangling
52-
'
53-
54-
test_expect_success '--exists with nonexistent object ID' '
55-
test-tool ref-store main update-ref msg refs/heads/missing-oid $(test_oid 001) $ZERO_OID REF_SKIP_OID_VERIFICATION &&
56-
git show-ref --exists refs/heads/missing-oid
57-
'
58-
59-
test_expect_success '--exists with non-commit object' '
60-
tree_oid=$(git rev-parse HEAD^{tree}) &&
61-
test-tool ref-store main update-ref msg refs/heads/tree ${tree_oid} $ZERO_OID REF_SKIP_OID_VERIFICATION &&
62-
git show-ref --exists refs/heads/tree
63-
'
64-
65-
test_expect_success '--exists with directory fails with generic error' '
66-
cat >expect <<-EOF &&
67-
error: reference does not exist
68-
EOF
69-
test_expect_code 2 git show-ref --exists refs/heads 2>err &&
70-
test_cmp expect err
71-
'
72-
73-
test_expect_success '--exists with non-existent special ref' '
74-
test_expect_code 2 git show-ref --exists FETCH_HEAD
75-
'
76-
77-
test_expect_success '--exists with existing special ref' '
78-
test_when_finished "rm .git/FETCH_HEAD" &&
79-
git rev-parse HEAD >.git/FETCH_HEAD &&
80-
git show-ref --exists FETCH_HEAD
81-
'
82-
83-
test_done
9+
. "$TEST_DIRECTORY"/show-ref-exists-tests.sh

0 commit comments

Comments
 (0)