maintain compatibility options when testing (#4376)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 13 Dec 2020 06:26:45 +0000 (06:26 +0000)
committerGitHub <noreply@github.com>
Sun, 13 Dec 2020 06:26:45 +0000 (14:26 +0800)
test/reduce.js
test/ufuzz/index.js

index 3cd7f67..40c447b 100644 (file)
@@ -18,9 +18,18 @@ var sandbox = require("./sandbox");
 
 Error.stackTraceLimit = Infinity;
 module.exports = function reduce_test(testcase, minify_options, reduce_options) {
-    if (testcase instanceof U.AST_Node) testcase = testcase.print_to_string();
     minify_options = minify_options || {};
     reduce_options = reduce_options || {};
+    var print_options = {};
+    [
+        "ie8",
+        "v8",
+        "webkit",
+    ].forEach(function(name) {
+        var value = minify_options[name] || minify_options.output && minify_options.output[name];
+        if (value) print_options[name] = value;
+    });
+    if (testcase instanceof U.AST_Node) testcase = testcase.print_to_string(print_options);
     var max_iterations = reduce_options.max_iterations || 1000;
     var max_timeout = reduce_options.max_timeout || 10000;
     var warnings = [];
@@ -459,7 +468,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
                         return node;
                     }
                 }));
-                var code = testcase_ast.print_to_string();
+                var code = testcase_ast.print_to_string(print_options);
                 var diff = test_for_diff(code, minify_options, result_cache, max_timeout);
                 if (diff && !diff.timed_out && !diff.error) {
                     testcase = code;
@@ -483,7 +492,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
                 var code_ast = testcase_ast.clone(true).transform(tt);
                 if (!CHANGED) break;
                 try {
-                    var code = code_ast.print_to_string();
+                    var code = code_ast.print_to_string(print_options);
                 } catch (ex) {
                     // AST is not well formed.
                     // no harm done - just log the error, ignore latest change and continue iterating.
@@ -525,11 +534,13 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
         var beautified = U.minify(testcase, {
             compress: false,
             mangle: false,
-            output: {
-                beautify: true,
-                braces: true,
-                comments: true,
-            },
+            output: function() {
+                var options = JSON.parse(JSON.stringify(print_options));
+                options.beautify = true;
+                options.braces = true;
+                options.comments = true;
+                return options;
+            }(),
         });
         testcase = {
             code: testcase,
index 0b16a59..976fc44 100644 (file)
@@ -1445,25 +1445,35 @@ function errorln(msg) {
     writeln(process.stderr, msg);
 }
 
-function try_beautify(code, toplevel, result, printfn) {
-    var beautified = UglifyJS.minify(code, {
-        compress: false,
-        mangle: false,
-        output: {
-            beautify: true,
-            braces: true,
-        },
-    });
+function try_beautify(code, toplevel, result, printfn, options) {
+    var beautified = UglifyJS.minify(code, JSON.parse(beautify_options));
     if (beautified.error) {
         printfn("// !!! beautify failed !!!");
         printfn(beautified.error);
-    } else if (sandbox.same_stdout(sandbox.run_code(beautified.code, toplevel), result)) {
+        beautified = null;
+    } else if (!sandbox.same_stdout(sandbox.run_code(beautified.code, toplevel), result)) {
+        beautified = null;
+    } else if (options) {
+        var uglified = UglifyJS.minify(beautified.code, JSON.parse(options));
+        var expected, actual;
+        if (typeof uglify_code != "string" || uglified.error) {
+            expected = uglify_code;
+            actual = uglified.error;
+        } else {
+            expected = uglify_result;
+            actual = sandbox.run_code(uglified.code, toplevel);
+        }
+        if (!sandbox.same_stdout(expected, actual)) {
+            beautified = null;
+        }
+    }
+    if (beautified) {
         printfn("// (beautified)");
         printfn(beautified.code);
-        return;
+    } else {
+        printfn("//");
+        printfn(code);
     }
-    printfn("//");
-    printfn(code);
 }
 
 var default_options = UglifyJS.default_options();
@@ -1536,37 +1546,7 @@ function log(options) {
     errorln("//=============================================================");
     if (!ok) errorln("// !!!!!! Failed... round " + round);
     errorln("// original code");
-    var beautified = UglifyJS.minify(original_code, {
-        compress: false,
-        mangle: false,
-        output: {
-            beautify: true,
-            braces: true,
-        },
-    });
-    if (beautified.error) {
-        errorln("// !!! beautify failed !!!");
-        errorln(beautified.error);
-        errorln("//");
-        errorln(original_code);
-    } else {
-        var uglified = UglifyJS.minify(beautified.code, JSON.parse(options));
-        var expected, actual;
-        if (typeof uglify_code != "string" || uglified.error) {
-            expected = uglify_code;
-            actual = uglified.error;
-        } else {
-            expected = uglify_result;
-            actual = sandbox.run_code(uglified.code, toplevel);
-        }
-        if (sandbox.same_stdout(expected, actual)) {
-            errorln("// (beautified)");
-            errorln(beautified.code);
-        } else {
-            errorln("//");
-            errorln(original_code);
-        }
-    }
+    try_beautify(original_code, toplevel, original_result, errorln, options);
     errorln();
     errorln();
     errorln("//-------------------------------------------------------------");
@@ -1698,13 +1678,23 @@ function patch_try_catch(orig, toplevel) {
     }
 }
 
+var beautify_options = {
+    compress: false,
+    mangle: false,
+    output: {
+        beautify: true,
+        braces: true,
+    },
+};
 var minify_options = require("./options.json");
 if (SUPPORT.destructuring && typeof sandbox.run_code("console.log([ 1 ], {} = 2);") != "string") {
+    beautify_options.output.v8 = true;
     minify_options.forEach(function(o) {
         if (!("output" in o)) o.output = {};
         o.output.v8 = true;
     });
 }
+beautify_options = JSON.stringify(beautify_options);
 minify_options = minify_options.map(JSON.stringify);
 var original_code, original_result, errored;
 var uglify_code, uglify_result, ok;