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