Skip to content

Commit

Permalink
test(cli): improve BaseGenerator test suite to support required args
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Sep 23, 2019
1 parent 3e640ae commit 0c1ab24
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions packages/cli/test/integration/lib/base-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const path = require('path');
const mockStdin = require('mock-stdin');
const process = require('process');

module.exports = function(generator) {
module.exports = function(generator, env = {}) {
if (!env.args) env = {...env, args: []};

return function() {
describe('usage', () => {
it('prints lb4', () => {
process.chdir(path.resolve(__dirname, '..', '..', '..'));
const gen = testUtils.testSetUpGen(generator);
const gen = testUtils.testSetUpGen(generator, env);
const helpText = gen.help();
assert(helpText.match(/lb4 /));
assert(!helpText.match(/loopback4:/));
Expand All @@ -25,17 +27,17 @@ module.exports = function(generator) {

describe('exit', () => {
it('does nothing if false is passed', () => {
const gen = testUtils.testSetUpGen(generator);
const gen = testUtils.testSetUpGen(generator, env);
gen.exit(false);
assert(gen.exitGeneration === undefined);
});
it('sets "exitGeneration" to true if called with no argument', () => {
const gen = testUtils.testSetUpGen(generator);
const gen = testUtils.testSetUpGen(generator, env);
gen.exit();
assert(gen.exitGeneration === true);
});
it('sets "exitGeneration" to the error passed to itself', () => {
const gen = testUtils.testSetUpGen(generator);
const gen = testUtils.testSetUpGen(generator, env);
gen.exit(new Error('oh no'));
assert(gen.exitGeneration instanceof Error);
assert.equal(gen.exitGeneration.message, 'oh no');
Expand All @@ -51,7 +53,8 @@ module.exports = function(generator) {
it('accepts --config', async () => {
const jsonFile = path.join(__dirname, 'base-config.json');
const gen = testUtils.testSetUpGen(generator, {
args: ['--config', jsonFile],
...env,
args: ['--config', jsonFile, ...env.args],
});
await gen.setOptions();
assert.equal(gen.options['config'], jsonFile);
Expand All @@ -61,7 +64,8 @@ module.exports = function(generator) {
it('options from json file do not override', async () => {
const jsonFile = path.join(__dirname, 'base-config.json');
const gen = testUtils.testSetUpGen(generator, {
args: ['--name', 'abc', '--config', jsonFile],
...env,
args: ['--name', 'abc', '--config', jsonFile, ...env.args],
});
await gen.setOptions();
assert.equal(gen.options['config'], jsonFile);
Expand All @@ -71,7 +75,12 @@ module.exports = function(generator) {

it('fails fast if --config has invalid value', async () => {
const gen = testUtils.testSetUpGen(generator, {
args: ['--config', path.join(__dirname, 'base-config-invalid.json')],
...env,
args: [
'--config',
path.join(__dirname, 'base-config-invalid.json'),
...env.args,
],
});
await gen.setOptions();
assert(gen.exitGeneration instanceof Error);
Expand All @@ -91,7 +100,8 @@ module.exports = function(generator) {

it('accepts --config', async () => {
const gen = testUtils.testSetUpGen(generator, {
args: ['--config', jsonValue],
...env,
args: ['--config', jsonValue, ...env.args],
});
await gen.setOptions();
assert.equal(gen.options['config'], jsonValue);
Expand All @@ -100,7 +110,8 @@ module.exports = function(generator) {

it('options from json file do not override', async () => {
const gen = testUtils.testSetUpGen(generator, {
args: ['--name', 'abc', '--config', jsonValue],
...env,
args: ['--name', 'abc', '--config', jsonValue, ...env.args],
});
await gen.setOptions();
assert.equal(gen.options['config'], jsonValue);
Expand All @@ -110,7 +121,8 @@ module.exports = function(generator) {

it('fails fast if --config has invalid value', async () => {
const gen = testUtils.testSetUpGen(generator, {
args: ['--config', invalidJsonValue],
...env,
args: ['--config', invalidJsonValue, ...env.args],
});
await gen.setOptions();
assert(gen.exitGeneration instanceof Error);
Expand All @@ -134,7 +146,8 @@ module.exports = function(generator) {

it('accepts --config stdin', () => {
const gen = testUtils.testSetUpGen(generator, {
args: ['--config', 'stdin'],
...env,
args: ['--config', 'stdin', ...env.args],
});
const promise = gen.setOptions();
assert.equal(gen.options['config'], 'stdin');
Expand All @@ -151,7 +164,8 @@ module.exports = function(generator) {

it('reports invalid json from stdin', () => {
const gen = testUtils.testSetUpGen(generator, {
args: ['--config', 'stdin'],
...env,
args: ['--config', 'stdin', ...env.args],
});
const promise = gen.setOptions();
assert.equal(gen.options['config'], 'stdin');
Expand Down

0 comments on commit 0c1ab24

Please sign in to comment.