aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/test_util.fish
diff options
context:
space:
mode:
authorGravatar Kevin Ballard <kevin@sb.org>2014-10-27 19:42:19 -0700
committerGravatar Kevin Ballard <kevin@sb.org>2014-11-24 01:51:07 -0800
commite13d423b680015a75371f849b8f4bd57f5f7663c (patch)
tree1915e6c43e220faa916970bd485b382fda56c7bf /tests/test_util.fish
parenteafd5776292c37d37870fc6013029f7146f34f70 (diff)
Tweak test runner to set up environment better
Update the test runners so they set up their own environment in test_util.fish. This simplifies the Makefile and paves the way for adding utility functions for use in the tests themselves.
Diffstat (limited to 'tests/test_util.fish')
-rw-r--r--tests/test_util.fish78
1 files changed, 66 insertions, 12 deletions
diff --git a/tests/test_util.fish b/tests/test_util.fish
index 659cbda4..2aef2b98 100644
--- a/tests/test_util.fish
+++ b/tests/test_util.fish
@@ -1,18 +1,73 @@
# Utilities for the test runners
+if test "$argv[1]" = (status -f)
+ echo 'test_util.fish requires sourcing script as argument to `source`' >&2
+ echo 'use `source test_util.fish (status -f); or exit`' >&2
+ status --print-stack-trace >&2
+ exit 1
+end
+
function die
- echo $argv[1] >&2
+ set -q argv[1]; and echo $argv[1] >&2
exit 1
end
-if not tty 0>&1 >/dev/null
- function set_color
- # do nothing
- return 0
+# check if we're running in the test environment
+# if not, set it up and rerun fish with exec.
+# The test is whether the special var __fish_is_running_tests
+# exists and contains the same value as XDG_CONFIG_HOME. It checks
+# the value and not just the presence because we're going to delete
+# the config directory later if we're exiting successfully.
+if not set -q __fish_is_running_tests
+ # set up our test environment and re-run the original script
+ set -l script $argv[1]
+ switch $script
+ case '/*'
+ # path is absolute
+ case '*'
+ # path is relative, make it absolute
+ set script $PWD/$script
+ end
+ set -l IFS # clear IFS so cmd substitution doesn't split
+ cd (dirname $script); or die
+ set -xl XDG_CONFIG_HOME (printf '%s/tmp.config/%s' $PWD %self); or die
+ if test -d $XDG_CONFIG_HOME
+ # if the dir already exists, we've reused a pid
+ # this would be surprising to reuse a fish pid that failed in the past,
+ # but clear it anyway
+ rm -r $XDG_CONFIG_HOME; or die
end
+ mkdir -p $XDG_CONFIG_HOME/fish; or die
+ set -l escaped_parent (dirname $PWD | sed -e 's/[\'\\\\]/\\\\&/g'); or die
+ printf 'set fish_function_path \'%s/share/functions\'\n' $escaped_parent > $XDG_CONFIG_HOME/fish/config.fish; or die
+ set -xl __fish_is_running_tests $XDG_CONFIG_HOME
+ exec ../fish $script
+ die 'exec failed'
+else if test "$__fish_is_running_tests" != "$XDG_CONFIG_HOME"
+ echo 'Something went wrong with the test runner.' >&2
+ echo "__fish_is_running_tests: $__fish_is_running_tests" >&2
+ echo "XDG_CONFIG_HOME: $XDG_CONFIG_HOME" >&2
+ exit 10
+else
+ # we're running tests with a temporary config directory
+ function test_util_on_exit --on-process-exit %self -V __fish_is_running_tests
+ # remove the temporary config directory
+ # unfortunately if this fails we can't alter the exit status of fish
+ if not rm -r "$__fish_is_running_tests"
+ echo "error: Couldn't remove temporary config directory '$__fish_is_running_tests'" >&2
+ end
+ end
+ # unset __fish_is_running_tests so any children that source
+ # test_util.fish will set up a new test environment.
+ set -e __fish_is_running_tests
end
-function say
+set -l suppress_color
+if not tty 0>&1 >/dev/null
+ set suppress_color yes
+end
+
+function say -V suppress_color
set -l color_flags
set -l suppress_newline
while set -q argv[1]
@@ -32,15 +87,14 @@ function say
set -e argv[1]
end
- if not set -q argv[1]
- echo 'usage: say [flags] color [string...]' >&2
+ if not set -q argv[2]
+ echo 'usage: say [flags] color string [string...]' >&2
return 1
end
- if set_color $color_flags $argv[1]
- set -e argv[1]
- echo -n $argv
- set_color reset
+ if begin; test -n "$suppress_color"; or set_color $color_flags $argv[1]; end
+ printf '%s' $argv[2..-1]
+ test -z "$suppress_color"; and set_color reset
if test -z "$suppress_newline"
echo
end