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