Skip to content

Commit 47b64df

Browse files
committed
feat: Add ability to override endpoints ordering
Add `--sorted` cmdline option (like Dredd) to trigger endpoint reordering. Sorts endpoint requests in a sensible way so that objects are not modified before they are created. Order: CONNECT, OPTIONS, POST, GET, HEAD, PUT, PATCH, DELETE, TRACE. Closes: #22
1 parent 2958260 commit 47b64df

File tree

7 files changed

+186
-70
lines changed

7 files changed

+186
-70
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,15 @@ Options:
221221
[boolean]
222222
--grep, -g Only run tests matching <pattern> [string]
223223
--invert, -i Invert --grep matches [boolean]
224+
--sorted Sorts requests in a sensible way so that objects are not
225+
modified before they are created. Order: CONNECT, OPTIONS,
226+
POST, GET, HEAD, PUT, PATCH, DELETE, TRACE. [boolean]
224227
--timeout, -t Set test-case timeout in milliseconds
225228
[number] [default: 2000]
226229
--template Specify the template file to use for generating hooks
227-
[string]
230+
[string]
228231
--names, -n List names of requests and exit [boolean]
232+
--generate-hooks Output hooks generated from template file and exit [boolean]
229233
--reporters Display available reporters and exit [boolean]
230234
--help Show usage information and exit [boolean]
231235
--version Show version number and exit [boolean]

lib/abao.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Abao
4242
if !config.options.server
4343
if raml.baseUri
4444
config.options.server = raml.baseUri
45-
addTests raml, tests, hooks, callback, factory
45+
addTests raml, tests, hooks, callback, factory, config.options.sorted
4646
,
4747
# Run tests
4848
(callback) ->

lib/add-tests.coffee

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
async = require 'async'
2-
_ = require 'underscore'
2+
_ = require 'lodash'
33
csonschema = require 'csonschema'
44

55

@@ -21,11 +21,11 @@ parseHeaders = (raml) ->
2121

2222
headers
2323

24-
# addTests(raml, tests, [parent], callback, config)
25-
addTests = (raml, tests, hooks, parent, callback, testFactory) ->
24+
addTests = (raml, tests, hooks, parent, callback, testFactory, sortFirst) ->
2625

2726
# Handle 4th optional param
2827
if _.isFunction(parent)
28+
sortFirst = testFactory
2929
testFactory = callback
3030
callback = parent
3131
parent = null
@@ -41,7 +41,7 @@ addTests = (raml, tests, hooks, parent, callback, testFactory) ->
4141
# Apply parent properties
4242
if parent
4343
path = parent.path + path
44-
params = _.clone parent.params
44+
params = _.clone parent.params # shallow copy
4545

4646
# Setup param
4747
if resource.uriParameters
@@ -52,6 +52,49 @@ addTests = (raml, tests, hooks, parent, callback, testFactory) ->
5252
# In case of issue #8, resource does not define methods
5353
resource.methods ?= []
5454

55+
if sortFirst && resource.methods.length > 1
56+
methodTests = [
57+
method: 'CONNECT', tests: []
58+
,
59+
method: 'OPTIONS', tests: []
60+
,
61+
method: 'POST', tests: []
62+
,
63+
method: 'GET', tests: []
64+
,
65+
method: 'HEAD', tests: []
66+
,
67+
method: 'PUT', tests: []
68+
,
69+
method: 'PATCH', tests: []
70+
,
71+
method: 'DELETE', tests: []
72+
,
73+
method: 'TRACE', tests: []
74+
]
75+
76+
# Group endpoint tests by method name
77+
_.each methodTests, (methodTest) ->
78+
isSameMethod = (test) ->
79+
return methodTest.method == test.method.toUpperCase()
80+
81+
ans = _.partition resource.methods, isSameMethod
82+
if ans[0].length != 0
83+
_.each ans[0], (test) -> methodTest.tests.push test
84+
resource.methods = ans[1]
85+
86+
# Shouldn't happen unless new HTTP method introduced...
87+
leftovers = resource.methods
88+
if leftovers.length > 1
89+
console.error 'unknown method calls present!', leftovers
90+
91+
# Now put them back, but in order of methods listed above
92+
sortedTests = _.map methodTests, (methodTest) -> return methodTest.tests
93+
leftoverTests = _.map leftovers, (leftover) -> return leftover
94+
reassembled = _.flattenDeep [_.reject sortedTests, _.isEmpty,
95+
_.reject leftoverTests, _.isEmpty]
96+
resource.methods = reassembled
97+
5598
# Iterate response method
5699
async.each resource.methods, (api, callback) ->
57100
method = api.method.toUpperCase()
@@ -108,7 +151,7 @@ addTests = (raml, tests, hooks, parent, callback, testFactory) ->
108151
return callback(err) if err
109152

110153
# Recursive
111-
addTests resource, tests, hooks, {path, params}, callback, testFactory
154+
addTests resource, tests, hooks, {path, params}, callback, testFactory, sortFirst
112155
, callback
113156

114157

lib/apply-configuration.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ applyConfiguration = (config) ->
3333
grep: ''
3434
invert: false
3535
'hooks-only': false
36+
sorted: false
3637

3738
# Normalize options and config
3839
for own key, value of config

lib/options.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ options =
4040
description: 'Invert --grep matches'
4141
type: 'boolean'
4242

43+
sorted:
44+
description: 'Sorts requests in a sensible way so that objects are not ' +
45+
'modified before they are created.\nOrder: ' +
46+
'CONNECT, OPTIONS, POST, GET, HEAD, PUT, PATCH, DELETE, TRACE.'
47+
type: 'boolean'
48+
4349
timeout:
4450
alias: 't'
4551
description: 'Set test-case timeout in milliseconds'

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"source-map-support": "^0.4.0",
6161
"tv4": "^1.2.7",
6262
"underscore": "^1.8.2",
63+
"lodash": "^4.16.4",
6364
"yargs": "~6.0.0"
6465
}
6566
}

0 commit comments

Comments
 (0)