Skip to content

Commit 3313346

Browse files
committed
Merge pull request #119 from cybertk/generate_hooks
Generate hooks
2 parents 1faab22 + 83932c3 commit 3313346

15 files changed

+420
-211
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,16 @@ $ abao single-get.raml --names
8484
GET /machines -> 200
8585
```
8686

87-
Write a hookfile in *JavaScript* named `test_machines_hooks.js`:
87+
**Abao** can generate a hookfile to help validate more than just the
88+
response code for each path.
89+
90+
```bash
91+
$ abao single-get.raml --generate-hooks > test_machines_hooks.js
92+
93+
```
94+
95+
Then edit the *JavaScript* hookfile `test_machines_hooks.js` created in the
96+
previous step to add request parameters and response validation logic.
8897

8998
```js
9099
var hooks = require('hooks'),

bin/abao

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var argv = yargs
1010
.usage("Usage:\n abao </path/to/raml> [OPTIONS]" +
1111
"\n\nExample:\n " + "abao api.raml --server http://api.example.com")
1212
.options(Abao.options)
13-
.wrap(80)
13+
.implies('template', 'generate-hooks')
1414
.check(function (argv) {
1515
if (argv.reporters === true) {
1616
showReporters();
@@ -26,6 +26,7 @@ var argv = yargs
2626

2727
return true;
2828
})
29+
.wrap(80)
2930
.help('help', 'Show usage information and exit')
3031
.version().describe('version', 'Show version number and exit')
3132
.epilog("Website:\n " + 'https://github.com/cybertk/abao')

lib/abao.coffee

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
sms = require("source-map-support").install({handleUncaughtExceptions: false})
2-
raml = require 'raml-parser'
2+
ramlParser = require 'raml-parser'
33
async = require 'async'
44

55
options = require './options'
@@ -22,7 +22,7 @@ class Abao
2222
tests = @tests
2323
hooks = @hooks
2424

25-
# init the test factory to inject the json refs schemas
25+
# Inject the JSON refs schemas
2626
factory = new TestFactory(config.options.schemas)
2727

2828
async.waterfall [
@@ -33,7 +33,7 @@ class Abao
3333
,
3434
# Load RAML
3535
(callback) ->
36-
raml.loadFile(config.ramlPath).then (raml) ->
36+
ramlParser.loadFile(config.ramlPath).then (raml) ->
3737
callback(null, raml)
3838
, callback
3939
,
@@ -46,7 +46,7 @@ class Abao
4646
,
4747
# Run tests
4848
(callback) ->
49-
runner = new Runner config.options
49+
runner = new Runner config.options, config.ramlPath
5050
runner.run tests, hooks, callback
5151
], done
5252

lib/add-hooks.coffee

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
path = require 'path'
2-
31
require 'coffee-script/register'
42
proxyquire = require('proxyquire').noCallThru()
53
glob = require 'glob'
4+
path = require 'path'
65

76

87
addHooks = (hooks, pattern) ->

lib/add-tests.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ async = require 'async'
22
_ = require 'underscore'
33
csonschema = require 'csonschema'
44

5+
56
parseSchema = (source) ->
67
if source.contains('$schema')
78
#jsonschema

lib/apply-configuration.coffee

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
applyConfiguration = (config) ->
32

43
coerceToArray = (value) ->
@@ -35,11 +34,11 @@ applyConfiguration = (config) ->
3534
invert: false
3635
'hooks-only': false
3736

38-
# normalize options and config
37+
# Normalize options and config
3938
for own key, value of config
4039
configuration[key] = value
4140

42-
# coerce some options into an dict
41+
# Coerce some options into an dict
4342
configuration.options.header = coerceToDict(configuration.options.header)
4443

4544
# TODO(quanlong): OAuth2 Bearer Token

lib/generate-hooks.coffee

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
fs = require 'fs'
2+
Mustache = require 'mustache'
3+
4+
generateHooks = (names, ramlFile, templateFile, callback) ->
5+
if !names
6+
callback new Error 'no names found for which to generate hooks'
7+
8+
if !templateFile
9+
callback new Error 'missing template file'
10+
11+
try
12+
template = fs.readFileSync templateFile, 'utf8'
13+
datetime = new Date().toISOString().replace('T', ' ').substr(0, 19)
14+
view =
15+
ramlFile: ramlFile
16+
timestamp: datetime
17+
hooks:
18+
{ 'name': name } for name in names
19+
view.hooks[0].comment = true
20+
21+
content = Mustache.render template, view
22+
console.log content
23+
catch error
24+
console.error 'failed to generate skeleton hooks'
25+
callback error
26+
27+
callback
28+
29+
module.exports = generateHooks
30+

lib/hooks.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
async = require 'async'
22
_ = require 'underscore'
33

4+
45
class Hooks
56
constructor: () ->
67
@beforeHooks = {}

lib/options.coffee

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
options =
22
server:
3-
description: 'Specify the API endpoint to use. The RAML-specified baseUri value will be used if not provided'
3+
description: 'Specify API endpoint to use. The RAML-specified baseUri value will be used if not provided'
44
type: 'string'
55

66
hookfiles:
77
alias: 'f'
8-
description: 'Specify a pattern to match files with before/after hooks for running tests'
8+
description: 'Specify pattern to match files with before/after hooks for running tests'
99
type: 'string'
1010

1111
schemas:
1212
alias: 's'
13-
description: 'Specify a pattern to match schema files to be loaded for use as JSON refs'
13+
description: 'Specify pattern to match schema files to be loaded for use as JSON refs'
1414
type: 'string'
1515

1616
reporter:
1717
alias: 'r'
18-
description: 'Specify the reporter to use'
18+
description: 'Specify reporter to use'
1919
type: 'string'
2020
default: 'spec'
2121

2222
header:
2323
alias: 'h'
24-
description: 'Add header to include in each request. The header must be in KEY:VALUE format, e.g. "-h Accept:application/json".\nReuse to add multiple headers'
24+
description: 'Add header to include in each request. Header must be in KEY:VALUE format (e.g., "-h Accept:application/json").\nReuse option to add multiple headers'
2525
type: 'string'
2626

2727
'hooks-only':
@@ -45,11 +45,20 @@ options =
4545
type: 'number'
4646
default: 2000
4747

48+
template:
49+
description: 'Specify template file to use for generating hooks'
50+
type: 'string'
51+
normalize: true
52+
4853
names:
4954
alias: 'n'
5055
description: 'List names of requests and exit'
5156
type: 'boolean'
5257

58+
'generate-hooks':
59+
description: 'Output hooks generated from template file and exit'
60+
type: 'boolean'
61+
5362
reporters:
5463
description: 'Display available reporters and exit'
5564
type: 'boolean'

lib/server.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
express = require 'express'
22

3+
34
PORT = '3333'
45

56
app = express()

0 commit comments

Comments
 (0)