Skip to content

Commit

Permalink
fix(spawn): Now binaries are spawned directly with array arguments, r…
Browse files Browse the repository at this point in the history
…ather than quoted strings. Should fix #118
  • Loading branch information
TimothyJones committed Oct 30, 2019
1 parent ccee4e2 commit 378f256
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 87 deletions.
106 changes: 95 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"bunyan-prettystream": "0.1.3",
"chalk": "2.3.1",
"check-types": "7.3.0",
"cross-spawn": "^7.0.1",
"decompress": "4.2.0",
"mkdirp": "0.5.1",
"q": "1.5.1",
Expand All @@ -74,6 +75,7 @@
"@types/chai-as-promised": "7.1.0",
"@types/check-types": "^7.3.1",
"@types/cors": "^2.8.6",
"@types/cross-spawn": "^6.0.1",
"@types/decompress": "^4.2.3",
"@types/express": "4.11.1",
"@types/mkdirp": "^0.5.2",
Expand Down
10 changes: 7 additions & 3 deletions src/can-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import q = require('q');
import logger from './logger';
import spawn from './spawn';
import pactStandalone from './pact-standalone';
import { PACT_NODE_NO_VALUE } from './spawn';
import { PACT_NODE_NO_VALUE, DEFAULT_ARG } from './spawn';
import * as _ from 'underscore';

// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -38,6 +38,7 @@ export class CanDeploy {

public readonly options: CanDeployOptions;
private readonly __argMapping = {
cliVerb: DEFAULT_ARG,
name: '--pacticipant',
version: '--version',
latest: '--latest',
Expand Down Expand Up @@ -95,8 +96,11 @@ export class CanDeploy {
);
const deferred = q.defer<CanDeployResponse | string>();
const instance = spawn.spawnBinary(
`${pactStandalone.brokerPath} can-i-deploy`,
CanDeploy.convertForSpawnBinary(this.options),
pactStandalone.brokerPath,
[
{ cliVerb: 'can-i-deploy' },
...CanDeploy.convertForSpawnBinary(this.options),
],
this.__argMapping,
);
const output: Array<string | Buffer> = [];
Expand Down
2 changes: 1 addition & 1 deletion src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Message {
logger.info(`Creating message pact`);
const deferred = q.defer();
const instance = spawn.spawnBinary(
`${pactStandalone.messagePath}`,
pactStandalone.messagePath,
this.options,
this.__argMapping,
);
Expand Down
5 changes: 3 additions & 2 deletions src/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class Publisher {
public readonly options: PublisherOptions;
private readonly __argMapping = {
pactFilesOrDirs: DEFAULT_ARG,
cliVerb: DEFAULT_ARG,
pactBroker: '--broker-base-url',
pactBrokerUsername: '--broker-username',
pactBrokerPassword: '--broker-password',
Expand Down Expand Up @@ -93,8 +94,8 @@ export class Publisher {
logger.info(`Publishing pacts to broker at: ${this.options.pactBroker}`);
const deferred = q.defer<string[]>();
const instance = spawn.spawnBinary(
`${pactStandalone.brokerPath} publish`,
this.options,
pactStandalone.brokerPath,
[{ cliVerb: 'publish' }, this.options],
this.__argMapping,
);
const output: Array<string | Buffer> = [];
Expand Down
39 changes: 23 additions & 16 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { AbstractService } from './service';
import { deprecate } from 'util';
import pact from './pact-standalone';
import { DEFAULT_ARG } from './spawn';
import path = require('path');
import fs = require('fs');
import mkdirp = require('mkdirp');
Expand Down Expand Up @@ -89,22 +90,28 @@ export class Server extends AbstractService {
opts.logLevel = options.logLevel.toUpperCase() as LogLevel;
}

super(`${pact.mockServicePath} service`, opts, {
port: '--port',
host: '--host',
log: '--log',
ssl: '--ssl',
sslcert: '--sslcert',
sslkey: '--sslkey',
cors: '--cors',
dir: '--pact_dir',
spec: '--pact_specification_version',
pactFileWriteMode: '--pact-file-write-mode',
consumer: '--consumer',
provider: '--provider',
monkeypatch: '--monkeypatch',
logLevel: '--log-level',
});
super(
pact.mockServicePath,
opts,
{
cliVerb: DEFAULT_ARG,
port: '--port',
host: '--host',
log: '--log',
ssl: '--ssl',
sslcert: '--sslcert',
sslkey: '--sslkey',
cors: '--cors',
dir: '--pact_dir',
spec: '--pact_specification_version',
pactFileWriteMode: '--pact-file-write-mode',
consumer: '--consumer',
provider: '--provider',
monkeypatch: '--monkeypatch',
logLevel: '--log-level',
},
{ cliVerb: 'service' },
);
}
}

Expand Down
46 changes: 27 additions & 19 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import events = require('events');
import http = require('request');
import q = require('q');
import logger from './logger';
import spawn from './spawn';
import spawn, { CliVerbOptions } from './spawn';
import { ChildProcess } from 'child_process';
import mkdirp = require('mkdirp');
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -20,9 +20,9 @@ const RETRY_AMOUNT = 60;
const PROCESS_TIMEOUT = 30000;

interface AbstractServiceEventInterface {
START_EVENT: string;
STOP_EVENT: string;
DELETE_EVENT: string;
START_EVENT: string;
STOP_EVENT: string;
DELETE_EVENT: string;
}

export abstract class AbstractService extends events.EventEmitter {
Expand All @@ -35,14 +35,21 @@ export abstract class AbstractService extends events.EventEmitter {
}

public readonly options: ServiceOptions;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected __argMapping: any;
protected __running: boolean;
protected __instance: ChildProcess;
protected __cliVerb?: CliVerbOptions;
protected __serviceCommand: string;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected constructor(command: string, options: ServiceOptions, argMapping: any) {
protected constructor(
command: string,
options: ServiceOptions,
argMapping: any,
cliVerb?: CliVerbOptions,
) {
super();

// defaults
Expand Down Expand Up @@ -122,6 +129,7 @@ export abstract class AbstractService extends events.EventEmitter {

this.options = options;
this.__running = false;
this.__cliVerb = cliVerb;
this.__serviceCommand = command;
this.__argMapping = argMapping;
}
Expand Down Expand Up @@ -152,7 +160,7 @@ export abstract class AbstractService extends events.EventEmitter {
this.__instance.stdout.on('data', catchPort);
}

this.__instance.stderr.on('data', (data) =>
this.__instance.stderr.on('data', data =>
logger.error(`Pact Binary Error: ${data}`),
);

Expand Down Expand Up @@ -193,7 +201,7 @@ export abstract class AbstractService extends events.EventEmitter {
protected spawnBinary(): ChildProcess {
return spawn.spawnBinary(
this.__serviceCommand,
this.options,
this.__cliVerb ? [this.__cliVerb, this.options] : [this.options],
this.__argMapping,
);
}
Expand Down Expand Up @@ -269,7 +277,7 @@ export abstract class AbstractService extends events.EventEmitter {
headers: {
'X-Pact-Mock-Service': true,
'Content-Type': 'application/json',
}
},
};

if (options.ssl) {
Expand Down Expand Up @@ -301,15 +309,15 @@ export interface ServiceOptions {
}

export interface HTTPConfig {
uri: string;
method: string;
headers: {
'X-Pact-Mock-Service': boolean;
'Content-Type': string;
};
agentOptions?: {
rejectUnauthorized?: boolean;
requestCert?: boolean;
agent?: boolean;
};
uri: string;
method: string;
headers: {
'X-Pact-Mock-Service': boolean;
'Content-Type': string;
};
agentOptions?: {
rejectUnauthorized?: boolean;
requestCert?: boolean;
agent?: boolean;
};
}
Loading

0 comments on commit 378f256

Please sign in to comment.