From c8e97cec343310279067b219f0df1840a9e81e95 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 10 Oct 2014 23:10:06 +1100 Subject: [PATCH] add bin tests --- test/bin-fixtures/6to5/blacklist/stdin.js | 2 + test/bin-fixtures/6to5/blacklist/stdout.js | 4 + test/bin-fixtures/6to5/out-file/stdin.js | 2 + test/bin-fixtures/6to5/whitelist/stdin.js | 2 + test/bin-fixtures/6to5/whitelist/stdout.js | 4 + test/bin.js | 97 +++++++++++++++++----- 6 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 test/bin-fixtures/6to5/blacklist/stdin.js create mode 100644 test/bin-fixtures/6to5/blacklist/stdout.js create mode 100644 test/bin-fixtures/6to5/out-file/stdin.js create mode 100644 test/bin-fixtures/6to5/whitelist/stdin.js create mode 100644 test/bin-fixtures/6to5/whitelist/stdout.js diff --git a/test/bin-fixtures/6to5/blacklist/stdin.js b/test/bin-fixtures/6to5/blacklist/stdin.js new file mode 100644 index 0000000000..030f13f9fa --- /dev/null +++ b/test/bin-fixtures/6to5/blacklist/stdin.js @@ -0,0 +1,2 @@ +let MULTIPLER = 5; +arr.map(x => x * MULTIPLIER); diff --git a/test/bin-fixtures/6to5/blacklist/stdout.js b/test/bin-fixtures/6to5/blacklist/stdout.js new file mode 100644 index 0000000000..386cf5664a --- /dev/null +++ b/test/bin-fixtures/6to5/blacklist/stdout.js @@ -0,0 +1,4 @@ +(function() { + var MULTIPLER = 5; + arr.map(x => x * MULTIPLIER); +})(); diff --git a/test/bin-fixtures/6to5/out-file/stdin.js b/test/bin-fixtures/6to5/out-file/stdin.js new file mode 100644 index 0000000000..030f13f9fa --- /dev/null +++ b/test/bin-fixtures/6to5/out-file/stdin.js @@ -0,0 +1,2 @@ +let MULTIPLER = 5; +arr.map(x => x * MULTIPLIER); diff --git a/test/bin-fixtures/6to5/whitelist/stdin.js b/test/bin-fixtures/6to5/whitelist/stdin.js new file mode 100644 index 0000000000..030f13f9fa --- /dev/null +++ b/test/bin-fixtures/6to5/whitelist/stdin.js @@ -0,0 +1,2 @@ +let MULTIPLER = 5; +arr.map(x => x * MULTIPLIER); diff --git a/test/bin-fixtures/6to5/whitelist/stdout.js b/test/bin-fixtures/6to5/whitelist/stdout.js new file mode 100644 index 0000000000..bb8db60f68 --- /dev/null +++ b/test/bin-fixtures/6to5/whitelist/stdout.js @@ -0,0 +1,4 @@ +let MULTIPLER = 5; +arr.map(function(x) { + return x * MULTIPLIER; +}); diff --git a/test/bin.js b/test/bin.js index 8099967e35..057a04b43b 100644 --- a/test/bin.js +++ b/test/bin.js @@ -1,25 +1,67 @@ -var child = require("child_process"); -var fs = require("fs"); +var assert = require("assert"); +var child = require("child_process"); +var path = require("path"); +var fs = require("fs"); +var _ = require("lodash"); +var fixtureLoc = __dirname + "/bin-fixtures"; var tmpLoc = __dirname + "/tmp"; -var readTree = function () { +var build = function (binName, testName, opts) { + var testFixtureLoc = fixtureLoc + "/" + binName + "/" + testName; + if (fs.existsSync(testFixtureLoc)) { + _.each(fs.readdirSync(testFixtureLoc), function (filename) { + var key = path.basename(filename, path.extname(filename)); + var file = fs.readFileSync(testFixtureLoc + "/" + filename, "utf8").trim(); + opts[key] = file; + }); + } -}; + return function (callback) { + var args = [__dirname + "/../bin/" + binName].concat(opts.args); + var spawn = child.spawn(process.execPath, args); -var run = function (name, args, callback) { - args = [__dirname + "/../bin." + name].concat(args); - var spawn = child.spawn(process.execPath, args); + var stderr = ""; + var stdout = ""; - var data = ""; + spawn.stderr.on("data", function (chunk) { + stderr += chunk; + }); - spawn.stdout.on("write", function (chunk) { - data += chunk; - }); + spawn.stdout.on("data", function (chunk) { + stdout += chunk; + }); - spawn.on("close", function () { - callback(data); - }); + spawn.on("close", function () { + var err; + try { + if (opts.stderr) { + assert.equal(stderr.trim(), opts.stderr); + } else if (stderr) { + throw new Error("stderr: " + stderr); + } + + if (opts.stdout) { + assert.equal(stdout.trim(), opts.stdout); + } else if (stdout) { + throw new Error("stdout: " + stdout); + } + + _.each(opts.files, function (expect, filename) { + var actual = fs.readFileSync(filename, "utf8"); + assert.equal(actual, expect); + }); + } catch (e) { + err = e; + } + callback(err); + }); + + if (opts.stdin) { + spawn.stdin.write(opts.stdin); + spawn.stdin.end(); + } + }; }; before(function () { @@ -32,19 +74,32 @@ suite("bin/6to5", function () { test("--source-maps"); - test("--whitelist"); + test("--whitelist", build("6to5", "whitelist", { + args: ["--whitelist", "arrowFunctions"] + })); - test("--blacklist"); + test("--blacklist", build("6to5", "blacklist", { + args: ["--blacklist", "arrowFunctions"] + })); - test("--out-file"); + test("--out-file", build("6to5", "out-file", { + args: ["--out-file", "script.js"], + files: { + "script.js": "(function() {\n var MULTIPLER = 5;\n arr.map(function(x) {\n return x * MULTIPLIER;\n });\n})();" + } + })); test("--out-dir"); - - test("stdout"); }); suite("bin/6to5-node", function () { - test("--eval"); + test("--eval", build("6to5-node", "eval", { + args: ["--eval", "console.log([1, 2, 3].map(x => x * x));"], + stdout: "[ 1, 4, 9 ]" + })); - test("--print"); + test("--print", build("6to5-node", "print", { + args: ["--print", "--eval", "([1, 2, 3].map(x => x * x))"], + stdout: "[ 1, 4, 9 ]" + })); });