Skip to content

Commit d960c47

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 212ad94 commit d960c47

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

t/test-lib.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,24 @@ test_chmod () {
357357
git update-index --add "--chmod=$@"
358358
}
359359

360+
# Unset a configuration variable, but don't fail if it doesn't exist.
361+
test_unconfig () {
362+
git config --unset-all "$@"
363+
config_status=$?
364+
case "$config_status" in
365+
5) # ok, nothing to unset
366+
config_status=0
367+
;;
368+
esac
369+
return $config_status
370+
}
371+
372+
# Set git config, automatically unsetting it after the test is over.
373+
test_config () {
374+
test_when_finished "test_unconfig '$1'" &&
375+
git config "$@"
376+
}
377+
360378
# Use test_set_prereq to tell that a particular prerequisite is available.
361379
# The prerequisite can later be checked for in two ways:
362380
#

0 commit comments

Comments
 (0)