use vm instead of child_process
This commit is contained in:
parent
b2f63543bc
commit
c35069e80b
|
|
@ -6,7 +6,7 @@ var U = require("../tools/node");
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var assert = require("assert");
|
var assert = require("assert");
|
||||||
var execFile = require("child_process").execFile;
|
var vm = require("vm");
|
||||||
|
|
||||||
var tests_dir = path.dirname(module.filename);
|
var tests_dir = path.dirname(module.filename);
|
||||||
|
|
||||||
|
|
@ -173,15 +173,9 @@ function run_compress_tests(done) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (test.expect_stdout) {
|
if (test.expect_stdout) {
|
||||||
return execFile(process.argv[0], ["-e", input_code], function(ex, stdout, stderr) {
|
try {
|
||||||
if (ex || stderr) {
|
var stdout = run_code(input_code);
|
||||||
log("!!! Execution of input failed\n---INPUT---\n{input}\n--ERROR--\n{error}\n\n", {
|
if (test.expect_stdout != stdout) {
|
||||||
input: input_code,
|
|
||||||
error: stderr || ex.toString(),
|
|
||||||
});
|
|
||||||
failures++;
|
|
||||||
failed_files[file] = 1;
|
|
||||||
} else if (test.expect_stdout != stdout) {
|
|
||||||
log("!!! Invalid input or expected stdout\n---INPUT---\n{input}\n---EXPECTED STDOUT---\n{expected}\n---ACTUAL STDOUT---\n{actual}\n\n", {
|
log("!!! Invalid input or expected stdout\n---INPUT---\n{input}\n---EXPECTED STDOUT---\n{expected}\n---ACTUAL STDOUT---\n{actual}\n\n", {
|
||||||
input: input_code,
|
input: input_code,
|
||||||
expected: test.expect_stdout,
|
expected: test.expect_stdout,
|
||||||
|
|
@ -190,16 +184,9 @@ function run_compress_tests(done) {
|
||||||
failures++;
|
failures++;
|
||||||
failed_files[file] = 1;
|
failed_files[file] = 1;
|
||||||
} else {
|
} else {
|
||||||
return execFile(process.argv[0], ["-e", output], function(ex, stdout, stderr) {
|
try {
|
||||||
if (ex || stderr) {
|
stdout = run_code(output);
|
||||||
log("!!! Execution of output failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n--ERROR--\n{error}\n\n", {
|
if (test.expect_stdout != stdout) {
|
||||||
input: input_code,
|
|
||||||
output: output,
|
|
||||||
error: stderr || ex.toString(),
|
|
||||||
});
|
|
||||||
failures++;
|
|
||||||
failed_files[file] = 1;
|
|
||||||
} else if (test.expect_stdout != stdout) {
|
|
||||||
log("!!! failed\n---INPUT---\n{input}\n---EXPECTED STDOUT---\n{expected}\n---ACTUAL STDOUT---\n{actual}\n\n", {
|
log("!!! failed\n---INPUT---\n{input}\n---EXPECTED STDOUT---\n{expected}\n---ACTUAL STDOUT---\n{actual}\n\n", {
|
||||||
input: input_code,
|
input: input_code,
|
||||||
expected: test.expect_stdout,
|
expected: test.expect_stdout,
|
||||||
|
|
@ -208,11 +195,24 @@ function run_compress_tests(done) {
|
||||||
failures++;
|
failures++;
|
||||||
failed_files[file] = 1;
|
failed_files[file] = 1;
|
||||||
}
|
}
|
||||||
test_case();
|
} catch (ex) {
|
||||||
});
|
log("!!! Execution of output failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n--ERROR--\n{error}\n\n", {
|
||||||
|
input: input_code,
|
||||||
|
output: output,
|
||||||
|
error: ex.toString(),
|
||||||
|
});
|
||||||
|
failures++;
|
||||||
|
failed_files[file] = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
test_case();
|
} catch (ex) {
|
||||||
});
|
log("!!! Execution of input failed\n---INPUT---\n{input}\n--ERROR--\n{error}\n\n", {
|
||||||
|
input: input_code,
|
||||||
|
error: ex.toString(),
|
||||||
|
});
|
||||||
|
failures++;
|
||||||
|
failed_files[file] = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test_case();
|
test_case();
|
||||||
|
|
@ -329,3 +329,17 @@ function evaluate(code) {
|
||||||
code = make_code(code, { beautify: true });
|
code = make_code(code, { beautify: true });
|
||||||
return new Function("return(" + code + ")")();
|
return new Function("return(" + code + ")")();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function run_code(code) {
|
||||||
|
var stdout = "";
|
||||||
|
var tmp = process.stdout.write;
|
||||||
|
process.stdout.write = function(chunk) {
|
||||||
|
stdout += chunk;
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
new vm.Script(code).runInNewContext({ console: console }, { timeout: 5000 });
|
||||||
|
return stdout;
|
||||||
|
} finally {
|
||||||
|
process.stdout.write = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user