chiark / gitweb /
d7cf396ad06e1d00374e4a476ff486fefea40643
[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         (set -e; cd $tmp; tar cf $import.tar $savedirs)
524
525         printf >&4 "\n%s\n" "$importeval"
526
527         mv -f $tmp/$import.new $tmp/$import
528 }
529
530 t-setup-import () {
531         local setupname=$1
532
533         local setupsrc
534         local lock
535         if [ "x$tmpbase" = x ]; then
536                 # ADTTMP was set on entry to tests/lib, so we
537                 # are not sharing tmp area between tests
538                 setupsrc="$tmp"
539                 lock="$tmp/.dummy.lock"
540         else
541                 setupsrc="$tmpbase/$setupname"
542                 lock="$setupsrc.lock"
543         fi
544
545         local simport="$setupsrc/IMPORT.$setupname"
546
547         if ! [ -e "$simport" ]; then
548                 with-lock-ex -w "$lock" \
549                 xargs -0 -a $tmp/.save-env \
550                 bash -xec '
551                         cd "$1"; shift
552                         setupname="$1"; shift
553                         simport="$1"; shift
554                         if [ -e "$simport" ]; then exit 0; fi
555                         env - "$@" \
556                         "tests/setup/$setupname"
557                 ' x "$root" "$setupname" "$simport"
558         fi
559
560         if [ "x$setupsrc" != "x$tmp" ]; then
561                 (set -e; cd $tmp; tar xf "$simport.tar")
562         fi
563
564         . "$simport"
565 }
566
567 t-git-get-ref-exact () {
568         local ref=$1
569         # does not dereference, unlike t-git-get-ref
570         case "$ref" in
571         refs/*) ;;
572         *) fail "t-git-get-ref-exact bad $ref" ;;
573         esac
574         git for-each-ref --format='%(objectname)' "[r]efs/${ref#refs/}"
575 }
576
577 t-git-get-ref () {
578         local ref=$1
579         case "$ref" in
580         refs/*) ;;
581         *) fail "t-git-get-ref bad $ref" ;;
582         esac
583         (git show-ref -d $1 || test $? = 1) | perl -ne '
584                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
585                 END { print "$x\n" if length $x; }
586         '
587 }
588
589 t-ref-same-exact () {
590         local name="$1"
591         local val; val=`t-git-get-ref-exact $name`
592         t-ref-same-val "$name" $val
593 }
594
595 t-ref-same () {
596         local name="$1"
597         local val; val=`t-git-get-ref $name`
598         t-ref-same-val "$name" $val
599 }
600
601 t-ref-head () {
602         local val; val=`git rev-parse HEAD`
603         t-ref-same-val HEAD $val
604 }
605
606 t-ref-same-val () {
607         local name="$1"
608         local val=$2
609         case "${t_ref_val-unset}" in
610         unset)          ;;
611         "$val")         ;;
612         *)              fail "ref varies: ($name)\
613  ${val:-nothing} != ${t_ref_val:-nothing} (${t_ref_names[*]})" ;;
614         esac
615         t_ref_val="$val"
616         t_ref_names+=("$name")
617 }
618
619 t-refs-same-start () {
620         unset t_ref_val
621         t_ref_names=()
622 }
623
624 t-refs-same () {
625         local g
626         for g in $*; do
627                 t-ref-same $g
628         done
629 }
630
631 t-refs-notexist () {
632         local val
633         for g in $*; do
634                 val=`t-git-get-ref $g`
635                 if [ "x$val" != x ]; then
636                         fail "ref $g unexpectedly exists ($val)"
637                 fi
638         done
639 }
640
641 t-v-tag () {
642         echo refs/tags/$tagpfx/${v//\~/_}
643 }
644
645 t-format-ref () {
646         git log -n1 --pretty=format:"$1" "$2"
647 }
648
649 t-sametree-parent () {
650         local ref=$1
651         local parent
652         local ctree; ctree=$(t-format-ref '%T' "$ref")
653         while :; do
654                 local psame=''
655                 for parent in $(t-format-ref '%P' "$ref"); do
656                         local ptree; ptree=$(t-format-ref '%T' "$parent")
657                         if [ "x$ptree" = "x$ctree" ]; then
658                                 psame+=" $parent"
659                         fi
660                 done
661                 case "$psame" in ""|" * *") break ;; esac
662                 ref="${psame# }"
663         done
664         echo "$ref"
665 }
666
667 t-check-pushed-master () {
668         local master; master=`t-git-get-ref refs/heads/master`
669         if [ x$master = x$t_ref_val ]; then return; fi
670         if [ x$master = x ]; then fail "failed to push master"; fi
671         # didn't update master, it must be not FF
672         local mbase; mbase=`t-git-merge-base $master $t_ref_val`
673         if [ x$mbase = x$master ]; then fail "failed to ff master"; fi
674 }
675
676 t-push-was-source-only () {
677         local f
678         t-files-notexist $tmp/incoming/${p}_${v}_*.deb \
679                          $tmp/incoming/${p}_${v}_*.udeb
680         # we permit _source.buildinfo files; see test_changes_source_only()
681         for f in $tmp/incoming/${p}_${v}_*.buildinfo; do
682             if [ -e $f ]; then
683                 case "$f" in
684                     *_source.buildinfo) ;;
685                     *) fail "non-source-only file $f exists!" ;;
686                 esac
687             fi
688         done
689 }
690
691 t-push-included () {
692     for f in $@; do
693         stat $tmp/incoming/$f
694     done
695 }
696
697 t-pushed-good () {
698         local branch=$1
699         local suite=${2:-sid}
700         t-refs-same \
701                 refs/heads/$branch
702         t-pushed-good-core
703 }
704         
705 t-pushed-good-core () {
706         t-ref-dsc-dgit
707         t-refs-same \
708                 `t-v-tag` \
709                 refs/remotes/dgit/dgit/$suite
710         t-refs-notexist \
711                 refs/heads/dgit/unstable \
712                 refs/remotes/dgit/dgit/unstable
713         (set -e; cd $dgitrepo
714          t-refs-same \
715                 refs/dgit/$suite \
716                 `t-v-tag`
717          ${t_check_pushed_master:- : NOT-DRS-NO-CHECK-PUSHED-MASTER}
718          t-refs-notexist \
719                 refs/dgit/unstable
720         )
721         git verify-tag `t-v-tag`
722 }
723
724 t-pushed-good-check-changes () {
725         changes_filename="$tmp/incoming/${p}_${v}_*.changes"
726         grep -E "^Distribution: $suite" $changes_filename
727         grep -E "^Version: $v" $changes_filename
728 }
729
730 t-splitbrain-pushed-good--unpack () {
731         cd $tmp
732         rm -rf t-unpack
733         mkdir t-unpack
734         cd t-unpack
735         ln -s $tmp/mirror/pool/main/*.orig*.tar* .
736         ln -s $tmp/incoming/*.orig*.tar* . ||:
737         ln -s $incoming_dsc .
738         ln -s ${incoming_dsc/.dsc/.debian.tar}* .
739         dpkg-source "$@" -x *.dsc
740         cd */.
741         git init
742         git fetch ../../$p "refs/tags/*:refs/tags/*"
743 }
744
745 t-splitbrain-pushed-good--checkprep () {
746         git add -Af .
747         git rm --cached -r --ignore-unmatch .pc
748 }
749
750 t-splitbrain-pushed-good--checkdiff () {
751         local tag=$1
752         t-splitbrain-pushed-good--checkprep
753         t-output "" git diff --stat --cached $tag
754 }
755
756 t-splitbrain-pushed-good-start () {
757         dep14tag=refs/tags/test-dummy/${v//\~/_}
758         dgittag=$(t-v-tag)
759         t-output "" git status --porcelain
760         t-ref-head
761         t-refs-same $dep14tag
762         (set -e; cd $dgitrepo; t-refs-same $dep14tag)
763         git merge-base --is-ancestor $dep14tag $dgittag
764
765         t-refs-same-start
766         t-ref-same refs/heads/split.p
767         case "$(t-git-get-ref refs/heads/split.b)" in
768         "$t_ref_val") ;;
769         "$(git rev-parse refs/heads/split.p^0)") ;;
770         "$(git rev-parse refs/heads/split.p^1)") ;;
771         *) fail 'bad b/p' ;;
772         esac
773         t-pushed-good-core
774
775         t-incoming-dsc
776
777         t-splitbrain-pushed-good--unpack
778         t-splitbrain-pushed-good--checkdiff $dgittag
779 }
780 t-splitbrain-pushed-good-end-made-dep14 () {
781         t-splitbrain-pushed-good--checkdiff $dep14tag
782         cd $tmp/$p
783 }
784
785 t-splitbrain-rm-1-patch () {
786         local patchname=$1
787         perl -i -pe '
788                 next unless $_ eq "'"$patchname"'\n";
789                 die if $counter++;
790                 chomp;
791                 rename "debian/patches/$_", "../t-'"$patchname"'" or die $!;
792                 $_ = "";
793         ' debian/patches/series
794 }
795
796 t-splitbrain-rm-gitignore-patch () {
797         t-splitbrain-rm-1-patch auto-gitignore
798 }
799
800 t-gbp-pushed-good () {
801         local suite=${1:-sid}
802         t-splitbrain-pushed-good-start
803
804         # Right, now we want to check that the maintainer tree and
805         # the dgit tree differ in exactly the ways we expect.  We
806         # achieve this by trying to reconstruct the maintainer tree
807         # from the dgit tree.
808
809         # So, unpack it withut the patches applied
810         t-splitbrain-pushed-good--unpack --skip-patches
811
812         # dgit might have added a .gitignore patch, which we need to
813         # drop and remove
814         t-splitbrain-rm-gitignore-patch
815
816         # Now the result should differ only in non-debian/ .gitignores
817         t-splitbrain-pushed-good--checkprep
818         git diff --cached --name-only $dep14tag >../changed
819         perl -ne '
820                 next if !m#^debian/# && m#(^|/)\.gitignore#;
821                 die "$_ mismatch";
822         ' <../changed
823
824         # If we actually apply the gitignore patch by hand, it
825         # should be perfect:
826         if [ -f ../t-auto-gitignore ]; then
827                 patch --backup-if-mismatch -p1 -u <../t-auto-gitignore
828         fi
829
830         t-splitbrain-pushed-good-end-made-dep14
831 }
832
833 t-unapplied-pushed-good () {
834         local suite=${1:-sid}
835         t-splitbrain-pushed-good-start
836         t-splitbrain-pushed-good--unpack --skip-patches
837         t-splitbrain-pushed-good-end-made-dep14
838 }
839
840 t-dpm-pushed-good () {
841         local suite=${1:-sid}
842         t-splitbrain-pushed-good-start
843         t-splitbrain-pushed-good--unpack
844         t-splitbrain-rm-gitignore-patch
845         t-splitbrain-pushed-good-end-made-dep14
846 }
847
848 t-commit-build-push-expect-log () {
849         local msg=$1
850         local mpat=$2
851         t-commit "$msg"
852         t-dgit build
853         LC_MESSAGES=C \
854         t-dgit push --new 2>&1 |tee $tmp/push.log
855         t-grep-mpat "$mpat" $tmp/push.log
856 }
857
858 t-822-field () {
859         local file=$1
860         local field=$2
861         perl -e '
862                 use Dpkg::Control::Hash;
863                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
864                 $h->parse(\*STDIN,"'"$file"'");
865                 my $val = $h->{"'$field'"},"\n";
866                 die "'"$file $field"'" unless defined $val;
867                 print $val,"\n";
868         ' <$file
869 }
870
871 t-defdistro () {
872         export DGIT_TEST_DISTRO=''
873         distro=''
874         t-git-config dgit-suite.unstable.distro test-dummy
875 }
876
877 t-stunt-envvar () {
878         local var=$1
879         local tstunt=$2
880         eval '
881                 case "$'$var'" in
882                 "$tstunt:"*)    ;;
883                 *":$tstunt:"*)  ;;
884                 "")             '$var'="$tstunt" ;;
885                 *)              '$var'="$tstunt:$'$var'" ;;
886                 esac
887                 export '$var'
888         '
889 }
890
891 t-tstunt--save-real () {
892         local f="$1"
893         case "$f" in
894         */*) return ;;
895         esac
896
897         local rc
898         local real
899         set +e
900         real=$(
901                 p=":$PATH:"
902                 p="${p/:"$tmp/tstunt":/:}"
903                 p="${p%:}"
904                 p="${p#:}"
905                 PATH="$p"
906                 type -p "$f"
907         )
908         rc=$?
909         set -e
910
911         case $rc in
912         1)      return ;;
913         0)      ;;
914         *)      fail "did not find $f on PATH $PATH" ;;
915         esac
916
917         local varname=${f//[^_0-9a-zA-Z]/_}
918         varname=DGIT_TEST_REAL_${varname^^}
919
920         eval "
921                 : \${$varname:=\$real}
922                 export $varname
923         "
924 }
925
926 t-tstunt () {
927         local tstunt=$tmp/tstunt
928         t-stunt-envvar PATH $tstunt
929         t-stunt-envvar PERLLIB $tstunt
930         local f
931         for f in "$@"; do
932                 t-tstunt--save-real $f
933                 f="./$f"
934                 local d="$tstunt/${f%/*}"
935                 mkdir -p $d
936                 ln -sf "$troot/tstunt/$f" "$d"/.
937         done
938 }
939
940 t-tstunt-parsechangelog () {
941         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
942 }
943
944 t-tstunt-lintian () {
945         t-tstunt lintian
946 }
947
948 t-tstunt-debuild () {
949         t-tstunt debuild
950 }
951
952 t-incoming-dsc () {
953         local dsc=${p}_${v}.dsc
954         incoming_dsc=$tmp/incoming/$dsc
955 }
956
957 t-ref-dsc-dgit () {
958         t-incoming-dsc
959         local val; val=`t-822-field $incoming_dsc Dgit`
960         val=$( perl -e '
961                 $_=shift @ARGV;
962                 die "Dgit $_ ?" unless m/^\w+\b/;
963                 print $&,"\n" or die $!;
964         ' "$val")
965         t-ref-same-val $incoming_dsc "$val"
966 }
967
968 t-apply-diff () {
969         local v1=$1
970         local v2=$2
971         (cd $troot/pkg-srcs;
972          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
973          | patch -p1 -u
974 }
975
976 t-gbp-unapplied-pq2qc () {
977         # does `gbp pq export'
978         # commits the resulting debian/patches on  qc/BRANCH
979         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
980         # qc/BRANCH is not fast-forwarding
981
982         gbp pq export
983
984         branch=`git symbolic-ref HEAD`
985         branch=${branch#refs/heads/}
986
987         case "$branch" in
988         */*) fail "unexpected branch $branch" ;;
989         esac
990
991         git branch -f qc/$branch
992         git checkout qc/$branch
993         git add debian/patches
994         git commit -m 'Commit patch queue'
995 }
996
997 t-git-pseudo-merge () {
998         # like   git merge -s ours
999         if [ ! "$git_pseuomerge_opts" ]; then
1000                 if git merge --help \
1001                  | grep -q allow-unrelated-histories; then
1002                         git_pseuomerge_opts='--allow-unrelated-histories'
1003                 fi
1004                 git_pseuomerge_opts+=' -s ours'
1005         fi
1006         git merge $git_pseuomerge_opts "$@"
1007 }
1008
1009 t-gbp-example-prep-no-ff () {
1010         t-tstunt-parsechangelog
1011         t-archive example 1.0-1
1012         t-git-none
1013         t-worktree 1.0
1014
1015         cd example
1016
1017         t-dgit fetch
1018
1019         git checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
1020         gbp pq rebase
1021
1022         echo '/* some comment */' >>src.c
1023         git add src.c
1024         git commit -m 'Add a comment to an upstream file'
1025
1026         t-gbp-unapplied-pq2qc
1027
1028         t-commit 'some updates' 1.0-2
1029 }
1030
1031 t-gbp-example-prep () {
1032         t-gbp-example-prep-no-ff
1033
1034         t-git-pseudo-merge \
1035                 -m 'Pseudo-merge to make descendant of archive' \
1036                 remotes/dgit/dgit/sid
1037 }
1038
1039 t-make-badcommit () {
1040         badcommit=$(
1041                 git cat-file commit HEAD | \
1042                 perl -pe 's/^committer /commiter /' | \
1043                 git hash-object -w -t commit --stdin
1044         )
1045         t-expect-fsck-fail $badcommit
1046 }
1047
1048 t-make-orig () {
1049         # leaves ust set to filename of orig tarball
1050         local p=$1
1051         local v=$2
1052         local tag=${3-v$2}
1053         ust=${p}_${v}.orig.tar.gz
1054         GZIP=-1 git archive -o $bpd/$ust --prefix=${p}-${v}/ $tag
1055 }
1056
1057 t-merge-conflicted-stripping-conflict-markers () {
1058         local otherbranch=$1
1059         local file=$2
1060
1061         t-expect-fail F:"Merge conflict in $file" \
1062         git merge $otherbranch
1063
1064         perl -i~ -ne 'print unless m{^(?:\<\<\<|\>\>\>|===)}' "$file"
1065         git add "$file"
1066         git commit --no-edit
1067 }
1068
1069 t-commit () {
1070         local msg=$1
1071         v=${2:-${majorv:-1}.$revision}
1072         $troot/tstunt/debchange \
1073                 --force-distribution -v$v --distribution ${3:-unstable} "$1"
1074         git add debian/changelog
1075         debcommit
1076         revision=$(( ${revision-0} + 1 ))
1077 }
1078
1079 t-dch-r-rune () {
1080         local cmd="$1"; shift
1081         local suite=${1-unstable}
1082         $cmd -r -D "$suite" ''
1083 }
1084
1085 t-dch-commit-r () {
1086         t-dch-r-rune t-dch-commit "$@"
1087 }
1088
1089 t-dch-commit () {
1090         $troot/tstunt/debchange "$@"
1091         git commit -m "dch $*" debian/changelog
1092 }
1093
1094 t-git-config () {
1095         git config --global "$@"
1096 }
1097
1098 t-drs () {
1099  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
1100  t-git-config dgit-distro.test-dummy.git-check true
1101  t-git-config dgit-distro.test-dummy.git-create true
1102  t-git-config dgit-distro.test-dummy.dgit-tag-format new,old,maint
1103         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
1104         cp $troot/suites $tmp/.
1105         cp $troot/suites $tmp/suites-master
1106
1107         export t_check_pushed_master=t-check-pushed-master
1108
1109         drs_dispatch=$tmp/distro=test-dummy
1110         mkdir $drs_dispatch
1111
1112         if [ "x$DGIT_TEST_INTREE" != x ]; then
1113                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
1114         fi
1115
1116         ln -sf $tmp/git $drs_dispatch/repos
1117         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
1118         mkdir -p $drs_dispatch/keyrings
1119         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
1120         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
1121         ln -sf /bin/true $drs_dispatch/policy-hook
1122 }
1123
1124 t-newtag () {
1125  export tagpfx=archive/test-dummy
1126  t-git-config dgit-distro.test-dummy.dgit-tag-format new,maint
1127 }
1128 t-oldtag () {
1129  export tagpfx=test-dummy
1130  t-git-config dgit-distro.test-dummy.dgit-tag-format old
1131 }
1132
1133 t-dsd () {
1134         t-drs
1135  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
1136  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
1137  t-git-config dgit-distro.test-dummy.git-create true
1138  t-git-config dgit-distro.test-dummy.git-url \
1139                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
1140
1141  t-git-config dgit-distro.test-dummy.diverts.drs /drs
1142  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
1143  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
1144  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
1145  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
1146
1147         echo 'no-such-package* drs' >$drs_dispatch/diverts
1148 }
1149
1150 t-policy-admin () {
1151         : '(((((((((((((((((((((((((((((((((((((((('
1152         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
1153         : '))))))))))))))))))))))))))))))))))))))))'
1154 }
1155
1156 t-policy-nonexist () {
1157         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
1158 }
1159
1160 t-make-hook-link () {
1161         local hook=$1 # in infra/
1162         local linkpath=$2
1163         hook=${DGIT_INFRA_PFX}$hook
1164         case $hook in
1165         */*)    ;;
1166         *)      hook=`type -P $hook` ;;
1167         esac
1168         ln -sf "$hook" $linkpath
1169 }
1170
1171 t-policy () {
1172         local policyhook=$1
1173         t-make-hook-link $policyhook $drs_dispatch/policy-hook
1174 }
1175
1176 t-debpolicy () {
1177         t-dsd
1178         t-policy dgit-repos-policy-debian
1179
1180         mkdir $tmp/git
1181         t-policy-admin create-db
1182 }
1183
1184 t-policy-periodic () {
1185         : '(((((((((((((((((((((((((((((((((((((((('
1186         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
1187                 test-dummy $drs_dispatch '' --cron
1188         : '))))))))))))))))))))))))))))))))))))))))'
1189 }
1190
1191 t-buildproductsdir-config () {
1192         bpd=$tmp/bpd
1193         t-git-config dgit.default.build-products-dir $bpd
1194         mkdir -p $bpd
1195         cat <<END >>$tmp/.gbp.conf
1196 [buildpackage]
1197 export-dir = $bpd
1198 END
1199 }
1200
1201 t-restrict () {
1202         local restriction=$1
1203         (cd $root; t-restriction-$restriction >&2)
1204 }
1205
1206 t-dependencies () {
1207         : "Hopefully installed: $*"
1208 }
1209
1210 t-chain-test () {
1211         local ct=$1
1212         local d=${0%/*}
1213         cd $root
1214         export DGIT_TEST_TESTNAME="$testname"
1215         export DGIT_TEST_TMPBASE="$tmpbase"
1216         export ADTTMP=$tmp
1217         exec "$d/$ct"
1218 }       
1219
1220 t-alt-test () {
1221         local t=${0##*/}
1222         t-${t%%-*}
1223         t-chain-test "${t#*-}"
1224 }
1225
1226 t-git-config dgit.default.old-dsc-distro test-dummy
1227
1228 for import in ${autoimport-gnupg}; do
1229         case "$0" in
1230         */$import) ;;
1231         *)
1232                 t-setup-import $import
1233                 ;;
1234         esac
1235 done