@pact-foundation/pact-node
Advanced tools
Comparing version 4.5.3 to 4.5.4
@@ -15,3 +15,3 @@ /* global describe:true, before:true, after:true, it:true, global:true, process:true */ | ||
var procs; | ||
function spawn(args) { | ||
@@ -28,6 +28,6 @@ args = args || {port: 1234, host: 'localhost'}; | ||
check(); | ||
function check() { | ||
amount++; | ||
call(args).then(function () { | ||
call(args).tap(function () { | ||
if (amount >= 10) { | ||
@@ -39,3 +39,3 @@ deferred.reject(new Error("Pact stop failed; tried calling service 10 times with no result.")); | ||
} | ||
function call(options) { | ||
@@ -65,13 +65,13 @@ var deferred = q.defer(); | ||
}); | ||
return deferred.promise; | ||
} | ||
return deferred.promise.timeout(10000, 'Process took too long'); | ||
} | ||
beforeEach(function () { | ||
procs = []; | ||
}); | ||
afterEach(function () { | ||
@@ -86,3 +86,3 @@ for (var i = 0, len = procs.length; i < len; i++) { | ||
}); | ||
describe("run pact-node command", function () { | ||
@@ -95,3 +95,3 @@ context("when no options are set", function () { | ||
}); | ||
/*context("when user specifies valid options", function () { | ||
@@ -106,3 +106,3 @@ var dirPath; | ||
}); | ||
it("should return serverFactory using specified options", function () { | ||
@@ -120,22 +120,22 @@ var options = { | ||
}; | ||
}); | ||
}); | ||
context("when user specifies invalid port", function () { | ||
it("should return an error on negative port number", function () { | ||
}); | ||
it("should return an error on non-integer", function () { | ||
}); | ||
it("should return an error on non-number", function () { | ||
}); | ||
it("should return an error on outside port range", function () { | ||
}); | ||
}); | ||
context("when user specifies port that's currently in use", function () { | ||
@@ -145,45 +145,45 @@ it("should return a port conflict error", function () { | ||
}); | ||
context("when user specifies invalid host", function () { | ||
it("should return an error on non-string", function () { | ||
}); | ||
}); | ||
context("when user specifies invalid directory", function () { | ||
it("should return an error on invalid path", function () { | ||
}); | ||
}); | ||
context("when user specifies invalid ssl", function () { | ||
it("should return an error on non-boolean", function () { | ||
}); | ||
}); | ||
context("when user specifies invalid cors", function () { | ||
it("should return an error on non-boolean", function () { | ||
}); | ||
}); | ||
context("when user specifies invalid log", function () { | ||
it("should return an error on invalid path", function () { | ||
}); | ||
}); | ||
context("when user specifies invalid spec", function () { | ||
it("should return an error on non-number", function () { | ||
}); | ||
it("should return an error on negative number", function () { | ||
}); | ||
it("should return an error on non-integer", function () { | ||
}); | ||
}); | ||
context("when user specifies invalid consumer name", function () { | ||
@@ -193,3 +193,3 @@ it("should return an error on non-string", function () { | ||
}); | ||
context("when user specifies invalid provider name", function () { | ||
@@ -196,0 +196,0 @@ it("should return an error on non-string", function () { |
{ | ||
"name": "@pact-foundation/pact-node", | ||
"version": "4.5.3", | ||
"version": "4.5.4", | ||
"description": "A wrapper for the Ruby version of Pact to work within Node", | ||
@@ -75,6 +75,6 @@ "main": "./src/pact.js", | ||
"pretest": "npm run clean && npm run lint", | ||
"test": "./node_modules/.bin/mocha ./**/*.spec.js", | ||
"test": "./node_modules/.bin/cross-env LOGLEVEL=debug ./node_modules/.bin/mocha ./**/*.spec.js", | ||
"watch": "nodemon -x npm run dev", | ||
"watch:debug": "nodemon --debug -q -w assets/ --ext '.' --exec 'npm run lint'", | ||
"dev": "npm test && ./node_modules/.bin/cross-env node .", | ||
"dev": "npm test && node .", | ||
"start": "npm run watch" | ||
@@ -81,0 +81,0 @@ }, |
@@ -65,3 +65,3 @@ <img src="https://raw.githubusercontent.com/pact-foundation/pact-logo/master/media/logo-black.png" width="200"> | ||
providerStatesUrl: <String>, // URL to fetch the provider states for the given provider API. Optional. | ||
providerStatesSetupUrl <String>, // URL to send PUT requests to setup a given provider state. Optional. | ||
providerStatesSetupUrl: <String>, // URL to send PUT requests to setup a given provider state. Optional. | ||
pactBrokerUsername: <String>, // Username for Pact Broker basic authentication. Optional | ||
@@ -71,3 +71,3 @@ pactBrokerPassword: <String>, // Password for Pact Broker basic authentication. Optional | ||
pact.verifyPacts(opts)).then(function () { | ||
pact.verifyPacts(opts).then(function () { | ||
// do something | ||
@@ -74,0 +74,0 @@ }); |
@@ -12,3 +12,3 @@ var bunyan = require('bunyan'), | ||
{ | ||
level: 'warn', | ||
level: process.env.LOGLEVEL || 'warn', | ||
type: 'raw', | ||
@@ -15,0 +15,0 @@ stream: prettyStdOut |
@@ -25,3 +25,3 @@ 'use strict'; | ||
if (options && options.port && _.some(servers, function (s) { | ||
return s.options.port == options.port | ||
return s._options.port == options.port | ||
})) { | ||
@@ -32,13 +32,13 @@ var msg = 'Port `' + options.port + '` is already in use by another process.'; | ||
} | ||
var server = serverFactory(options); | ||
servers.push(server); | ||
logger.info('Creating Pact Server with options: \n' + stringify(server.options)); | ||
logger.info('Creating Pact Server with options: \n' + stringify(server._options)); | ||
// Listen to server delete events, to remove from server list | ||
server.once('delete', function (server) { | ||
logger.info('Deleting Pact Server with options: \n' + stringify(server.options)); | ||
logger.info('Deleting Pact Server with options: \n' + stringify(server._options)); | ||
servers = _.without(servers, server); | ||
}); | ||
return server; | ||
@@ -45,0 +45,0 @@ } |
@@ -1,3 +0,1 @@ | ||
/* global describe:true, before:true, after:true, it:true, global:true, process:true */ | ||
var expect = require('chai').expect, | ||
@@ -14,6 +12,2 @@ pact = require('./pact.js'), | ||
describe("Pact Spec", function () { | ||
before(function () { | ||
logger.level('debug'); | ||
}); | ||
afterEach(function (done) { | ||
@@ -24,3 +18,3 @@ pact.removeAllServers().then(function () { | ||
}); | ||
describe("Set Log Level", function () { | ||
@@ -32,3 +26,3 @@ context("when setting a log level", function () { | ||
}); | ||
it("should be able to set log level 'debug'", function () { | ||
@@ -38,3 +32,3 @@ pact.logLevel('debug'); | ||
}); | ||
it("should be able to set log level 'info'", function () { | ||
@@ -44,3 +38,3 @@ pact.logLevel('info'); | ||
}); | ||
it("should be able to set log level 'warn'", function () { | ||
@@ -50,3 +44,3 @@ pact.logLevel('warn'); | ||
}); | ||
it("should be able to set log level 'error'", function () { | ||
@@ -56,3 +50,3 @@ pact.logLevel('error'); | ||
}); | ||
it("should be able to set log level 'fatal'", function () { | ||
@@ -64,13 +58,13 @@ pact.logLevel('fatal'); | ||
}); | ||
describe("Create serverFactory", function () { | ||
var fs = require('fs'), | ||
path = require('path'), | ||
dirPath; | ||
beforeEach(function () { | ||
dirPath = path.resolve(__dirname, '../.tmp/' + Math.floor(Math.random() * 1000)); | ||
}); | ||
afterEach(function (done) { | ||
@@ -85,3 +79,3 @@ try { | ||
}); | ||
context("when no options are set", function () { | ||
@@ -91,4 +85,4 @@ it("should use defaults and return serverFactory", function () { | ||
expect(server).to.be.an('object'); | ||
expect(server.options).to.be.an('object'); | ||
expect(server.options).to.contain.all.keys(['port', 'cors', 'ssl', 'host', 'dir', 'log', 'spec', 'consumer', 'provider']); | ||
expect(server._options).to.be.an('object'); | ||
expect(server._options).to.contain.all.keys(['port', 'cors', 'ssl', 'host', 'dir', 'log', 'spec', 'consumer', 'provider']); | ||
expect(server.start).to.be.a('function'); | ||
@@ -99,3 +93,3 @@ expect(server.stop).to.be.a('function'); | ||
}); | ||
context("when user specifies valid options", function () { | ||
@@ -116,15 +110,15 @@ it("should return serverFactory using specified options", function () { | ||
expect(server).to.be.an('object'); | ||
expect(server.options).to.be.an('object'); | ||
expect(server.options.port).to.equal(options.port); | ||
expect(server.options.host).to.equal(options.host); | ||
expect(server.options.dir).to.equal(options.dir); | ||
expect(server.options.ssl).to.equal(options.ssl); | ||
expect(server.options.cors).to.equal(options.cors); | ||
expect(server.options.log).to.equal(options.log); | ||
expect(server.options.spec).to.equal(options.spec); | ||
expect(server.options.consumer).to.equal(options.consumer); | ||
expect(server.options.provider).to.equal(options.provider); | ||
expect(server._options).to.be.an('object'); | ||
expect(server._options.port).to.equal(options.port); | ||
expect(server._options.host).to.equal(options.host); | ||
expect(server._options.dir).to.equal(options.dir); | ||
expect(server._options.ssl).to.equal(options.ssl); | ||
expect(server._options.cors).to.equal(options.cors); | ||
expect(server._options.log).to.equal(options.log); | ||
expect(server._options.spec).to.equal(options.spec); | ||
expect(server._options.consumer).to.equal(options.consumer); | ||
expect(server._options.provider).to.equal(options.provider); | ||
}); | ||
}); | ||
context("when user specifies invalid port", function () { | ||
@@ -136,3 +130,3 @@ it("should return an error on negative port number", function () { | ||
}); | ||
it("should return an error on non-integer", function () { | ||
@@ -143,3 +137,3 @@ expect(function () { | ||
}); | ||
it("should return an error on non-number", function () { | ||
@@ -150,3 +144,3 @@ expect(function () { | ||
}); | ||
it("should return an error on outside port range", function () { | ||
@@ -158,3 +152,3 @@ expect(function () { | ||
}); | ||
context("when user specifies port that's currently in use", function () { | ||
@@ -168,3 +162,3 @@ it("should return a port conflict error", function () { | ||
}); | ||
context("when user specifies invalid host", function () { | ||
@@ -177,3 +171,3 @@ it("should return an error on non-string", function () { | ||
}); | ||
context("when user specifies invalid pact directory", function () { | ||
@@ -185,3 +179,3 @@ it("should create the directory for us", function () { | ||
}); | ||
context("when user specifies invalid ssl", function () { | ||
@@ -194,3 +188,3 @@ it("should return an error on non-boolean", function () { | ||
}); | ||
context("when user specifies invalid cors", function () { | ||
@@ -203,3 +197,3 @@ it("should return an error on non-boolean", function () { | ||
}); | ||
context("when user specifies invalid log", function () { | ||
@@ -211,3 +205,3 @@ it("should return an error on invalid path", function () { | ||
}); | ||
context("when user specifies invalid spec", function () { | ||
@@ -219,3 +213,3 @@ it("should return an error on non-number", function () { | ||
}); | ||
it("should return an error on negative number", function () { | ||
@@ -226,3 +220,3 @@ expect(function () { | ||
}); | ||
it("should return an error on non-integer", function () { | ||
@@ -234,3 +228,3 @@ expect(function () { | ||
}); | ||
context("when user specifies invalid consumer name", function () { | ||
@@ -243,3 +237,3 @@ it("should return an error on non-string", function () { | ||
}); | ||
context("when user specifies invalid provider name", function () { | ||
@@ -253,3 +247,3 @@ it("should return an error on non-string", function () { | ||
}); | ||
describe("List servers", function () { | ||
@@ -261,3 +255,3 @@ context("when called and there are no servers", function () { | ||
}); | ||
context("when called and there are servers in list", function () { | ||
@@ -271,3 +265,3 @@ it("should return a list of all servers", function () { | ||
}); | ||
context("when server is removed", function () { | ||
@@ -284,3 +278,3 @@ it("should update the list", function (done) { | ||
}); | ||
describe("Remove all servers", function () { | ||
@@ -299,3 +293,3 @@ context("when removeAll() is called and there are servers to remove", function () { | ||
}); | ||
describe("Verify Pacts", function () { | ||
@@ -312,3 +306,3 @@ context("With provider states", function () { | ||
}); | ||
describe("Publish Pacts", function () { | ||
@@ -315,0 +309,0 @@ it("should start running the Pact publishing process", function () { |
@@ -26,3 +26,3 @@ 'use strict'; | ||
logger.info('Publishing pacts to broker at: ' + options.pactBroker); | ||
// Stat all paths in pactUrls to make sure they exist | ||
@@ -47,11 +47,8 @@ // publish template $pactHost/pacts/provider/$provider/consumer/$client/$version | ||
.value(); | ||
// Return a merge of all promises... | ||
return q.allSettled( | ||
_.chain(uris) | ||
.map(function (uri) { | ||
return getPactFile(options, uri); | ||
}) | ||
.map(function (file) { | ||
return file.then(function (data) { | ||
_.map(uris, function (uri) { | ||
return getPactFile(options, uri) | ||
.then(function (data) { | ||
return callPact(options, { | ||
@@ -69,9 +66,6 @@ uri: constructPutUrl(options, data), | ||
}); | ||
}); | ||
}) | ||
.map(function (publish) { | ||
return publish.then(function (data) { | ||
}) | ||
.tap(function (data) { | ||
if (!options.tags.length) { | ||
return data; | ||
return; | ||
} | ||
@@ -88,3 +82,3 @@ return q.allSettled(_.map(options.tags, function (tag) { | ||
}); | ||
})).then(function (results) { | ||
})).tap(function (results) { | ||
_.each(results, function (result) { | ||
@@ -95,7 +89,5 @@ if (result.state !== "fulfilled") { | ||
}); | ||
return data; | ||
}); | ||
}); | ||
}) | ||
.value() | ||
}) | ||
).then(function (results) { | ||
@@ -123,3 +115,3 @@ var reject = false; | ||
}, config); | ||
// Authentication | ||
@@ -132,3 +124,3 @@ if (options.pactBrokerUsername && options.pactBrokerPassword) { | ||
} | ||
return request(config).then(function (data) { | ||
@@ -147,9 +139,7 @@ return data[0]; // return response only | ||
if (/\.json$/.test(uri)) { | ||
var readFile = q.denodeify(fs.readFile); | ||
return readFile(uri, 'utf8') | ||
.then(function (data) { | ||
return JSON.parse(data); | ||
}, function (err) { | ||
return q.reject("Invalid Pact file: " + uri + ". Nested exception: " + err.message); | ||
}) | ||
try { | ||
return q(require(uri)); | ||
} catch (err) { | ||
return q.reject("Invalid Pact file: " + uri + ". Nested exception: " + err); | ||
} | ||
} else { | ||
@@ -171,7 +161,7 @@ return callPact(options, { | ||
} | ||
if (!_.has(options, 'consumerVersion')) { | ||
throw new Error("Cannot construct Pact publish URL: 'consumerVersion' not specified"); | ||
} | ||
if (!_.isObject(options) | ||
@@ -185,3 +175,3 @@ || !_.has(data, 'consumer') | ||
} | ||
return urlJoin(options.pactBroker, 'pacts/provider', data.provider.name, 'consumer', data.consumer.name, 'version', options.consumerVersion) | ||
@@ -194,7 +184,7 @@ } | ||
} | ||
if (!_.has(options, 'consumerVersion')) { | ||
throw new Error("Cannot construct Pact Tag URL: 'consumerVersion' not specified"); | ||
} | ||
if (!_.isObject(options) | ||
@@ -206,17 +196,17 @@ || !_.has(data, 'consumer') | ||
} | ||
return urlJoin(options.pactBroker, 'pacticipants', data.consumer.name, 'versions', options.consumerVersion, 'tags', tag) | ||
} | ||
// Creates a new instance of the pact server with the specified option | ||
module.exports = function (options) { | ||
options = options || {}; | ||
// Setting defaults | ||
options.pactBroker = options.pactBroker || ''; | ||
options.pactUrls = options.pactUrls || []; | ||
options.tags = options.tags || []; | ||
if (options.pactUrls) { | ||
checkTypes.assert.array.of.string(options.pactUrls); | ||
} | ||
// Stat all paths in pactUrls to make sure they exist | ||
@@ -235,24 +225,24 @@ var url = require('url'); | ||
}); | ||
checkTypes.assert.nonEmptyString(options.pactBroker, 'Must provide the pactBroker argument'); | ||
checkTypes.assert.nonEmptyString(options.consumerVersion, 'Must provide the consumerVersion argument'); | ||
checkTypes.assert.not.emptyArray(options.pactUrls, 'Must provide the pactUrls argument'); | ||
if (options.pactBrokerUsername) { | ||
checkTypes.assert.string(options.pactBrokerUsername); | ||
} | ||
if (options.pactBrokerPassword) { | ||
checkTypes.assert.string(options.pactBrokerPassword); | ||
} | ||
if ((options.pactBrokerUsername && !options.pactBrokerPassword) || (options.pactBrokerPassword && !options.pactBrokerUsername)) { | ||
throw new Error('Must provide both or none of --provider-states-url and --provider-states-setup-url.'); | ||
} | ||
if (options.pactBroker) { | ||
checkTypes.assert.string(options.pactBroker); | ||
} | ||
return new Publisher(options.pactBroker, options.pactUrls, options.consumerVersion, options.pactBrokerUsername, options.pactBrokerPassword, options.tags); | ||
}; |
@@ -1,3 +0,1 @@ | ||
/* global describe:true, before:true, after:true, it:true, global:true, process:true */ | ||
var publisherFactory = require('./publisher'), | ||
@@ -22,5 +20,4 @@ expect = require('chai').expect, | ||
authenticatedPactBrokerBaseUrl = 'http://localhost:' + PORT + '/auth'; | ||
before(function (done) { | ||
logger.level('debug'); | ||
broker.listen(PORT, function () { | ||
@@ -31,3 +28,3 @@ console.log('Broker (Mock) running on port: ' + PORT); | ||
}); | ||
describe("Publisher", function () { | ||
@@ -44,3 +41,3 @@ context("when not given pactUrls", function () { | ||
}); | ||
context("when not given pactBroker", function () { | ||
@@ -56,3 +53,3 @@ it("should fail with an error", function () { | ||
}); | ||
context("when not given consumerVersion", function () { | ||
@@ -68,3 +65,3 @@ it("should fail with an error", function () { | ||
}); | ||
context("when given local Pact URLs that don't exist", function () { | ||
@@ -80,3 +77,3 @@ it("should fail with an error", function () { | ||
}); | ||
context("when given local Pact URLs that do exist", function () { | ||
@@ -93,3 +90,3 @@ it("should not fail", function () { | ||
}); | ||
context("when given the correct arguments", function () { | ||
@@ -107,3 +104,3 @@ it("should return a Publisher object", function () { | ||
}); | ||
context("constructPutUrl", function () { | ||
@@ -152,3 +149,3 @@ context("when given a valid config object and pact JSON", function () { | ||
}); | ||
context("constructTagUrl", function () { | ||
@@ -155,0 +152,0 @@ context("when given a valid config object and pact JSON", function () { |
@@ -15,2 +15,3 @@ 'use strict'; | ||
mkdirp = require('mkdirp'), | ||
pactUtil = require('./pact-util'), | ||
isWindows = process.platform === 'win32'; | ||
@@ -24,13 +25,16 @@ | ||
function Server(port, host, dir, ssl, cors, log, spec, consumer, provider) { | ||
this.options = {}; | ||
this.options.port = port; | ||
this.options.host = host; | ||
this.options.dir = dir; | ||
this.options.ssl = ssl; | ||
this.options.cors = cors; | ||
this.options.log = log; | ||
this.options.spec = spec; | ||
this.options.consumer = consumer; | ||
this.options.provider = provider; | ||
this.$running = false; | ||
this._options = {}; | ||
this._options.port = port; | ||
this._options.host = host; | ||
this._options.dir = dir; | ||
this._options.ssl = ssl; | ||
this._options.cors = cors; | ||
this._options.log = log; | ||
this._options.spec = spec; | ||
this._options.consumer = consumer; | ||
this._options.provider = provider; | ||
this._running = false; | ||
this.START_EVENT = 'start'; | ||
this.STOP_EVENT = 'stop'; | ||
this.DELETE_EVENT = 'delete'; | ||
} | ||
@@ -42,4 +46,4 @@ | ||
Server.prototype.start = function () { | ||
if (this.instance && this.instance.connected) { | ||
logger.warn('You already have a process running with PID: ' + this.instance.pid); | ||
if (this._instance && this._instance.connected) { | ||
logger.warn('You already have a process running with PID: ' + this._instance.pid); | ||
return; | ||
@@ -59,3 +63,3 @@ } | ||
}, | ||
mapping = { | ||
args = pactUtil.createArguments(this._options, { | ||
'port': '--port', | ||
@@ -70,8 +74,4 @@ 'host': '--host', | ||
'provider': '--provider' | ||
}; | ||
}); | ||
var args = _.compact(_.map(mapping, (function (value, key) { | ||
return this.options[key] ? value + ' ' + this.options[key] : null; | ||
}).bind(this))); | ||
var cmd = [pactPath.file].concat(args).join(' '); | ||
@@ -88,11 +88,11 @@ | ||
} | ||
logger.debug('Starting binary with `' + _.flatten([file, args, _.map(opts, function (v, k) {return k + ':' + v;})]) + '`'); | ||
this._instance = cp.spawn(file, args, opts); | ||
this.instance = cp.spawn(file, args, opts); | ||
this._instance.stdout.setEncoding('utf8'); | ||
this._instance.stdout.on('data', logger.debug.bind(logger)); | ||
this._instance.stderr.setEncoding('utf8'); | ||
this._instance.stderr.on('data', logger.debug.bind(logger)); | ||
this._instance.on('error', logger.error.bind(logger)); | ||
this.instance.stdout.setEncoding('utf8'); | ||
this.instance.stdout.on('data', logger.debug.bind(logger)); | ||
this.instance.stderr.setEncoding('utf8'); | ||
this.instance.stderr.on('data', logger.debug.bind(logger)); | ||
this.instance.on('error', logger.error.bind(logger)); | ||
// if port isn't specified, listen for it when pact runs | ||
@@ -102,16 +102,16 @@ function catchPort(data) { | ||
if (match && match[1]) { | ||
this.options.port = parseInt(match[1]); | ||
this.instance.stdout.removeListener('data', catchPort.bind(this)); | ||
this.instance.stderr.removeListener('data', catchPort.bind(this)); | ||
this._options.port = parseInt(match[1]); | ||
this._instance.stdout.removeListener('data', catchPort.bind(this)); | ||
this._instance.stderr.removeListener('data', catchPort.bind(this)); | ||
} | ||
} | ||
if (!this.options.port) { | ||
this.instance.stdout.on('data', catchPort.bind(this)); | ||
this.instance.stderr.on('data', catchPort.bind(this)); | ||
if (!this._options.port) { | ||
this._instance.stdout.on('data', catchPort.bind(this)); | ||
this._instance.stderr.on('data', catchPort.bind(this)); | ||
} | ||
logger.info('Creating Pact with PID: ' + this.instance.pid); | ||
logger.info('Creating Pact with PID: ' + this._instance.pid); | ||
this.instance.once('close', (function (code) { | ||
this._instance.once('close', (function (code) { | ||
if (code !== 0) { | ||
@@ -124,8 +124,7 @@ logger.warn('Pact exited with code ' + code + '.'); | ||
// check service is available | ||
return waitForServerUp(this.options) | ||
.timeout(PROCESS_TIMEOUT, "Couldn't start Pact with PID: " + this.instance.pid) | ||
.then((function () { | ||
this.$running = true; | ||
this.emit('start', this); | ||
return this; | ||
return waitForServerUp(this._options) | ||
.timeout(PROCESS_TIMEOUT, "Couldn't start Pact with PID: " + this._instance.pid) | ||
.tap((function () { | ||
this._running = true; | ||
this.emit(this.START_EVENT, this); | ||
}).bind(this)); | ||
@@ -137,6 +136,6 @@ }; | ||
var pid = -1; | ||
if (this.instance) { | ||
pid = this.instance.pid; | ||
if (this._instance) { | ||
pid = this._instance.pid; | ||
logger.info('Removing Pact with PID: ' + pid); | ||
this.instance.removeAllListeners(); | ||
this._instance.removeAllListeners(); | ||
// Killing instance, since windows can't send signals, must kill process forcefully | ||
@@ -148,11 +147,10 @@ if (isWindows) { | ||
} | ||
this.instance = undefined; | ||
this._instance = undefined; | ||
} | ||
return waitForServerDown(this.options) | ||
return waitForServerDown(this._options) | ||
.timeout(PROCESS_TIMEOUT, "Couldn't stop Pact with PID: " + pid) | ||
.then((function () { | ||
this.$running = false; | ||
this.emit('stop', this); | ||
return this; | ||
.tap((function () { | ||
this._running = false; | ||
this.emit(this.STOP_EVENT, this); | ||
}).bind(this)); | ||
@@ -163,5 +161,4 @@ }; | ||
Server.prototype.delete = function () { | ||
return this.stop().then((function () { | ||
this.emit('delete', this); | ||
return this; | ||
return this.stop().tap((function () { | ||
this.emit(this.DELETE_EVENT, this); | ||
}).bind(this)); | ||
@@ -168,0 +165,0 @@ }; |
@@ -1,2 +0,1 @@ | ||
/* global describe:true, before:true, after:true, it:true, global:true, process:true */ | ||
var rewire = require("rewire"), | ||
@@ -13,7 +12,3 @@ serverFactory = rewire('./server'), | ||
var server; | ||
before(function () { | ||
logger.level('debug'); | ||
}); | ||
afterEach(function (done) { | ||
@@ -28,3 +23,3 @@ if (server) { | ||
}); | ||
describe("Start server", function () { | ||
@@ -39,11 +34,11 @@ context("when no options are set", function () { | ||
}); | ||
context("when valid options are set", function () { | ||
var dirPath; | ||
beforeEach(function () { | ||
dirPath = path.resolve(__dirname, '../.tmp/' + Math.floor(Math.random() * 1000)); | ||
}); | ||
afterEach(function (done) { | ||
@@ -58,9 +53,9 @@ try { | ||
}); | ||
it("should start correctly when instance is delayed", function (done) { | ||
server = serverFactory(); | ||
var waitForServerUp = serverFactory.__get__('waitForServerUp'); | ||
q.allSettled([ | ||
waitForServerUp(server.options), | ||
waitForServerUp(server._options), | ||
q.delay(5000).then(function () { | ||
@@ -76,59 +71,59 @@ server.start() | ||
}); | ||
it("should start correctly with ssl", function (done) { | ||
server = serverFactory({ssl: true}); | ||
server.start().then(function () { | ||
expect(server.options.ssl).to.equal(true); | ||
expect(server._options.ssl).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it("should start correctly with cors", function (done) { | ||
server = serverFactory({cors: true}); | ||
server.start().then(function () { | ||
expect(server.options.cors).to.equal(true); | ||
expect(server._options.cors).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it("should start correctly with port", function (done) { | ||
server = serverFactory({port: 9500}); | ||
server.start().then(function () { | ||
expect(server.options.port).to.equal(9500); | ||
expect(server._options.port).to.equal(9500); | ||
done(); | ||
}); | ||
}); | ||
it("should start correctly with host", function (done) { | ||
server = serverFactory({host: 'localhost'}); | ||
server.start().then(function () { | ||
expect(server.options.host).to.equal('localhost'); | ||
expect(server._options.host).to.equal('localhost'); | ||
done(); | ||
}); | ||
}); | ||
it("should start correctly with spec version 1", function (done) { | ||
server = serverFactory({spec: 1}); | ||
server.start().then(function () { | ||
expect(server.options.spec).to.equal(1); | ||
expect(server._options.spec).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
it("should start correctly with spec version 2", function (done) { | ||
server = serverFactory({spec: 2}); | ||
server.start().then(function () { | ||
expect(server.options.spec).to.equal(2); | ||
expect(server._options.spec).to.equal(2); | ||
done(); | ||
}); | ||
}); | ||
it("should start correctly with dir", function (done) { | ||
server = serverFactory({dir: dirPath}); | ||
server.start().then(function () { | ||
expect(server.options.dir).to.equal(dirPath); | ||
expect(server._options.dir).to.equal(dirPath); | ||
done(); | ||
}); | ||
}); | ||
it("should start correctly with log", function (done) { | ||
@@ -138,19 +133,19 @@ var logPath = path.resolve(dirPath, 'log.txt'); | ||
server.start().then(function () { | ||
expect(server.options.log).to.equal(logPath); | ||
expect(server._options.log).to.equal(logPath); | ||
done(); | ||
}); | ||
}); | ||
it("should start correctly with consumer name", function (done) { | ||
server = serverFactory({consumer: 'cName'}); | ||
server.start().then(function () { | ||
expect(server.options.consumer).to.equal('cName'); | ||
expect(server._options.consumer).to.equal('cName'); | ||
done(); | ||
}); | ||
}); | ||
it("should start correctly with provider name", function (done) { | ||
server = serverFactory({provider: 'pName'}); | ||
server.start().then(function () { | ||
expect(server.options.provider).to.equal('pName'); | ||
expect(server._options.provider).to.equal('pName'); | ||
done(); | ||
@@ -160,3 +155,3 @@ }); | ||
}); | ||
it("should dispatch event when starting", function (done) { | ||
@@ -169,7 +164,7 @@ server = serverFactory(); | ||
}); | ||
it("should change running state to true", function (done) { | ||
server = serverFactory(); | ||
server.start().then(function () { | ||
expect(server.$running).to.be.true; | ||
expect(server._running).to.be.true; | ||
done(); | ||
@@ -179,3 +174,3 @@ }); | ||
}); | ||
describe("Stop server", function () { | ||
@@ -191,3 +186,3 @@ context("when already started", function () { | ||
}); | ||
it("should dispatch event when stopping", function (done) { | ||
@@ -202,3 +197,3 @@ server = serverFactory(); | ||
}); | ||
it("should change running state to false", function (done) { | ||
@@ -209,3 +204,3 @@ server = serverFactory(); | ||
}).then(function () { | ||
expect(server.$running).to.be.false; | ||
expect(server._running).to.be.false; | ||
done(); | ||
@@ -216,3 +211,3 @@ }); | ||
}); | ||
describe("Delete server", function () { | ||
@@ -228,3 +223,3 @@ context("when already running", function () { | ||
}); | ||
it("should dispatch event when deleting", function (done) { | ||
@@ -239,3 +234,3 @@ server = serverFactory(); | ||
}); | ||
it("should change running state to false", function (done) { | ||
@@ -246,3 +241,3 @@ server = serverFactory(); | ||
}).then(function () { | ||
expect(server.$running).to.be.false; | ||
expect(server._running).to.be.false; | ||
done(); | ||
@@ -253,3 +248,3 @@ }); | ||
}); | ||
}); |
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
var checkTypes = require('check-types'), | ||
@@ -13,2 +11,3 @@ _ = require('underscore'), | ||
verifierPath = require('@pact-foundation/pact-provider-verifier'), | ||
pactUtil = require('./pact-util'), | ||
isWindows = process.platform === 'win32'; | ||
@@ -18,9 +17,9 @@ | ||
function Verifier(providerBaseUrl, pactUrls, providerStatesUrl, providerStatesSetupUrl, pactBrokerUsername, pactBrokerPassword) { | ||
this.options = {}; | ||
this.options.providerBaseUrl = providerBaseUrl; | ||
this.options.pactUrls = pactUrls; | ||
this.options.providerStatesUrl = providerStatesUrl; | ||
this.options.providerStatesSetupUrl = providerStatesSetupUrl; | ||
this.options.pactBrokerUsername = pactBrokerUsername; | ||
this.options.pactBrokerPassword = pactBrokerPassword; | ||
this._options = {}; | ||
this._options.providerBaseUrl = providerBaseUrl; | ||
this._options.pactUrls = pactUrls; | ||
this._options.providerStatesUrl = providerStatesUrl; | ||
this._options.providerStatesSetupUrl = providerStatesSetupUrl; | ||
this._options.pactBrokerUsername = pactBrokerUsername; | ||
this._options.pactBrokerPassword = pactBrokerPassword; | ||
} | ||
@@ -31,3 +30,3 @@ | ||
var deferred = q.defer(); | ||
var output = ''; // Store output here in case of error | ||
@@ -38,3 +37,3 @@ function outputHandler(data) { | ||
} | ||
var envVars = JSON.parse(JSON.stringify(process.env)); // Create copy of environment variables | ||
@@ -45,3 +44,3 @@ // Remove environment variable if there | ||
delete envVars['RUBYGEMS_GEMDEPS']; | ||
var file, | ||
@@ -53,3 +52,3 @@ opts = { | ||
}, | ||
mapping = { | ||
args = pactUtil.createArguments(this._options, { | ||
'providerBaseUrl': '--provider-base-url', | ||
@@ -61,10 +60,6 @@ 'pactUrls': '--pact-urls', | ||
'pactBrokerPassword': '--broker-password' | ||
}; | ||
var args = _.compact(_.map(mapping, (function (value, key) { | ||
return this.options[key] ? value + ' ' + (checkTypes.array(this.options[key]) ? this.options[key].join(',') : this.options[key]) : null; | ||
}).bind(this))); | ||
}); | ||
var cmd = [verifierPath.file].concat(args).join(' '); | ||
if (isWindows) { | ||
@@ -79,20 +74,19 @@ file = 'cmd.exe'; | ||
} | ||
this.instance = cp.spawn(file, args, opts); | ||
this.instance.stdout.setEncoding('utf8'); | ||
this.instance.stdout.on('data', outputHandler); | ||
this.instance.stderr.setEncoding('utf8'); | ||
this.instance.stderr.on('data', outputHandler); | ||
this.instance.on('error', logger.error.bind(logger)); | ||
this.instance.once('close', function (code) { | ||
this._instance = cp.spawn(file, args, opts); | ||
this._instance.stdout.setEncoding('utf8'); | ||
this._instance.stdout.on('data', outputHandler); | ||
this._instance.stderr.setEncoding('utf8'); | ||
this._instance.stderr.on('data', outputHandler); | ||
this._instance.on('error', logger.error.bind(logger)); | ||
this._instance.once('close', function (code) { | ||
code == 0 ? deferred.resolve(output) : deferred.reject(new Error(output)); | ||
}); | ||
logger.info('Created Pact Verifier process with PID: ' + this.instance.pid); | ||
return deferred.promise.timeout(10000, "Couldn't start Pact Verifier process with PID: " + this.instance.pid) | ||
.then(function (data) { | ||
logger.info('Created Pact Verifier process with PID: ' + this._instance.pid); | ||
return deferred.promise.timeout(10000, "Couldn't start Pact Verifier process with PID: " + this._instance.pid) | ||
.tap(function (data) { | ||
logger.info('Pact Verification succeeded.'); | ||
return data; | ||
}); | ||
@@ -108,3 +102,3 @@ }; | ||
options.providerStatesSetupUrl = options.providerStatesSetupUrl || ''; | ||
options.pactUrls = _.chain(options.pactUrls) | ||
@@ -116,3 +110,3 @@ .map(function (uri) { | ||
fs.statSync(path.normalize(uri)).isFile(); | ||
// Unixify the paths. Pact in multiple places uses URI and matching and | ||
@@ -132,35 +126,35 @@ // hasn't really taken Windows into account. This is much easier, albeit | ||
.value(); | ||
checkTypes.assert.nonEmptyString(options.providerBaseUrl, 'Must provide the --provider-base-url argument'); | ||
checkTypes.assert.not.emptyArray(options.pactUrls, 'Must provide the --pact-urls argument'); | ||
if (options.providerStatesSetupUrl) { | ||
checkTypes.assert.string(options.providerStatesSetupUrl); | ||
} | ||
if (options.providerStatesUrl) { | ||
checkTypes.assert.string(options.providerStatesUrl); | ||
} | ||
if (options.pactBrokerUsername) { | ||
checkTypes.assert.string(options.pactBrokerUsername); | ||
} | ||
if (options.pactBrokerPassword) { | ||
checkTypes.assert.string(options.pactBrokerPassword); | ||
} | ||
if ((options.providerStatesUrl && !options.providerStatesSetupUrl) || (options.providerStatesSetupUrl && !options.providerStatesUrl)) { | ||
throw new Error('Must provide both or none of --provider-states-url and --provider-states-setup-url.'); | ||
} | ||
if (options.pactUrls) { | ||
checkTypes.assert.array.of.string(options.pactUrls); | ||
} | ||
if (options.providerBaseUrl) { | ||
checkTypes.assert.string(options.providerBaseUrl); | ||
} | ||
return new Verifier(options.providerBaseUrl, options.pactUrls, options.providerStatesUrl, options.providerStatesSetupUrl, options.pactBrokerUsername, options.pactBrokerPassword); | ||
}; |
@@ -1,3 +0,1 @@ | ||
/* global describe:true, before:true, after:true, it:true, global:true, process:true */ | ||
var verifierFactory = require('./verifier'), | ||
@@ -16,7 +14,2 @@ logger = require('./logger'), | ||
describe("Verifier Spec", function () { | ||
before(function () { | ||
logger.level('debug'); | ||
}); | ||
describe("Verifier", function () { | ||
@@ -85,3 +78,3 @@ context("when not given --pact-urls or --provider-base-url", function () { | ||
}); | ||
describe("verify", function () { | ||
@@ -88,0 +81,0 @@ context("when given a successful scenario", function () { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
97170
39
2378
21