git-p4: add missing && in test
[git] / t / t9800-git-p4.sh
1 #!/bin/sh
2
3 test_description='git-p4 tests'
4
5 . ./test-lib.sh
6
7 ( p4 -h && p4d -h ) >/dev/null 2>&1 || {
8         skip_all='skipping git-p4 tests; no p4 or p4d'
9         test_done
10 }
11
12 GITP4=$GIT_BUILD_DIR/contrib/fast-import/git-p4
13 P4DPORT=10669
14
15 export P4PORT=localhost:$P4DPORT
16
17 db="$TRASH_DIRECTORY/db"
18 cli="$TRASH_DIRECTORY/cli"
19 git="$TRASH_DIRECTORY/git"
20
21 test_debug 'echo p4d -q -d -r "$db" -p $P4DPORT'
22 test_expect_success setup '
23         mkdir -p "$db" &&
24         p4d -q -d -r "$db" -p $P4DPORT &&
25         mkdir -p "$cli" &&
26         mkdir -p "$git" &&
27         export P4PORT=localhost:$P4DPORT
28 '
29
30 test_expect_success 'add p4 files' '
31         cd "$cli" &&
32         p4 client -i <<-EOF &&
33         Client: client
34         Description: client
35         Root: $cli
36         View: //depot/... //client/...
37         EOF
38         export P4CLIENT=client &&
39         echo file1 >file1 &&
40         p4 add file1 &&
41         p4 submit -d "file1" &&
42         echo file2 >file2 &&
43         p4 add file2 &&
44         p4 submit -d "file2" &&
45         cd "$TRASH_DIRECTORY"
46 '
47
48 cleanup_git() {
49         cd "$TRASH_DIRECTORY" &&
50         rm -rf "$git" &&
51         mkdir "$git"
52 }
53
54 test_expect_success 'basic git-p4 clone' '
55         "$GITP4" clone --dest="$git" //depot &&
56         test_when_finished cleanup_git &&
57         cd "$git" &&
58         git log --oneline >lines &&
59         test_line_count = 1 lines
60 '
61
62 test_expect_success 'git-p4 clone @all' '
63         "$GITP4" clone --dest="$git" //depot@all &&
64         test_when_finished cleanup_git &&
65         cd "$git" &&
66         git log --oneline >lines &&
67         test_line_count = 2 lines
68 '
69
70 test_expect_success 'git-p4 sync uninitialized repo' '
71         test_create_repo "$git" &&
72         test_when_finished cleanup_git &&
73         cd "$git" &&
74         test_must_fail "$GITP4" sync
75 '
76
77 #
78 # Create a git repo by hand.  Add a commit so that HEAD is valid.
79 # Test imports a new p4 repository into a new git branch.
80 #
81 test_expect_success 'git-p4 sync new branch' '
82         test_create_repo "$git" &&
83         test_when_finished cleanup_git &&
84         cd "$git" &&
85         test_commit head &&
86         "$GITP4" sync --branch=refs/remotes/p4/depot //depot@all &&
87         git log --oneline p4/depot >lines &&
88         test_line_count = 2 lines
89 '
90
91 test_expect_success 'exit when p4 fails to produce marshaled output' '
92         badp4dir="$TRASH_DIRECTORY/badp4dir" &&
93         mkdir -p "$badp4dir" &&
94         test_when_finished "rm -rf $badp4dir" &&
95         cat >"$badp4dir"/p4 <<-EOF &&
96         #!$SHELL_PATH
97         exit 1
98         EOF
99         chmod 755 "$badp4dir"/p4 &&
100         PATH="$badp4dir:$PATH" "$GITP4" clone --dest="$git" //depot >errs 2>&1 ; retval=$? &&
101         test $retval -eq 1 &&
102         test_must_fail grep -q Traceback errs
103 '
104
105 test_expect_success 'add p4 files with wildcards in the names' '
106         cd "$cli" &&
107         echo file-wild-hash >file-wild#hash &&
108         echo file-wild-star >file-wild\*star &&
109         echo file-wild-at >file-wild@at &&
110         echo file-wild-percent >file-wild%percent &&
111         p4 add -f file-wild* &&
112         p4 submit -d "file wildcards"
113 '
114
115 test_expect_success 'wildcard files git-p4 clone' '
116         "$GITP4" clone --dest="$git" //depot &&
117         test_when_finished cleanup_git &&
118         cd "$git" &&
119         test -f file-wild#hash &&
120         test -f file-wild\*star &&
121         test -f file-wild@at &&
122         test -f file-wild%percent
123 '
124
125 test_expect_success 'clone bare' '
126         "$GITP4" clone --dest="$git" --bare //depot &&
127         test_when_finished cleanup_git &&
128         cd "$git" &&
129         test ! -d .git &&
130         bare=`git config --get core.bare` &&
131         test "$bare" = true
132 '
133
134 p4_add_user() {
135     name=$1
136     fullname=$2
137     p4 user -f -i <<EOF &&
138 User: $name
139 Email: $name@localhost
140 FullName: $fullname
141 EOF
142     p4 passwd -P secret $name
143 }
144
145 p4_grant_admin() {
146     name=$1
147     p4 protect -o |\
148         awk "{print}END{print \"    admin user $name * //depot/...\"}" |\
149         p4 protect -i
150 }
151
152 p4_check_commit_author() {
153     file=$1
154     user=$2
155     if p4 changes -m 1 //depot/$file | grep $user > /dev/null ; then
156         return 0
157     else
158         echo "file $file not modified by user $user" 1>&2
159         return 1
160     fi
161 }
162
163 make_change_by_user() {
164         file=$1 name=$2 email=$3 &&
165         echo "username: a change by $name" >>"$file" &&
166         git add "$file" &&
167         git commit --author "$name <$email>" -m "a change by $name"
168 }
169
170 # Test username support, submitting as user 'alice'
171 test_expect_success 'preserve users' '
172         p4_add_user alice Alice &&
173         p4_add_user bob Bob &&
174         p4_grant_admin alice &&
175         "$GITP4" clone --dest="$git" //depot &&
176         test_when_finished cleanup_git &&
177         cd "$git" &&
178         echo "username: a change by alice" >> file1 &&
179         echo "username: a change by bob" >> file2 &&
180         git commit --author "Alice <alice@localhost>" -m "a change by alice" file1 &&
181         git commit --author "Bob <bob@localhost>" -m "a change by bob" file2 &&
182         git config git-p4.skipSubmitEditCheck true &&
183         P4EDITOR=touch P4USER=alice P4PASSWD=secret "$GITP4" commit --preserve-user &&
184         p4_check_commit_author file1 alice &&
185         p4_check_commit_author file2 bob
186 '
187
188 # Test username support, submitting as bob, who lacks admin rights. Should
189 # not submit change to p4 (git diff should show deltas).
190 test_expect_success 'refuse to preserve users without perms' '
191         "$GITP4" clone --dest="$git" //depot &&
192         test_when_finished cleanup_git &&
193         cd "$git" &&
194         echo "username-noperms: a change by alice" >> file1 &&
195         git commit --author "Alice <alice@localhost>" -m "perms: a change by alice" file1 &&
196         ! P4EDITOR=touch P4USER=bob P4PASSWD=secret "$GITP4" commit --preserve-user &&
197         ! git diff --exit-code HEAD..p4/master > /dev/null
198 '
199
200 # What happens with unknown author? Without allowMissingP4Users it should fail.
201 test_expect_success 'preserve user where author is unknown to p4' '
202         "$GITP4" clone --dest="$git" //depot &&
203         test_when_finished cleanup_git &&
204         cd "$git" &&
205         git config git-p4.skipSubmitEditCheck true &&
206         echo "username-bob: a change by bob" >> file1 &&
207         git commit --author "Bob <bob@localhost>" -m "preserve: a change by bob" file1 &&
208         echo "username-unknown: a change by charlie" >> file1 &&
209         git commit --author "Charlie <charlie@localhost>" -m "preserve: a change by charlie" file1 &&
210         ! P4EDITOR=touch P4USER=alice P4PASSWD=secret "$GITP4" commit --preserve-user &&
211         ! git diff --exit-code HEAD..p4/master > /dev/null &&
212         echo "$0: repeat with allowMissingP4Users enabled" &&
213         git config git-p4.allowMissingP4Users true &&
214         git config git-p4.preserveUser true &&
215         P4EDITOR=touch P4USER=alice P4PASSWD=secret "$GITP4" commit &&
216         git diff --exit-code HEAD..p4/master > /dev/null &&
217         p4_check_commit_author file1 alice
218 '
219
220 # If we're *not* using --preserve-user, git-p4 should warn if we're submitting
221 # changes that are not all ours.
222 # Test: user in p4 and user unknown to p4.
223 # Test: warning disabled and user is the same.
224 test_expect_success 'not preserving user with mixed authorship' '
225         "$GITP4" clone --dest="$git" //depot &&
226         test_when_finished cleanup_git &&
227         cd "$git" &&
228         git config git-p4.skipSubmitEditCheck true &&
229         p4_add_user derek Derek &&
230
231         make_change_by_user usernamefile3 Derek derek@localhost &&
232         P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
233         grep "git author derek@localhost does not match" actual &&
234
235         make_change_by_user usernamefile3 Charlie charlie@localhost &&
236         P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
237         grep "git author charlie@localhost does not match" actual &&
238
239         make_change_by_user usernamefile3 alice alice@localhost &&
240         P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
241         ! grep "git author.*does not match" actual &&
242
243         git config git-p4.skipUserNameCheck true &&
244         make_change_by_user usernamefile3 Charlie charlie@localhost &&
245         P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
246         ! grep "git author.*does not match" actual &&
247
248         p4_check_commit_author usernamefile3 alice
249 '
250
251
252 test_expect_success 'shutdown' '
253         pid=`pgrep -f p4d` &&
254         test -n "$pid" &&
255         test_debug "ps wl `echo $pid`" &&
256         kill $pid
257 '
258
259 test_done