From 0c1ab24c722753d8854f88b830fa61664d8ed792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 23 Sep 2019 13:18:58 +0200 Subject: [PATCH] test(cli): improve BaseGenerator test suite to support required args MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miroslav Bajtoš --- .../test/integration/lib/base-generator.js | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/cli/test/integration/lib/base-generator.js b/packages/cli/test/integration/lib/base-generator.js index 4a76e18ff2aa..18dffa1b8bc8 100644 --- a/packages/cli/test/integration/lib/base-generator.js +++ b/packages/cli/test/integration/lib/base-generator.js @@ -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:/)); @@ -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'); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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'); @@ -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');