chiark / gitweb /
Tests: t-prep-newpackage: don't brokenly set v=3-1
[dgit.git] / tests / lib
1 #
2
3 exec 2>&1
4 set -x
5
6 . tests/lib-core
7
8 t-set-intree
9
10 : ${DGIT_TEST_DEBUG:=-D}
11 export DGIT_TEST_DEBUG
12
13 root=`pwd`
14 troot=$root/tests
15 testname="${DGIT_TEST_TESTNAME-${0##*/}}"
16
17 tmp=$ADTTMP
18 if [ x"$tmp" = x ]; then
19         mkdir -p tests/tmp
20         tmp=tests/tmp/$testname
21         rm -rf $tmp
22         mkdir $tmp
23 fi
24 cd $tmp
25
26 tmp=`pwd`
27 export HOME=$tmp
28 export DGIT_TEST_DUMMY_DIR=$tmp
29 ln -f $troot/ssh ssh
30
31 mkdir -p $tmp/gnupg
32 cp $troot/gnupg/* $tmp/gnupg
33 chmod go-rw $tmp/gnupg/*
34 export GNUPGHOME=$tmp/gnupg
35
36 mkdir -p $tmp/incoming
37 cat <<END >$tmp/dput.cf
38 [test-dummy]
39 method                  = local
40 incoming                = $tmp/incoming
41 run_dinstall            = 0
42 END
43
44 t-expect-fail () {
45         local mpat="$1"; shift
46         LC_MESSAGES=C "$@" 2>&1 | tee $tmp/t.output
47         test ${PIPESTATUS[0]} != 0
48         egrep "$mpat" $tmp/t.output ||false
49 }
50
51 t-untar () {
52         local tarfile=$1.tar
53         local edittree=$1.edit
54         if test -d "$edittree"; then
55                 cp -al "$edittree"/* .
56         else
57                 tar xf "$tarfile"
58         fi
59 }
60
61 t-worktree () {
62         rm -rf $p
63         t-untar $troot/worktrees/${p}_$1
64 }
65
66 t-git () {
67         p=$1
68         v=$2
69         mkdir -p $tmp/git
70         local gs=$troot/git-srcs/${p}_$v.git
71         (set -e; cd $tmp/git; t-untar $gs)
72 }
73
74 t-git-none () {
75         mkdir -p $tmp/git
76         (set -e; cd $tmp/git; tar xf $troot/git-template.tar)
77 }
78
79 t-has-ancestor () {
80         local now=`git rev-parse HEAD`
81         local ancestor=`git rev-parse $1^{}`
82         local mbase=`git merge-base $ancestor $now`
83         if [ x$mbase != x$ancestor ]; then
84                 fail "not ff $ancestor..$now, $mbase != $ancestor"
85         fi
86 }
87
88 t-prep-newpackage () {
89         p=$1
90         v=$2
91         t-archive-none $p
92         t-git-none
93         t-worktree $v
94         cd $p
95         git branch -m dgit/sid master
96         git remote rm dgit
97         cd ..
98 }
99
100 t-archive-none () {
101         p=$1
102         mkdir -p $tmp/aq $tmp/mirror/pool/main
103
104         local suite=sid
105
106         >$tmp/aq/package.$suite.$p
107         t-archive-updated $suite $p
108
109         >$tmp/aq/package.new.$p
110         t-archive-updated new $p
111
112         ln -s sid $tmp/aq/dsc_in_suite/unstable
113         cat <<'END' >$tmp/aq/suites
114 [
115    {
116       "archive" : "ftp-master",
117       "codename" : "sid",
118       "components" : [
119          "main",
120          "contrib",
121          "non-free"
122       ],
123       "name" : "unstable",
124       "dakname" : "unstable"
125    }
126 ]
127 END
128 }
129
130 t-archive-updated () {
131         local suite=$1
132         local p=$2
133         local suitedir=$tmp/aq/dsc_in_suite/$suite
134         mkdir -p $suitedir
135         perl <$tmp/aq/package.$suite.$p >$suitedir/$p -wne '
136                 use JSON;
137                 use strict;
138                 our @v;
139                 m{^(\S+) (\w+) ([^ \t/]+)/(\S+)} or die;
140                 push @v, {
141                         "version" => "$1",
142                         "sha256sum" => "$2",
143                         "component" => "$3",
144                         "filename" => "$4",
145                 };
146                 END {
147                         print to_json \@v or die $!;
148                 }
149         '
150 }
151
152 t-archive-process-incoming () {
153         mv incoming/${p}_${v}[._]* mirror/pool/main/
154         t-archive-query
155 }
156
157 t-archive-query () {
158         local dscf=main/${p}_${v}.dsc
159         local sha=`sha256sum <mirror/pool/$dscf`
160         echo "${v} ${sha%  -} $dscf" >>$tmp/aq/package.sid.${p}
161         t-archive-updated sid $p
162 }
163
164 t-archive () {
165         t-archive-none $1
166         v=$2
167         local dscf=${p}_$2.dsc
168         rm -f $tmp/mirror/pool/main/${p}_*
169         ln $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/pool/main/
170         t-archive-query
171         rm -rf $tmp/extract
172         mkdir $tmp/extract
173         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/pool/main/$dscf)
174 }
175
176 t-dgit () {
177         local dgit=${DGIT_TEST-dgit}
178         : '
179 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
180         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
181                 -dtest-dummy $DGIT_TEST_OPTS $DGIT_TEST_DEBUG \
182                 -k39B13D8A "$@"
183         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
184 '
185 }
186
187 t-diff-nogit () {
188         diff --exclude=.git --exclude=.pc -ruN $*
189 }
190
191 t-cloned-fetched-good () {
192         t-diff-nogit ../extract/$p-${v%-*} .
193         t-clean-on-branch dgit/sid
194         t-refs-same-start
195         t-refs-same \
196                 refs/heads/dgit/sid \
197                 refs/remotes/dgit/dgit/sid
198         t-refs-notexist dgit/unstable remotes/dgit/dgit/unstable
199 }
200
201 t-output () {
202         printf "%s\n" "$1" >$tmp/t.want
203         shift
204         "$@" >$tmp/t.got
205         diff $tmp/t.want $tmp/t.got
206 }
207
208 t-clean-on-branch () {
209         t-output "## $1" git status -b --porcelain
210 }
211
212 t-git-get-ref () {
213         local ref=$1
214         git show-ref -d $1 | perl -ne '
215                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
216                 END { print "$x\n" if length $x; }
217         '
218 }
219
220 t-ref-same () {
221         local name="$1"
222         local val=`t-git-get-ref $name`
223         t-ref-same-val "$name" $val
224 }
225
226 t-ref-head () {
227         local val=`git rev-parse HEAD`
228         t-ref-same-val HEAD $val
229 }
230
231 t-ref-same-val () {
232         local name="$1"
233         local val=$2
234         case "$t_ref_val" in
235         '')             ;;
236         "$val")         ;;
237         *)              fail "ref varies: $name: $val != $t_ref_val" ;;
238         esac
239         t_ref_val="$val"
240 }
241
242 t-refs-same-start () {
243         t_ref_val=''
244 }
245
246 t-refs-same () {
247         local g
248         for g in $*; do
249                 t-ref-same $g
250         done
251 }
252
253 t-refs-notexist () {
254         local val
255         for g in $*; do
256                 val=`t-git-get-ref $g >$tmp/t.refx`
257                 if [ "x$val" != x ]; then
258                         fail "ref $g unexpectedly exists ($val)"
259                 fi
260         done
261 }
262
263 t-v-tag () {
264         echo refs/tags/debian/${v//\~/_}
265 }
266
267 t-pushed-good () {
268         local branch=$1
269         t-ref-dsc-dgit
270         t-refs-same \
271                 refs/heads/$branch \
272                 `t-v-tag` \
273                 refs/remotes/dgit/dgit/sid
274         t-refs-notexist \
275                 refs/heads/dgit/unstable \
276                 refs/remotes/dgit/dgit/unstable
277         (set -e; cd $tmp/git/$p.git
278          t-refs-same \
279                 refs/dgit/sid \
280                 `t-v-tag`
281          t-refs-notexist \
282                 refs/dgit/unstable
283         )
284         git verify-tag `t-v-tag`
285 }
286
287 t-822-field () {
288         local file=$1
289         local field=$2
290         perl -e '
291                 use Dpkg::Control::Hash;
292                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
293                 $h->parse(\*STDIN,"'"$file"'");
294                 my $val = $h->{"'$field'"},"\n";
295                 die "'"$file $field"'" unless defined $val;
296                 print $val,"\n";
297         ' <$file
298 }
299
300 t-ref-dsc-dgit () {
301         local dsc=${p}_${v}.dsc
302         local val=`t-822-field $tmp/incoming/$dsc Dgit`
303         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
304         t-ref-same-val $dsc "$val"
305 }
306
307 t-apply-diff () {
308         local v1=$1
309         local v2=$2
310         (cd $troot/pkg-srcs; debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc) \
311                 | patch -p1 -u
312 }
313
314 t-git-config () {
315         git config --global "$@"
316 }
317
318 t-drs () {
319         export DGIT_TEST_TROOT=$troot
320         export DGIT_TEST_TMP=$tmp
321  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
322  t-git-config dgit-distro.test-dummy.git-check true
323  t-git-config dgit-distro.test-dummy.git-create true
324         cp $root/tests/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
325         cp $root/tests/suites $tmp/.
326
327         drs_dispatch=$tmp/distro=test-dummy
328         mkdir $drs_dispatch
329         ln -sf $root $drs_dispatch/dgit-live
330         ln -sf $tmp/git $drs_dispatch/repos
331         ln -sf $tmp/suites $tmp/dm.txt $drs_dispatch/
332         mkdir -p $drs_dispatch/keyrings
333         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
334         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
335         ln -sf /bin/true $drs_dispatch/policy-hook
336 }
337
338 t-dsd () {
339         t-drs
340  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
341  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
342  t-git-config dgit-distro.test-dummy.git-create true
343  t-git-config dgit-distro.test-dummy.git-url \
344                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
345
346  t-git-config dgit-distro.test-dummy.diverts.drs /drs
347  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
348  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
349  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
350  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
351
352         echo 'no-such-package* drs' >$drs_dispatch/diverts
353 }
354
355 t-policy-admin () {
356         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
357 }
358
359 t-debpolicy () {
360         t-dsd
361
362         ln -sf ${DGIT_INFRA_PFX}dgit-repos-policy-debian \
363                 $drs_dispatch/policy-hook
364
365         mkdir $tmp/git
366         t-policy-admin create-db
367 }
368
369 t-chain-test () {
370         local ct=$1
371         local d=${0%/*}
372         cd $root
373         export DGIT_TEST_TESTNAME="$testname"
374         export ADTTMP=$tmp
375         exec "$d/$ct"
376 }       
377
378 t-alt-test () {
379         local t=${0##*/}
380         t-${t%%-*}
381         t-chain-test "${t#*-}"
382 }