diff --git a/tests/modules/__init__.py b/tests/modules/__init__.py index 42a67171..12aae20d 100644 --- a/tests/modules/__init__.py +++ b/tests/modules/__init__.py @@ -13,6 +13,8 @@ else: from unittest import main as _main # NOQA from unittest.case import SkipTest # NOQA +from tests.modules.lib import PowerlineSingleTest + class PowerlineDummyTest(object): def __enter__(self): diff --git a/tests/modules/lib/terminal.py b/tests/modules/lib/terminal.py index 72f7c047..7f4bee58 100644 --- a/tests/modules/lib/terminal.py +++ b/tests/modules/lib/terminal.py @@ -221,7 +221,11 @@ def get_env(vterm_path, test_dir, *args, **kwargs): def do_terminal_tests(tests, cmd, dim, args, env, suite, cwd=None, fin_cb=None, - last_attempt_cb=None, attempts=3): + last_attempt_cb=None, attempts=None): + debugging_tests = not not os.environ.get('_POWERLINE_DEBUGGING_TESTS') + default_attempts = 1 if debugging_tests else 3 + if attempts is None: + attempts = default_attempts lib = os.environ.get('POWERLINE_LIBVTERM') if not lib: if os.path.exists('tests/bot-ci/deps/libvterm/libvterm.so'): @@ -252,9 +256,10 @@ def do_terminal_tests(tests, cmd, dim, args, env, suite, cwd=None, fin_cb=None, pass else: test_prep(p) - test_result = test_expected_result(p, test, attempts == 0, - last_attempt_cb, - test.get('attempts', 3)) + test_result = test_expected_result( + p, test, attempts == 0, last_attempt_cb, + test.get('attempts', default_attempts) + ) if not test_result: ptest.fail('Result does not match expected') ret = ret and test_result diff --git a/tests/test_in_vterm/shell/inits/dash b/tests/test_in_vterm/shell/inits/dash new file mode 100644 index 00000000..2bf62e6c --- /dev/null +++ b/tests/test_in_vterm/shell/inits/dash @@ -0,0 +1,12 @@ +# vim: ft=sh + +set_theme_option() { + export POWERLINE_THEME_OVERRIDES="${POWERLINE_THEME_OVERRIDES};$1=$2" +} +set_theme() { + export POWERLINE_CONFIG_OVERRIDES="ext.shell.theme=$1" +} +set_theme_option default_leftonly.segment_data.hostname.args.only_if_ssh false +set_theme default_leftonly +. "$ROOT/powerline/bindings/shell/powerline.sh" +export VIRTUAL_ENV= diff --git a/tests/test_in_vterm/test_shell.py b/tests/test_in_vterm/test_shell.py new file mode 100755 index 00000000..4da7bafb --- /dev/null +++ b/tests/test_in_vterm/test_shell.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8:noet +from __future__ import (unicode_literals, division, absolute_import, print_function) + +import os +import sys + +from time import sleep +from subprocess import check_call +from glob import glob1 +from traceback import print_exc + +from argparse import ArgumentParser + +from powerline.lib.dict import updated + +from tests.modules.lib.terminal import (ExpectProcess, MutableDimensions, + do_terminal_tests, get_env) +from tests.modules import PowerlineTestSuite + + +TEST_ROOT = os.path.abspath(os.environ['TEST_ROOT']) + + +def get_parser(): + parser = ArgumentParser() + parser.add_argument('--type', action='store') + parser.add_argument('--client', action='store') + parser.add_argument('--binding', action='store') + parser.add_argument('args', action='append') + return parser + + +BINDING_OPTIONS = { + 'dash': { + 'cmd': 'dash', + 'args': ['-i'], + 'init': [ + '. "$ROOT/tests/test_in_vterm/shell/inits/dash"', + ], + }, +} + + +def main(argv): + script_args = get_parser().parse_args(argv) + + vterm_path = os.path.join(TEST_ROOT, 'path') + + env = get_env(vterm_path, TEST_ROOT) + env['ROOT'] = os.path.abspath('.') + env['TEST_ROOT'] = TEST_ROOT + env['TEST_TYPE'] = script_args.type + env['TEST_CLIENT'] = script_args.client + env['LANG'] = 'en_US.UTF_8' + + dim = MutableDimensions(rows=50, cols=200) + + binding_opts = BINDING_OPTIONS[script_args.binding] + + cmd = os.path.join(vterm_path, binding_opts['cmd']) + args = binding_opts['args'] + + def gen_init(binding): + def init(p): + for line in binding_opts['init']: + p.send(line + '\n') + while not p[dim.rows - 1, 0].text: + p.send('\n') + sleep(0.01) + + return init + + base_attrs = { + ((255, 204,0), (204, 51, 0), 0, 0, 0): 'N', + ((204, 51, 0), (0, 102, 153), 0, 0, 0): 'H', + ((255, 255, 255), (0, 102, 153), 1, 0, 0): 'sHU', + ((0, 102, 153), (44, 44, 44), 0, 0, 0): 'U', + ((199, 199, 199), (44, 44, 44), 0, 0, 0): 'sUB', + ((44, 44, 44), (88, 88, 88), 0, 0, 0): 'B', + ((199, 199, 199), (88, 88, 88), 0, 0, 0): 'sBD', + ((144, 144, 144), (88, 88, 88), 0, 0, 0): 'D', + ((221, 221, 221), (88, 88, 88), 1, 0, 0): 'sD', + ((88, 88, 88), (0, 0, 0), 0, 0, 0): 'C', + ((240, 240, 240), (0, 0, 0), 0, 0, 0): 'sCp', + } + + tests = ( + { + 'expected_result': ( + '', base_attrs + ), + 'prep_cb': gen_init(script_args.binding), + 'row': dim.rows - 1, + }, + ) + + with PowerlineTestSuite('shell') as suite: + return do_terminal_tests( + tests=tests, + cmd=cmd, + dim=dim, + args=args, + env=env, + cwd=TEST_ROOT, + suite=suite, + ) + + +if __name__ == '__main__': + if main(sys.argv[1:]): + raise SystemExit(0) + else: + raise SystemExit(1) diff --git a/tests/test_in_vterm/test_shells.sh b/tests/test_in_vterm/test_shells.sh new file mode 100755 index 00000000..25a49200 --- /dev/null +++ b/tests/test_in_vterm/test_shells.sh @@ -0,0 +1,99 @@ +#!/bin/sh +. tests/shlib/common.sh +. tests/shlib/vterm.sh + +enter_suite vshells + +vterm_setup + +HAS_SOCAT= +HAS_C_CLIENT= + +ln -s "$(which env)" "$TEST_ROOT/path" +ln -s "$(which git)" "$TEST_ROOT/path" +ln -s "$(which sleep)" "$TEST_ROOT/path" +ln -s "$(which cat)" "$TEST_ROOT/path" +ln -s "$(which false)" "$TEST_ROOT/path" +ln -s "$(which true)" "$TEST_ROOT/path" +ln -s "$(which kill)" "$TEST_ROOT/path" +ln -s "$(which echo)" "$TEST_ROOT/path" +ln -s "$(which which)" "$TEST_ROOT/path" +ln -s "$(which dirname)" "$TEST_ROOT/path" +ln -s "$(which wc)" "$TEST_ROOT/path" +ln -s "$(which stty)" "$TEST_ROOT/path" +ln -s "$(which cut)" "$TEST_ROOT/path" +ln -s "$(which bc)" "$TEST_ROOT/path" +ln -s "$(which expr)" "$TEST_ROOT/path" +ln -s "$(which mktemp)" "$TEST_ROOT/path" +ln -s "$(which grep)" "$TEST_ROOT/path" +ln -s "$(which sed)" "$TEST_ROOT/path" +ln -s "$(which rm)" "$TEST_ROOT/path" +ln -s "$(which tr)" "$TEST_ROOT/path" +ln -s "$(which uname)" "$TEST_ROOT/path" +ln -s "$(which test)" "$TEST_ROOT/path" +ln -s "$(which pwd)" "$TEST_ROOT/path" +ln -s "$(which hostname)" "$TEST_ROOT/path" +ln -s "$ROOT/tests/test_shells/bgscript.sh" "$TEST_ROOT/path" +ln -s "$ROOT/tests/test_shells/waitpid.sh" "$TEST_ROOT/path" + +ln -s "$ROOT/scripts/powerline-config" "$TEST_ROOT/path" +ln -s "$ROOT/scripts/powerline-render" "$TEST_ROOT/path" +ln -s "$ROOT/client/powerline.py" "$TEST_ROOT/path" + +if test -e "$ROOT/scripts/powerline" ; then + ln -s "$ROOT/scripts/powerline" "$TEST_ROOT/path" +elif test -e client/powerline ; then + ln -s "$ROOT/client/powerline" "$TEST_ROOT/path" +else + echo "Executable powerline was not found" + exit 1 +fi + +if test "$( + file --mime-type --brief --dereference "$TEST_ROOT/path/powerline" \ + | cut -d/ -f1)" = "application" ; then + HAS_C_CLIENT=1 +fi + +if which socat ; then + HAS_SOCAT=1 + ln -s "$(which socat)" "$TEST_ROOT/path" + ln -s "$ROOT/client/powerline.sh" "$TEST_ROOT/path" +fi + +# Test type: daemon, renderer, … +# Test client: python, shell, c, none +# Test binding: *sh, ipython, pdb, … +test_shell() { + local test_type="$1" ; shift + local test_client="$1" ; shift + local test_binding="$1" ; shift + + if test "$test_client" = shell && test -z "$HAS_SOCAT" ; then + echo "Skipping test, socat not available" + return + fi + if test "$test_client" = c && test -z "$HAS_C_CLIENT" ; then + echo "Skipping test, C client not available" + return + fi + if which "$test_binding" ; then + ln -s "$(which "$test_binding")" "$TEST_ROOT/path" + fi + + if ! "${PYTHON}" "$ROOT/tests/test_in_vterm/test_shell.py" \ + --type=$test_type \ + --client=$test_client \ + --binding=$test_binding \ + -- "$@" + then + local test_name="$test_type-$test_client-$test_binding" + fail "$test_name" F "Failed vterm shell test" + fi +} + +test_shell renderer python dash -i || true + +vterm_shutdown + +exit_suite