Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 #
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 . ./common.sh
19 function test_checkout_basic {
20 local testroot=`test_init checkout_basic`
22 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
23 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
24 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
25 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
26 echo "Now shut up and hack" >> $testroot/stdout.expected
28 got checkout $testroot/repo $testroot/wt > $testroot/stdout
29 ret="$?"
30 if [ "$ret" != "0" ]; then
31 test_done "$testroot" "$ret"
32 return 1
33 fi
35 cmp -s $testroot/stdout.expected $testroot/stdout
36 ret="$?"
37 if [ "$ret" != "0" ]; then
38 diff -u $testroot/stdout.expected $testroot/stdout
39 test_done "$testroot" "$ret"
40 return 1
41 fi
43 echo "alpha" > $testroot/content.expected
44 echo "beta" >> $testroot/content.expected
45 echo "zeta" >> $testroot/content.expected
46 echo "delta" >> $testroot/content.expected
47 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
48 $testroot/wt/gamma/delta > $testroot/content
50 cmp -s $testroot/content.expected $testroot/content
51 ret="$?"
52 if [ "$ret" != "0" ]; then
53 diff -u $testroot/content.expected $testroot/content
54 fi
55 test_done "$testroot" "$ret"
56 }
58 function test_checkout_dir_exists {
59 local testroot=`test_init checkout_dir_exists`
61 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
62 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
63 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
64 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
65 echo "Now shut up and hack" >> $testroot/stdout.expected
67 mkdir $testroot/wt
69 got checkout $testroot/repo $testroot/wt > $testroot/stdout
70 ret="$?"
71 if [ "$ret" != "0" ]; then
72 test_done "$testroot" "$ret"
73 return 1
74 fi
76 cmp -s $testroot/stdout.expected $testroot/stdout
77 ret="$?"
78 if [ "$ret" != "0" ]; then
79 diff -u $testroot/stdout.expected $testroot/stdout
80 test_done "$testroot" "$ret"
81 return 1
82 fi
84 echo "alpha" > $testroot/content.expected
85 echo "beta" >> $testroot/content.expected
86 echo "zeta" >> $testroot/content.expected
87 echo "delta" >> $testroot/content.expected
88 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
89 $testroot/wt/gamma/delta > $testroot/content
91 cmp -s $testroot/content.expected $testroot/content
92 ret="$?"
93 if [ "$ret" != "0" ]; then
94 diff -u $testroot/content.expected $testroot/content
95 fi
96 test_done "$testroot" "$ret"
97 }
99 function test_checkout_dir_not_empty {
100 local testroot=`test_init checkout_dir_not_empty`
102 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
103 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
104 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
105 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
106 echo "Now shut up and hack" >> $testroot/stdout.expected
108 mkdir $testroot/wt
109 touch $testroot/wt/foo
111 got checkout $testroot/repo $testroot/wt > $testroot/stdout \
112 2> $testroot/stderr
113 ret="$?"
114 if [ "$ret" == "0" ]; then
115 echo "checkout succeeded unexpectedly" >&2
116 test_done "$testroot" "1"
117 return 1
118 fi
120 echo "got: $testroot/wt: directory exists and is not empty" \
121 > $testroot/stderr.expected
122 cmp -s $testroot/stderr.expected $testroot/stderr
123 ret="$?"
124 if [ "$ret" != "0" ]; then
125 diff -u $testroot/stderr.expected $testroot/stderr
126 test_done "$testroot" "$ret"
127 return 1
128 fi
130 echo -n > $testroot/stdout.expected
131 cmp -s $testroot/stdout.expected $testroot/stdout
132 ret="$?"
133 if [ "$ret" != "0" ]; then
134 diff -u $testroot/stdout.expected $testroot/stdout
135 fi
136 test_done "$testroot" "$ret"
140 function test_checkout_sets_xbit {
141 local testroot=`test_init checkout_sets_xbit 1`
143 touch $testroot/repo/xfile
144 chmod +x $testroot/repo/xfile
145 (cd $testroot/repo && git add .)
146 git_commit $testroot/repo -m "adding executable file"
148 echo "A $testroot/wt/xfile" > $testroot/stdout.expected
149 echo "Now shut up and hack" >> $testroot/stdout.expected
151 got checkout $testroot/repo $testroot/wt > $testroot/stdout
152 ret="$?"
153 if [ "$ret" != "0" ]; then
154 test_done "$testroot" "$ret"
155 return 1
156 fi
158 cmp -s $testroot/stdout.expected $testroot/stdout
159 ret="$?"
160 if [ "$ret" != "0" ]; then
161 diff -u $testroot/stdout.expected $testroot/stdout
162 test_done "$testroot" "$ret"
163 return 1
164 fi
166 ls -l $testroot/wt/xfile | grep -q '^-rwx'
167 ret="$?"
168 if [ "$ret" != "0" ]; then
169 echo "file is not executable" >&2
170 ls -l $testroot/wt/xfile >&2
171 fi
172 test_done "$testroot" "$ret"
175 function test_checkout_commit_from_wrong_branch {
176 local testroot=`test_init checkout_commit_from_wrong_branch`
178 (cd $testroot/repo && git checkout -q -b newbranch)
179 echo "modified alpha on new branch" > $testroot/repo/alpha
180 git_commit $testroot/repo -m "modified alpha on new branch"
182 local head_rev=`git_show_head $testroot/repo`
183 got checkout -b master -c $head_rev $testroot/repo $testroot/wt \
184 > $testroot/stdout 2> $testroot/stderr
185 ret="$?"
186 if [ "$ret" == "0" ]; then
187 test_done "$testroot" "1"
188 return 1
189 fi
191 echo -n "" > $testroot/stdout.expected
192 cmp -s $testroot/stdout.expected $testroot/stdout
193 ret="$?"
194 if [ "$ret" != "0" ]; then
195 diff -u $testroot/stdout.expected $testroot/stdout
196 test_done "$testroot" "$ret"
197 return 1
198 fi
200 echo "got: target commit is on a different branch" \
201 > $testroot/stderr.expected
202 cmp -s $testroot/stderr.expected $testroot/stderr
203 ret="$?"
204 if [ "$ret" != "0" ]; then
205 diff -u $testroot/stderr.expected $testroot/stderr
206 test_done "$testroot" "$ret"
207 return 1
208 fi
210 test_done "$testroot" "$ret"
213 function test_checkout_tag {
214 local testroot=`test_init checkout_tag`
215 local tag="1.0.0"
217 (cd $testroot/repo && git tag -a -m "test" $tag)
219 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
220 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
221 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
222 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
223 echo "Now shut up and hack" >> $testroot/stdout.expected
225 got checkout -c $tag $testroot/repo $testroot/wt > $testroot/stdout
226 ret="$?"
227 if [ "$ret" != "0" ]; then
228 test_done "$testroot" "$ret"
229 return 1
230 fi
232 cmp -s $testroot/stdout.expected $testroot/stdout
233 ret="$?"
234 if [ "$ret" != "0" ]; then
235 diff -u $testroot/stdout.expected $testroot/stdout
236 test_done "$testroot" "$ret"
237 return 1
238 fi
240 echo "alpha" > $testroot/content.expected
241 echo "beta" >> $testroot/content.expected
242 echo "zeta" >> $testroot/content.expected
243 echo "delta" >> $testroot/content.expected
244 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
245 $testroot/wt/gamma/delta > $testroot/content
247 cmp -s $testroot/content.expected $testroot/content
248 ret="$?"
249 if [ "$ret" != "0" ]; then
250 diff -u $testroot/content.expected $testroot/content
251 fi
252 test_done "$testroot" "$ret"
255 function test_checkout_ignores_submodules {
256 local testroot=`test_init checkout_ignores_submodules`
258 (cd $testroot && git clone -q repo repo2 >/dev/null)
259 (cd $testroot/repo && git submodule -q add ../repo2)
260 (cd $testroot/repo && git commit -q -m 'adding submodule')
262 echo "A $testroot/wt/.gitmodules" > $testroot/stdout.expected
263 echo "A $testroot/wt/alpha" >> $testroot/stdout.expected
264 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
265 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
266 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
267 echo "Now shut up and hack" >> $testroot/stdout.expected
269 got checkout $testroot/repo $testroot/wt > $testroot/stdout
270 ret="$?"
271 if [ "$ret" != "0" ]; then
272 test_done "$testroot" "$ret"
273 return 1
274 fi
276 cmp -s $testroot/stdout.expected $testroot/stdout
277 ret="$?"
278 if [ "$ret" != "0" ]; then
279 diff -u $testroot/stdout.expected $testroot/stdout
280 test_done "$testroot" "$ret"
281 return 1
282 fi
284 echo "alpha" > $testroot/content.expected
285 echo "beta" >> $testroot/content.expected
286 echo "zeta" >> $testroot/content.expected
287 echo "delta" >> $testroot/content.expected
288 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
289 $testroot/wt/gamma/delta > $testroot/content
291 cmp -s $testroot/content.expected $testroot/content
292 ret="$?"
293 if [ "$ret" != "0" ]; then
294 diff -u $testroot/content.expected $testroot/content
295 fi
296 test_done "$testroot" "$ret"
299 function test_checkout_read_only {
300 local testroot=`test_init checkout_read_only`
302 # Make the repostiory read-only
303 chmod -R a-w $testroot/repo
305 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
306 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
307 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
308 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
309 echo "Now shut up and hack" >> $testroot/stdout.expected
311 got checkout $testroot/repo $testroot/wt \
312 > $testroot/stdout 2> $testroot/stderr
313 ret="$?"
314 if [ "$ret" != "0" ]; then
315 test_done "$testroot" "$ret"
316 return 1
317 fi
319 cmp -s $testroot/stdout.expected $testroot/stdout
320 ret="$?"
321 if [ "$ret" != "0" ]; then
322 diff -u $testroot/stdout.expected $testroot/stdout
323 test_done "$testroot" "$ret"
324 return 1
325 fi
327 echo -n "got: warning: could not create a reference " \
328 > $testroot/stderr.expected
329 echo -n "to the work tree's base commit; the commit could " \
330 >> $testroot/stderr.expected
331 echo -n "be garbage-collected by Git; making the repository " \
332 >> $testroot/stderr.expected
333 echo "writable and running 'got update' will prevent this" \
334 >> $testroot/stderr.expected
335 cmp -s $testroot/stderr.expected $testroot/stderr
336 ret="$?"
337 if [ "$ret" != "0" ]; then
338 diff -u $testroot/stderr.expected $testroot/stderr
339 test_done "$testroot" "$ret"
340 return 1
341 fi
343 echo "alpha" > $testroot/content.expected
344 echo "beta" >> $testroot/content.expected
345 echo "zeta" >> $testroot/content.expected
346 echo "delta" >> $testroot/content.expected
347 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
348 $testroot/wt/gamma/delta > $testroot/content
350 cmp -s $testroot/content.expected $testroot/content
351 ret="$?"
352 if [ "$ret" != "0" ]; then
353 diff -u $testroot/content.expected $testroot/content
354 fi
355 chmod -R u+w $testroot/repo # make repo cleanup work
356 test_done "$testroot" "$ret"
359 function test_checkout_into_nonempty_dir {
360 local testroot=`test_init checkout_into_nonempty_dir`
362 mkdir -p $testroot/wt
363 make_test_tree $testroot/wt
365 got checkout $testroot/repo $testroot/wt > $testroot/stdout \
366 2> $testroot/stderr
367 ret="$?"
368 if [ "$ret" == "0" ]; then
369 echo "checkout succeeded unexpectedly" >&2
370 test_done "$testroot" "1"
371 return 1
372 fi
374 echo -n > $testroot/stdout.expected
375 cmp -s $testroot/stdout.expected $testroot/stdout
376 ret="$?"
377 if [ "$ret" != "0" ]; then
378 diff -u $testroot/stdout.expected $testroot/stdout
379 test_done "$testroot" "$ret"
380 return 1
381 fi
383 echo "got: $testroot/wt: directory exists and is not empty" \
384 > $testroot/stderr.expected
385 cmp -s $testroot/stderr.expected $testroot/stderr
386 ret="$?"
387 if [ "$ret" != "0" ]; then
388 diff -u $testroot/stderr.expected $testroot/stderr
389 test_done "$testroot" "$ret"
390 return 1
391 fi
393 echo "U $testroot/wt/alpha" > $testroot/stdout.expected
394 echo "U $testroot/wt/beta" >> $testroot/stdout.expected
395 echo "U $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
396 echo "U $testroot/wt/gamma/delta" >> $testroot/stdout.expected
397 echo "Now shut up and hack" >> $testroot/stdout.expected
399 got checkout -E $testroot/repo $testroot/wt > $testroot/stdout
400 ret="$?"
401 if [ "$ret" != "0" ]; then
402 test_done "$testroot" "$ret"
403 return 1
404 fi
406 cmp -s $testroot/stdout.expected $testroot/stdout
407 ret="$?"
408 if [ "$ret" != "0" ]; then
409 diff -u $testroot/stdout.expected $testroot/stdout
410 test_done "$testroot" "$ret"
411 return 1
412 fi
414 echo "E $testroot/wt/alpha" > $testroot/stdout.expected
415 echo "E $testroot/wt/beta" >> $testroot/stdout.expected
416 echo "E $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
417 echo "E $testroot/wt/gamma/delta" >> $testroot/stdout.expected
418 echo "Now shut up and hack" >> $testroot/stdout.expected
420 got checkout -E $testroot/repo $testroot/wt > $testroot/stdout
421 ret="$?"
422 if [ "$ret" != "0" ]; then
423 test_done "$testroot" "$ret"
424 return 1
425 fi
427 cmp -s $testroot/stdout.expected $testroot/stdout
428 ret="$?"
429 if [ "$ret" != "0" ]; then
430 diff -u $testroot/stdout.expected $testroot/stdout
431 test_done "$testroot" "$ret"
432 return 1
433 fi
435 echo "alpha" > $testroot/content.expected
436 echo "beta" >> $testroot/content.expected
437 echo "zeta" >> $testroot/content.expected
438 echo "delta" >> $testroot/content.expected
439 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
440 $testroot/wt/gamma/delta > $testroot/content
442 cmp -s $testroot/content.expected $testroot/content
443 ret="$?"
444 if [ "$ret" != "0" ]; then
445 diff -u $testroot/content.expected $testroot/content
446 test_done "$testroot" "$ret"
447 return 1
448 fi
450 echo "modified alpha" > $testroot/wt/alpha
452 echo "E $testroot/wt/alpha" > $testroot/stdout.expected
453 echo "E $testroot/wt/beta" >> $testroot/stdout.expected
454 echo "E $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
455 echo "E $testroot/wt/gamma/delta" >> $testroot/stdout.expected
456 echo "Now shut up and hack" >> $testroot/stdout.expected
458 got checkout -E $testroot/repo $testroot/wt > $testroot/stdout
459 ret="$?"
460 if [ "$ret" != "0" ]; then
461 test_done "$testroot" "$ret"
462 return 1
463 fi
465 cmp -s $testroot/stdout.expected $testroot/stdout
466 ret="$?"
467 if [ "$ret" != "0" ]; then
468 diff -u $testroot/stdout.expected $testroot/stdout
469 test_done "$testroot" "$ret"
470 return 1
471 fi
473 echo "modified alpha" > $testroot/content.expected
474 echo "beta" >> $testroot/content.expected
475 echo "zeta" >> $testroot/content.expected
476 echo "delta" >> $testroot/content.expected
477 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
478 $testroot/wt/gamma/delta > $testroot/content
480 cmp -s $testroot/content.expected $testroot/content
481 ret="$?"
482 if [ "$ret" != "0" ]; then
483 diff -u $testroot/content.expected $testroot/content
484 test_done "$testroot" "$ret"
485 return
486 fi
488 echo 'M alpha' > $testroot/stdout.expected
489 (cd $testroot/wt && got status > $testroot/stdout)
491 cmp -s $testroot/stdout.expected $testroot/stdout
492 ret="$?"
493 if [ "$ret" != "0" ]; then
494 diff -u $testroot/stdout.expected $testroot/stdout
495 fi
496 test_done "$testroot" "$ret"
499 run_test test_checkout_basic
500 run_test test_checkout_dir_exists
501 run_test test_checkout_dir_not_empty
502 run_test test_checkout_sets_xbit
503 run_test test_checkout_commit_from_wrong_branch
504 run_test test_checkout_tag
505 run_test test_checkout_ignores_submodules
506 run_test test_checkout_read_only
507 run_test test_checkout_into_nonempty_dir