2016-06-04 02:05:13 +00:00
|
|
|
# vim: set ts=4 sw=4 tw=100 et:
|
2014-09-23 23:29:36 +00:00
|
|
|
# Utilities for the test runners
|
|
|
|
|
2014-10-28 02:42:19 +00:00
|
|
|
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
|
|
|
|
|
2014-12-23 20:20:44 +00:00
|
|
|
# Any remaining arguments are passed back to test.fish
|
|
|
|
set -l args_for_test_script
|
|
|
|
if set -q argv[2]
|
|
|
|
set args_for_test_script $argv[2..-1]
|
|
|
|
end
|
|
|
|
|
2014-09-23 23:29:36 +00:00
|
|
|
function die
|
2014-10-28 02:42:19 +00:00
|
|
|
set -q argv[1]; and echo $argv[1] >&2
|
2014-09-23 23:29:36 +00:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2016-06-04 02:05:13 +00:00
|
|
|
# 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.
|
2014-10-28 02:42:19 +00:00
|
|
|
if not set -q __fish_is_running_tests
|
2016-06-04 02:05:13 +00:00
|
|
|
# Set up our test environment and re-run the original script.
|
2014-10-28 02:42:19 +00:00
|
|
|
set -l script $argv[1]
|
|
|
|
switch $script
|
2019-05-20 19:07:37 +00:00
|
|
|
case '/*'
|
|
|
|
# path is absolute
|
|
|
|
case '*'
|
|
|
|
# path is relative, make it absolute
|
|
|
|
set script $PWD/$script
|
2014-10-28 02:42:19 +00:00
|
|
|
end
|
2016-06-04 02:05:13 +00:00
|
|
|
|
|
|
|
begin
|
2017-07-25 03:45:43 +00:00
|
|
|
dirname $script | read -l dir
|
|
|
|
cd $dir
|
|
|
|
or die
|
2016-06-04 02:05:13 +00:00
|
|
|
end
|
2016-02-07 02:08:22 +00:00
|
|
|
|
|
|
|
set -lx XDG_DATA_HOME ../test/data
|
|
|
|
rm -rf $XDG_DATA_HOME/fish
|
|
|
|
mkdir -p $XDG_DATA_HOME/fish; or die
|
|
|
|
|
|
|
|
set -lx XDG_CONFIG_HOME ../test/home
|
|
|
|
rm -rf $XDG_CONFIG_HOME/fish
|
2014-10-28 02:42:19 +00:00
|
|
|
mkdir -p $XDG_CONFIG_HOME/fish; or die
|
2014-10-28 03:48:39 +00:00
|
|
|
ln -s $PWD/test_functions $XDG_CONFIG_HOME/fish/functions; or die
|
2016-02-07 02:08:22 +00:00
|
|
|
|
2014-10-28 02:42:19 +00:00
|
|
|
set -l escaped_parent (dirname $PWD | sed -e 's/[\'\\\\]/\\\\&/g'); or die
|
2014-10-28 03:48:39 +00:00
|
|
|
set -l escaped_config (printf '%s/fish' $XDG_CONFIG_HOME | sed -e 's/[\'\\\\]/\\\\&/g'); or die
|
2019-05-20 19:07:37 +00:00
|
|
|
printf 'set fish_function_path \'%s/functions\' \'%s/share/functions\'\n' $escaped_config $escaped_parent >$XDG_CONFIG_HOME/fish/config.fish; or die
|
2014-10-28 02:42:19 +00:00
|
|
|
set -xl __fish_is_running_tests $XDG_CONFIG_HOME
|
2016-02-07 02:08:22 +00:00
|
|
|
|
2016-06-04 02:05:13 +00:00
|
|
|
# Set locale information for consistent tests. Fish should work with a lot of locales but the
|
|
|
|
# tests assume an english UTF-8 locale unless they explicitly override this default. We do not
|
|
|
|
# want the users locale to affect the tests since they might, for example, change the wording of
|
|
|
|
# logged messages.
|
|
|
|
#
|
|
|
|
# TODO: set LANG to en_US.UTF-8 so we test the locale message conversions (i.e., gettext).
|
|
|
|
set -e LANGUAGE
|
|
|
|
set -x LANG C
|
|
|
|
# Remove "LC_" env vars from the test environment.
|
|
|
|
for var in (set -xn)
|
|
|
|
string match -q 'LC_*' $var
|
|
|
|
and set -e $var
|
2014-11-25 08:52:22 +00:00
|
|
|
end
|
2016-06-04 02:05:13 +00:00
|
|
|
set -x LC_CTYPE en_US.UTF-8
|
|
|
|
|
2016-02-07 02:08:22 +00:00
|
|
|
exec ../test/root/bin/fish $script $args_for_test_script
|
2014-10-28 02:42:19 +00:00
|
|
|
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
|
2014-09-23 23:29:36 +00:00
|
|
|
end
|
|
|
|
|
2014-10-28 02:42:19 +00:00
|
|
|
set -l suppress_color
|
|
|
|
if not tty 0>&1 >/dev/null
|
|
|
|
set suppress_color yes
|
|
|
|
end
|
|
|
|
|
|
|
|
function say -V suppress_color
|
2014-09-23 23:29:36 +00:00
|
|
|
set -l color_flags
|
2014-09-24 05:50:28 +00:00
|
|
|
set -l suppress_newline
|
2014-09-23 23:29:36 +00:00
|
|
|
while set -q argv[1]
|
|
|
|
switch $argv[1]
|
|
|
|
case -b -o -u
|
|
|
|
set color_flags $color_flags $argv[1]
|
2014-09-24 05:50:28 +00:00
|
|
|
case -n
|
|
|
|
set suppress_newline 1
|
2014-09-23 23:29:36 +00:00
|
|
|
case --
|
|
|
|
set -e argv[1]
|
|
|
|
break
|
|
|
|
case -\*
|
2014-09-24 05:50:28 +00:00
|
|
|
continue
|
2014-09-23 23:29:36 +00:00
|
|
|
case \*
|
|
|
|
break
|
|
|
|
end
|
|
|
|
set -e argv[1]
|
|
|
|
end
|
|
|
|
|
2014-10-28 02:42:19 +00:00
|
|
|
if not set -q argv[2]
|
|
|
|
echo 'usage: say [flags] color string [string...]' >&2
|
2014-09-23 23:29:36 +00:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2019-05-20 19:07:37 +00:00
|
|
|
if begin
|
|
|
|
test -n "$suppress_color"; or set_color $color_flags $argv[1]
|
|
|
|
end
|
2014-10-28 02:42:19 +00:00
|
|
|
printf '%s' $argv[2..-1]
|
2016-11-12 03:07:52 +00:00
|
|
|
test -z "$suppress_color"; and set_color normal
|
2014-09-24 05:50:28 +00:00
|
|
|
if test -z "$suppress_newline"
|
|
|
|
echo
|
|
|
|
end
|
2014-09-23 23:29:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function colordiff -d 'Colored diff output for unified diffs'
|
|
|
|
diff $argv | while read -l line
|
|
|
|
switch $line
|
|
|
|
case '+*'
|
|
|
|
say green $line
|
|
|
|
case '-*'
|
|
|
|
say red $line
|
|
|
|
case '@*'
|
|
|
|
say cyan $line
|
|
|
|
case '*'
|
|
|
|
echo $line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-19 21:24:46 +00:00
|
|
|
|
|
|
|
# lame timer
|
|
|
|
for program in {g,}date
|
2019-09-22 01:42:55 +00:00
|
|
|
if command -q $program && $program --version 1>/dev/null 2>/dev/null
|
2019-10-19 12:25:03 +00:00
|
|
|
set -g milli $program
|
2019-09-19 21:24:46 +00:00
|
|
|
set unit ms
|
|
|
|
break
|
|
|
|
else
|
|
|
|
set unit sec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-19 22:06:51 +00:00
|
|
|
function timestamp
|
2019-09-19 21:24:46 +00:00
|
|
|
set -q milli[1]
|
|
|
|
and $milli +%s%3N
|
|
|
|
or date +%s
|
|
|
|
end
|
|
|
|
|
2019-09-19 22:06:51 +00:00
|
|
|
function delta
|
2019-09-19 21:24:46 +00:00
|
|
|
set -q milli[1]
|
|
|
|
and math "( "($milli +%s%3N)" - $argv[1])"
|
2019-10-19 12:25:03 +00:00
|
|
|
or math (date +%s) - $argv[1]
|
2019-09-19 21:24:46 +00:00
|
|
|
end
|