Skip to content

Commit

Permalink
fix(templates): improved test templates, ESLinting
Browse files Browse the repository at this point in the history
improved test templates and skeletons, improved code, now using ESLint with AirBnB JavaScript style
  • Loading branch information
Antonio Pintus committed May 8, 2017
1 parent f81631c commit 599af10
Show file tree
Hide file tree
Showing 21 changed files with 577 additions and 561 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "airbnb-base",
"plugins": [
"import",
"mocha"
],

"rules": {
"no-console": "off",
"no-restricted-syntax": "off",
"no-shadow": ["error", { "allow": ["done", "resolve", "reject", "done", "cb", "callback", "err", "req", "res"] }],
"mocha/no-exclusive-tests": "error"
}
}
82 changes: 40 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,57 @@
#!/usr/bin/env node


//API-PIKI command line tool
// API-PIKI command line tool
const program = require('commander');
const generator = require('./src/lib/generator');
const path = require('path');
const fs = require('fs');

let specFile;

program
.usage('[options] <spec>')
.version(require('./package.json').version)
.arguments('<spec>')
.action( spec => {
specFile = spec;
.action((spec) => {
specFile = spec;
})
.option('-o, --out [outDir]', 'Output directory for generated test files. Default: ./test/api-swag')
.option('-f, --forceAuth', 'Force generation of authenticated tests using Basic Authentication, even if spec doesn\'t declare it. In this case be sure to set USERNAME and USERPASSWD env variables when running tests')
.option('-o, --out [outDir]', 'Output directory for generated test files. Default: ./test/api-swag')
.option('-f, --forceAuth', 'Force generation of authenticated tests using Basic Authentication, even if spec doesn\'t declare it. In this case be sure to set USERNAME and USERPASSWD env variables when running tests')
.parse(process.argv);

if(typeof specFile === 'undefined') {
console.log('\n Nothing to do here... API specification file / URL not provided');
process.exit(1);
} else {
let swaggerPath = ( specFile.startsWith('http:') || specFile.startsWith('https:') ) ? specFile : `file://${path.resolve(specFile)}`;
if( swaggerPath.startsWith('file:') && !fs.existsSync(path.resolve(specFile)) ) {
console.log('\n Specified file doesn\'t exist. Please check it.\n');
process.exit(1);
}
console.log(`\nGenerating API tests for ${specFile}`);
console.log(`Target directory is ${program.out ? program.out : '(DEFAULTING to) ./api-swag'}`);
if(program.forceAuth) {
console.log('Basic Authorization forcing enabled.');
}

generator.run(swaggerPath, program.out, {forcedAuth: program.forceAuth ? true : false})
.then( files => {
console.log('\nGenerated files:');
for( f of files ) {
console.log(f);
}
console.log(`${program.out ? program.out : process.cwd()+'/api-swag'}/package.json`);
console.log('\n');
console.log('HOW TO run tests:');
console.log('1. (optional) if APIs are authenticated through Basic Authentication (or -f flag is used), set USERNAME and USERPASSWD env variables');
console.log(`2. cd ${program.out ? program.out : './api-swag'} && npm i && [USERNAME=<username> USERPASSWD=<userpasswd>] npm test\n`);
console.log('Example: USERNAME=james USERPASSWD=my$trongPa$$word npm test\n');

} )
.catch( err => {
console.error(err);
process.exit(1);
});

}
if (typeof specFile === 'undefined') {
console.log('\n Nothing to do here... API specification file / URL not provided');
process.exit(1);
} else {
const swaggerPath = (specFile.startsWith('http:') || specFile.startsWith('https:')) ? specFile : `file://${path.resolve(specFile)}`;
if (swaggerPath.startsWith('file:') && !fs.existsSync(path.resolve(specFile))) {
console.log('\n Specified file doesn\'t exist. Please check it.\n');
process.exit(1);
}
console.log(`\nGenerating API tests for ${specFile}`);
console.log(`Target directory is ${program.out ? program.out : '(DEFAULTING to) ./api-swag'}`);
if (program.forceAuth) {
console.log('Basic Authorization forcing enabled.');
}

generator.run(swaggerPath, program.out, { forcedAuth: Boolean(program.forceAuth) })
.then((files) => {
console.log('\nGenerated files:');
for (const f of files) {
console.log(f);
}
const altPath = `${process.cwd()}/api-swag`;
console.log(`${program.out ? program.out : altPath}/package.json`);
console.log('\n');
console.log('HOW TO run tests:');
console.log('1. (optional) if APIs are authenticated through Basic Authentication (or -f flag is used), set USERNAME and USERPASSWD env variables');
console.log(`2. cd ${program.out ? program.out : './api-swag'} && npm i && [USERNAME=<username> USERPASSWD=<userpasswd>] npm test\n`);
console.log('Example: USERNAME=james USERPASSWD=my$trongPa$$word npm test\n');
})
.catch((err) => {
console.error(err);
process.exit(1);
});
}



13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,25 @@
"commitizen": "2.9.6",
"coveralls": "^2.13.1",
"cz-conventional-changelog": "2.0.0",
"eslint": "^3.19.0",
"eslint-config-airbnb-base": "^11.1.3",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-mocha": "^4.9.0",
"husky": "^0.13.3",
"mocha": "^3.3.0",
"nyc": "^10.3.0",
"nyc": "^10.3.2",
"rewire": "^2.5.2",
"semantic-release": "^6.3.2",
"semantic-release": "^6.3.6",
"supertest": "^3.0.0"
},
"dependencies": {
"chai": "^3.5.0",
"commander": "^2.9.0",
"debug": "^2.6.6",
"jsonpolice": "^5.2.0",
"lodash": "^4.17.4",
"request": "^2.81.0"
"request": "^2.81.0",
"supertest": "^3.0.0"
},
"config": {
"commitizen": {
Expand Down
Loading

0 comments on commit 599af10

Please sign in to comment.