mirror of
https://github.com/powerline/powerline.git
synced 2025-07-27 07:44:36 +02:00
Refactor vterm tests: move most of things out of the try
This commit is contained in:
parent
519f08541b
commit
883aa73e9f
@ -26,6 +26,17 @@ class MutableDimensions(Dimensions):
|
|||||||
def __setitem__(self, idx, val):
|
def __setitem__(self, idx, val):
|
||||||
self._list[idx] = val
|
self._list[idx] = val
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self._list)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return 2
|
||||||
|
|
||||||
|
def __nonzero__(self):
|
||||||
|
return True
|
||||||
|
|
||||||
|
__bool__ = __nonzero__
|
||||||
|
|
||||||
rows = property(
|
rows = property(
|
||||||
fget = lambda self: self._list[0],
|
fget = lambda self: self._list[0],
|
||||||
fset = lambda self, val: self._list.__setitem__(0, val),
|
fset = lambda self, val: self._list.__setitem__(0, val),
|
||||||
@ -41,7 +52,7 @@ class ExpectProcess(threading.Thread):
|
|||||||
super(ExpectProcess, self).__init__()
|
super(ExpectProcess, self).__init__()
|
||||||
self.vterm = VTerm(lib, dim)
|
self.vterm = VTerm(lib, dim)
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
self.dim = dim
|
self.dim = Dimensions(dim[0], dim[1])
|
||||||
self.cmd = cmd
|
self.cmd = cmd
|
||||||
self.args = args
|
self.args = args
|
||||||
self.cwd = cwd
|
self.cwd = cwd
|
||||||
@ -79,9 +90,9 @@ class ExpectProcess(threading.Thread):
|
|||||||
|
|
||||||
def resize(self, dim):
|
def resize(self, dim):
|
||||||
with self.child_lock:
|
with self.child_lock:
|
||||||
self.dim = dim
|
self.dim = Dimensions(dim[0], dim[1])
|
||||||
self.child.setwinsize(dim.rows, dim.cols)
|
self.child.setwinsize(self.dim.rows, self.dim.cols)
|
||||||
self.vterm.resize(dim)
|
self.vterm.resize(self.dim)
|
||||||
|
|
||||||
def __getitem__(self, position):
|
def __getitem__(self, position):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
@ -22,14 +22,6 @@ from tests.lib.terminal import ExpectProcess, MutableDimensions
|
|||||||
|
|
||||||
VTERM_TEST_DIR = os.path.abspath('tests/vterm_tmux')
|
VTERM_TEST_DIR = os.path.abspath('tests/vterm_tmux')
|
||||||
|
|
||||||
class RetValues:
|
|
||||||
# Poor man enum
|
|
||||||
do_restart = () # Falsy
|
|
||||||
not_run = () # Falsy
|
|
||||||
failed = () # Falsy
|
|
||||||
successfull = (1,) # Truthy
|
|
||||||
partial_success = (1,) # Truthy
|
|
||||||
|
|
||||||
|
|
||||||
def convert_expected_result(p, expected_result):
|
def convert_expected_result(p, expected_result):
|
||||||
return p.get_highlighted_text(expected_result, {})
|
return p.get_highlighted_text(expected_result, {})
|
||||||
@ -46,12 +38,12 @@ def cell_properties_key_to_shell_escape(cell_properties_key):
|
|||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
def test_expected_result(p, expected_result, dim, print_logs):
|
def test_expected_result(p, expected_result, print_logs):
|
||||||
expected_text, attrs = expected_result
|
expected_text, attrs = expected_result
|
||||||
attempts = 3
|
attempts = 3
|
||||||
result = None
|
result = None
|
||||||
while attempts:
|
while attempts:
|
||||||
actual_text, all_attrs = p.get_row(dim.rows - 1, attrs)
|
actual_text, all_attrs = p.get_row(p.dim.rows - 1, attrs)
|
||||||
if actual_text == expected_text:
|
if actual_text == expected_text:
|
||||||
return True
|
return True
|
||||||
attempts -= 1
|
attempts -= 1
|
||||||
@ -74,12 +66,13 @@ def test_expected_result(p, expected_result, dim, print_logs):
|
|||||||
if print_logs:
|
if print_logs:
|
||||||
for f in glob1(VTERM_TEST_DIR, '*.log'):
|
for f in glob1(VTERM_TEST_DIR, '*.log'):
|
||||||
print('_' * 80)
|
print('_' * 80)
|
||||||
print(os.path.basename(f) + ':')
|
print(f + ':')
|
||||||
print('=' * 80)
|
print('=' * 80)
|
||||||
with open(f, 'r') as F:
|
full_f = os.path.join(VTERM_TEST_DIR, f)
|
||||||
for line in F:
|
with open(full_f, 'r') as fp:
|
||||||
|
for line in fp:
|
||||||
sys.stdout.write(line)
|
sys.stdout.write(line)
|
||||||
os.unlink(f)
|
os.unlink(full_f)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -100,10 +93,6 @@ def get_expected_result(tmux_version,
|
|||||||
|
|
||||||
def main(attempts=3):
|
def main(attempts=3):
|
||||||
vterm_path = os.path.join(VTERM_TEST_DIR, 'path')
|
vterm_path = os.path.join(VTERM_TEST_DIR, 'path')
|
||||||
socket_path = os.path.abspath('tmux-socket-{0}'.format(attempts))
|
|
||||||
if os.path.exists(socket_path):
|
|
||||||
os.unlink(socket_path)
|
|
||||||
dim = MutableDimensions(rows=50, cols=200)
|
|
||||||
|
|
||||||
tmux_exe = os.path.join(vterm_path, 'tmux')
|
tmux_exe = os.path.join(vterm_path, 'tmux')
|
||||||
|
|
||||||
@ -268,6 +257,19 @@ def main(attempts=3):
|
|||||||
})),
|
})),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def prepare_test_1():
|
||||||
|
sleep(5)
|
||||||
|
|
||||||
|
def prepare_test_2():
|
||||||
|
dim.cols = 40
|
||||||
|
p.resize(dim)
|
||||||
|
sleep(5)
|
||||||
|
|
||||||
|
test_preps = (
|
||||||
|
prepare_test_1,
|
||||||
|
prepare_test_2,
|
||||||
|
)
|
||||||
args = [
|
args = [
|
||||||
# Specify full path to tmux socket (testing tmux instance must not
|
# Specify full path to tmux socket (testing tmux instance must not
|
||||||
# interfere with user one)
|
# interfere with user one)
|
||||||
@ -284,6 +286,11 @@ def main(attempts=3):
|
|||||||
'new-window', 'bash --norc --noprofile -i', ';',
|
'new-window', 'bash --norc --noprofile -i', ';',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
socket_path = os.path.abspath('tmux-socket-{0}'.format(attempts))
|
||||||
|
if os.path.exists(socket_path):
|
||||||
|
os.unlink(socket_path)
|
||||||
|
dim = MutableDimensions(rows=50, cols=200)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
p = ExpectProcess(
|
p = ExpectProcess(
|
||||||
lib=lib,
|
lib=lib,
|
||||||
@ -294,33 +301,22 @@ def main(attempts=3):
|
|||||||
env=env,
|
env=env,
|
||||||
)
|
)
|
||||||
p.start()
|
p.start()
|
||||||
sleep(5)
|
|
||||||
ret = RetValues.not_run
|
ret = True
|
||||||
if not test_expected_result(p, expected_results[0], dim, not attempts):
|
|
||||||
if attempts:
|
for test_prep, expected_result in zip(test_preps, expected_results):
|
||||||
ret = RetValues.do_restart
|
test_prep()
|
||||||
else:
|
ret = (
|
||||||
ret = RetValues.failed
|
ret
|
||||||
elif ret is RetValues.not_run:
|
and test_expected_result(p, expected_result, attempts == 0)
|
||||||
ret = RetValues.partial_success
|
)
|
||||||
dim.cols = 40
|
|
||||||
p.resize(dim)
|
if ret or attempts == 0:
|
||||||
sleep(5)
|
return ret
|
||||||
if not test_expected_result(p, expected_results[1], dim, not attempts):
|
|
||||||
if attempts:
|
|
||||||
ret = RetValues.do_restart
|
|
||||||
else:
|
|
||||||
ret = RetValues.failed
|
|
||||||
elif ret is RetValues.partial_success:
|
|
||||||
ret = RetValues.successfull
|
|
||||||
if ret is RetValues.successfull or ret is RetValues.failed:
|
|
||||||
return bool(ret)
|
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
check_call([tmux_exe, '-S', socket_path, 'kill-server'], env={
|
check_call([tmux_exe, '-S', socket_path, 'kill-server'], env=env,
|
||||||
'PATH': vterm_path,
|
cwd=VTERM_TEST_DIR)
|
||||||
'LD_LIBRARY_PATH': os.environ.get('LD_LIBRARY_PATH', ''),
|
|
||||||
}, cwd=VTERM_TEST_DIR)
|
|
||||||
except Exception:
|
except Exception:
|
||||||
print_exc()
|
print_exc()
|
||||||
p.kill()
|
p.kill()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user