Skip to content

Commit

Permalink
support asynchronous test cases properly
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jan 19, 2021
1 parent 90ec468 commit 273c7bc
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 134 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ufuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
- uses: actions/checkout@v2
- name: Install GNU Core Utilities
if: ${{ startsWith(matrix.os, 'macos') }}
env:
HOMEBREW_NO_INSTALL_CLEANUP: 1
shell: bash
run: |
brew install coreutils
Expand Down
9 changes: 5 additions & 4 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ merge(Compressor.prototype, {
});
}

var RE_POSITIVE_INTEGER = /^(0|[1-9][0-9]*)$/;
(function(def) {
def(AST_Node, noop);

Expand Down Expand Up @@ -603,7 +604,7 @@ merge(Compressor.prototype, {
if (!is_arguments(def)) return;
var key = node.property;
if (key.is_constant()) key = key.value;
if (!(key instanceof AST_Node) && !/^[1-9]*[0-9]$/.test(key)) return;
if (!(key instanceof AST_Node) && !RE_POSITIVE_INTEGER.test(key)) return;
def.reassigned = true;
(key instanceof AST_Node ? def.scope.argnames : [ def.scope.argnames[key] ]).forEach(function(argname) {
if (argname instanceof AST_SymbolFunarg) argname.definition().fixed = false;
Expand Down Expand Up @@ -8160,7 +8161,7 @@ merge(Compressor.prototype, {
try {
var code = "n(function(" + self.args.slice(0, -1).map(function(arg) {
return arg.value;
}).join(",") + "){" + self.args[self.args.length - 1].value + "})";
}).join() + "){" + self.args[self.args.length - 1].value + "})";
var ast = parse(code);
var mangle = { ie8: compressor.option("ie8") };
ast.figure_out_scope(mangle);
Expand All @@ -8183,7 +8184,7 @@ merge(Compressor.prototype, {
make_node(AST_String, self, {
value: fun.argnames.map(function(arg) {
return arg.print_to_string();
}).join(",")
}).join(),
}),
make_node(AST_String, self.args[self.args.length - 1], {
value: code.get().replace(/^\{|\}$/g, "")
Expand Down Expand Up @@ -10846,7 +10847,7 @@ merge(Compressor.prototype, {
flush();
values.push(prop);
}
if (found && !generated && typeof key == "string" && /^[1-9]*[0-9]$/.test(key)) {
if (found && !generated && typeof key == "string" && RE_POSITIVE_INTEGER.test(key)) {
generated = true;
if (keys.has(key)) prop = keys.get(key)[0];
prop.key = make_node(AST_Number, prop, {
Expand Down
11 changes: 3 additions & 8 deletions test/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function reminify(orig_options, input_code, input_formatted, stdout) {
} else {
var toplevel = sandbox.has_toplevel(options);
var expected = stdout[toplevel ? 1 : 0];
var actual = run_code(result.code, toplevel);
var actual = sandbox.run_code(result.code, toplevel);
if (typeof expected != "string" && typeof actual != "string" && expected.name == actual.name) {
actual = expected;
}
Expand Down Expand Up @@ -244,11 +244,6 @@ function reminify(orig_options, input_code, input_formatted, stdout) {
return true;
}

function run_code(code, toplevel) {
var result = sandbox.run_code(code, toplevel);
return typeof result == "string" ? result.replace(/\u001b\[\d+m/g, "") : result;
}

function test_case(test) {
log(" Running test [{name}]", { name: test.name });
U.AST_Node.enable_validation();
Expand Down Expand Up @@ -380,7 +375,7 @@ function test_case(test) {
}
}
if (test.expect_stdout && (!test.node_version || semver.satisfies(process.version, test.node_version))) {
var stdout = [ run_code(input_code), run_code(input_code, true) ];
var stdout = [ sandbox.run_code(input_code), sandbox.run_code(input_code, true) ];
var toplevel = sandbox.has_toplevel({
compress: test.options,
mangle: test.mangle
Expand Down Expand Up @@ -409,7 +404,7 @@ function test_case(test) {
});
return false;
}
actual = run_code(output_code, toplevel);
actual = sandbox.run_code(output_code, toplevel);
if (!sandbox.same_stdout(test.expect_stdout, actual)) {
log([
"!!! failed",
Expand Down
30 changes: 19 additions & 11 deletions test/compress/awaits.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,22 +612,32 @@ issue_4340: {
call_expression: {
input: {
console.log(typeof async function(log) {
(await log)("FAIL");
(await log)("foo");
}(console.log).then);
console.log("bar");
}
expect_exact: 'console.log(typeof async function(log){(await log)("FAIL")}(console.log).then);'
expect_stdout: "function"
expect_exact: 'console.log(typeof async function(log){(await log)("foo")}(console.log).then);console.log("bar");'
expect_stdout: [
"function",
"bar",
"foo",
]
node_version: ">=8"
}

property_access_expression: {
input: {
console.log(typeof async function(con) {
(await con).log("FAIL");
(await con).log("foo");
}(console).then);
console.log("bar");
}
expect_exact: 'console.log(typeof async function(con){(await con).log("FAIL")}(console).then);'
expect_stdout: "function"
expect_exact: 'console.log(typeof async function(con){(await con).log("foo")}(console).then);console.log("bar");'
expect_stdout: [
"function",
"bar",
"foo",
]
node_version: ">=8"
}

Expand Down Expand Up @@ -685,20 +695,18 @@ reduce_iife_3: {
input: {
var a = "foo";
(async function() {
console.log(a);
console.log(await a);
console.log(a, await a, a, await a);
})();
a = "bar";
}
expect: {
var a = "foo";
(async function() {
console.log(a);
console.log(await a);
console.log(a, await a, a, await a);
})();
a = "bar";
}
expect_stdout: "foo"
expect_stdout: "foo foo bar bar"
node_version: ">=8"
}

Expand Down
49 changes: 49 additions & 0 deletions test/compress/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ console_log: {
]
}

console_log_console: {
input: {
var log = console.log;
log(console);
log(typeof console.log);
}
expect: {
var log = console.log;
log(console);
log(typeof console.log);
}
expect_stdout: [
"{ log: 'function(){}' }",
"function",
]
}

typeof_arguments: {
options = {
evaluate: true,
Expand Down Expand Up @@ -81,6 +98,38 @@ log_global: {
expect_stdout: "[object global]"
}

timers: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var count = 0, interval = 1000, duration = 3210;
var timer = setInterval(function() {
console.log(++count);
}, interval);
setTimeout(function() {
clearInterval(timer);
}, duration);
}
expect: {
var count = 0;
var timer = setInterval(function() {
console.log(++count);
}, 1000);
setTimeout(function() {
clearInterval(timer);
}, 3210);
}
expect_stdout: [
"1",
"2",
"3",
]
node_version: ">=0.12"
}

issue_4054: {
input: {
console.log({
Expand Down
6 changes: 3 additions & 3 deletions test/mocha/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("bin/uglifyjs", function() {
"--source-map", [
"names=true",
"url=inline",
].join(","),
].join(),
].join(" "), function(err, stdout) {
if (err) throw err;
var expected = [
Expand Down Expand Up @@ -84,7 +84,7 @@ describe("bin/uglifyjs", function() {
"--source-map", [
"names=false",
"url=inline",
].join(","),
].join(),
].join(" "), function(err, stdout) {
if (err) throw err;
var expected = [
Expand Down Expand Up @@ -171,7 +171,7 @@ describe("bin/uglifyjs", function() {
"content=" + mapFile,
"includeSources",
"url=inline",
].join(","),
].join(),
].join(" ");

var child = exec(command, function(err, stdout) {
Expand Down
24 changes: 8 additions & 16 deletions test/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
} else if (differs.error) {
differs.warnings = warnings;
return differs;
} else if (is_error(differs.unminified_result)
&& is_error(differs.minified_result)
} else if (sandbox.is_error(differs.unminified_result)
&& sandbox.is_error(differs.minified_result)
&& differs.unminified_result.name == differs.minified_result.name) {
return {
code: [
Expand Down Expand Up @@ -558,8 +558,8 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
log(code);
log(diff.error.stack);
log("*** Discarding permutation and continuing.");
} else if (is_error(diff.unminified_result)
&& is_error(diff.minified_result)
} else if (sandbox.is_error(diff.unminified_result)
&& sandbox.is_error(diff.minified_result)
&& diff.unminified_result.name == diff.minified_result.name) {
// ignore difference in error messages caused by minification
diff_error_message = testcase;
Expand Down Expand Up @@ -600,10 +600,10 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
}
var lines = [ "" ];
if (isNaN(max_timeout)) {
lines.push("// minify error: " + to_comment(strip_color_codes(differs.minified_result.stack)));
lines.push("// minify error: " + to_comment(differs.minified_result.stack));
} else {
var unminified_result = strip_color_codes(differs.unminified_result);
var minified_result = strip_color_codes(differs.minified_result);
var unminified_result = differs.unminified_result;
var minified_result = differs.minified_result;
if (trim_trailing_whitespace(unminified_result) == trim_trailing_whitespace(minified_result)) {
lines.push(
"// (stringified)",
Expand All @@ -624,10 +624,6 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
}
};

function strip_color_codes(value) {
return ("" + value).replace(/\u001b\[\d+m/g, "");
}

function to_comment(value) {
return ("" + value).replace(/\n/g, "\n// ");
}
Expand Down Expand Up @@ -665,12 +661,8 @@ function has_loopcontrol(body, loop, label) {
return found;
}

function is_error(result) {
return result && typeof result.name == "string" && typeof result.message == "string";
}

function is_timed_out(result) {
return is_error(result) && /timed out/.test(result.message);
return sandbox.is_error(result) && /timed out/.test(result.message);
}

function is_statement(node) {
Expand Down
Loading

0 comments on commit 273c7bc

Please sign in to comment.