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