Skip to content

Commit 067b981

Browse files
addaleaxmcollina
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: nodejs/node#23053 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 852d25b commit 067b981

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
self.Cleanup()
114119
return test.TestOutput(self,
115120
full_command,
@@ -140,11 +145,12 @@ def ListTests(self, current_path, path, arch, mode):
140145
if self.Contains(path, test):
141146
file_prefix = join(self.root, reduce(join, test[1:], ""))
142147
file_path = file_prefix + ".js"
148+
input_path = file_prefix + ".in"
143149
output_path = file_prefix + ".out"
144150
if not exists(output_path):
145151
raise Exception("Could not find %s" % output_path)
146152
result.append(TTYTestCase(test, file_path, output_path,
147-
arch, mode, self.context, self))
153+
input_path, arch, mode, self.context, self))
148154
return result
149155

150156
def GetBuildRequirements(self):

tools/test.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -717,12 +717,23 @@ def CheckedUnlink(name):
717717
PrintError("os.unlink() " + str(e))
718718
break
719719

720-
def Execute(args, context, timeout=None, env={}, faketty=False, disable_core_files=False):
720+
def Execute(args, context, timeout=None, env={}, faketty=False, disable_core_files=False, input=None):
721721
if faketty:
722722
import pty
723723
(out_master, fd_out) = pty.openpty()
724724
fd_in = fd_err = fd_out
725725
pty_out = out_master
726+
727+
if input is not None:
728+
# Before writing input data, disable echo so the input doesn't show
729+
# up as part of the output.
730+
import termios
731+
attr = termios.tcgetattr(fd_in)
732+
attr[3] = attr[3] & ~termios.ECHO
733+
termios.tcsetattr(fd_in, termios.TCSADRAIN, attr)
734+
735+
os.write(pty_out, input)
736+
os.write(pty_out, '\x04') # End-of-file marker (Ctrl+D)
726737
else:
727738
(fd_out, outname) = tempfile.mkstemp()
728739
(fd_err, errname) = tempfile.mkstemp()

0 commit comments

Comments
 (0)