# Shell library for Morph yarns.
#
# The shell functions in this library are meant to make writing IMPLEMENTS
# sections for yarn scenario tests easier.
# Copyright (C) 2013-2015 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see .
# Add $SRCDIR to PYTHONPATH.
case "$PYTHONPATH" in
'') PYTHONPATH="$SRCDIR)" ;;
*) PYTHONPATH="$SRCDIR:$PYTHONPATH" ;;
esac
export PYTHONPATH
# Added until it's fixed in upstream.
# It's a solution to create an empty home directory each execution
export HOME="$DATADIR/home"
if [ ! -d "$HOME" ]
then
mkdir "$HOME"
fi
# Generating a default git user to run the tests
if ! test -r "$HOME/.gitconfig"
then
cat > "$HOME/.gitconfig" < "$DATADIR/result-$1" > "$DATADIR/out-$1"
local exit_code="$?"
for o in log result out; do
ln -sf "$o-$1" "$DATADIR/$o-latest"
done
cat "$DATADIR/out-$1"
cat "$DATADIR/result-$1" >&2
return "$exit_code"
}
}
# Sometimes we want to try running morph, but are OK if it fails, we just
# need to remember that it did.
attempt_morph()
{
if run_morph "$@"
then
echo 0 > "$DATADIR/morph-exit"
else
echo "$?" > "$DATADIR/morph-exit"
fi
}
# Perl's die() function is often very useful: it prints an error message
# and terminates the process with a non-zero exit code. Let's have a
# shell function to do that.
die()
{
echo "ERROR: $@" 1>&2
exit 1
}
# Sometimes it's nice to run a command in a different directory, without
# having to bother changing the directory before and after the command,
# or spawning subshells. This function helps with that.
run_in()
{
(cd "$1" && shift && exec "$@")
}
find_alias () {
ALIASES="$1"
WHICH="$2"
for alias in $ALIASES; do
alias=$(echo $alias | sed -e's/,$//')
prefix=$(echo $alias | cut -d= -f1)
if test "x$WHICH" = "x$prefix"; then
echo $alias
exit 0
fi
done
}
start_cache_server(){
mkfifo "$1"
start-stop-daemon --start --pidfile="$2" \
--background --make-pidfile --verbose \
--startas="$SRCDIR/morph-cache-server" -- \
--port-file="$1" --no-fcgi \
--repo-dir="$DATADIR/gits" --direct-mode \
--bundle-dir="$DATADIR/bundles" \
--artifact-dir="$3" "$@"
port="$(cat "$1")"
rm "$1"
echo "$port" >"$1"
}
stop_daemon(){
if [ -e "$1" ]; then
start-stop-daemon --stop --pidfile "$1" --oknodo
fi
}