From 3563d8c09e36be8f8b9cb9500852778f8d191d5d Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 16 Mar 2017 23:20:06 +0800 Subject: [PATCH] extend `test/run-tests.js` to optionally execute uglified output (#1604) fixes #1588 --- test/compress/issue-1588.js | 87 +++++++++++++++++++++++++++++++++++++ test/run-tests.js | 85 ++++++++++++++++++++++++++++++++---- 2 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 test/compress/issue-1588.js diff --git a/test/compress/issue-1588.js b/test/compress/issue-1588.js new file mode 100644 index 00000000..ff25635c --- /dev/null +++ b/test/compress/issue-1588.js @@ -0,0 +1,87 @@ +screw_ie8: { + options = { + screw_ie8: true, + } + mangle = { + screw_ie8: true, + } + input: { + try { throw "foo"; } catch (x) { console.log(x); } + } + expect_exact: 'try{throw"foo"}catch(o){console.log(o)}' + expect_stdout: [ + "foo" + ] +} + +support_ie8: { + options = { + screw_ie8: false, + } + mangle = { + screw_ie8: false, + } + input: { + try { throw "foo"; } catch (x) { console.log(x); } + } + expect_exact: 'try{throw"foo"}catch(x){console.log(x)}' + expect_stdout: "foo" +} + +safe_undefined: { + options = { + conditionals: true, + if_return: true, + unsafe: false, + } + mangle = {} + input: { + var a, c; + console.log(function(undefined) { + return function() { + if (a) + return b; + if (c) + return d; + }; + }(1)()); + } + expect: { + var a, c; + console.log(function(n) { + return function() { + return a ? b : c ? d : void 0; + }; + }(1)()); + } + expect_stdout: true +} + +unsafe_undefined: { + options = { + conditionals: true, + if_return: true, + unsafe: true, + } + mangle = {} + input: { + var a, c; + console.log(function(undefined) { + return function() { + if (a) + return b; + if (c) + return d; + }; + }()()); + } + expect: { + var a, c; + console.log(function(n) { + return function() { + return a ? b : c ? d : n; + }; + }()()); + } + expect_stdout: true +} diff --git a/test/run-tests.js b/test/run-tests.js index 36d26ef7..898bb793 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -6,6 +6,7 @@ var U = require("../tools/node"); var path = require("path"); var fs = require("fs"); var assert = require("assert"); +var vm = require("vm"); var tests_dir = path.dirname(module.filename); var failures = 0; @@ -165,6 +166,51 @@ function run_compress_tests() { failed_files[file] = 1; } } + 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", { + input: input_code, + expected: test.expect_stdout, + 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; + } + } } } var tests = parse_test(path.resolve(dir, file)); @@ -215,9 +261,9 @@ function parse_test(file) { } function read_string(stat) { - if (stat.TYPE === "SimpleStatement") { + if (stat.TYPE == "SimpleStatement") { var body = stat.body; - out: switch(body.TYPE) { + switch(body.TYPE) { case "String": return body.value; case "Array": @@ -243,12 +289,13 @@ function parse_test(file) { return true; } if (node instanceof U.AST_LabeledStatement) { + var label = node.label; assert.ok( - ["input", "expect", "expect_exact", "expect_warnings"].indexOf(node.label.name) >= 0, + ["input", "expect", "expect_exact", "expect_warnings", "expect_stdout"].indexOf(label.name) >= 0, tmpl("Unsupported label {name} [{line},{col}]", { - name: node.label.name, - line: node.label.start.line, - col: node.label.start.col + name: label.name, + line: label.start.line, + col: label.start.col }) ); var stat = node.body; @@ -256,10 +303,16 @@ function parse_test(file) { if (stat.body.length == 1) stat = stat.body[0]; else if (stat.body.length == 0) stat = new U.AST_EmptyStatement(); } - if (node.label.name === "expect_exact") { - test[node.label.name] = read_string(stat); + if (label.name == "expect_exact") { + test[label.name] = read_string(stat); + } else if (label.name == "expect_stdout") { + if (stat.TYPE == "SimpleStatement" && stat.body instanceof U.AST_Boolean) { + test[label.name] = stat.body.value; + } else { + test[label.name] = read_string(stat) + "\n"; + } } else { - test[node.label.name] = stat; + test[label.name] = stat; } return true; } @@ -281,3 +334,17 @@ function evaluate(code) { code = make_code(code, { beautify: true }); return new Function("return(" + code + ")")(); } + +function run_code(code) { + var stdout = ""; + var original_write = 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 = original_write; + } +} -- 2.34.1