chiark / gitweb /
Test suite: Provide t-setup-* machinery
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 7 Aug 2016 19:19:35 +0000 (20:19 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 5 Sep 2016 12:41:48 +0000 (13:41 +0100)
Setups are, essentially, prefixes to test cases.  We arrange to be
able to reuse their state (as explicitly recorded in t-setup-done), so
that manual runs of the test suite can be faster.

We save the tmp base directory (the directory shared by tests), if
there is one.  That will be used for cacheing of setups.  And we save
the environment in form we can use xargs and env to restore.

We use a conventional with-lock-ex and stamp file pattern for the
cache.  The IMPORT file (containing shell variables) doubles as the
stamp.

The setup execution rune is exciting.  This mostly results from the
fact that we want xargs to pass all the env vars to env, and that
xargs cannot be persuaded to append fixed arguments to the arguments
read from its input file.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
tests/lib

index cebe10d9452e79d649662f42404f3b80847c0152..81accebf0f6f11d53639af725298416606102e33 100644 (file)
--- a/tests/lib
+++ b/tests/lib
@@ -36,6 +36,7 @@ testname="${DGIT_TEST_TESTNAME-${0##*/}}"
 tmp=$ADTTMP
 if [ x"$tmp" = x ]; then
        mkdir -p tests/tmp
+       tmpbase=$troot/tmp
        tmp=tests/tmp/$testname
        rm -rf $tmp
        mkdir $tmp
@@ -46,6 +47,8 @@ tmp=`pwd`
 
 t-set-using-tmp
 
+env -0 >$tmp/.save-env
+
 ln -f $troot/ssh ssh
 
 mkdir -p $tmp/gnupg
@@ -371,6 +374,60 @@ t-clean-on-branch () {
        t-output "## $1" git status -b --porcelain
 }
 
+t-setup-done () {
+       local savevars=$1
+       local savedirs=$2
+       local importeval=$3
+
+       exec 4>$tmp/IMPORT.new
+
+       local vn
+       for vn in $savevars; do
+               perl >&4 -I. -MDebian::Dgit -e '
+                       printf "%s=%s\n", $ARGV[0], shellquote $ARGV[1]
+               ' $vn "$(eval "printf '%s\n' \"\$$vn\"")"
+       done
+
+       (set -e; cd $tmp; tar cf IMPORT.tar $savedirs)
+
+       printf >&4 "\n%s\n" "$importeval"
+
+       mv -f $tmp/IMPORT.new $tmp/IMPORT
+}
+
+t-setup-import () {
+       local setupname=$1
+
+       if [ "x$tmpbase" = x ]; then
+               # ADTTMP was set on entry to tests/lib, so we
+               # are not sharing tmp area between tests
+               setupsrc="$tmp"
+       else
+               setupsrc="$tmpbase/$setupname"
+       fi
+
+       local simport="$setupsrc/IMPORT"
+
+       if ! [ -e "$simport" ]; then
+               with-lock-ex -w "$simport.lock" \
+               xargs -0 -a $tmp/.save-env \
+               bash -xec '
+                       cd "$1"; shift
+                       setupname="$1"; shift
+                       simport="$1"; shift
+                       if [ -e "$simport" ]; then exit 0; fi
+                       env - "$@" \
+                       "tests/setup/$setupname"
+               ' x "$root" "$setupname" "$simport"
+       fi
+
+       if [ "x$setupsrc" != "x$tmp" ]; then
+               (set -e; cd $tmp; tar xf "$simport.tar")
+       fi
+
+       . "$simport"
+}
+
 t-git-get-ref-exact () {
        local ref=$1
        # does not dereference, unlike t-git-get-ref