[ADD] WEXPECT_SPAWN_CLASS environment variable set the working spawn class, the default is SpawnSocket if env var not set;[FIX][TST] interact testcases skipped

This commit is contained in:
Benedek Racz 2020-01-24 10:11:14 +01:00
parent 46519bb4cf
commit 51426bc41d
3 changed files with 31 additions and 11 deletions

View File

@ -33,6 +33,8 @@ import unittest
from . import PexpectTestCase from . import PexpectTestCase
class InteractTestCase(PexpectTestCase.PexpectTestCase): class InteractTestCase(PexpectTestCase.PexpectTestCase):
@unittest.skipIf(not hasattr(wexpect.spawn, 'interact'), "spawn does not support runtime interact switching.")
def test_interact(self): def test_interact(self):
# Path of cmd executable: # Path of cmd executable:
cmd_exe = 'cmd' cmd_exe = 'cmd'
@ -55,6 +57,7 @@ class InteractTestCase(PexpectTestCase.PexpectTestCase):
self.assertEqual('hello', p.before.splitlines()[1]) self.assertEqual('hello', p.before.splitlines()[1])
@unittest.skipIf(not hasattr(wexpect.spawn, 'interact'), "spawn does not support runtime interact switching.")
def test_interact_dead(self): def test_interact_dead(self):
# Path of cmd executable: # Path of cmd executable:
echo = 'echo hello' echo = 'echo hello'

View File

@ -1,5 +1,7 @@
# __init__.py # __init__.py
import os
from .wexpect_util import split_command_line from .wexpect_util import split_command_line
from .wexpect_util import join_args from .wexpect_util import join_args
from .wexpect_util import ExceptionPexpect from .wexpect_util import ExceptionPexpect
@ -11,8 +13,18 @@ from .console_reader import ConsoleReaderPipe
from .spawn import SpawnSocket from .spawn import SpawnSocket
from .spawn import SpawnPipe from .spawn import SpawnPipe
from .spawn import SpawnSocket as spawn
from .spawn import run from .spawn import run
try:
spawn_class_name = os.environ['WEXPECT_SPAWN_CLASS']
try:
spawn = globals()[spawn_class_name]
except KeyError:
print(f'Error: no spawn class: {spawn_class_name}')
print('Using SpawnSocket.')
spawn = SpawnSocket
except KeyError:
spawn = SpawnSocket
__all__ = ['split_command_line', 'join_args', 'ExceptionPexpect', 'EOF', 'TIMEOUT', __all__ = ['split_command_line', 'join_args', 'ExceptionPexpect', 'EOF', 'TIMEOUT',
'ConsoleReaderSocket', 'ConsoleReaderPipe', 'spawn', 'SpawnSocket', 'SpawnPipe', 'run'] 'ConsoleReaderSocket', 'ConsoleReaderPipe', 'spawn', 'SpawnSocket', 'SpawnPipe', 'run']

View File

@ -249,7 +249,7 @@ class SpawnBase:
self.delayafterterminate = 0.1 # Sets delay in terminate() method to allow kernel time to update process status. Time in seconds. self.delayafterterminate = 0.1 # Sets delay in terminate() method to allow kernel time to update process status. Time in seconds.
self.buffer = '' # This is the read buffer. See maxread. self.buffer = '' # This is the read buffer. See maxread.
self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched. self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched.
self.interact = interact self.interact_state = interact
# If command is an int type then it may represent a file descriptor. # If command is an int type then it may represent a file descriptor.
@ -356,7 +356,15 @@ class SpawnBase:
else: else:
python_executable = os.path.join(os.path.dirname(sys.executable), 'python.exe') python_executable = os.path.join(os.path.dirname(sys.executable), 'python.exe')
child_class_initializator = self.get_child_class_initializator(args) self.console_class_parameters.update({
'host_pid': self.host_pid,
'local_echo': self.echo,
'interact': self.interact_state
})
console_class_parameters_kv_pairs = [f'{k}={v}' for k,v in self.console_class_parameters.items() ]
console_class_parameters_str = ', '.join(console_class_parameters_kv_pairs)
child_class_initializator = f"wexpect.{self.console_class_name}(wexpect.join_args({args}), {console_class_parameters_str});"
commandLine = '"%s" %s "%s"' % (python_executable, commandLine = '"%s" %s "%s"' % (python_executable,
' '.join(pyargs), ' '.join(pyargs),
@ -817,6 +825,8 @@ class SpawnPipe(SpawnBase):
def __init__(self, command, args=[], timeout=30, maxread=60000, searchwindowsize=None, def __init__(self, command, args=[], timeout=30, maxread=60000, searchwindowsize=None,
logfile=None, cwd=None, env=None, codepage=None, echo=True, port=4321, host='localhost', interact=False): logfile=None, cwd=None, env=None, codepage=None, echo=True, port=4321, host='localhost', interact=False):
self.pipe = None self.pipe = None
self.console_class_name = 'ConsoleReaderPipe'
self.console_class_parameters = {}
super().__init__(command=command, args=args, timeout=timeout, maxread=maxread, super().__init__(command=command, args=args, timeout=timeout, maxread=maxread,
searchwindowsize=searchwindowsize, cwd=cwd, env=env, codepage=codepage, echo=echo, interact=interact) searchwindowsize=searchwindowsize, cwd=cwd, env=env, codepage=codepage, echo=echo, interact=interact)
@ -922,9 +932,6 @@ class SpawnPipe(SpawnBase):
raise raise
return len(s) return len(s)
def get_child_class_initializator(self, args, **kwargs):
child_class_initializator = f"wexpect.ConsoleReaderPipe(wexpect.join_args({args}), {self.host_pid}, local_echo={self.echo}, interact={self.interact});"
return child_class_initializator
class SpawnSocket(SpawnBase): class SpawnSocket(SpawnBase):
@ -933,6 +940,8 @@ class SpawnSocket(SpawnBase):
self.port = port self.port = port
self.host = host self.host = host
self.sock = None self.sock = None
self.console_class_name = 'ConsoleReaderPipe'
self.console_class_parameters = {'port': port}
super().__init__(command=command, args=args, timeout=timeout, maxread=maxread, super().__init__(command=command, args=args, timeout=timeout, maxread=maxread,
searchwindowsize=searchwindowsize, cwd=cwd, env=env, codepage=codepage, echo=echo, interact=interact) searchwindowsize=searchwindowsize, cwd=cwd, env=env, codepage=codepage, echo=echo, interact=interact)
@ -996,10 +1005,6 @@ class SpawnSocket(SpawnBase):
return s.decode() return s.decode()
def get_child_class_initializator(self, args, **kwargs):
child_class_initializator = f"wexpect.ConsoleReaderSocket(wexpect.join_args({args}), {self.host_pid}, port={self.port}, local_echo={self.echo}, interact={self.interact});"
return child_class_initializator
class searcher_re (object): class searcher_re (object):
"""This is regular expression string search helper for the """This is regular expression string search helper for the