chiark / gitweb /
60abb90945611d440f25f61a8d8682fb8a076746
[dgit.git] / tests / lib
1 #
2
3 exec 2>&1
4 set -x
5
6 root=`pwd`
7 troot=$root/tests
8 testname="${DGIT_TEST_TESTNAME-${0##*/}}"
9
10 tmp=$ADTTMP
11 if [ x"$tmp" = x ]; then
12         mkdir -p tests/tmp
13         tmp=tests/tmp/$testname
14         rm -rf $tmp
15         mkdir $tmp
16 fi
17 cd $tmp
18 export HOME=$tmp
19
20 tmp=`pwd`
21 export DGIT_TEST_DUMMY_DIR=$tmp
22 ln -f $troot/ssh ssh
23
24 mkdir -p $tmp/gnupg
25 cp $troot/gnupg/* $tmp/gnupg
26 chmod go-rw $tmp/gnupg/*
27 export GNUPGHOME=$tmp/gnupg
28
29 mkdir -p $tmp/incoming
30 cat <<END >$tmp/dput.cf
31 [test-dummy]
32 method                  = local
33 incoming                = $tmp/incoming
34 run_dinstall            = 0
35 END
36
37 fail () {
38         echo >&2 "failed: $*"
39         exit 1
40 }
41
42 t-worktree () {
43         rm -rf $p
44         tar xf $troot/worktrees/${p}_$1.tar
45 }
46
47 t-git () {
48         p=$1
49         v=$2
50         mkdir -p $tmp/git
51         (set -e; cd $tmp/git; tar xf $troot/git-srcs/${p}_$v.git.tar)
52 }
53
54 t-git-none () {
55         mkdir -p $tmp/git
56         (set -e; cd $tmp/git; tar xf $troot/git-template.tar)
57 }
58
59 t-has-ancestor () {
60         local now=`git rev-parse HEAD`
61         local ancestor=`git rev-parse $1^{}`
62         local mbase=`git merge-base $ancestor $now`
63         if [ x$mbase != x$ancestor ]; then
64                 fail "not ff $ancestor..$now, $mbase != $ancestor"
65         fi
66 }       
67
68 t-archive-none () {
69         p=$1
70         mkdir -p $tmp/aq $tmp/mirror
71         echo sid >$tmp/aq/suite.unstable
72 }
73
74 t-archive () {
75         t-archive-none $1
76         v=$2
77         local dscf=${p}_$2.dsc
78         rm -f $tmp/mirror/${p}_*
79         ln $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/
80         echo "$2 $dscf" >>$tmp/aq/package.sid.${p}
81         rm -rf $tmp/extract
82         mkdir $tmp/extract
83         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/$dscf)
84 }
85
86 t-dgit () {
87         : '
88
89 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
90         ${DGIT_TEST-dgit} --dget:-u --dput:--config=$tmp/dput.cf \
91                 -dtest-dummy $DGIT_TEST_OPTS -D -k39B13D8A "$@"
92         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
93 '
94 }
95
96 t-diff-nogit () {
97         diff --exclude=.git -ruN $*
98 }
99
100 t-cloned-fetched-good () {
101         t-diff-nogit ../extract/$p-${v%-*} .
102         t-clean-on-branch dgit/sid
103         t-refs-same-start
104         t-refs-same \
105                 refs/heads/dgit/sid \
106                 refs/remotes/dgit/dgit/sid
107         t-refs-notexist dgit/unstable remotes/dgit/dgit/unstable
108 }
109
110 t-output () {
111         printf "%s\n" "$1" >$tmp/t.want
112         shift
113         "$@" >$tmp/t.got
114         diff $tmp/t.want $tmp/t.got
115 }
116
117 t-clean-on-branch () {
118         t-output "## $1" git status -b --porcelain
119 }
120
121 t-git-get-ref () {
122         local ref=$1
123         git show-ref -d $1 | perl -ne '
124                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
125                 END { print "$x\n" if length $x; }
126         '
127 }
128
129 t-ref-same () {
130         local name="$1"
131         local val=`t-git-get-ref $name`
132         t-ref-same-val "$name" $val
133 }
134
135 t-ref-head () {
136         local val=`git rev-parse HEAD`
137         t-ref-same-val HEAD $val
138 }
139
140 t-ref-same-val () {
141         local name="$1"
142         local val=$2
143         case "$t_ref_val" in
144         '')             ;;
145         "$val")         ;;
146         *)              fail "ref varies: $name: $val != $t_ref_val" ;;
147         esac
148         t_ref_val="$val"
149 }
150
151 t-refs-same-start () {
152         t_ref_val=''
153 }
154
155 t-refs-same () {
156         local g
157         for g in $*; do
158                 t-ref-same $g
159         done
160 }
161
162 t-refs-notexist () {
163         local val
164         for g in $*; do
165                 val=`t-git-get-ref $g >$tmp/t.refx`
166                 if [ "x$val" != x ]; then
167                         fail "ref $g unexpectedly exists ($val)"
168                 fi
169         done
170 }
171
172 t-v-tag () {
173         echo refs/tags/debian/${v//\~/_}
174 }
175
176 t-pushed-good () {
177         local branch=$1
178         t-ref-dsc-dgit
179         t-refs-same \
180                 refs/heads/$branch \
181                 `t-v-tag` \
182                 refs/remotes/dgit/dgit/sid
183         t-refs-notexist \
184                 refs/heads/dgit/unstable \
185                 refs/remotes/dgit/dgit/unstable
186         (set -e; cd $tmp/git/$p.git
187          t-refs-same \
188                 refs/dgit/sid \
189                 `t-v-tag`
190          t-refs-notexist \
191                 refs/dgit/unstable
192         )
193         git verify-tag `t-v-tag`
194 }
195
196 t-822-field () {
197         local file=$1
198         local field=$2
199         perl -e '
200                 use Dpkg::Control::Hash;
201                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
202                 $h->parse(\*STDIN,"'"$file"'");
203                 my $val = $h->{"'$field'"},"\n";
204                 die "'"$file $field"'" unless defined $val;
205                 print $val,"\n";
206         ' <$file
207 }
208
209 t-ref-dsc-dgit () {
210         local dsc=${p}_${v}.dsc
211         local val=`t-822-field $tmp/incoming/$dsc Dgit`
212         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
213         t-ref-same-val $dsc "$val"
214 }
215
216 t-apply-diff () {
217         local v1=$1
218         local v2=$2
219         (cd $troot/pkg-srcs; debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc) \
220                 | patch -p1 -u
221 }
222
223 t-git-config () {
224         git config --global "$@"
225 }
226
227 t-drs () {
228         export DGIT_TEST_TROOT=$troot
229         export DGIT_TEST_TMP=$tmp
230  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
231  t-git-config dgit-distro.test-dummy.git-check true
232  t-git-config dgit-distro.test-dummy.git-create true
233 }
234
235 t-drs-test () {
236         t-drs
237         cd $root
238         export DGIT_TEST_TESTNAME="$testname"
239         export ADTTMP=$tmp
240         exec "${0///drs-//}" "$@"
241 }