1# -*- mode: sh; coding: utf-8 -*-
2# Various functions for tla testsuite
3# Copyright © 2003 Colin Walters <walters@verbum.org>
4#
5# See the file "COPYING" for further information about
6# the copyright and warranty status of this work.
7
8set -e
9
10builddir=$(pwd)
11
12TESTNUM=1
13
14verbose () {
15  if test -n "$TESTS_VERBOSE"; then
16    echo "$@" >&2
17  fi
18}
19
20verbosecmd () {
21  if test -n "$TESTS_VERBOSE"; then
22    echo "=>" "$@" >&2
23  fi
24}
25
26tla () {
27  verbosecmd "${builddir}/../tla/tla" "$@"
28  ${builddir}/../tla/tla "$@"
29}
30
31test_class () {
32  echo ""
33  echo "======= TESTING: $@ =======" >&2
34  if test -z "$TESTS_VERBOSE"; then
35    exec 1>/dev/null
36  fi
37}
38
39begin_test_savectx () {
40  echo "Test $TESTNUM: $@" >&2
41}
42
43begin_test () {
44  begin_test_savectx "$@"
45  _begin_test_dir="$(pwd)"
46  mkdir test-$TESTNUM-workdir
47  cd test-$TESTNUM-workdir
48}
49
50end_test_savectx () {
51  echo "Test $TESTNUM: PASS" >&2
52  TESTNUM=$(($TESTNUM + 1))
53}
54
55end_test () {
56  end_test_savectx "$@"
57  cd "${_begin_test_dir}"
58  rm -rf test-$TESTNUM-workdir
59}
60
61test_fail () {
62  # Failures display error message and exit
63  # usage: test_fail "where: why"
64  echo "Test $TESTNUM: FAILED: $@" >&2
65  exit 1
66}
67
68# Is there a portable way to do this?
69# trap "test_fail" ERR
70
71clean_workdir () {
72  verbose '! cleaning workdir'
73  rm -rf archivedir
74  rm -rf workdir
75  rm -rf homedir
76}
77
78setup_workdir () {
79  clean_workdir
80  verbose '! setting up workdir'
81  mkdir archivedir
82  ARCHIVEDIR=$(pwd)/archivedir
83  export ARCHIVEDIR
84  mkdir workdir
85  WORKDIR=$(pwd)/workdir
86  export WORKDIR
87  mkdir homedir
88  HOME=$(pwd)/homedir  # we do this so we don't mess with the user's archives
89  export HOME
90  cd workdir
91  cd $(if test -x /bin/pwd; then /bin/pwd; else pwd; fi)  # canonicalize
92}
93
94initial_setup () {
95  setup_workdir
96  verbose '! doing initial setup'
97  tla my-id 'Jane Doe <jane@example.com>'
98}
99
100setup_with_empty_archives () {
101  initial_setup
102  verbose '! creating empty archives'
103  tla make-archive --baz jane@example.com--2003 ${ARCHIVEDIR}/jane-archive
104  tla make-archive --baz foo@example.org--2003 ${ARCHIVEDIR}/foo-archive
105}
106
107setup_with_empty_tla_archives () {
108  initial_setup
109  verbose '! creating empty archives'
110  tla make-archive --tla jane@example.com--2003 ${ARCHIVEDIR}/jane-archive
111  tla make-archive --tla foo@example.org--2003 ${ARCHIVEDIR}/foo-archive
112}
113
114setup_with_setup_archives () {
115  initial_setup
116  verbose '! creating and setting up archives'
117  tla make-archive --baz jane@example.com--2003 ${ARCHIVEDIR}/jane-archive 1>/dev/null
118  tla make-archive --baz foo@example.org--2003 ${ARCHIVEDIR}/foo-archive 1>/dev/null
119}
120
121setup_with_setup_tla_archives () {
122  initial_setup
123  verbose '! creating and setting up archives'
124  tla make-archive --tla jane@example.com--2003 ${ARCHIVEDIR}/jane-archive 1>/dev/null
125  tla archive-setup jane@example.com--2003/hello-world--mainline--1.0 1>/dev/null
126  tla archive-setup jane@example.com--2003/cat--bar--1.1 1>/dev/null
127  tla archive-setup jane@example.com--2003/cow--baz--2.3 1>/dev/null
128  tla make-archive --tla foo@example.org--2003 ${ARCHIVEDIR}/foo-archive 1>/dev/null
129  tla archive-setup foo@example.org--2003/hello-world--devo--1.3 1>/dev/null
130}
131
132make_hello_world () {
133  dir=$1
134  shift
135  mkdir "$dir"
136  (echo '#include <stdio.h>'; echo '#include <stdlib.h>'; echo 'int main(int argc, char **argv) {';
137   echo '  printf("Hello world!");'; echo 'exit (0);'; echo '}') > "${dir}"/hello-world.c
138  echo 'This is hello-world, 1.0' > "${dir}"/README
139}
140
141setup_with_trivial_archives () {
142  tagging_method=$1
143  if test -z "${tagging_method}"; then
144    tagging_method=explicit
145  fi
146  setup_with_setup_archives
147  verbose '! doing initial imports'
148  make_hello_world tmp-tree
149  cd tmp-tree
150  tla init-tree --nested jane@example.com--2003/hello-world--mainline--1.0
151  tla id-tagging-method "${tagging_method}" 1>/dev/null
152  tla add-id hello-world.c README
153  tla import -S -L 'initial import' 1>/dev/null
154  cd ..
155  rm -rf tmp-tree
156}
157
158setup_with_trivial_tla_archives () {
159  tagging_method=$1
160  if test -z "${tagging_method}"; then
161    tagging_method=explicit
162  fi
163  setup_with_setup_tla_archives
164  verbose '! doing initial imports'
165  make_hello_world tmp-tree
166  cd tmp-tree
167  tla init-tree --nested jane@example.com--2003/hello-world--mainline--1.0
168  tla id-tagging-method "${tagging_method}" 1>/dev/null
169  tla add-id hello-world.c README
170  tla import -L 'initial import' 1>/dev/null
171  cd ..
172  rm -rf tmp-tree
173}
174
175setup_with_somewhat_interesting_explicit_archives () {
176  setup_with_setup_archives
177
178  verbose '! doing initial imports'
179  make_hello_world tmp-tree
180  cd tmp-tree
181
182  tla init-tree --nested jane@example.com--2003/hello-world--mainline--1.0
183  tla id-tagging-method explicit 1>/dev/null
184
185  mkdir new_directory old_directory
186  ln -s old_directory link_to_old_directory
187  ln -s new_directory link_to_new_directory
188
189  cp hello-world.c goodbye-world.c
190  ln -s README link_to_README
191  ln -s hello-world.c link_to_hello-world.c
192  ln -s goodbye-world.c link_to_goodbye-world.c
193
194  mkdir -p hierarchy
195  mkdir -p hierarchy/left/left hierarchy/left/right
196  mkdir -p hierarchy/right/left hierarchy/right/right
197  tla add-id $(find hierarchy -type d)
198
199  tla add-id new_directory old_directory link_to_old_directory link_to_new_directory
200  tla add-id hello-world.c goodbye-world.c link_to_hello-world.c link_to_goodbye-world.c README link_to_README
201  tla import -S -L 'initial import' 1>/dev/null
202
203  cd ..
204  rm -rf tmp-tree
205}
206
207setup_with_somewhat_interesting_explicit_tla_archives () {
208  setup_with_setup_tla_archives
209
210  verbose '! doing initial imports'
211  make_hello_world tmp-tree
212  cd tmp-tree
213
214  tla init-tree --nested jane@example.com--2003/hello-world--mainline--1.0
215  tla id-tagging-method explicit 1>/dev/null
216
217  mkdir new_directory old_directory
218  ln -s old_directory link_to_old_directory
219  ln -s new_directory link_to_new_directory
220
221  cp hello-world.c goodbye-world.c
222  ln -s README link_to_README
223  ln -s hello-world.c link_to_hello-world.c
224  ln -s goodbye-world.c link_to_goodbye-world.c
225
226  mkdir -p hierarchy
227  mkdir -p hierarchy/left/left hierarchy/left/right
228  mkdir -p hierarchy/right/left hierarchy/right/right
229  tla add-id $(find hierarchy -type d)
230
231  tla add-id new_directory old_directory link_to_old_directory link_to_new_directory
232  tla add-id hello-world.c goodbye-world.c link_to_hello-world.c link_to_goodbye-world.c README link_to_README
233  tla import -L 'initial import' 1>/dev/null
234
235  cd ..
236  rm -rf tmp-tree
237}
238
239### Utility functions
240
241copy_tree () {
242  tar_prog=$("$srcroot/build-tools/scripts/option" gnu-tar)
243  export tar_prog
244  (cd $1 ; "$tar_prog" cf - .) | (mkdir -p $2 ; cd $2 ; "$tar_prog" xf -)
245}
246
247compare_trees_simple () {
248  verbosecmd 'diff -uNr -x ,*' "$@"
249  diff -uNr -x ',*' "$@"
250}
251
252assert_trees_equal () {
253  output=",what-changed-$$"
254  tla mkpatch "$1" "$2" "${output}"
255  changeset_is_null_changeset "${output}"
256}
257
258assert_working_tree_equivalence () {
259  diff -uNr -x ',*' -x '{arch}' "$@"
260}
261
262dir_exists () {
263  verbosecmd test -d "$1"
264  test -d "$1"
265}
266
267file_exists () {
268  verbosecmd test -f "$1"
269  test -f "$1"
270}
271
272file_is_empty () {
273  f=$1
274  shift
275  verbosecmd "test -f $f && test ! -s $f"
276  test -f "$f" && test ! -s "$f"
277}
278
279dir_is_empty () {
280  d=$1
281  shift
282  verbosecmd 'test -z $(ls ' "$d"')'
283  test -z "$(ls "$d")"
284}
285
286file_matches () {
287  verbosecmd egrep "$1" "$2"
288  egrep "$1" "$2" 1>/dev/null
289}
290
291archive_has_revision_with_summary () {
292  archive=$1
293  revision=$2
294  summary=$3
295  location=$(tla whereis-archive "$archive")
296  category=$(tla parse-package-name -c $revision)
297  branch=$(tla parse-package-name -b $revision)
298  version=$(tla parse-package-name -v $revision)
299  patchlvl=$(tla parse-package-name -l $revision)
300  filename="${location}/${category}--${branch}--${version}/${patchlvl}/log"
301  file_exists "${filename}"
302  file_matches '^Summary: '"${summary}" "${filename}"
303}
304
305missing_patches_are () {
306  archive=$1
307  shift
308  tla whats-missing "${archive}" >,whats-missing
309  for patch in "$@"; do
310    sed -e "/$patch/d" <,whats-missing >,whats-missing.new
311    mv ,whats-missing.new ,whats-missing
312  done
313  file_is_empty ,whats-missing
314}
315
316changeset_is_null_changeset () {
317  cset=$1
318  shift
319  verbose "testing for null changeset $cset"
320  file_is_empty "${cset}/mod-dirs-index"
321  file_is_empty "${cset}/mod-files-index"
322  file_is_empty "${cset}/modified-only-dir-metadata"
323  dir_is_empty  "${cset}/new-files-archive"
324  file_is_empty "${cset}/modified-only-dir-metadata"
325  dir_is_empty  "${cset}/patches"
326  dir_is_empty  "${cset}/removed-files-archive"
327}
328
329changeset_patches_file () {
330  cset=$1
331  shift
332  file=$1
333  shift
334  verbosecmd "${cset}/patches/${file}.patch"
335  test -f "${cset}/patches/${file}.patch"
336}
337
338changeset_adds_file () {
339  cset=$1
340  shift
341  file=$1
342  shift
343  verbosecmd test -f "${cset}/new-files-archive/${file}"
344  test -f "${cset}/new-files-archive/${file}"
345}
346
347changeset_deletes_file () {
348  cset=$1
349  shift
350  file=$1
351  shift
352  verbosecmd test -f "${cset}/removed-files-archive/${file}"
353  test -f "${cset}/removed-files-archive/${file}"
354}
355
356changeset_renames_file () {
357  cset=$1
358  shift
359  origfile=$1
360  shift
361  newfile=$1
362  shift
363  verbosecmd egrep "^${origfile}" "${cset}/orig-files-index"
364  egrep "^\./${origfile}" "${cset}/orig-files-index" 1>/dev/null
365  verbosecmd egrep "^${newfile}" "${cset}/mod-files-index"
366  egrep "^\./${newfile}" "${cset}/mod-files-index" 1>/dev/null
367}
368
369# tag: Colin Walters Tue, 16 Sep 2003 20:48:33 -0400 (test-framework.sh)
370#
371