Skip to content

Commit 2a08958

Browse files
authored
Test reading from stdin in shell environments (#21926)
Also refactor FS_stdin_getChar so that it only includes the shell code when needed. I verified that this test runs under spidermonkey and v8.
1 parent 7c7ea15 commit 2a08958

20 files changed

+33
-29
lines changed

src/library_fs_shared.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,38 @@ addToLibrary({
136136
try {
137137
bytesRead = fs.readSync(fd, buf, 0, BUFSIZE);
138138
} catch(e) {
139-
// Cross-platform differences: on Windows, reading EOF throws an exception, but on other OSes,
140-
// reading EOF returns 0. Uniformize behavior by treating the EOF exception to return 0.
139+
// Cross-platform differences: on Windows, reading EOF throws an
140+
// exception, but on other OSes, reading EOF returns 0. Uniformize
141+
// behavior by treating the EOF exception to return 0.
141142
if (e.toString().includes('EOF')) bytesRead = 0;
142143
else throw e;
143144
}
144145

145146
if (bytesRead > 0) {
146147
result = buf.slice(0, bytesRead).toString('utf-8');
147-
} else {
148-
result = null;
149148
}
150149
} else
151150
#endif
151+
#if ENVIRONMENT_MAY_BE_WEB
152152
if (typeof window != 'undefined' &&
153153
typeof window.prompt == 'function') {
154154
// Browser.
155155
result = window.prompt('Input: '); // returns null on cancel
156156
if (result !== null) {
157157
result += '\n';
158158
}
159-
} else if (typeof readline == 'function') {
159+
} else
160+
#endif
161+
#if ENVIRONMENT_MAY_BE_SHELL
162+
if (typeof readline == 'function') {
160163
// Command line.
161164
result = readline();
162-
if (result !== null) {
165+
if (result) {
163166
result += '\n';
164167
}
165-
}
168+
} else
169+
#endif
170+
{}
166171
if (!result) {
167172
return null;
168173
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9840
1+
9828
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24247
1+
24180
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9823
1+
9811
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24215
1+
24148
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10931
1+
10915
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
28164
1+
28097
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9807
1+
9794
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24140
1+
24073
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10932
1+
10915
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
28164
1+
28097
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9840
1+
9828
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24247
1+
24180
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5210
1+
5194
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12062
1+
11995
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7802
1+
7790
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19271
1+
19204
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3133
1+
3110
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6812
1+
6745

test/test_other.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,19 +1649,18 @@ def test_export_all_and_exported_functions(self):
16491649
@also_with_wasmfs
16501650
@crossplatform
16511651
def test_stdin(self, args):
1652-
create_file('in.txt', 'abcdef\nghijkl')
1652+
create_file('in.txt', 'abcdef\nghijkl\n')
1653+
self.set_setting('ENVIRONMENT', 'node,shell')
16531654
self.emcc(test_file('module/test_stdin.c'), args=args, output_filename='out.js')
16541655

16551656
for engine in config.JS_ENGINES:
1656-
if engine == config.V8_ENGINE:
1657-
continue # no stdin support in v8 shell
16581657
engine[0] = os.path.normpath(engine[0])
1659-
print(engine, file=sys.stderr)
16601658
# work around a bug in python's subprocess module
16611659
# (we'd use self.run_js() normally)
16621660
delete_file('out.txt')
16631661
cmd = jsrun.make_command(os.path.normpath('out.js'), engine)
16641662
cmd = shared.shlex_join(cmd)
1663+
print(cmd, file=sys.stderr)
16651664
if WINDOWS:
16661665
os.system(f'type "in.txt" | {cmd} >out.txt')
16671666
else: # posix

0 commit comments

Comments
 (0)