Skip to content

Commit

Permalink
fix: non-zero value on failure and print errors #92 (#99)
Browse files Browse the repository at this point in the history
* fix #92

* throw error instead of exit process

* cover non-zero exit code cases
  • Loading branch information
raphaelsoul authored Jul 1, 2020
1 parent b66e2e8 commit 99564bf
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 40 deletions.
3 changes: 2 additions & 1 deletion build/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
18 changes: 11 additions & 7 deletions build/swagger-inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
});
});
});
Expand Down
71 changes: 39 additions & 32 deletions src/swagger-inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
Expand Down
31 changes: 31 additions & 0 deletions tests/cli.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
19 changes: 19 additions & 0 deletions tests/fixtures/code/swagger-api-with-error.js
Original file line number Diff line number Diff line change
@@ -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', () => {

});

0 comments on commit 99564bf

Please sign in to comment.