Skip to content

Commit

Permalink
chore(logging): Improve trace logging and use clearer types
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 'fatal' log level has been removed. Nothing was logged at fatal, and the underlying core doesn't support it.
  • Loading branch information
TimothyJones authored and mefellows committed Aug 24, 2021
1 parent 357c929 commit 060daa9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/dsl/options.ts
Expand Up @@ -6,7 +6,7 @@ import { VerifierOptions as PactCoreVerifierOptions } from '@pact-foundation/pac
import { PactfileWriteMode } from './mockService';
import { MessageProviders, StateHandlers } from './message';

export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error';

export interface PactOptions {
// The name of the consumer
Expand Down
1 change: 1 addition & 0 deletions src/dsl/verifier/proxy/proxy.ts
Expand Up @@ -25,6 +25,7 @@ export const createProxy = (
): http.Server => {
const app = express();
const proxy = new HttpProxy();
logger.trace(`Setting up state proxy with path: ${stateSetupPath}`);

app.use(stateSetupPath, bodyParser.json());
app.use(stateSetupPath, bodyParser.urlencoded({ extended: true }));
Expand Down
8 changes: 4 additions & 4 deletions src/dsl/verifier/proxy/stateHandler/stateHandler.ts
Expand Up @@ -9,7 +9,7 @@ export const createProxyStateHandler = (config: ProxyOptions) => (
): Promise<express.Response> => {
const message: ProviderState = req.body;

return setupStates(message, config)
.then((data) => res.json(data))
.catch((e) => res.status(500).send(e));
};
return Promise.resolve(setupStates(message, config))
.then((data) => res.json(data))
.catch((e) => res.status(500).send(e));
};
12 changes: 7 additions & 5 deletions src/dsl/verifier/verifier.spec.ts
@@ -1,13 +1,13 @@
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import serviceFactory from '@pact-foundation/pact-core';
import serviceFactory, { LogLevel } from '@pact-foundation/pact-core';

import * as proxy from './proxy/proxy';
import logger from '../../common/logger';

import { VerifierOptions } from './types';

import * as proxy from './proxy';

import { Verifier } from './verifier';

chai.use(chaiAsPromised);
Expand Down Expand Up @@ -74,8 +74,9 @@ describe('Verifier', () => {

context('when logLevel is not provided', () => {
it('does not modify the log setting', () => {
const { logLevel, ...rest } = opts;
v = new Verifier({
...opts,
...rest,
});
expect(spy.callCount).to.eql(0);
});
Expand All @@ -99,6 +100,7 @@ describe('Verifier', () => {
close: () => {
executed = true;
},
address: () => 'https://mock.server.example.com',
});
sinon.stub(proxy, 'waitForServerReady' as any).returns(Promise.resolve());
});
Expand All @@ -112,7 +114,7 @@ describe('Verifier', () => {

describe('when the verifier has been configured', () => {
beforeEach(() => {
v = new Verifier(opts);
v = new Verifier({ ...opts, logLevel: 'trace' as LogLevel });
});
context('and the verification runs successfully', () => {
it('closes the server and returns the result', () => {
Expand Down
9 changes: 8 additions & 1 deletion src/dsl/verifier/verifier.ts
Expand Up @@ -71,15 +71,22 @@ export class Verifier {

// Start the verification CLI proxy server
const server = createProxy(this.config, this.stateSetupPath);
logger.trace(`proxy created at ${server.address().address}`);

// Run the verification once the proxy server is available
return waitForServerReady(server)
.then((passOn) => {
logger.trace('Server is ready');
return passOn;
})
.then(this.runProviderVerification())
.then((result) => {
logger.trace('Verification completed, closing server');
server.close();
return result;
})
.catch((e) => {
logger.trace(`Verification failed(${e.message}), closing server`);
server.close();
throw e;
});
Expand All @@ -95,7 +102,7 @@ export class Verifier {
...omit(this.config, 'handlers'),
providerBaseUrl: `${this.address}:${server.address().port}`,
};

logger.trace(`Verifying pacts with: ${JSON.stringify(opts)}`);
return serviceFactory.verifyPacts(opts);
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/v3/verifier.spec.ts
Expand Up @@ -19,7 +19,7 @@ describe('V3 Verifier', () => {
expect(
() =>
new VerifierV3({
logLevel: 'fatal',
logLevel: 'info',
provider: '',
providerBaseUrl: 'http://localhost',
})
Expand All @@ -29,7 +29,7 @@ describe('V3 Verifier', () => {
expect(
() =>
new VerifierV3({
logLevel: 'fatal',
logLevel: 'info',
provider: 'unitTest',
providerBaseUrl: 'http://localhost',
pactUrls: [],
Expand All @@ -40,7 +40,7 @@ describe('V3 Verifier', () => {
expect(
() =>
new VerifierV3({
logLevel: 'fatal',
logLevel: 'info',
provider: 'unitTest',
providerBaseUrl: 'http://localhost',
})
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -17,7 +17,7 @@
"declaration": true,
"experimentalDecorators": true,
"target": "es5",
"lib": ["es2016", "dom", "esnext.asynciterable", "esnext"]
"lib": ["es2016", "es2017", "dom", "esnext.asynciterable", "esnext"]
},
"include": ["src", "test", "native/index.node.d.ts", "v3"],
"exclude": ["./node_modules/**", "dist", "examples"]
Expand Down

0 comments on commit 060daa9

Please sign in to comment.