From: Alex Lam S.L Date: Fri, 17 Mar 2017 18:33:51 +0000 (+0800) Subject: handle runtime errors in `expect_stdout` (#1618) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=0489d6de6499154c758a6464387546ec5a060f67;p=UglifyJS.git handle runtime errors in `expect_stdout` (#1618) allow test to pass if both `input` and `expect` throws the same kind of error --- diff --git a/test/compress/issue-1588.js b/test/compress/issue-1588.js index ff25635c..fce9ba54 100644 --- a/test/compress/issue-1588.js +++ b/test/compress/issue-1588.js @@ -85,3 +85,15 @@ unsafe_undefined: { } expect_stdout: true } + +runtime_error: { + input: { + const a = 1; + console.log(a++); + } + expect: { + const a = 1; + console.log(a++); + } + expect_stdout: true +} diff --git a/test/run-tests.js b/test/run-tests.js index c3c142d1..7f3256e0 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -172,48 +172,33 @@ function run_compress_tests() { } } if (test.expect_stdout) { - try { - var stdout = run_code(input_code); - if (test.expect_stdout === true) { - test.expect_stdout = stdout; - } - 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", { + var stdout = run_code(input_code); + if (test.expect_stdout === true) { + test.expect_stdout = stdout; + } + if (!same_stdout(test.expect_stdout, stdout)) { + log("!!! Invalid input or expected stdout\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", { + input: input_code, + expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR", + expected: test.expect_stdout, + actual_type: typeof stdout == "string" ? "STDOUT" : "ERROR", + actual: stdout, + }); + failures++; + failed_files[file] = 1; + } else { + stdout = run_code(output); + if (!same_stdout(test.expect_stdout, stdout)) { + log("!!! failed\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", { input: input_code, + expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR", expected: test.expect_stdout, + actual_type: typeof stdout == "string" ? "STDOUT" : "ERROR", actual: stdout, }); failures++; failed_files[file] = 1; - } else { - try { - stdout = run_code(output); - if (test.expect_stdout != stdout) { - log("!!! failed\n---INPUT---\n{input}\n---EXPECTED STDOUT---\n{expected}\n---ACTUAL STDOUT---\n{actual}\n\n", { - input: input_code, - expected: test.expect_stdout, - actual: stdout, - }); - failures++; - failed_files[file] = 1; - } - } 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; - } } - } 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; } } } @@ -345,7 +330,13 @@ function run_code(code) { try { new vm.Script(code).runInNewContext({ console: console }, { timeout: 5000 }); return stdout; + } catch (ex) { + return ex; } finally { process.stdout.write = original_write; } } + +function same_stdout(expected, actual) { + return typeof expected == typeof actual && expected.toString() == actual.toString(); +}