From 441cf15ffbafea7aa6be4cc04bb0085c8d79063e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 7 Aug 2016 20:19:35 +0100 Subject: [PATCH] Test suite: Provide t-setup-* machinery 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 --- tests/lib | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/lib b/tests/lib index cebe10d9..81accebf 100644 --- 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 -- 2.30.2