@matheusbsbs / matheus-bagliano / commits / 39ac7a7d10
Fork 0

add tests for checkout -b

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tay Ray Chuan committed Jun 24, 2010 at 03:28 UTC 39ac7a7d10564ffdd6634e3a8f23fda069040cc1
1 file changed +121
t/t2018-checkout-branch.sh new
+121
@@ -0,0 +1,121 @@
1 +#!/bin/sh
2 +
3 +test_description='checkout '
4 +
5 +. ./test-lib.sh
6 +
7 +# Arguments: <branch> <sha> [<checkout options>]
8 +#
9 +# Runs "git checkout" to switch to <branch>, testing that
10 +#
11 +# 1) we are on the specified branch, <branch>;
12 +# 2) HEAD is <sha>; if <sha> is not specified, the old HEAD is used.
13 +#
14 +# If <checkout options> is not specified, "git checkout" is run with -b.
15 +do_checkout() {
16 + exp_branch=$1 &&
17 + exp_ref="refs/heads/$exp_branch" &&
18 +
19 + # if <sha> is not specified, use HEAD.
20 + exp_sha=${2:-$(git rev-parse --verify HEAD)} &&
21 +
22 + # default options for git checkout: -b
23 + if [ -z "$3" ]; then
24 + opts="-b"
25 + else
26 + opts="$3"
27 + fi
28 +
29 + git checkout $opts $exp_branch $exp_sha &&
30 +
31 + test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
32 + test $exp_sha = $(git rev-parse --verify HEAD)
33 +}
34 +
35 +test_dirty_unmergeable() {
36 + ! git diff --exit-code >/dev/null
37 +}
38 +
39 +setup_dirty_unmergeable() {
40 + echo >>file1 change2
41 +}
42 +
43 +test_dirty_mergeable() {
44 + ! git diff --cached --exit-code >/dev/null
45 +}
46 +
47 +setup_dirty_mergeable() {
48 + echo >file2 file2 &&
49 + git add file2
50 +}
51 +
52 +test_expect_success 'setup' '
53 + test_commit initial file1 &&
54 + HEAD1=$(git rev-parse --verify HEAD) &&
55 +
56 + test_commit change1 file1 &&
57 + HEAD2=$(git rev-parse --verify HEAD) &&
58 +
59 + git branch -m branch1
60 +'
61 +
62 +test_expect_success 'checkout -b to a new branch, set to HEAD' '
63 + do_checkout branch2
64 +'
65 +
66 +test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
67 + git checkout branch1 &&
68 + git branch -D branch2 &&
69 +
70 + do_checkout branch2 $HEAD1
71 +'
72 +
73 +test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
74 + git checkout branch1 &&
75 +
76 + # clean up from previous test
77 + git branch -D branch2 &&
78 +
79 + setup_dirty_unmergeable &&
80 + test_must_fail do_checkout branch2 $HEAD1 &&
81 + test_dirty_unmergeable
82 +'
83 +
84 +test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
85 + # still dirty and on branch1
86 + do_checkout branch2 $HEAD1 "-f -b" &&
87 + test_must_fail test_dirty_unmergeable
88 +'
89 +
90 +test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
91 + git checkout branch1 &&
92 +
93 + # clean up from previous test
94 + git branch -D branch2 &&
95 +
96 + setup_dirty_mergeable &&
97 + do_checkout branch2 $HEAD1 &&
98 + test_dirty_mergeable
99 +'
100 +
101 +test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
102 + # clean up from previous test
103 + git reset --hard &&
104 +
105 + git checkout branch1 &&
106 +
107 + # clean up from previous test
108 + git branch -D branch2 &&
109 +
110 + setup_dirty_mergeable &&
111 + do_checkout branch2 $HEAD1 "-f -b" &&
112 + test_must_fail test_dirty_mergeable
113 +'
114 +
115 +test_expect_success 'checkout -b to an existing branch fails' '
116 + git reset --hard HEAD &&
117 +
118 + test_must_fail do_checkout branch2 $HEAD2
119 +'
120 +
121 +test_done