chiark / gitweb /
68bf9d7353726fe18a393dbbd7b10cee8b81d8b7
[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-splitbrain-pushed-good--unpack () {
712         cd $tmp
713         rm -rf t-unpack
714         mkdir t-unpack
715         cd t-unpack
716         ln -s $tmp/mirror/pool/main/*.orig*.tar* .
717         ln -s $tmp/incoming/*.orig*.tar* . ||:
718         ln -s $incoming_dsc .
719         ln -s ${incoming_dsc/.dsc/.debian.tar}* .
720         dpkg-source "$@" -x *.dsc
721         cd */.
722         git init
723         git fetch ../../$p "refs/tags/*:refs/tags/*"
724 }
725
726 t-splitbrain-pushed-good--checkprep () {
727         git add -Af .
728         git rm --cached -r --ignore-unmatch .pc
729 }
730
731 t-splitbrain-pushed-good--checkdiff () {
732         local tag=$1
733         t-splitbrain-pushed-good--checkprep
734         t-output "" git diff --stat --cached $tag
735 }
736
737 t-splitbrain-pushed-good-start () {
738         dep14tag=refs/tags/test-dummy/${v//\~/_}
739         dgittag=$(t-v-tag)
740         t-output "" git status --porcelain
741         t-ref-head
742         t-refs-same $dep14tag
743         (set -e; cd $dgitrepo; t-refs-same $dep14tag)
744         git merge-base --is-ancestor $dep14tag $dgittag
745
746         t-refs-same-start
747         t-ref-same refs/heads/split.p
748         case "$(t-git-get-ref refs/heads/split.b)" in
749         "$t_ref_val") ;;
750         "$(git rev-parse refs/heads/split.p^0)") ;;
751         "$(git rev-parse refs/heads/split.p^1)") ;;
752         *) fail 'bad b/p' ;;
753         esac
754         t-pushed-good-core
755
756         t-incoming-dsc
757
758         t-splitbrain-pushed-good--unpack
759         t-splitbrain-pushed-good--checkdiff $dgittag
760 }
761 t-splitbrain-pushed-good-end-made-dep14 () {
762         t-splitbrain-pushed-good--checkdiff $dep14tag
763         cd $tmp/$p
764 }
765
766 t-splitbrain-rm-gitignore-patch () {
767         perl -i -pe '
768                 next unless $_ eq "auto-gitignore\n";
769                 die if $counter++;
770                 chomp;
771                 rename "debian/patches/$_", "../t-auto-gitignore" or die $!;
772                 $_ = "";
773         ' debian/patches/series
774 }
775
776 t-gbp-pushed-good () {
777         local suite=${1:-sid}
778         t-splitbrain-pushed-good-start
779
780         # Right, now we want to check that the maintainer tree and
781         # the dgit tree differ in exactly the ways we expect.  We
782         # achieve this by trying to reconstruct the maintainer tree
783         # from the dgit tree.
784
785         # So, unpack it withut the patches applied
786         t-splitbrain-pushed-good--unpack --skip-patches
787
788         # dgit might have added a .gitignore patch, which we need to
789         # drop and remove
790         t-splitbrain-rm-gitignore-patch
791
792         # Now the result should differ only in non-debian/ .gitignores
793         t-splitbrain-pushed-good--checkprep
794         git diff --cached --name-only $dep14tag >../changed
795         perl -ne '
796                 next if !m#^debian/# && m#(^|/)\.gitignore#;
797                 die "$_ mismatch";
798         ' <../changed
799
800         # If we actually apply the gitignore patch by hand, it
801         # should be perfect:
802         if [ -f ../t-auto-gitignore ]; then
803                 patch --backup-if-mismatch -p1 -u <../t-auto-gitignore
804         fi
805
806         t-splitbrain-pushed-good-end-made-dep14
807 }
808
809 t-unapplied-pushed-good () {
810         t-splitbrain-pushed-good-start
811         t-splitbrain-pushed-good--unpack --skip-patches
812         t-splitbrain-pushed-good-end-made-dep14
813 }
814
815 t-dpm-pushed-good () {
816         t-splitbrain-pushed-good-start
817         t-splitbrain-pushed-good--unpack
818         t-splitbrain-rm-gitignore-patch
819         t-splitbrain-pushed-good-end-made-dep14
820 }
821
822 t-commit-build-push-expect-log () {
823         local msg=$1
824         local mpat=$2
825         t-commit "$msg"
826         t-dgit build
827         LC_MESSAGES=C \
828         t-dgit push --new 2>&1 |tee $tmp/push.log
829         t-grep-mpat "$mpat" $tmp/push.log
830 }
831
832 t-822-field () {
833         local file=$1
834         local field=$2
835         perl -e '
836                 use Dpkg::Control::Hash;
837                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
838                 $h->parse(\*STDIN,"'"$file"'");
839                 my $val = $h->{"'$field'"},"\n";
840                 die "'"$file $field"'" unless defined $val;
841                 print $val,"\n";
842         ' <$file
843 }
844
845 t-defdistro () {
846         export DGIT_TEST_DISTRO=''
847         distro=''
848         t-git-config dgit-suite.unstable.distro test-dummy
849 }
850
851 t-stunt-envvar () {
852         local var=$1
853         local tstunt=$2
854         eval '
855                 case "$'$var'" in
856                 "$tstunt:"*)    ;;
857                 *":$tstunt:"*)  ;;
858                 "")             '$var'="$tstunt" ;;
859                 *)              '$var'="$tstunt:$'$var'" ;;
860                 esac
861                 export '$var'
862         '
863 }
864
865 t-tstunt--save-real () {
866         local f="$1"
867         case "$f" in
868         */*) return ;;
869         esac
870
871         local rc
872         local real
873         set +e
874         real=$(
875                 p=":$PATH:"
876                 p="${p/:"$tmp/tstunt":/:}"
877                 p="${p%:}"
878                 p="${p#:}"
879                 PATH="$p"
880                 type -p "$f"
881         )
882         rc=$?
883         set -e
884
885         case $rc in
886         1)      return ;;
887         0)      ;;
888         *)      fail "did not find $f on PATH $PATH" ;;
889         esac
890
891         local varname=${f//[^_0-9a-zA-Z]/_}
892         varname=DGIT_TEST_REAL_${varname^^}
893
894         eval "
895                 : \${$varname:=\$real}
896                 export $varname
897         "
898 }
899
900 t-tstunt () {
901         local tstunt=$tmp/tstunt
902         t-stunt-envvar PATH $tstunt
903         t-stunt-envvar PERLLIB $tstunt
904         local f
905         for f in "$@"; do
906                 t-tstunt--save-real $f
907                 f="./$f"
908                 local d="$tstunt/${f%/*}"
909                 mkdir -p $d
910                 ln -sf "$troot/tstunt/$f" "$d"/.
911         done
912 }
913
914 t-tstunt-parsechangelog () {
915         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
916 }
917
918 t-tstunt-lintian () {
919         t-tstunt lintian
920 }
921
922 t-tstunt-debuild () {
923         t-tstunt debuild
924 }
925
926 t-incoming-dsc () {
927         local dsc=${p}_${v}.dsc
928         incoming_dsc=$tmp/incoming/$dsc
929 }
930
931 t-ref-dsc-dgit () {
932         t-incoming-dsc
933         local val; val=`t-822-field $incoming_dsc Dgit`
934         val=$( perl -e '
935                 $_=shift @ARGV;
936                 die "Dgit $_ ?" unless m/^\w+\b/;
937                 print $&,"\n" or die $!;
938         ' "$val")
939         t-ref-same-val $incoming_dsc "$val"
940 }
941
942 t-apply-diff () {
943         local v1=$1
944         local v2=$2
945         (cd $troot/pkg-srcs;
946          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
947          | patch -p1 -u
948 }
949
950 t-gbp-unapplied-pq2qc () {
951         # does `gbp pq export'
952         # commits the resulting debian/patches on  qc/BRANCH
953         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
954         # qc/BRANCH is not fast-forwarding
955
956         gbp pq export
957
958         branch=`git symbolic-ref HEAD`
959         branch=${branch#refs/heads/}
960
961         case "$branch" in
962         */*) fail "unexpected branch $branch" ;;
963         esac
964
965         git branch -f qc/$branch
966         git checkout qc/$branch
967         git add debian/patches
968         git commit -m 'Commit patch queue'
969 }
970
971 t-git-pseudo-merge () {
972         # like   git merge -s ours
973         if [ ! "$git_pseuomerge_opts" ]; then
974                 if git merge --help \
975                  | grep -q allow-unrelated-histories; then
976                         git_pseuomerge_opts='--allow-unrelated-histories'
977                 fi
978                 git_pseuomerge_opts+=' -s ours'
979         fi
980         git merge $git_pseuomerge_opts "$@"
981 }
982
983 t-gbp-example-prep-no-ff () {
984         t-tstunt-parsechangelog
985         t-archive example 1.0-1
986         t-git-none
987         t-worktree 1.0
988
989         cd example
990
991         t-dgit fetch
992
993         git checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
994         gbp pq rebase
995
996         echo '/* some comment */' >>src.c
997         git add src.c
998         git commit -m 'Add a comment to an upstream file'
999
1000         t-gbp-unapplied-pq2qc
1001
1002         t-commit 'some updates' 1.0-2
1003 }
1004
1005 t-gbp-example-prep () {
1006         t-gbp-example-prep-no-ff
1007
1008         t-git-pseudo-merge \
1009                 -m 'Pseudo-merge to make descendant of archive' \
1010                 remotes/dgit/dgit/sid
1011 }
1012
1013 t-make-badcommit () {
1014         badcommit=$(
1015                 git cat-file commit HEAD | \
1016                 perl -pe 's/^committer /commiter /' | \
1017                 git hash-object -w -t commit --stdin
1018         )
1019         t-expect-fsck-fail $badcommit
1020 }
1021
1022 t-commit () {
1023         local msg=$1
1024         v=${2:-${majorv:-1}.$revision}
1025         dch --force-distribution -v$v --distribution ${3:-unstable} "$1"
1026         git add debian/changelog
1027         debcommit
1028         revision=$(( ${revision-0} + 1 ))
1029 }
1030
1031 t-dch-r-rune () {
1032         local cmd="$1"; shift
1033         local suite=${1-unstable}
1034         $cmd -r -D "$suite" ''
1035 }
1036
1037 t-dch-commit-r () {
1038         t-dch-r-rune t-dch-commit "$@"
1039 }
1040
1041 t-dch-commit () {
1042         faketime @"${GIT_AUTHOR_DATE% *}" dch "$@"
1043         git commit -m "dch $*" debian/changelog
1044 }
1045
1046 t-git-config () {
1047         git config --global "$@"
1048 }
1049
1050 t-drs () {
1051  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
1052  t-git-config dgit-distro.test-dummy.git-check true
1053  t-git-config dgit-distro.test-dummy.git-create true
1054  t-git-config dgit-distro.test-dummy.dgit-tag-format new,old,maint
1055         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
1056         cp $troot/suites $tmp/.
1057         cp $troot/suites $tmp/suites-master
1058
1059         export t_check_pushed_master=t-check-pushed-master
1060
1061         drs_dispatch=$tmp/distro=test-dummy
1062         mkdir $drs_dispatch
1063
1064         if [ "x$DGIT_TEST_INTREE" != x ]; then
1065                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
1066         fi
1067
1068         ln -sf $tmp/git $drs_dispatch/repos
1069         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
1070         mkdir -p $drs_dispatch/keyrings
1071         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
1072         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
1073         ln -sf /bin/true $drs_dispatch/policy-hook
1074 }
1075
1076 t-newtag () {
1077  export tagpfx=archive/test-dummy
1078  t-git-config dgit-distro.test-dummy.dgit-tag-format new,maint
1079 }
1080 t-oldtag () {
1081  export tagpfx=test-dummy
1082  t-git-config dgit-distro.test-dummy.dgit-tag-format old
1083 }
1084
1085 t-dsd () {
1086         t-drs
1087  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
1088  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
1089  t-git-config dgit-distro.test-dummy.git-create true
1090  t-git-config dgit-distro.test-dummy.git-url \
1091                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
1092
1093  t-git-config dgit-distro.test-dummy.diverts.drs /drs
1094  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
1095  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
1096  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
1097  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
1098
1099         echo 'no-such-package* drs' >$drs_dispatch/diverts
1100 }
1101
1102 t-policy-admin () {
1103         : '(((((((((((((((((((((((((((((((((((((((('
1104         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
1105         : '))))))))))))))))))))))))))))))))))))))))'
1106 }
1107
1108 t-policy-nonexist () {
1109         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
1110 }
1111
1112 t-make-hook-link () {
1113         local hook=$1 # in infra/
1114         local linkpath=$2
1115         hook=${DGIT_INFRA_PFX}$hook
1116         case $hook in
1117         */*)    ;;
1118         *)      hook=`type -P $hook` ;;
1119         esac
1120         ln -sf "$hook" $linkpath
1121 }
1122
1123 t-policy () {
1124         local policyhook=$1
1125         t-make-hook-link $policyhook $drs_dispatch/policy-hook
1126 }
1127
1128 t-debpolicy () {
1129         t-dsd
1130         t-policy dgit-repos-policy-debian
1131
1132         mkdir $tmp/git
1133         t-policy-admin create-db
1134 }
1135
1136 t-policy-periodic () {
1137         : '(((((((((((((((((((((((((((((((((((((((('
1138         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
1139                 test-dummy $drs_dispatch '' --cron
1140         : '))))))))))))))))))))))))))))))))))))))))'
1141 }
1142
1143 t-buildproductsdir-config () {
1144         bpd=$tmp/bpd
1145         t-git-config dgit.default.build-products-dir $bpd
1146         mkdir -p $bpd
1147         cat <<END >>$tmp/.gbp.conf
1148 [buildpackage]
1149 export-dir = $bpd
1150 END
1151 }
1152
1153 t-restrict () {
1154         local restriction=$1
1155         (cd $root; t-restriction-$restriction >&2)
1156 }
1157
1158 t-dependencies () {
1159         : "Hopefully installed: $*"
1160 }
1161
1162 t-chain-test () {
1163         local ct=$1
1164         local d=${0%/*}
1165         cd $root
1166         export DGIT_TEST_TESTNAME="$testname"
1167         export DGIT_TEST_TMPBASE="$tmpbase"
1168         export ADTTMP=$tmp
1169         exec "$d/$ct"
1170 }       
1171
1172 t-alt-test () {
1173         local t=${0##*/}
1174         t-${t%%-*}
1175         t-chain-test "${t#*-}"
1176 }
1177
1178 t-git-config dgit.default.old-dsc-distro test-dummy
1179
1180 for import in ${autoimport-gnupg}; do
1181         case "$0" in
1182         */$import) ;;
1183         *)
1184                 t-setup-import $import
1185                 ;;
1186         esac
1187 done