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