Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from apiaryio/fix-10
Browse files Browse the repository at this point in the history
Fixing exit status and adding CLI tests
  • Loading branch information
Adam Kliment committed Oct 31, 2013
2 parents 517d7ef + 42ac534 commit 79643f4
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 25 deletions.
10 changes: 5 additions & 5 deletions bin/dredd
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ cli.main( function (args, options) {

dredd = new Dredd(configuration);

dredd.run(function(error){
if (error) {
cli.fatal(error);
dredd.run(function(error, reporter){
if(reporter['stats']['failures'] > 0){
process.exit(1);
} else {
process.exit(0);
};
process.exit(0);
});

});
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dredd",
"version": "0.1.4",
"version": "0.1.5",
"description": "API Blueprint testing tool",
"main": "lib/dredd.js",
"bin": {
Expand Down Expand Up @@ -37,7 +37,8 @@
"codo": "1.7.0",
"sinon": "~1.7.3",
"proxyquire": "~0.5.1",
"nock": "~0.22.1"
"nock": "~0.22.1",
"express": "~3.4.3"
},
"keywords": [
"api",
Expand Down
2 changes: 1 addition & 1 deletion scripts/mocha
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
cd $(dirname $0)/..
find ./test/unit -name '*-test.coffee' | xargs mocha --reporter spec --compilers 'coffee:coffee-script'
find ./test/ -name '*-test.coffee' | xargs mocha --reporter spec --compilers 'coffee:coffee-script'
19 changes: 9 additions & 10 deletions src/dredd.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,23 @@ class Dredd
run: (callback) ->
config = @configuration

fs.readFile config.blueprintPath, 'utf8', (error, data) ->
return callback error if error
fs.readFile config.blueprintPath, 'utf8', (parseError, data) ->
return callback(parseError, config.reporter) if parseError

protagonist.parse data, (error, result) ->
return callback error if error
protagonist.parse data, (protagonistError, result) ->
return callback(protagonistError, config.reporter) if protagonistError

runtime = blueprintAstToRuntime result['ast']

runtimeError = handleRuntimeProblems runtime
return callback runtimeError if runtimeError
return callback(runtimeError, config.reporter) if runtimeError

async.eachSeries configuredTransactions(runtime, config), executeTransaction, (error) ->
return callback error if error
if error
return callback error, config.reporter

config.reporter.createReport (error) ->
return callback error if error

return callback()
config.reporter.createReport (reporterError) ->
return callback reporterError, config.reporter

handleRuntimeProblems = (runtime) ->
if runtime['warnings'].length > 0
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/single_get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FORMAT: X-1A

# Machines API

# Group Machines

# Machines collection [/machines]

## Get Machines [GET]

- Response 202 (application/json)

[
{
"type": "bulldozer",
"name": "willy"
}
]
91 changes: 89 additions & 2 deletions test/integration/cli-test.coffee
Original file line number Diff line number Diff line change
@@ -1,2 +1,89 @@
describe 'Command-Line interface', () ->
describe '--dry-run option', () ->
{assert} = require('chai')
{exec} = require('child_process')
express = require 'express'



PORT = '3333'
CMD_PREFIX = ''
describe "Command line interface", () ->

describe "Arguments with existing bleurpint and responding server", () ->
describe "when executing the command and the server is responding as specified in the blueprint", () ->
stderr = ''
stdout = ''
exitStatus = null
requests = []

before (done) ->
cmd = "./bin/dredd ./test/fixtures/single_get.md http://localhost:#{PORT}"

app = express()

app.get '/machines', (req, res) ->
res.setHeader 'Content-Type', 'application/json'
machine =
type: 'bulldozer'
name: 'willy'
response = [machine]
res.send 200, response

server = app.listen PORT, () ->
cli = exec CMD_PREFIX + cmd, (error, out, err) ->
cli = exec cmd, (error, out, err) ->
stdout = out
stderr = err

if error
exitStatus = error.code

eventName = if process.version.split('.')[1] is '6' then 'exit' else 'close'

cli.on eventName, (code) ->
exitStatus = code if exitStatus == null and code != undefined
server.close()

server.on 'close', done

it 'exit status should be 0', () ->
assert.equal exitStatus, 0

describe "when executing the command and the server is sending different response", () ->
stderr = ''
stdout = ''
exitStatus = null
requests = []

before (done) ->
cmd = "./bin/dredd ./test/fixtures/single_get.md http://localhost:#{PORT}"

app = express()

app.get '/machines', (req, res) ->
res.setHeader 'Content-Type', 'application/json'
machine =
kind: 'bulldozer'
imatriculation: 'willy'
response = [machine]
res.send 201, response

server = app.listen PORT, () ->
cli = exec CMD_PREFIX + cmd, (error, out, err) ->
cli = exec cmd, (error, out, err) ->
stdout = out
stderr = err

if error
exitStatus = error.code

eventName = if process.version.split('.')[1] is '6' then 'exit' else 'close'

cli.on eventName, (code) ->
exitStatus = code if exitStatus == null and code != undefined

server.close()

server.on 'close', done

it 'exit status should be 1', () ->
assert.equal exitStatus, 1
17 changes: 12 additions & 5 deletions test/unit/dredd-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Dredd = proxyquire '../../src/dredd', {
'cli': cliStub
}

describe 'dredd', () ->
describe 'Dredd class', () ->

configuration = {}

Expand Down Expand Up @@ -62,11 +62,17 @@ describe 'dredd', () ->
assert.ok protagonistStub.parse.called
done()

it 'should exit without throwing an error', (done) ->
it 'should not pass any error to the caclback function', (done) ->
runner = new Dredd(configuration)
runner.run (error) ->
assert.notOk(error)
done()
assert.isUndefined(error)
done()

it 'should pass the reporter as second argument', (done) ->
runner = new Dredd(configuration)
runner.run (error, reporter) ->
assert.isDefined reporter
done()

it 'should convert ast to runtime', (done) ->
runner = new Dredd(configuration)
Expand Down Expand Up @@ -125,7 +131,7 @@ describe 'dredd', () ->
options:
silent: true

it 'should exit with an error', (done) ->
it 'should pass the error to the callback function', (done) ->
runner = new Dredd(configuration)
runner.run (error) ->
assert.ok error
Expand Down Expand Up @@ -197,3 +203,4 @@ describe 'dredd', () ->




0 comments on commit 79643f4

Please sign in to comment.