Skip to content

Commit

Permalink
Merge pull request #16 from tlivings/master
Browse files Browse the repository at this point in the history
Fixes to tests generator, documentation.
  • Loading branch information
Poornima Venkat committed Sep 2, 2014
2 parents 1af310b + 8a02244 commit 9525487
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 32 deletions.
13 changes: 7 additions & 6 deletions QUICKSTART.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,21 @@ Paste the following:
}
```

### 3.
### 3.

```bash
$ npm init
$ npm install --save express swaggerize-express
$ npm install --save-dev tape
$ node_modules/.bin/swaggerize --api api.json --handlers handlers --tests tests
$ node_modules/.bin/swaggerize --api api.json --handlers handlers --models models --tests tests
```

### 4.
### 4.

```bash
$ vim index.js
```

Paste the following:
Paste the following:

```javascript
var http = require('http');
Expand All @@ -84,4 +83,6 @@ server.listen(8000, 'localhost', function () {
});
```

### Done!
### Done!

You now have working services and can use something like [Swagger UI](https://github.com/wordnik/swagger-ui) to explore your API.
5 changes: 4 additions & 1 deletion bin/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function createHandlers(apis, handlersPath) {
}

function createTests(api, testsPath, apiPath, handlersPath, modelsPath) {
var models, template;
var models, template, resourcePath;

models = {};
template = fs.readFileSync(testTemplate);
Expand Down Expand Up @@ -146,6 +146,8 @@ function createTests(api, testsPath, apiPath, handlersPath, modelsPath) {

}

resourcePath = api.resourcePath;

api.apis.forEach(function (api) {
var fileName;

Expand All @@ -155,6 +157,7 @@ function createTests(api, testsPath, apiPath, handlersPath, modelsPath) {
fs.writeFileSync(fileName, lodash.template(template, {
apiPath: apiPath,
handlers: handlersPath,
resourcePath: resourcePath,
api: api,
models: models
}));
Expand Down
17 changes: 7 additions & 10 deletions bin/lib/swaggerize.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
'use strict';

var minimist = require('minimist'),
fs = require('fs'),
var fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp'),
schema = require('swaggerize-builder/lib/schema'),
create = require('./create');

module.exports = function (argp) {
var argv, apiPath, modelsPath, handlersPath, testsPath, validation, api;
module.exports = function (options) {
var apiPath, modelsPath, handlersPath, testsPath, validation, api;

function usage() {
console.error('swaggerize --api <swagger document> [[--models <models dir>] | [--handlers <handlers dir>] | [--tests <tests dir>]]');
return 1;
}

argv = minimist(argp.slice(2));

apiPath = argv.api;
modelsPath = argv.models;
handlersPath = argv.handlers;
testsPath = argv.tests;
apiPath = options.api;
modelsPath = options.models;
handlersPath = options.handlers;
testsPath = options.tests;

if (!apiPath || !(modelsPath || handlersPath || testsPath)) {
return usage();
Expand Down
12 changes: 7 additions & 5 deletions bin/lib/templates/test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use strict';

var express = require('express'),
test = require('tape'),
var test = require('tape'),
path = require('path'),
express = require('express'),
swaggerize = require('swaggerize-express'),
request = require('supertest');

test('api', function (t) {
var app = express();
<%_.forEach(api.operations, function (operation) { if (operation.method.toLowerCase() === 'post' || operation.method.toLowerCase() === 'put') { %>
app.use(require('body-parser')());<%}});%>

app.use(swaggerize({
api: require('<%=apiPath%>'),
handlers: '<%=handlers%>',
outputvalidation: true
handlers: path.join(__dirname, '<%=handlers%>'),
}));

<%_.forEach(api.operations, function (operation) {%>
Expand Down Expand Up @@ -47,7 +49,7 @@ test('api', function (t) {
%><%if (operation.method.toLowerCase() === 'post' || operation.method.toLowerCase() === 'put'){%>var body = <%=JSON.stringify(body)%>;<%}%>
t.plan(2);

request(app).<%=operation.method.toLowerCase()%>('<%=path%>')
request(app).<%=operation.method.toLowerCase()%>('<%=resourcePath%><%=path%>')
.expect(200)<%if (operation.method.toLowerCase() === 'post' || operation.method.toLowerCase() === 'put'){%>.send(body)<%}%>
.end(function (err, res) {
t.ok(!err, '<%=operation.method.toLowerCase()%> <%=api.path%> no error.');
Expand Down
23 changes: 21 additions & 2 deletions bin/swaggerize.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
#!/usr/bin/env node
'use strict';

var swaggerize = require('swaggerize-express/bin/lib/swaggerize');
var minimist = require('minimist'),
swaggerize = require('./lib/swaggerize'),
spawn = require('child_process').spawn;

var result = swaggerize(process.argv);
var swaggerize, result, args, npm;

args = minimist(process.argv.slice(2));

result = swaggerize(args);

if (result === 0 && args.tests) {
npm = spawn('npm', ['install', '--save-dev', 'tape', 'body-parser', 'supertest']);

npm.stdout.pipe(process.stdout);
npm.stderr.pipe(process.stdout);

npm.on('close', function (code) {
process.exit(code);
});

return;
}

process.exit(result);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"scripts": {
"test": "tape test/*.js",
"cover": "istanbul cover tape -- test/*.js",
"lint": "jshint -c .jshintrc lib/**/*.js"
"lint": "jshint -c .jshintrc lib/*.js bin/*.js bin/lib/*.js"
},
"licenses": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@
}
}
}
}
}
10 changes: 10 additions & 0 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "testpkg",
"version": "0.0.0",
"description": "test package.json",
"devDependencies": {
"body-parser": "^1.7.0",
"tape": "^2.14.0",
"supertest": "^0.13.0"
}
}
31 changes: 25 additions & 6 deletions test/test-command.js → test/test-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,42 @@ test('swaggerize command', function (t) {
t.test('no handlers or models', function (t) {
t.plan(1);

var code = swaggerize(['foo', 'bar', '--api', 'test/fixtures/api.json']);
var code = swaggerize({
api: 'test/fixtures/api.json'
});

t.strictEqual(code, 1, 'error code 1.');
});

t.test('tests but no handlers and models', function (t) {
t.plan(1);

var code = swaggerize(['foo', 'bar', '--api', 'test/fixtures/api.json', '--tests', 'test/temp/tests']);
var code = swaggerize({
api: 'test/fixtures/api.json',
tests: '/test/temp/tests'
});

t.strictEqual(code, 1, 'error code 1.');
});

t.test('invalid schema fails', function (t) {
t.plan(1);

var code = swaggerize(['foo', 'bar', '--api', 'test/fixtures/badapi.json', '--handlers', 'test/temp/handlers']);
var code = swaggerize({
api: 'test/fixtures/badapi.json',
handlers: 'test/temp/handlers'
});

t.strictEqual(code, 1, 'error code 1.');
});

t.test('handlers', function (t) {
t.plan(9);

var code = swaggerize(['foo', 'bar', '--api', 'test/fixtures/api.json', '--handlers', 'test/temp/handlers']);
var code = swaggerize({
api: 'test/fixtures/api.json',
handlers: 'test/temp/handlers'
});

t.ok(!code, 'no error code.');
t.ok(fs.existsSync(path.resolve('test/temp/handlers')), 'handlers dir exists');
Expand All @@ -70,7 +81,10 @@ test('swaggerize command', function (t) {
t.test('models', function (t) {
t.plan(3);

var code = swaggerize(['foo', 'bar', '--api', 'test/fixtures/api.json', '--models', 'test/temp/models']);
var code = swaggerize({
api: 'test/fixtures/api.json',
models: 'test/temp/models'
});

t.ok(!code, 'no error code.');
t.ok(fs.existsSync(path.resolve('test/temp/models')), 'models dir exists');
Expand All @@ -80,7 +94,12 @@ test('swaggerize command', function (t) {
t.test('tests', function (t) {
t.plan(6);

var code = swaggerize(['foo', 'bar', '--api', 'test/fixtures/api.json', '--handlers', 'test/temp/handlers', '--models', 'test/temp/models', '--tests', 'test/temp/tests']);
var code = swaggerize({
api: 'test/fixtures/api.json',
handlers: 'test/temp/handlers',
models: 'test/temp/models',
tests: 'test/temp/tests'
});

t.ok(!code, 'no error code.');
t.ok(fs.existsSync(path.resolve('test/temp/tests')), 'tests dir exists');
Expand Down
56 changes: 56 additions & 0 deletions test/test-swaggerize-command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

var test = require('tape'),
execFile = require('child_process').execFile,
fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp');

test('swaggerize command', function (t) {
var cwd = process.cwd();

mkdirp.sync(path.resolve('test/temp'));

process.chdir(path.resolve('test/fixtures'));

t.on('end', function () {
process.chdir(cwd);

function rm(dir) {
var files = fs.readdirSync(dir);

files && files.forEach(function (file) {
var info = fs.statSync(file = path.join(dir, file));

info.isFile() && fs.unlinkSync(file);
info.isDirectory() && rm(file);
});

fs.rmdirSync(dir);
}

rm(path.resolve('test/temp'));
});

t.test('npm devDependencies', function (t) {
t.plan(5);

execFile('../../bin/swaggerize.js', ['--api', 'api.json', '--handlers', '../temp/handlers', '--models', '../temp/models', '--tests', '../temp/tests'], function (error, stdout, stderr) {
var pkg;

stdout && console.log(stdout.toString());
stderr && console.log(stderr.toString());
error && console.error(error);

t.ok(!error, 'no error.');

pkg = require(path.join(__dirname, 'fixtures/package.json'));

t.ok(pkg.devDependencies, 'devDependencies exists.');
t.ok(pkg.devDependencies.tape, 'tape exists.');
t.ok(pkg.devDependencies['body-parser'], 'body-parser exists.');
t.ok(pkg.devDependencies.supertest, 'supertest exists.');
});
});

});

0 comments on commit 9525487

Please sign in to comment.