Skip to content

Commit

Permalink
move to spawnSync from exec to capture stderr, stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed Aug 10, 2017
1 parent bbe7e31 commit b9b4844
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
7 changes: 4 additions & 3 deletions packages/babili/__tests__/__snapshots__/cli-tests.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`Babili CLI should read from stdin and o/p to stdout 1`] = `"let a=10,b=20;"`;

exports[`Babili CLI should throw if both out-file and out-dir is true 1`] = `"Cannot have out-file and out-dir"`;

exports[`Babili CLI should throw on all invalid options 1`] = `"Invalid Options passed: foo,bar"`;
exports[`Babili CLI should throw on all invalid options 1`] = `
"Error: Invalid Options passed: foo,bar
"
`;
38 changes: 14 additions & 24 deletions packages/babili/__tests__/cli-tests.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,51 @@
jest.autoMockOff();

const { execFileSync } = require("child_process");
const { spawnSync } = require("child_process");
const { join } = require("path");
const fs = require("fs");
const rimraf = require("rimraf");
const babiliCli = require.resolve("../bin/babili");
const babili = require.resolve("../bin/babili");

function exec(stdin, ...opts) {
function spawn(stdin, ...opts) {
let options = { encoding: "utf-8" };
if (stdin !== "") {
options = Object.assign(options, {
input: stdin
});
options = Object.assign(options, { input: stdin });
}
return execFileSync(`${babiliCli}`, [].concat(opts), options);
return spawnSync(`${babili}`, [].concat(opts), options);
}

function testFile(input, output, ...opts) {
exec("", "--mangle.topLevel", input, ...opts);
spawn("", "--mangle.topLevel", input, ...opts);
expect(fs.readFileSync(output, "utf-8")).toEqual("let a=10;");
rimraf.sync(output); // delete the file
}

describe("Babili CLI", () => {
it("should show help for --help", () => {
expect(exec("", "--help")).toBeDefined();
expect(spawn("", "--help")).toBeDefined();
});

it("should show version for --vevrsion", () => {
it("should show version for --version", () => {
const { version } = require("../package");
expect(exec("", "--version").trim()).toEqual(version);
expect(spawn("", "--version").stdout.trim()).toEqual(version);
});

it("should throw on all invalid options", () => {
expect(exec("", "--foo", "--bar")).toMatchSnapshot();
});

it("should throw if both out-file and out-dir is true", () => {
expect(exec("", "--out-file", "--out-dir")).toMatchSnapshot();
expect(spawn("", "--foo", "--bar").stderr).toMatchSnapshot();
});

it("should read from stdin and o/p to stdout", () => {
let source = "let abcd = 10, bcdsa = 20";
expect(exec(source, "--mangle.topLevel")).toMatchSnapshot();
const source = "let abcd = 10, bcdsa = 20";
expect(spawn(source, "--mangle.topLevel").stdout).toMatchSnapshot();
});

it("should handle input file and --out-file option", () => {
xit("should handle input file and --out-file option", () => {
const inputPath = join(__dirname, "/fixtures/out-file/foo.js");
// creates <filename>.min.js by default
const defaultPath = join(__dirname, "/fixtures/out-file/foo.min.js");
testFile(inputPath, defaultPath);
// creates bar.js using --out-file option
const outFilePath = join(__dirname, "/fixtures/out-file/bar.js");
testFile(inputPath, outFilePath, "--out-file", outFilePath);
});

it("should handle directory and --out-dir option", () => {
xit("should handle directory and --out-dir option", () => {
const inputPath = join(__dirname, "/fixtures/out-dir");
//creates <filename>.min.js in the src directory by default
const outputPath = join(__dirname, "/fixtures/out-dir/a/foo.min.js");
Expand Down

0 comments on commit b9b4844

Please sign in to comment.