From 99564bfc9e41285daa252e7b3bbc1a51105be8c7 Mon Sep 17 00:00:00 2001 From: Dechen Zhuang Date: Wed, 1 Jul 2020 14:56:38 +0800 Subject: [PATCH] fix: non-zero value on failure and print errors #92 (#99) * fix #92 * throw error instead of exit process * cover non-zero exit code cases --- build/cli.js | 3 +- build/swagger-inline.js | 18 +++-- src/swagger-inline.js | 71 ++++++++++--------- tests/cli.test.js | 31 ++++++++ tests/fixtures/code/swagger-api-with-error.js | 19 +++++ 5 files changed, 102 insertions(+), 40 deletions(-) create mode 100644 tests/fixtures/code/swagger-api-with-error.js diff --git a/build/cli.js b/build/cli.js index 4608c8d..e0725f1 100644 --- a/build/cli.js +++ b/build/cli.js @@ -30,7 +30,8 @@ function Cli(args) { console.log(output); })["catch"](function (err) { console.log('An error occured:'); - console.log(err); + console.error(err); + process.exit(-1); }); } diff --git a/build/swagger-inline.js b/build/swagger-inline.js index 3af14ac..3d2d672 100644 --- a/build/swagger-inline.js +++ b/build/swagger-inline.js @@ -85,7 +85,7 @@ function swaggerInline(globPatterns, providedOptions) { }); var endpoints = []; var schemas = []; - successfulFiles.forEach(function (fileInfo) { + return Promise.all(successfulFiles.map(function (fileInfo) { try { var newEndpoints = Extractor.extractEndpointsFromCode(fileInfo.fileData, { filename: fileInfo.fileName, @@ -104,15 +104,19 @@ function swaggerInline(globPatterns, providedOptions) { }); schemas = _.concat(schemas, scheme); + return Promise.resolve(); } catch (e) { - throw new Error(e.toString(), fileInfo.fileName); + return Promise.reject(new Error(e.toString(), fileInfo.fileName)); } + })).then(function () { + log("".concat(endpoints.length, " definitions found...")); + log("".concat(schemas.length, " schemas found...")); + var baseObjWithEndpoints = mergeEndpointsWithBase(baseObj, endpoints); + var swagger = mergeSchemasWithBase(baseObjWithEndpoints, schemas); + return outputResult(swagger, options); + })["catch"](function (e) { + return Promise.reject(e); }); - log("".concat(endpoints.length, " definitions found...")); - log("".concat(schemas.length, " schemas found...")); - var baseObjWithEndpoints = mergeEndpointsWithBase(baseObj, endpoints); - var swagger = mergeSchemasWithBase(baseObjWithEndpoints, schemas); - return outputResult(swagger, options); }); }); }); diff --git a/src/swagger-inline.js b/src/swagger-inline.js index 052adfe..5ae21c2 100644 --- a/src/swagger-inline.js +++ b/src/swagger-inline.js @@ -74,38 +74,45 @@ function swaggerInline(globPatterns, providedOptions) { let endpoints = []; let schemas = []; - successfulFiles.forEach(fileInfo => { - try { - let newEndpoints = Extractor.extractEndpointsFromCode(fileInfo.fileData, { - filename: fileInfo.fileName, - scope: options.getScope(), - }); - - newEndpoints = Loader.addResponse(newEndpoints); - - newEndpoints = Loader.expandParams(newEndpoints, swaggerVersion); - endpoints = _.concat(endpoints, newEndpoints); - - const scheme = Extractor.extractSchemasFromCode(fileInfo.fileData, { - filename: fileInfo.fileName, - scope: options.getScope(), - }); - _.remove(scheme, s => { - return _.isEmpty(s); - }); - schemas = _.concat(schemas, scheme); - } catch (e) { - throw new Error(e.toString(), fileInfo.fileName); - } - }); - - log(`${endpoints.length} definitions found...`); - log(`${schemas.length} schemas found...`); - - const baseObjWithEndpoints = mergeEndpointsWithBase(baseObj, endpoints); - const swagger = mergeSchemasWithBase(baseObjWithEndpoints, schemas); - - return outputResult(swagger, options); + return Promise.all( + successfulFiles.map(fileInfo => { + try { + let newEndpoints = Extractor.extractEndpointsFromCode(fileInfo.fileData, { + filename: fileInfo.fileName, + scope: options.getScope(), + }); + + newEndpoints = Loader.addResponse(newEndpoints); + + newEndpoints = Loader.expandParams(newEndpoints, swaggerVersion); + endpoints = _.concat(endpoints, newEndpoints); + + const scheme = Extractor.extractSchemasFromCode(fileInfo.fileData, { + filename: fileInfo.fileName, + scope: options.getScope(), + }); + _.remove(scheme, s => { + return _.isEmpty(s); + }); + schemas = _.concat(schemas, scheme); + return Promise.resolve(); + } catch (e) { + return Promise.reject(new Error(e.toString(), fileInfo.fileName)); + } + }) + ) + .then(() => { + log(`${endpoints.length} definitions found...`); + log(`${schemas.length} schemas found...`); + + const baseObjWithEndpoints = mergeEndpointsWithBase(baseObj, endpoints); + const swagger = mergeSchemasWithBase(baseObjWithEndpoints, schemas); + + return outputResult(swagger, options); + }) + .catch(e => { + return Promise.reject(e); + }); }); }); }); diff --git a/tests/cli.test.js b/tests/cli.test.js index a039564..7a7ff23 100644 --- a/tests/cli.test.js +++ b/tests/cli.test.js @@ -1,7 +1,38 @@ const cli = require('../src/cli'); +const path = require('path'); +const exec = require('child_process').exec; + +function runCommand(cmd, cwd) { + return new Promise(resolve => { + exec(cmd, { cwd }, (error, stdout, stderr) => { + resolve({ + code: error && error.code ? error.code : 0, + error, + stdout, + stderr, + }); + }); + }); +} describe('Cli', () => { it('is a function', () => { expect(typeof cli).toBe('function'); }); + + it('should exit process with non-zero code', () => { + const workDir = path.resolve(__dirname, '../'); + const cmd = `node src/index tests/fixtures/code/swagger-api-with-error.js --base tests/fixtures/project/swaggerBase.json`; + return runCommand(cmd, workDir).then(result => { + expect(result.code).not.toBe(0); + }); + }); + + it('should exit process with zero code', () => { + const workDir = path.resolve(__dirname, '../'); + const cmd = `node src/index tests/fixtures/code/swagger-api.js --base tests/fixtures/project/swaggerBase.json`; + return runCommand(cmd, workDir).then(result => { + expect(result.code).toBe(0); + }); + }); }); diff --git a/tests/fixtures/code/swagger-api-with-error.js b/tests/fixtures/code/swagger-api-with-error.js new file mode 100644 index 0000000..c62e9b2 --- /dev/null +++ b/tests/fixtures/code/swagger-api-with-error.js @@ -0,0 +1,19 @@ + +/** + * @api [post] /pets + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * required: + * - name + * properties: + * name: + * type: string + * properties there should be an error + */ +router.post('/pets', () => { + +});