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