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