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