summaryrefslogtreecommitdiff
path: root/yarns/morph.shell-lib
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2013-07-29 14:15:11 +0000
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2013-07-30 17:10:08 +0000
commitfac47acd5ab8e3410929e8c36ceaa4ecf070de10 (patch)
treea880bdeeffc2d62804b97e85551132319258dae9 /yarns/morph.shell-lib
parentb36072a5af5947e5dbb73395d821f1a57e0ef773 (diff)
downloadmorph-fac47acd5ab8e3410929e8c36ceaa4ecf070de10.tar.gz
Add scenario (yarn) tests for most of branching and merging
These scenarios test the basics of most of the subcommands the branch and merge plugin provides. They don't purport to be complete, but give some indication that things work, and form a basis upon which further things can be built. Yarn also isn't included in a Baserock release yet, so we need to keep the cmdtests until Baserock 10 has been released. The existing cmdtest tests are not modified by this: they are left intact, until they can analysed in detail for things to be added to the scenarios. After that, the cmdtest tests will start to go away. Merging is not covered by these tests: it is not clear how merge should work, and the current code is known to do the wrong thing in many cases. Scenarios for merge will be added later. Building is also not covered. Testing builds well needs some additional, careful thinking, and that isn't ready for this patch series. It will be added later.
Diffstat (limited to 'yarns/morph.shell-lib')
-rw-r--r--yarns/morph.shell-lib153
1 files changed, 153 insertions, 0 deletions
diff --git a/yarns/morph.shell-lib b/yarns/morph.shell-lib
new file mode 100644
index 00000000..f0a9c11b
--- /dev/null
+++ b/yarns/morph.shell-lib
@@ -0,0 +1,153 @@
+# 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 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, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+# Run Morph from the source tree, ignoring any configuration files.
+# This way the test suite is not affected by any configuration the user
+# or system may have. Instead, we'll use the `$DATADIR/morph.conf` file,
+# which tests can create, if they want to. Unfortunately, currently yarn
+# does not set a $SRCDIR that points at the source tree, so if the test
+# needs to cd away from there, things can break. We work around this
+# by allowing the caller to set $SRCDIR if they want to, and if it isn't
+# set, we default to . (current working directory).
+
+run_morph()
+{
+ "${SRCDIR:-.}"/morph \
+ --no-default-config --config "$DATADIR/morph.conf" "$@"
+}
+
+
+# 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
+}
+
+
+# Tests often need to check that specific files or directories exist
+# and have the right ownerships etc. Here's some shell functions to
+# test that kind of thing.
+
+is_dir()
+{
+ if [ ! -d "$1" ]
+ then
+ die "Expected $1 to be a directory"
+ fi
+}
+
+is_file()
+{
+ if [ ! -f "$1" ]
+ then
+ die "Expected $1 to be a regular file"
+ fi
+}
+
+
+# General assertions.
+
+assert_equal()
+{
+ case "$1" in
+ "$2") ;;
+ *) die "Expected '$1' and '$2' to be equal" ;;
+ esac
+}
+
+
+# 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 "$@")
+}
+
+
+# Extract all refs in all given morphologies. Each ref is reported
+# as filename:ref. The referred-to repository is not listed.
+
+list_refs()
+{
+ awk '/ ref: / { printf "%s %s\n", FILENAME, $NF }' "$@"
+}
+
+
+# Is a ref petrified? Or a specific branch?
+
+is_petrified_or_branch()
+{
+ if echo "$1" |
+ awk -v "branch=$2" '$NF ~ /[0-9a-fA-F]{40}/ || $NF == branch' |
+ grep .
+ then
+ return 0
+ else
+ return 1
+ fi
+}
+
+
+# Are named morphologies petrified? Die if not. First arg is the
+# branch that is allowed in addition to SHA1s.
+
+assert_morphologies_are_petrified()
+{
+ local branch="$1"
+ shift
+ list_refs "$@" |
+ while read filename ref
+ do
+ if ! is_petrified_or_branch "$ref" "$branch"
+ then
+ die "Found non-SHA1 ref in $filename: $ref"
+ fi
+ done
+}
+
+
+# Currently, yarn isn't setting $SRCDIR to point at the project source
+# directory. We simulate this here.
+
+if ! env | grep '^SRCDIR=' > /dev/null
+then
+ export SRCDIR="$(pwd)"
+fi