Only set options in cli if different from default (#4507)
* CLI: Only set options if different from default Currently default values (like comments: true) will be set always for the transform. This behaviour dos not allow for setting this options from babelrc as the default would always have precedence. * Add new tests for comments Also ensure that the babelrc in the fixture folder is copied over to the working directory during tests
This commit is contained in:
parent
993f887240
commit
ff78fb19f7
@ -107,7 +107,7 @@ if (errors.length) {
|
|||||||
let opts = exports.opts = {};
|
let opts = exports.opts = {};
|
||||||
|
|
||||||
each(options, function (opt, key) {
|
each(options, function (opt, key) {
|
||||||
if (commander[key] !== undefined) {
|
if (commander[key] !== undefined && commander[key] !== opt.default) {
|
||||||
opts[key] = commander[key];
|
opts[key] = commander[key];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
7
packages/babel-cli/test/fixtures/babel/filename --no-comments/in-files/script.js
vendored
Normal file
7
packages/babel-cli/test/fixtures/babel/filename --no-comments/in-files/script.js
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
/*
|
||||||
|
Test comment
|
||||||
|
*/
|
||||||
|
|
||||||
|
arr.map(x => x * MULTIPLIER);
|
||||||
|
|
||||||
|
// END OF FILE
|
||||||
3
packages/babel-cli/test/fixtures/babel/filename --no-comments/options.json
vendored
Normal file
3
packages/babel-cli/test/fixtures/babel/filename --no-comments/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"args": ["script.js", "--out-file", "script2.js", "--no-comments"]
|
||||||
|
}
|
||||||
5
packages/babel-cli/test/fixtures/babel/filename --no-comments/out-files/script2.js
vendored
Normal file
5
packages/babel-cli/test/fixtures/babel/filename --no-comments/out-files/script2.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
arr.map(function (x) {
|
||||||
|
return x * MULTIPLIER;
|
||||||
|
});
|
||||||
3
packages/babel-cli/test/fixtures/babel/filename babelrc no comments/.babelrc
vendored
Normal file
3
packages/babel-cli/test/fixtures/babel/filename babelrc no comments/.babelrc
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"comments": false
|
||||||
|
}
|
||||||
7
packages/babel-cli/test/fixtures/babel/filename babelrc no comments/in-files/script.js
vendored
Normal file
7
packages/babel-cli/test/fixtures/babel/filename babelrc no comments/in-files/script.js
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
/*
|
||||||
|
Test comment
|
||||||
|
*/
|
||||||
|
|
||||||
|
arr.map(x => x * MULTIPLIER);
|
||||||
|
|
||||||
|
// END OF FILE
|
||||||
3
packages/babel-cli/test/fixtures/babel/filename babelrc no comments/options.json
vendored
Normal file
3
packages/babel-cli/test/fixtures/babel/filename babelrc no comments/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"args": ["script.js", "--out-file", "script2.js"]
|
||||||
|
}
|
||||||
5
packages/babel-cli/test/fixtures/babel/filename babelrc no comments/out-files/script2.js
vendored
Normal file
5
packages/babel-cli/test/fixtures/babel/filename babelrc no comments/out-files/script2.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
arr.map(function (x) {
|
||||||
|
return x * MULTIPLIER;
|
||||||
|
});
|
||||||
7
packages/babel-cli/test/fixtures/babel/filename default comments/in-files/script.js
vendored
Normal file
7
packages/babel-cli/test/fixtures/babel/filename default comments/in-files/script.js
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
/*
|
||||||
|
Test comment
|
||||||
|
*/
|
||||||
|
|
||||||
|
arr.map(x => x * MULTIPLIER);
|
||||||
|
|
||||||
|
// END OF FILE
|
||||||
3
packages/babel-cli/test/fixtures/babel/filename default comments/options.json
vendored
Normal file
3
packages/babel-cli/test/fixtures/babel/filename default comments/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"args": ["script.js", "--out-file", "script2.js"]
|
||||||
|
}
|
||||||
11
packages/babel-cli/test/fixtures/babel/filename default comments/out-files/script2.js
vendored
Normal file
11
packages/babel-cli/test/fixtures/babel/filename default comments/out-files/script2.js
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
/*
|
||||||
|
Test comment
|
||||||
|
*/
|
||||||
|
|
||||||
|
arr.map(function (x) {
|
||||||
|
return x * MULTIPLIER;
|
||||||
|
});
|
||||||
|
|
||||||
|
// END OF FILE
|
||||||
@ -12,25 +12,24 @@ var fs = require("fs");
|
|||||||
var pathExists = require("path-exists");
|
var pathExists = require("path-exists");
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
var fixtureLoc = __dirname + "/fixtures";
|
var fixtureLoc = path.join(__dirname, "fixtures");
|
||||||
var tmpLoc = __dirname + "/tmp";
|
var tmpLoc = path.join(__dirname, "tmp");
|
||||||
|
|
||||||
var presetLocs = [
|
var presetLocs = [
|
||||||
__dirname + "/../../babel-preset-es2015",
|
path.join(__dirname, "../../babel-preset-es2015"),
|
||||||
__dirname + "/../../babel-preset-react"
|
path.join(__dirname, "../../babel-preset-react")
|
||||||
].join(",");
|
].join(",");
|
||||||
|
|
||||||
var pluginLocs = [
|
var pluginLocs = [
|
||||||
__dirname + "/../../babel-plugin-transform-strict-mode",
|
path.join(__dirname, "/../../babel-plugin-transform-strict-mode"),
|
||||||
__dirname + "/../../babel-plugin-transform-es2015-modules-commonjs",
|
path.join(__dirname, "/../../babel-plugin-transform-es2015-modules-commonjs"),
|
||||||
].join(",");
|
].join(",");
|
||||||
|
|
||||||
var readDir = function (loc) {
|
var readDir = function (loc) {
|
||||||
var files = {};
|
var files = {};
|
||||||
if (pathExists.sync(loc)) {
|
if (pathExists.sync(loc)) {
|
||||||
_.each(readdir(loc), function (filename) {
|
_.each(readdir(loc), function (filename) {
|
||||||
var contents = helper.readFile(loc + "/" + filename);
|
files[filename] = helper.readFile(path.join(loc, filename));
|
||||||
files[filename] = contents;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return files;
|
return files;
|
||||||
@ -77,7 +76,7 @@ var assertTest = function (stdout, stderr, opts) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var buildTest = function (binName, testName, opts) {
|
var buildTest = function (binName, testName, opts) {
|
||||||
var binLoc = path.normalize(__dirname + "/../../babel-cli/lib/" + binName);
|
var binLoc = path.join(__dirname, "../lib", binName);
|
||||||
|
|
||||||
return function (callback) {
|
return function (callback) {
|
||||||
this.timeout(5000);
|
this.timeout(5000);
|
||||||
@ -142,22 +141,22 @@ var clear = function () {
|
|||||||
_.each(fs.readdirSync(fixtureLoc), function (binName) {
|
_.each(fs.readdirSync(fixtureLoc), function (binName) {
|
||||||
if (binName[0] === ".") return;
|
if (binName[0] === ".") return;
|
||||||
|
|
||||||
var suiteLoc = fixtureLoc + "/" + binName;
|
var suiteLoc = path.join(fixtureLoc, binName);
|
||||||
suite("bin/" + binName, function () {
|
suite("bin/" + binName, function () {
|
||||||
_.each(fs.readdirSync(fixtureLoc + "/" + binName), function (testName) {
|
_.each(fs.readdirSync(suiteLoc), function (testName) {
|
||||||
if (testName[0] === ".") return;
|
if (testName[0] === ".") return;
|
||||||
|
|
||||||
var testLoc = suiteLoc + "/" + testName;
|
var testLoc = path.join(suiteLoc, testName);
|
||||||
|
|
||||||
var opts = {
|
var opts = {
|
||||||
args: []
|
args: []
|
||||||
};
|
};
|
||||||
|
|
||||||
var optionsLoc = testLoc + "/options.json"
|
var optionsLoc = path.join(testLoc, "options.json");
|
||||||
if (pathExists.sync(optionsLoc)) _.merge(opts, require(optionsLoc));
|
if (pathExists.sync(optionsLoc)) _.merge(opts, require(optionsLoc));
|
||||||
|
|
||||||
_.each(["stdout", "stdin", "stderr"], function (key) {
|
_.each(["stdout", "stdin", "stderr"], function (key) {
|
||||||
var loc = testLoc + "/" + key + ".txt";
|
var loc = path.join(testLoc, key + ".txt");
|
||||||
if (pathExists.sync(loc)) {
|
if (pathExists.sync(loc)) {
|
||||||
opts[key] = helper.readFile(loc);
|
opts[key] = helper.readFile(loc);
|
||||||
} else {
|
} else {
|
||||||
@ -165,8 +164,14 @@ _.each(fs.readdirSync(fixtureLoc), function (binName) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
opts.outFiles = readDir(testLoc + "/out-files");
|
opts.outFiles = readDir(path.join(testLoc, "out-files"));
|
||||||
opts.inFiles = readDir(testLoc + "/in-files");
|
opts.inFiles = readDir(path.join(testLoc, "in-files"));
|
||||||
|
|
||||||
|
var babelrcLoc = path.join(testLoc, ".babelrc");
|
||||||
|
if (pathExists.sync(babelrcLoc)) {
|
||||||
|
// copy .babelrc file to tmp directory
|
||||||
|
opts.inFiles['.babelrc'] = helper.readFile(babelrcLoc);
|
||||||
|
}
|
||||||
|
|
||||||
test(testName, buildTest(binName, testName, opts));
|
test(testName, buildTest(binName, testName, opts));
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user