Skip to content

Commit d7031df

Browse files
addaleaxtargos
authored andcommitted
tools: allow input for TTY tests
Since faking TTY input is not otherwise fake-able, we need support in the test runner for it. PR-URL: #23053 Reviewed-By: James M Snell <[email protected]>
1 parent a656268 commit d7031df

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

test/pseudo-tty/testcfg.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535

3636
class TTYTestCase(test.TestCase):
3737

38-
def __init__(self, path, file, expected, arch, mode, context, config):
38+
def __init__(self, path, file, expected, input, arch, mode, context, config):
3939
super(TTYTestCase, self).__init__(context, path, arch, mode)
4040
self.file = file
4141
self.expected = expected
42+
self.input = input
4243
self.config = config
4344
self.arch = arch
4445
self.mode = mode
@@ -104,12 +105,16 @@ def GetSource(self):
104105
+ open(self.expected).read())
105106

106107
def RunCommand(self, command, env):
108+
input = None
109+
if self.input is not None and exists(self.input):
110+
input = open(self.input).read()
107111
full_command = self.context.processor(command)
108112
output = test.Execute(full_command,
109113
self.context,
110114
self.context.GetTimeout(self.mode),
111115
env,
112-
True)
116+
faketty=True,
117+
input=input)
113118
return test.TestOutput(self,
114119
full_command,
115120
output,
@@ -139,11 +144,12 @@ def ListTests(self, current_path, path, arch, mode):
139144
if self.Contains(path, test):
140145
file_prefix = join(self.root, reduce(join, test[1:], ""))
141146
file_path = file_prefix + ".js"
147+
input_path = file_prefix + ".in"
142148
output_path = file_prefix + ".out"
143149
if not exists(output_path):
144150
raise Exception("Could not find %s" % output_path)
145151
result.append(TTYTestCase(test, file_path, output_path,
146-
arch, mode, self.context, self))
152+
input_path, arch, mode, self.context, self))
147153
return result
148154

149155
def GetBuildRequirements(self):

tools/test.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -705,12 +705,23 @@ def CheckedUnlink(name):
705705
PrintError("os.unlink() " + str(e))
706706
break
707707

708-
def Execute(args, context, timeout=None, env={}, faketty=False, disable_core_files=False):
708+
def Execute(args, context, timeout=None, env={}, faketty=False, disable_core_files=False, input=None):
709709
if faketty:
710710
import pty
711711
(out_master, fd_out) = pty.openpty()
712712
fd_in = fd_err = fd_out
713713
pty_out = out_master
714+
715+
if input is not None:
716+
# Before writing input data, disable echo so the input doesn't show
717+
# up as part of the output.
718+
import termios
719+
attr = termios.tcgetattr(fd_in)
720+
attr[3] = attr[3] & ~termios.ECHO
721+
termios.tcsetattr(fd_in, termios.TCSADRAIN, attr)
722+
723+
os.write(pty_out, input)
724+
os.write(pty_out, '\x04') # End-of-file marker (Ctrl+D)
714725
else:
715726
(fd_out, outname) = tempfile.mkstemp()
716727
(fd_err, errname) = tempfile.mkstemp()

0 commit comments

Comments
 (0)