chiark / gitweb /
Dgit.pm: Replace many die calls with confess
[dgit.git] / tests / lib
1 #
2
3 exec 2>&1
4 set -x
5 set -o pipefail
6
7 . tests/lib-core
8 . tests/lib-restricts
9
10 t-report-failure () {
11         set +x
12         rc=$1
13         cat <<END >&2
14 TEST FAILED
15 cwd: $PWD
16 funcs: ${FUNCNAME[*]}
17 lines: ${BASH_LINENO[*]}
18 files: ${BASH_SOURCE[*]}
19 END
20         t-save-artifacts
21
22         exit 16
23 }
24
25 trap 'test $? = 0 || t-report-failure' EXIT
26
27 t-filter-out-git-hyphen-dir
28
29 t-set-intree
30
31 : ${DGIT_TEST_DEBUG=-D}
32 export DGIT_TEST_DEBUG
33
34 : ${DGIT_TEST_DISTRO+ ${distro=${DGIT_TEST_DISTRO}}}
35
36 export GIT_COMMITTER_DATE='1530000000 +0100'
37 export GIT_AUTHOR_DATE='1530000000 +0100'
38
39 root=`pwd`
40 troot=$root/tests
41 testname="${DGIT_TEST_TESTNAME-${0##*/}}"
42 export DGIT_TEST_TROOT=$troot
43 bpd=..
44
45 tmp=$ADTTMP
46 if [ x"$tmp" = x ]; then
47         export DGIT_TEST_BYHAND=1
48         mkdir -p tests/tmp
49         tmpbase=$troot/tmp
50         tmp=tests/tmp/$testname
51         rm -rf $tmp
52         mkdir $tmp
53 elif [ "x$DGIT_TEST_TMPBASE" != x ]; then
54         tmpbase="$DGIT_TEST_TMPBASE"
55 fi
56 cd $tmp
57
58 tmp=`pwd`
59
60 t-set-using-tmp
61
62 test -f $tmp/.save-env || \
63 env -0 >$tmp/.save-env
64
65 ln -sf $troot/ssh ssh
66
67 export DEBCHANGE_VENDOR=dpkg
68 unset VISUAL
69 unset GIT_EDITOR
70
71 mkdir -p $tmp/incoming
72 cat <<END >$tmp/dput.cf
73 [test-dummy]
74 method                  = local
75 incoming                = $tmp/incoming
76 run_dinstall            = 0
77 END
78
79 schroot=${DGIT_SCHROOT_CHROOT:-build}
80 # Pretty much any Debian sid snapshot schroot will do.
81
82 : ${t_archive_method:=aq}
83 : ${tagpfx:=archive/test-dummy}
84 : ${suitespecs:=sid:unstable}
85
86 t-git-next-date () {
87         GIT_COMMITTER_DATE="$(( ${GIT_COMMITTER_DATE%% *} + 1 )) ${GIT_COMMITTER_DATE#* }"
88         GIT_AUTHOR_DATE="$GIT_COMMITTER_DATE"
89 }
90
91 t-expect-fail () {
92         local mpat="$1"; shift
93
94         set +o pipefail
95         LC_MESSAGES=${expect_fail_lcmessages-C} \
96         LANGUAGE=${expect_fail_lcmessages-C} \
97         "$@" 2>&1 | tee $tmp/t.output
98         local ps="${PIPESTATUS[*]}"
99         set -o pipefail
100
101         case $ps in
102         "0 0")  fail "command unexpectedly succeeded (instead of: $mpat)" ;;
103         *" 0")  ;;
104         *)      fail "tee failed"  ;;
105         esac
106
107         t-grep-mpat "$mpat" $tmp/t.output
108 }
109
110 t-grep-mpat () {
111         local mpat="$1"
112         local file="$2"
113
114         local grepper=fgrep
115         case "$mpat" in
116         [A-Z]:*)
117                 case "$mpat" in
118                 E:*)    grepper=egrep   ;;
119                 F:*)    grepper=fgrep   ;;
120                 *)      fail "bad mpat prefix in $mpat";;
121                 esac
122                 mpat=${mpat#[A-Z]:}
123                 ;;
124         esac
125
126         $grepper -e "$mpat" "$file" ||
127                 fail "message not found"
128 }
129
130 t-expect-push-fail () {
131         local mpat="$1"; shift
132
133         local triedpush; triedpush=`git rev-parse HEAD`
134
135         t-reporefs pre-push
136         t-expect-fail "$mpat"  "$@"
137         t-reporefs post-push
138         diff $tmp/show-refs.{pre,post}-push
139
140         t-git-objects-not-present '' $triedpush
141
142         eval "$t_expect_push_fail_hook"
143 }
144
145 t-git-objects-not-present () {
146         # t-git-objects-not-present GITDIR|'' OBJID [...]
147         # specifying '' means the repo for package $p
148         local gitdir="${1-$dgitrepo}"
149         local obj
150         if ! [ -e "$gitdir" ]; then return; fi
151         for obj in "$@"; do
152                 GIT_DIR=$gitdir \
153                 t-expect-fail 'unable to find' \
154                 git cat-file -t $obj
155         done
156 }
157
158 t-reporefs () {
159         local whichoutput=$1; shift
160         local whichrepo=${1-$dgitrepo}
161         local outputfile="$tmp/show-refs.$whichoutput"
162         (set -e
163          exec >"$outputfile"
164          if test -d $whichrepo; then
165                 cd $whichrepo
166                 git show-ref |t-sort
167         fi)
168 }
169
170 t-untar () {
171         local tarfile=$1.tar
172         local edittree=$1.edit
173         if test -d "$edittree"; then
174                 cp -a "$edittree"/* .
175         else
176                 tar xf "$tarfile"
177         fi
178 }
179
180 t-worktree () {
181         rm -rf $p
182         t-untar $troot/worktrees/${p}_$1
183 }
184
185 t-select-package () {
186         p=$1
187         dgitrepo=$tmp/git/$p.git
188 }
189
190 t-git () {
191         t-select-package $1
192         v=$2
193         mkdir -p $tmp/git
194         local gs=$troot/git-srcs/${p}_$v.git
195         (set -e; cd $tmp/git; t-untar $gs)
196 }
197
198 t-git-none () {
199         mkdir -p $tmp/git
200         (set -e; cd $tmp/git; tar xf $troot/git-template.tar)
201 }
202
203 t-salsa-add-remote () {
204         local d=$tmp/salsa/$p
205         mkdir -p $d
206         (set -e; cd $d; git init --bare)
207         git remote add ${1-origin} $d
208 }
209
210 t-git-merge-base () {
211         git merge-base $1 $2 || test $? = 1
212 }
213
214 t-has-ancestor () {
215         # t-has-ancestor ANCESTOR
216         # (CHILD is implicit, HEAD)
217         local now;      now=`git rev-parse HEAD`
218         local ancestor; ancestor=`git rev-parse $1^{}`
219         local mbase;    mbase=`t-git-merge-base $ancestor $now`
220         if [ x$mbase != x$ancestor ]; then
221                 fail "not ff $ancestor..$now, $mbase != $ancestor"
222         fi
223 }
224
225 t-has-parent-or-is () {
226         # t-has-parent-or-is CHILD PARENT
227         local child=$1
228         local parent=$2
229         local parents
230         parents=$(git show --pretty=format:' %P %H ' "$child")
231         parent=$(git rev-parse "$parent~0")
232         case "$parents" in
233         *" $parent "*)  ;;
234         *)      fail "child $child lacks parent $parent" ;;
235         esac
236 }
237
238 t-prep-newpackage () {
239         t-select-package $1
240         v=$2
241         t-archive-none $p
242         t-git-none
243         t-worktree $v
244         cd $p
245         if ! git show-ref --verify --quiet refs/heads/master; then
246                 git branch -m dgit/sid master
247                 git remote rm dgit
248         fi
249         cd ..
250 }
251
252 t-archive-none () {
253         t-select-package $1
254         t-archive-none-$t_archive_method
255 }
256 t-archive-none-aq () {
257         mkdir -p $tmp/aq/dsc_in_suite $tmp/mirror/pool/main
258
259         : >$tmp/aq/suites
260         local jsondelim="["
261
262         local suitespec
263         for suitespec in $suitespecs; do
264                 local suite=${suitespec%%:*}
265                 local sname=${suitespec#*:}
266
267                 >$tmp/aq/package.$suite.$p
268                 t-aq-archive-updated $suite $p
269
270                 >$tmp/aq/package.new.$p
271                 t-aq-archive-updated new $p
272
273                 ln -sf $suite $tmp/aq/dsc_in_suite/$sname
274
275                 cat <<END >>$tmp/aq/suites
276 $jsondelim
277    {
278       "archive" : "ftp-master",
279       "codename" : "$suite",
280       "components" : [
281          "main",
282          "contrib",
283          "non-free"
284       ],
285       "name" : "$sname",
286       "dakname" : "$sname"
287 END
288
289                 jsondelim="   },"
290
291         done
292         cat <<END >>$tmp/aq/suites
293     }
294 ]
295 END
296 }
297
298 t-aq-archive-updated () {
299         local suite=$1
300         local p=$2
301         local suitedir=$tmp/aq/dsc_in_suite/$suite
302         mkdir -p $suitedir
303         perl <$tmp/aq/package.$suite.$p >$suitedir/$p -wne '
304                 use JSON;
305                 use strict;
306                 our @v;
307                 m{^(\S+) (\w+) ([^ \t/]+)/(\S+)} or die;
308                 push @v, {
309                         "version" => "$1",
310                         "sha256sum" => "$2",
311                         "component" => "$3",
312                         "filename" => "$4",
313                 };
314                 END {
315                         my $json = JSON->new->canonical();
316                         print $json->encode(\@v) or die $!;
317                 }
318         '
319 }
320
321 t-archive-process-incoming () {
322         local suite=$1
323         mv $tmp/incoming/${p}_* $tmp/mirror/pool/main/
324         t-archive-query "$suite"
325 }
326
327 t-archive-query () {
328         local suite=${1-sid}
329         local dscf=main/${p}_${v}.dsc
330         t-archive-query-$t_archive_method "$suite" "$p" "$v" "$dscf"
331 }
332 t-archive-query-aq () {
333         local suite=$1
334         local p=$2
335         local v=$3
336         local dscf=$4
337         local sha; sha=`sha256sum <$tmp/mirror/pool/$dscf`
338         echo "${v} ${sha%  -} $dscf" >>$tmp/aq/package.$suite.${p}
339         t-aq-archive-updated $suite $p
340 }
341
342 t-archive () {
343         t-archive-none $1
344         v=$2
345         local dscf=${p}_$2.dsc
346         rm -f $tmp/mirror/pool/main/${p}_*
347         ln -s $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/pool/main/
348         t-archive-query $suite
349         rm -rf $tmp/extract
350         mkdir $tmp/extract
351         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/pool/main/$dscf)
352 }
353
354 t-git-dir-time-passes () {
355         touch -d 'last year' $dgitrepo
356 }
357
358 t-git-dir-check () {
359         local gitdir=$dgitrepo
360         case "$1" in
361         enoent)
362                 if test -e "$gitdir"; then fail "$gitdir exists"; fi
363                 return
364                 ;;
365         public) wantstat='7[75]5' ;;
366         secret) wantstat='7[70]0' ;;
367         *)      fail "$1 t-git-dir-check ?" ;;
368         esac
369         gotstat=`stat -c%a $gitdir`
370         case "$gotstat" in
371         *$wantstat) return ;;
372         *)      fail "$gitdir has mode $gotstat, expected $wantstat" ;;
373         esac
374 }
375
376 t-expect-fsck-fail () {
377         echo >>$tmp/fsck.expected-errors "$1"
378 }
379
380 t-git-fsck () {
381         local fsckerrs=$(git rev-parse --git-dir)/dgit-test-fsck.errs
382
383         set +e
384         LC_MESSAGES=C git fsck --no-dangling --strict 2>&1 \
385                 | tee $fsckerrs
386         ps="${PIPESTATUS[*]}"
387         set -e
388
389         local pats
390         if [ -f $tmp/fsck.expected-errors ]; then
391                 pats=(-w -f $tmp/fsck.expected-errors)
392         else
393                 test "$ps" = "0 0"
394         fi
395         pats+=(-e 'notice: HEAD points to an unborn branch')
396         pats+=(-e 'notice: No default references')
397
398         set +e
399         grep -v "${pats[@]}" $fsckerrs
400         rc=$?
401         set -e
402         case $rc in
403         1) ;; # no unexpected errors
404         0) fail "unexpected messages from git-fsck" ;;
405         *) fail "grep of git-fsck failed" ;;
406         esac
407 }
408
409 t-check-only-bpd () {
410         if [ "$bpd" = .. ]; then return; fi
411         t-files-notexist \
412                 $tmp/*.{deb,changes,dsc,buildinfo} \
413                 $tmp/*.{tar,diff}.*
414 }
415
416 t-fscks () {
417         (
418         shopt -s nullglob
419         for d in $tmp/*/.git $tmp/git/*.git; do
420                 cd "${d%/.git}"
421                 t-git-fsck
422         done
423         )
424 }
425
426 t-ok () {
427         : '========================================'
428         t-check-only-bpd
429         t-fscks
430         t-save-artifacts
431         echo ok.
432 }
433
434 t-save-artifacts () {
435         artifacts="$AUTOPKGTEST_ARTIFACTS"
436         if [ x"$artifacts" = x ]; then return; fi
437         if [ x"tmp" = x ]; then return; fi
438         GZIP=-1v tar -C "$tmp" -zc -f "$artifacts/${0##*/}.tar.gz" \
439                 --exclude=\*.tar .
440 }
441
442 t-rm-dput-dropping () {
443         rm -f $tmp/${p}_${v}_*.upload
444 }
445
446 t-dgit () {
447         local dgit=${DGIT_TEST-dgit}
448         pwd >&2
449         : '
450 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
451         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
452  ${dgit_config_debian_alias-"--config-lookup-explode=dgit-distro.debian.alias-canon"} \
453  ${DGIT_GITDEBREBASE_TEST+--git-debrebase=}${DGIT_GITDEBREBASE_TEST} \
454                 ${distro+${distro:+-d}}${distro--dtest-dummy} \
455                 $DGIT_TEST_OPTS $DGIT_TEST_DEBUG \
456                 -kBCD22CD83243B79D3DFAC33EA3DBCBC039B13D8A $t_dgit_xopts "$@"
457         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
458 '
459 }
460
461 t-dgit-manpage () {
462         local section=$1
463         local page=$2
464         (export LC_ALL=C.UTF-8
465          if [ "$DGIT_TEST_INTREE" ]; then
466                 make -C $DGIT_TEST_INTREE $page.$section.view
467          else
468                 man $section $page
469          fi)
470 }
471
472 t-diff-nogit () {
473         diff --exclude=.git --exclude=.pc -ruN $*
474 }
475
476 t-files-notexist () {
477         local f
478         for f in "$@"; do
479                 if [ -e $f ]; then
480                         fail "$f exists!"
481                 fi
482         done
483 }
484
485 t-cloned-fetched-good () {
486         t-diff-nogit ../extract/$p-${v%-*} .
487         t-clean-on-branch dgit/sid
488         t-refs-same-start
489         t-refs-same \
490                 refs/heads/dgit/sid \
491                 refs/remotes/dgit/dgit/sid
492         t-refs-notexist refs/dgit/unstable refs/remotes/dgit/dgit/unstable
493 }
494
495 t-output () {
496         printf "%s${1:+\n}" "$1" >$tmp/t.want
497         shift
498         "$@" >$tmp/t.got
499         diff $tmp/t.want $tmp/t.got
500 }
501
502 t-clean-on-branch () {
503         t-output "## $1" git status -b --porcelain
504 }
505
506 t-setup-done () {
507         local savevars=$1
508         local savedirs=$2
509         local importeval=$3
510
511         local import=IMPORT.${DGIT_TEST_TESTNAME-${0##*/}}
512         exec 4>$tmp/$import.new
513
514         local vn
515         for vn in $savevars; do
516                 perl >&4 -"I$root" -MDebian::Dgit -e '
517                         printf "%s=%s\n", $ARGV[0], shellquote $ARGV[1]
518                 ' $vn "$(eval "printf '%s\n' \"\$$vn\"")"
519         done
520
521         (set -e; cd $tmp; tar cf $import.tar $savedirs)
522
523         printf >&4 "\n%s\n" "$importeval"
524
525         mv -f $tmp/$import.new $tmp/$import
526 }
527
528 t-setup-import () {
529         local setupname=$1
530
531         local setupsrc
532         local lock
533         if [ "x$tmpbase" = x ]; then
534                 # ADTTMP was set on entry to tests/lib, so we
535                 # are not sharing tmp area between tests
536                 setupsrc="$tmp"
537                 lock="$tmp/.dummy.lock"
538         else
539                 setupsrc="$tmpbase/$setupname"
540                 lock="$setupsrc.lock"
541         fi
542
543         local simport="$setupsrc/IMPORT.$setupname"
544
545         if ! [ -e "$simport" ]; then
546                 with-lock-ex -w "$lock" \
547                 xargs -0 -a $tmp/.save-env \
548                 bash -xec '
549                         cd "$1"; shift
550                         setupname="$1"; shift
551                         simport="$1"; shift
552                         if [ -e "$simport" ]; then exit 0; fi
553                         env - "$@" \
554                         "tests/setup/$setupname"
555                 ' x "$root" "$setupname" "$simport"
556         fi
557
558         if [ "x$setupsrc" != "x$tmp" ]; then
559                 (set -e; cd $tmp; tar xf "$simport.tar")
560         fi
561
562         . "$simport"
563 }
564
565 t-git-get-ref-exact () {
566         local ref=$1
567         # does not dereference, unlike t-git-get-ref
568         case "$ref" in
569         refs/*) ;;
570         *) fail "t-git-get-ref-exact bad $ref" ;;
571         esac
572         git for-each-ref --format='%(objectname)' "[r]efs/${ref#refs/}"
573 }
574
575 t-git-get-ref () {
576         local ref=$1
577         case "$ref" in
578         refs/*) ;;
579         *) fail "t-git-get-ref bad $ref" ;;
580         esac
581         (git show-ref -d $1 || test $? = 1) | perl -ne '
582                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
583                 END { print "$x\n" if length $x; }
584         '
585 }
586
587 t-ref-same-exact () {
588         local name="$1"
589         local val; val=`t-git-get-ref-exact $name`
590         t-ref-same-val "$name" $val
591 }
592
593 t-ref-same () {
594         local name="$1"
595         local val; val=`t-git-get-ref $name`
596         t-ref-same-val "$name" $val
597 }
598
599 t-ref-head () {
600         local val; val=`git rev-parse HEAD`
601         t-ref-same-val HEAD $val
602 }
603
604 t-ref-same-val () {
605         local name="$1"
606         local val=$2
607         case "${t_ref_val-unset}" in
608         unset)          ;;
609         "$val")         ;;
610         *)              fail "ref varies: ($name)\
611  ${val:-nothing} != ${t_ref_val:-nothing} (${t_ref_names[*]})" ;;
612         esac
613         t_ref_val="$val"
614         t_ref_names+=("$name")
615 }
616
617 t-refs-same-start () {
618         unset t_ref_val
619         t_ref_names=()
620 }
621
622 t-refs-same () {
623         local g
624         for g in $*; do
625                 t-ref-same $g
626         done
627 }
628
629 t-refs-notexist () {
630         local val
631         for g in $*; do
632                 val=`t-git-get-ref $g`
633                 if [ "x$val" != x ]; then
634                         fail "ref $g unexpectedly exists ($val)"
635                 fi
636         done
637 }
638
639 t-v-tag () {
640         echo refs/tags/$tagpfx/${v//\~/_}
641 }
642
643 t-format-ref () {
644         git log -n1 --pretty=format:"$1" "$2"
645 }
646
647 t-sametree-parent () {
648         local ref=$1
649         local parent
650         local ctree; ctree=$(t-format-ref '%T' "$ref")
651         while :; do
652                 local psame=''
653                 for parent in $(t-format-ref '%P' "$ref"); do
654                         local ptree; ptree=$(t-format-ref '%T' "$parent")
655                         if [ "x$ptree" = "x$ctree" ]; then
656                                 psame+=" $parent"
657                         fi
658                 done
659                 case "$psame" in ""|" * *") break ;; esac
660                 ref="${psame# }"
661         done
662         echo "$ref"
663 }
664
665 t-check-pushed-master () {
666         local master; master=`t-git-get-ref refs/heads/master`
667         if [ x$master = x$t_ref_val ]; then return; fi
668         if [ x$master = x ]; then fail "failed to push master"; fi
669         # didn't update master, it must be not FF
670         local mbase; mbase=`t-git-merge-base $master $t_ref_val`
671         if [ x$mbase = x$master ]; then fail "failed to ff master"; fi
672 }
673
674 t-push-was-source-only () {
675         local f
676         t-files-notexist $tmp/incoming/${p}_${v}_*.deb \
677                          $tmp/incoming/${p}_${v}_*.udeb
678         # we permit _source.buildinfo files; see test_changes_source_only()
679         for f in $tmp/incoming/${p}_${v}_*.buildinfo; do
680             if [ -e $f ]; then
681                 case "$f" in
682                     *_source.buildinfo) ;;
683                     *) fail "non-source-only file $f exists!" ;;
684                 esac
685             fi
686         done
687 }
688
689 t-push-included () {
690     for f in $@; do
691         stat $tmp/incoming/$f
692     done
693 }
694
695 t-pushed-good () {
696         local branch=$1
697         local suite=${2:-sid}
698         t-refs-same \
699                 refs/heads/$branch
700         t-pushed-good-core
701 }
702         
703 t-pushed-good-core () {
704         t-ref-dsc-dgit
705         t-refs-same \
706                 `t-v-tag` \
707                 refs/remotes/dgit/dgit/$suite
708         t-refs-notexist \
709                 refs/heads/dgit/unstable \
710                 refs/remotes/dgit/dgit/unstable
711         (set -e; cd $dgitrepo
712          t-refs-same \
713                 refs/dgit/$suite \
714                 `t-v-tag`
715          ${t_check_pushed_master:- : NOT-DRS-NO-CHECK-PUSHED-MASTER}
716          t-refs-notexist \
717                 refs/dgit/unstable
718         )
719         git verify-tag `t-v-tag`
720 }
721
722 t-pushed-good-check-changes () {
723         changes_filename="$tmp/incoming/${p}_${v}_*.changes"
724         grep -E "^Distribution: $suite" $changes_filename
725         grep -E "^Version: $v" $changes_filename
726 }
727
728 t-splitbrain-pushed-good--unpack () {
729         cd $tmp
730         rm -rf t-unpack
731         mkdir t-unpack
732         cd t-unpack
733         ln -s $tmp/mirror/pool/main/*.orig*.tar* .
734         ln -s $tmp/incoming/*.orig*.tar* . ||:
735         ln -s $incoming_dsc .
736         ln -s ${incoming_dsc/.dsc/.debian.tar}* .
737         dpkg-source "$@" -x *.dsc
738         cd */.
739         git init
740         git fetch ../../$p "refs/tags/*:refs/tags/*"
741 }
742
743 t-splitbrain-pushed-good--checkprep () {
744         git add -Af .
745         git rm --cached -r --ignore-unmatch .pc
746 }
747
748 t-splitbrain-pushed-good--checkdiff () {
749         local tag=$1
750         t-splitbrain-pushed-good--checkprep
751         t-output "" git diff --stat --cached $tag
752 }
753
754 t-splitbrain-pushed-good-start () {
755         dep14tag=refs/tags/test-dummy/${v//\~/_}
756         dgittag=$(t-v-tag)
757         t-output "" git status --porcelain
758         t-ref-head
759         t-refs-same $dep14tag
760         (set -e; cd $dgitrepo; t-refs-same $dep14tag)
761         git merge-base --is-ancestor $dep14tag $dgittag
762
763         t-refs-same-start
764         t-ref-same refs/heads/split.p
765         case "$(t-git-get-ref refs/heads/split.b)" in
766         "$t_ref_val") ;;
767         "$(git rev-parse refs/heads/split.p^0)") ;;
768         "$(git rev-parse refs/heads/split.p^1)") ;;
769         *) fail 'bad b/p' ;;
770         esac
771         t-pushed-good-core
772
773         t-incoming-dsc
774
775         t-splitbrain-pushed-good--unpack
776         t-splitbrain-pushed-good--checkdiff $dgittag
777 }
778 t-splitbrain-pushed-good-end-made-dep14 () {
779         t-splitbrain-pushed-good--checkdiff $dep14tag
780         cd $tmp/$p
781 }
782
783 t-splitbrain-rm-gitignore-patch () {
784         perl -i -pe '
785                 next unless $_ eq "auto-gitignore\n";
786                 die if $counter++;
787                 chomp;
788                 rename "debian/patches/$_", "../t-auto-gitignore" or die $!;
789                 $_ = "";
790         ' debian/patches/series
791 }
792
793 t-gbp-pushed-good () {
794         local suite=${1:-sid}
795         t-splitbrain-pushed-good-start
796
797         # Right, now we want to check that the maintainer tree and
798         # the dgit tree differ in exactly the ways we expect.  We
799         # achieve this by trying to reconstruct the maintainer tree
800         # from the dgit tree.
801
802         # So, unpack it withut the patches applied
803         t-splitbrain-pushed-good--unpack --skip-patches
804
805         # dgit might have added a .gitignore patch, which we need to
806         # drop and remove
807         t-splitbrain-rm-gitignore-patch
808
809         # Now the result should differ only in non-debian/ .gitignores
810         t-splitbrain-pushed-good--checkprep
811         git diff --cached --name-only $dep14tag >../changed
812         perl -ne '
813                 next if !m#^debian/# && m#(^|/)\.gitignore#;
814                 die "$_ mismatch";
815         ' <../changed
816
817         # If we actually apply the gitignore patch by hand, it
818         # should be perfect:
819         if [ -f ../t-auto-gitignore ]; then
820                 patch --backup-if-mismatch -p1 -u <../t-auto-gitignore
821         fi
822
823         t-splitbrain-pushed-good-end-made-dep14
824 }
825
826 t-unapplied-pushed-good () {
827         t-splitbrain-pushed-good-start
828         t-splitbrain-pushed-good--unpack --skip-patches
829         t-splitbrain-pushed-good-end-made-dep14
830 }
831
832 t-dpm-pushed-good () {
833         t-splitbrain-pushed-good-start
834         t-splitbrain-pushed-good--unpack
835         t-splitbrain-rm-gitignore-patch
836         t-splitbrain-pushed-good-end-made-dep14
837 }
838
839 t-commit-build-push-expect-log () {
840         local msg=$1
841         local mpat=$2
842         t-commit "$msg"
843         t-dgit build
844         LC_MESSAGES=C \
845         t-dgit push --new 2>&1 |tee $tmp/push.log
846         t-grep-mpat "$mpat" $tmp/push.log
847 }
848
849 t-822-field () {
850         local file=$1
851         local field=$2
852         perl -e '
853                 use Dpkg::Control::Hash;
854                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
855                 $h->parse(\*STDIN,"'"$file"'");
856                 my $val = $h->{"'$field'"},"\n";
857                 die "'"$file $field"'" unless defined $val;
858                 print $val,"\n";
859         ' <$file
860 }
861
862 t-defdistro () {
863         export DGIT_TEST_DISTRO=''
864         distro=''
865         t-git-config dgit-suite.unstable.distro test-dummy
866 }
867
868 t-stunt-envvar () {
869         local var=$1
870         local tstunt=$2
871         eval '
872                 case "$'$var'" in
873                 "$tstunt:"*)    ;;
874                 *":$tstunt:"*)  ;;
875                 "")             '$var'="$tstunt" ;;
876                 *)              '$var'="$tstunt:$'$var'" ;;
877                 esac
878                 export '$var'
879         '
880 }
881
882 t-tstunt--save-real () {
883         local f="$1"
884         case "$f" in
885         */*) return ;;
886         esac
887
888         local rc
889         local real
890         set +e
891         real=$(
892                 p=":$PATH:"
893                 p="${p/:"$tmp/tstunt":/:}"
894                 p="${p%:}"
895                 p="${p#:}"
896                 PATH="$p"
897                 type -p "$f"
898         )
899         rc=$?
900         set -e
901
902         case $rc in
903         1)      return ;;
904         0)      ;;
905         *)      fail "did not find $f on PATH $PATH" ;;
906         esac
907
908         local varname=${f//[^_0-9a-zA-Z]/_}
909         varname=DGIT_TEST_REAL_${varname^^}
910
911         eval "
912                 : \${$varname:=\$real}
913                 export $varname
914         "
915 }
916
917 t-tstunt () {
918         local tstunt=$tmp/tstunt
919         t-stunt-envvar PATH $tstunt
920         t-stunt-envvar PERLLIB $tstunt
921         local f
922         for f in "$@"; do
923                 t-tstunt--save-real $f
924                 f="./$f"
925                 local d="$tstunt/${f%/*}"
926                 mkdir -p $d
927                 ln -sf "$troot/tstunt/$f" "$d"/.
928         done
929 }
930
931 t-tstunt-parsechangelog () {
932         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
933 }
934
935 t-tstunt-lintian () {
936         t-tstunt lintian
937 }
938
939 t-tstunt-debuild () {
940         t-tstunt debuild
941 }
942
943 t-incoming-dsc () {
944         local dsc=${p}_${v}.dsc
945         incoming_dsc=$tmp/incoming/$dsc
946 }
947
948 t-ref-dsc-dgit () {
949         t-incoming-dsc
950         local val; val=`t-822-field $incoming_dsc Dgit`
951         val=$( perl -e '
952                 $_=shift @ARGV;
953                 die "Dgit $_ ?" unless m/^\w+\b/;
954                 print $&,"\n" or die $!;
955         ' "$val")
956         t-ref-same-val $incoming_dsc "$val"
957 }
958
959 t-apply-diff () {
960         local v1=$1
961         local v2=$2
962         (cd $troot/pkg-srcs;
963          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
964          | patch -p1 -u
965 }
966
967 t-gbp-unapplied-pq2qc () {
968         # does `gbp pq export'
969         # commits the resulting debian/patches on  qc/BRANCH
970         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
971         # qc/BRANCH is not fast-forwarding
972
973         gbp pq export
974
975         branch=`git symbolic-ref HEAD`
976         branch=${branch#refs/heads/}
977
978         case "$branch" in
979         */*) fail "unexpected branch $branch" ;;
980         esac
981
982         git branch -f qc/$branch
983         git checkout qc/$branch
984         git add debian/patches
985         git commit -m 'Commit patch queue'
986 }
987
988 t-git-pseudo-merge () {
989         # like   git merge -s ours
990         if [ ! "$git_pseuomerge_opts" ]; then
991                 if git merge --help \
992                  | grep -q allow-unrelated-histories; then
993                         git_pseuomerge_opts='--allow-unrelated-histories'
994                 fi
995                 git_pseuomerge_opts+=' -s ours'
996         fi
997         git merge $git_pseuomerge_opts "$@"
998 }
999
1000 t-gbp-example-prep-no-ff () {
1001         t-tstunt-parsechangelog
1002         t-archive example 1.0-1
1003         t-git-none
1004         t-worktree 1.0
1005
1006         cd example
1007
1008         t-dgit fetch
1009
1010         git checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
1011         gbp pq rebase
1012
1013         echo '/* some comment */' >>src.c
1014         git add src.c
1015         git commit -m 'Add a comment to an upstream file'
1016
1017         t-gbp-unapplied-pq2qc
1018
1019         t-commit 'some updates' 1.0-2
1020 }
1021
1022 t-gbp-example-prep () {
1023         t-gbp-example-prep-no-ff
1024
1025         t-git-pseudo-merge \
1026                 -m 'Pseudo-merge to make descendant of archive' \
1027                 remotes/dgit/dgit/sid
1028 }
1029
1030 t-make-badcommit () {
1031         badcommit=$(
1032                 git cat-file commit HEAD | \
1033                 perl -pe 's/^committer /commiter /' | \
1034                 git hash-object -w -t commit --stdin
1035         )
1036         t-expect-fsck-fail $badcommit
1037 }
1038
1039 t-make-orig () {
1040         # leaves ust set to filename of orig tarball
1041         local p=$1
1042         local v=$2
1043         local tag=${3-v$2}
1044         ust=${p}_${v}.orig.tar.gz
1045         GZIP=-1 git archive -o $bpd/$ust --prefix=${p}-${v}/ $tag
1046 }
1047
1048 t-merge-conflicted-stripping-conflict-markers () {
1049         local otherbranch=$1
1050         local file=$2
1051
1052         t-expect-fail F:"Merge conflict in $file" \
1053         git merge $otherbranch
1054
1055         perl -i~ -ne 'print unless m{^(?:\<\<\<|\>\>\>|===)}' "$file"
1056         git add "$file"
1057         git commit --no-edit
1058 }
1059
1060 t-commit () {
1061         local msg=$1
1062         v=${2:-${majorv:-1}.$revision}
1063         $troot/tstunt/debchange \
1064                 --force-distribution -v$v --distribution ${3:-unstable} "$1"
1065         git add debian/changelog
1066         debcommit
1067         revision=$(( ${revision-0} + 1 ))
1068 }
1069
1070 t-dch-r-rune () {
1071         local cmd="$1"; shift
1072         local suite=${1-unstable}
1073         $cmd -r -D "$suite" ''
1074 }
1075
1076 t-dch-commit-r () {
1077         t-dch-r-rune t-dch-commit "$@"
1078 }
1079
1080 t-dch-commit () {
1081         $troot/tstunt/debchange "$@"
1082         git commit -m "dch $*" debian/changelog
1083 }
1084
1085 t-git-config () {
1086         git config --global "$@"
1087 }
1088
1089 t-drs () {
1090  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
1091  t-git-config dgit-distro.test-dummy.git-check true
1092  t-git-config dgit-distro.test-dummy.git-create true
1093  t-git-config dgit-distro.test-dummy.dgit-tag-format new,old,maint
1094         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
1095         cp $troot/suites $tmp/.
1096         cp $troot/suites $tmp/suites-master
1097
1098         export t_check_pushed_master=t-check-pushed-master
1099
1100         drs_dispatch=$tmp/distro=test-dummy
1101         mkdir $drs_dispatch
1102
1103         if [ "x$DGIT_TEST_INTREE" != x ]; then
1104                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
1105         fi
1106
1107         ln -sf $tmp/git $drs_dispatch/repos
1108         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
1109         mkdir -p $drs_dispatch/keyrings
1110         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
1111         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
1112         ln -sf /bin/true $drs_dispatch/policy-hook
1113 }
1114
1115 t-newtag () {
1116  export tagpfx=archive/test-dummy
1117  t-git-config dgit-distro.test-dummy.dgit-tag-format new,maint
1118 }
1119 t-oldtag () {
1120  export tagpfx=test-dummy
1121  t-git-config dgit-distro.test-dummy.dgit-tag-format old
1122 }
1123
1124 t-dsd () {
1125         t-drs
1126  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
1127  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
1128  t-git-config dgit-distro.test-dummy.git-create true
1129  t-git-config dgit-distro.test-dummy.git-url \
1130                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
1131
1132  t-git-config dgit-distro.test-dummy.diverts.drs /drs
1133  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
1134  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
1135  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
1136  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
1137
1138         echo 'no-such-package* drs' >$drs_dispatch/diverts
1139 }
1140
1141 t-policy-admin () {
1142         : '(((((((((((((((((((((((((((((((((((((((('
1143         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
1144         : '))))))))))))))))))))))))))))))))))))))))'
1145 }
1146
1147 t-policy-nonexist () {
1148         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
1149 }
1150
1151 t-make-hook-link () {
1152         local hook=$1 # in infra/
1153         local linkpath=$2
1154         hook=${DGIT_INFRA_PFX}$hook
1155         case $hook in
1156         */*)    ;;
1157         *)      hook=`type -P $hook` ;;
1158         esac
1159         ln -sf "$hook" $linkpath
1160 }
1161
1162 t-policy () {
1163         local policyhook=$1
1164         t-make-hook-link $policyhook $drs_dispatch/policy-hook
1165 }
1166
1167 t-debpolicy () {
1168         t-dsd
1169         t-policy dgit-repos-policy-debian
1170
1171         mkdir $tmp/git
1172         t-policy-admin create-db
1173 }
1174
1175 t-policy-periodic () {
1176         : '(((((((((((((((((((((((((((((((((((((((('
1177         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
1178                 test-dummy $drs_dispatch '' --cron
1179         : '))))))))))))))))))))))))))))))))))))))))'
1180 }
1181
1182 t-buildproductsdir-config () {
1183         bpd=$tmp/bpd
1184         t-git-config dgit.default.build-products-dir $bpd
1185         mkdir -p $bpd
1186         cat <<END >>$tmp/.gbp.conf
1187 [buildpackage]
1188 export-dir = $bpd
1189 END
1190 }
1191
1192 t-restrict () {
1193         local restriction=$1
1194         (cd $root; t-restriction-$restriction >&2)
1195 }
1196
1197 t-dependencies () {
1198         : "Hopefully installed: $*"
1199 }
1200
1201 t-chain-test () {
1202         local ct=$1
1203         local d=${0%/*}
1204         cd $root
1205         export DGIT_TEST_TESTNAME="$testname"
1206         export DGIT_TEST_TMPBASE="$tmpbase"
1207         export ADTTMP=$tmp
1208         exec "$d/$ct"
1209 }       
1210
1211 t-alt-test () {
1212         local t=${0##*/}
1213         t-${t%%-*}
1214         t-chain-test "${t#*-}"
1215 }
1216
1217 t-git-config dgit.default.old-dsc-distro test-dummy
1218
1219 for import in ${autoimport-gnupg}; do
1220         case "$0" in
1221         */$import) ;;
1222         *)
1223                 t-setup-import $import
1224                 ;;
1225         esac
1226 done