Skip to content

Commit 9d96f70

Browse files
committed
Rewrite readStdin()
Signed-off-by: Eric Wang <[email protected]>
1 parent 1884839 commit 9d96f70

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

lib/commands/test.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ function prettyLine(actual, expected, key) {
4949
log.info(' ' + h.prettyText(line, ok));
5050
}
5151

52-
cmd.handler = function(argv) {
53-
var testcase = argv.testcase;
54-
if (argv.i) {
55-
testcase = h.readStdin();
56-
}
57-
52+
function runTest(argv) {
5853
// use the 1st section in filename as keyword
5954
// e.g. two-sum.cpp, or two-sum.78502271.ac.cpp
6055
var keyword = h.getFilename(argv.filename).split('.')[0];
@@ -65,8 +60,8 @@ cmd.handler = function(argv) {
6560
if (!problem.testable)
6661
return log.fail('not testable? please submit directly!');
6762

68-
if (testcase)
69-
problem.testcase = testcase.replace(/\\n/g, '\n');
63+
if (argv.testcase)
64+
problem.testcase = argv.testcase.replace(/\\n/g, '\n');
7065

7166
if (!problem.testcase)
7267
return log.fail('missing testcase?');
@@ -91,6 +86,18 @@ cmd.handler = function(argv) {
9186
}
9287
});
9388
});
89+
}
90+
91+
cmd.handler = function(argv) {
92+
if (!argv.i)
93+
return runTest(argv);
94+
95+
h.readStdin(function(e, data) {
96+
if (e) return log.fail(e);
97+
98+
argv.testcase = data;
99+
return runTest(argv);
100+
});
94101
};
95102

96103
module.exports = cmd;

lib/helper.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,18 @@ h.getConfigFile = function() {
124124
return this.getHomeDir() + '/.lcconfig';
125125
};
126126

127-
h.readStdin = function() {
128-
// FIXME: not work for win32
129-
return fs.readFileSync('/dev/stdin').toString();
127+
h.readStdin = function(cb) {
128+
var stdin = process.stdin;
129+
var bufs = [];
130+
131+
stdin.on('readable', function() {
132+
var data = stdin.read();
133+
if (data) bufs.push(data);
134+
});
135+
stdin.on('end', function() {
136+
cb(null, Buffer.concat(bufs).toString());
137+
});
138+
stdin.on('error', cb);
130139
};
131140

132141
h.getSetCookieValue = function(resp, key) {

test/test_helper.js

+24
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,34 @@ describe('helper', function() {
144144
'key2=value2; path=/; Httponly']
145145
}
146146
};
147+
var respNoSetCookie = {
148+
headers: {}
149+
};
147150

148151
assert.equal(h.getSetCookieValue(resp, 'key1'), 'value1');
149152
assert.equal(h.getSetCookieValue(resp, 'key2'), 'value2');
150153
assert.equal(h.getSetCookieValue(resp, 'key3'), null);
154+
assert.equal(h.getSetCookieValue(respNoSetCookie, 'key1'), null);
151155
});
152156
}); // #getSetCookieValue
157+
158+
describe('#readStdin', function() {
159+
function hijackStdin(data) {
160+
var stream = require('stream');
161+
var rs = new stream.Readable();
162+
rs.push(data);
163+
rs.push(null);
164+
165+
Object.defineProperty(process, 'stdin', {value: rs});
166+
}
167+
168+
it('should ok', function(done) {
169+
hijackStdin('[1,2]\n3');
170+
171+
h.readStdin(function(e, data) {
172+
assert.equal(data, '[1,2]\n3');
173+
done();
174+
});
175+
});
176+
}); // #readStdin
153177
});

0 commit comments

Comments
 (0)