summaryrefslogtreecommitdiff
path: root/t/test-lib.sh
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2011-08-18 05:01:15 (GMT)
committerJunio C Hamano <gitster@pobox.com>2011-08-18 21:08:54 (GMT)
commitd960c47a881c613a71b1b7c8aab3a06fd91ca70a (patch)
tree507f3e910c08b707605463fb3fe197c0880eb4ef /t/test-lib.sh
parent212ad944209a928dbf79faa124de77be5c84532e (diff)
downloadgit-d960c47a881c613a71b1b7c8aab3a06fd91ca70a.zip
git-d960c47a881c613a71b1b7c8aab3a06fd91ca70a.tar.gz
git-d960c47a881c613a71b1b7c8aab3a06fd91ca70a.tar.bz2
test-lib: add helper functions for config
There are a few common tasks when working with configuration variables in tests; this patch aims to make them a little easier to write and less error-prone. When setting a variable, you should typically make sure to clean it up after the test is finished, so as not to pollute other tests. Like: test_when_finished 'git config --unset foo.bar' && git config foo.bar baz This patch lets you just write: test_config foo.bar baz When clearing a variable that does not exist, git-config will report a specific non-zero error code. Meaning that tests which call "git config --unset" often either rely on the prior tests having actually set it, or must use test_might_fail. With this patch, the previous: test_might_fail git config --unset foo.bar becomes: test_unconfig foo.bar Not only is this easier to type, but it is more robust; it will correctly detect errors from git-config besides "key was not set". Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 't/test-lib.sh')
-rw-r--r--t/test-lib.sh18
1 files changed, 18 insertions, 0 deletions
diff --git a/t/test-lib.sh b/t/test-lib.sh
index df25f17..395bf60 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -357,6 +357,24 @@ test_chmod () {
git update-index --add "--chmod=$@"
}
+# Unset a configuration variable, but don't fail if it doesn't exist.
+test_unconfig () {
+ git config --unset-all "$@"
+ config_status=$?
+ case "$config_status" in
+ 5) # ok, nothing to unset
+ config_status=0
+ ;;
+ esac
+ return $config_status
+}
+
+# Set git config, automatically unsetting it after the test is over.
+test_config () {
+ test_when_finished "test_unconfig '$1'" &&
+ git config "$@"
+}
+
# Use test_set_prereq to tell that a particular prerequisite is available.
# The prerequisite can later be checked for in two ways:
#