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