From 171cfd0478406eb6126d437c4e45d0b4968afbea Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Sat, 13 Aug 2022 22:38:21 +1000 Subject: [PATCH 1/4] fix: support customProviderHeaders as a string[] --- src/verifier/argumentMapper/arguments.ts | 29 ++++++++++++++--- src/verifier/types.ts | 2 +- src/verifier/validateOptions.spec.ts | 40 +++++++++++++++++++----- src/verifier/validateOptions.ts | 16 +++++++++- 4 files changed, 72 insertions(+), 15 deletions(-) diff --git a/src/verifier/argumentMapper/arguments.ts b/src/verifier/argumentMapper/arguments.ts index c7b2d58a..a73b6045 100644 --- a/src/verifier/argumentMapper/arguments.ts +++ b/src/verifier/argumentMapper/arguments.ts @@ -35,14 +35,33 @@ export const ffiFnMapping: FnMapping< > = { pactffiVerifierAddCustomHeader: { validateAndExecute(ffi, handle, options) { + const messages: string[] = []; + if (options.customProviderHeaders) { - if (options.customProviderHeaders) { - Object.entries(options.customProviderHeaders).forEach( - ([key, value]) => { - ffi.pactffiVerifierAddCustomHeader(handle, key, value); + if (Array.isArray(options.customProviderHeaders)) { + options.customProviderHeaders.forEach((item) => { + const parts = item.split(':'); + if (parts.length !== 2) { + messages.push( + `${item} is not a valid custom header. Must be in the format 'Header-Name: Value'` + ); + } else { + ffi.pactffiVerifierAddCustomHeader(handle, parts[0], parts[1]); } - ); + }); + } else { + if (options.customProviderHeaders) { + Object.entries(options.customProviderHeaders).forEach( + ([key, value]) => { + ffi.pactffiVerifierAddCustomHeader(handle, key, value); + } + ); + } + } + if (messages.length > 0) { + return { status: FnValidationStatus.FAIL }; } + return { status: FnValidationStatus.SUCCESS }; } diff --git a/src/verifier/types.ts b/src/verifier/types.ts index f77c4807..e2636b52 100644 --- a/src/verifier/types.ts +++ b/src/verifier/types.ts @@ -41,7 +41,7 @@ export interface VerifierOptions { logLevel?: LogLevel; disableSslVerification?: boolean; buildUrl?: string; - customProviderHeaders?: CustomHeaders; + customProviderHeaders?: CustomHeaders | string[]; consumerFilters?: string[]; } diff --git a/src/verifier/validateOptions.spec.ts b/src/verifier/validateOptions.spec.ts index db94a410..b11d2e0a 100644 --- a/src/verifier/validateOptions.spec.ts +++ b/src/verifier/validateOptions.spec.ts @@ -351,14 +351,38 @@ describe('Verifier argument validator', () => { }); }); - context('when given customProviderHeaders that are defined', () => { - it('should pass through to the Pact Verifier', () => { - expect(() => - validateOptions({ - providerBaseUrl: 'http://localhost', - customProviderHeaders: { my: 'header' }, - }) - ).to.not.throw(Error); + context.only('when given customProviderHeaders', () => { + context('using the object notation', () => { + it('should pass through to the Pact Verifier', () => { + expect(() => + validateOptions({ + providerBaseUrl: 'http://localhost', + customProviderHeaders: { my: 'header' }, + }) + ).to.not.throw(Error); + }); + }); + + context('using the legacy array notation', () => { + it('should pass through to the Pact Verifier', () => { + expect(() => + validateOptions({ + providerBaseUrl: 'http://localhost', + customProviderHeaders: ['My: Header'], + }) + ).to.not.throw(Error); + }); + + context('and the format is incorrect', () => { + it('should throw an error', () => { + expect(() => + validateOptions({ + providerBaseUrl: 'http://localhost', + customProviderHeaders: [1 as unknown as string], + }) + ).to.throw(Error); + }); + }); }); }); }); diff --git a/src/verifier/validateOptions.ts b/src/verifier/validateOptions.ts index 763aabeb..dc660247 100644 --- a/src/verifier/validateOptions.ts +++ b/src/verifier/validateOptions.ts @@ -165,6 +165,20 @@ const consumerVersionTagsValidator = return true; }; +const customProviderHeadersValidator = + (options: InternalPactVerifierOptions) => + (): boolean => { + if (options.customProviderHeaders) { + if (Array.isArray(options.customProviderHeaders)) { + checkTypes.assert.array.of.string(options.customProviderHeaders); + } else { + checkTypes.assert.nonEmptyObject(options.customProviderHeaders); + } + } + + return true; + }; + export type ArgumentValidationRules = { [Key in keyof T]-?: ((options: T) => AssertFunction)[]; }; @@ -175,7 +189,7 @@ export const validationRules: ArgumentValidationRules Date: Sat, 13 Aug 2022 22:38:31 +1000 Subject: [PATCH 2/4] chore: minor lint fixes --- src/verifier/argumentMapper/arguments.ts | 2 +- src/verifier/argumentMapper/types.ts | 4 ++-- src/verifier/validateOptions.ts | 18 ++++++------------ standalone/install.ts | 2 +- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/verifier/argumentMapper/arguments.ts b/src/verifier/argumentMapper/arguments.ts index a73b6045..a28bc179 100644 --- a/src/verifier/argumentMapper/arguments.ts +++ b/src/verifier/argumentMapper/arguments.ts @@ -158,7 +158,7 @@ export const ffiFnMapping: FnMapping< }, }, pactffiVerifierSetFilterInfo: { - validateAndExecute(ffi, handle, options) { + validateAndExecute(ffi, handle) { if ( process.env.PACT_DESCRIPTION || process.env.PACT_PROVIDER_STATE || diff --git a/src/verifier/argumentMapper/types.ts b/src/verifier/argumentMapper/types.ts index 72920394..b27bd0aa 100644 --- a/src/verifier/argumentMapper/types.ts +++ b/src/verifier/argumentMapper/types.ts @@ -12,7 +12,7 @@ export type FnObject = { }; export type FnMapping = { - [Key in keyof T]: FnArgumentMapping; + [Key in keyof T]: FnArgumentMapping; }; export enum FnValidationStatus { @@ -26,7 +26,7 @@ export type FnValidationResult = { messages?: string[]; }; -export type FnArgumentMapping = { +export type FnArgumentMapping = { validateAndExecute: ( ffi: Ffi, handle: FfiHandle, diff --git a/src/verifier/validateOptions.ts b/src/verifier/validateOptions.ts index dc660247..50301ff8 100644 --- a/src/verifier/validateOptions.ts +++ b/src/verifier/validateOptions.ts @@ -9,7 +9,7 @@ import { } from './types'; export const deprecatedFunction = - (_: InternalPactVerifierOptions) => + () => (_: any, property: string): boolean => { logger.warn(`${property} is deprecated and no longer has any effect`); @@ -70,15 +70,12 @@ export const requiresOneOf = export type AssertFunction = (a: any, ...args: any) => boolean; -export const wrapCheckType = - (fn: AssertFunction) => - (_: InternalPactVerifierOptions): AssertFunction => - fn; +export const wrapCheckType = (fn: AssertFunction) => (): AssertFunction => fn; const LogLevels: LogLevel[] = ['debug', 'error', 'info', 'trace', 'warn']; const logLevelValidator = - (_: InternalPactVerifierOptions) => + () => (l: LogLevel): boolean => { if (LogLevels.includes(l.toLowerCase() as LogLevel)) { l = l.toLowerCase() as LogLevel; @@ -93,8 +90,7 @@ const logLevelValidator = }; const consumerVersionSelectorValidator = - (options: InternalPactVerifierOptions) => - (l: LogLevel): boolean => { + (options: InternalPactVerifierOptions) => (): boolean => { if ( options.consumerVersionSelectors && Array.isArray(options.consumerVersionSelectors) @@ -139,8 +135,7 @@ const consumerVersionSelectorValidator = }; const consumerVersionTagsValidator = - (options: InternalPactVerifierOptions) => - (l: LogLevel): boolean => { + (options: InternalPactVerifierOptions) => (): boolean => { if (options.consumerVersionTags) { if ( !checkTypes.string(options.consumerVersionTags) && @@ -166,8 +161,7 @@ const consumerVersionTagsValidator = }; const customProviderHeadersValidator = - (options: InternalPactVerifierOptions) => - (): boolean => { + (options: InternalPactVerifierOptions) => (): boolean => { if (options.customProviderHeaders) { if (Array.isArray(options.customProviderHeaders)) { checkTypes.assert.array.of.string(options.customProviderHeaders); diff --git a/standalone/install.ts b/standalone/install.ts index ef513cd9..41a18322 100644 --- a/standalone/install.ts +++ b/standalone/install.ts @@ -7,7 +7,7 @@ function throwError(msg: string): never { throw new Error(chalk.red(`Error while locating pact binary: ${msg}`)); } -export function createConfig(location: string = process.cwd()): Config { +export function createConfig(): Config { const CHECKSUM_SUFFIX = '.checksum'; return { From c155f943a5bb9beb33c2b1fab35f4d8a7cfb7d24 Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Sat, 13 Aug 2022 23:03:55 +1000 Subject: [PATCH 3/4] fix: restore providerVersionBranch --- native/provider.cc | 8 ++++---- src/ffi/types.ts | 5 ++--- src/verifier/argumentMapper/arguments.ts | 4 ++-- src/verifier/types.ts | 6 +++++- src/verifier/validateOptions.spec.ts | 13 ++++++++++++- src/verifier/validateOptions.ts | 15 ++++++++++++++- 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/native/provider.cc b/native/provider.cc index 2ac6bc18..ad779f44 100644 --- a/native/provider.cc +++ b/native/provider.cc @@ -413,14 +413,14 @@ Napi::Value PactffiVerifierSetPublishOptions(const Napi::CallbackInfo& info) { std::string providerVersion = info[1].As().Utf8Value(); std::string buildUrl = info[2].As().Utf8Value(); Napi::Array providerTagsRaw = info[3].As(); - std::string providerBranch = info[4].As().Utf8Value(); + std::string providerVersionBranch = info[4].As().Utf8Value(); pactffi_verifier_set_publish_options(handles[handleId], providerVersion.c_str(), buildUrl.c_str(), &NapiArrayToCStringVector(providerTagsRaw)[0], providerTagsRaw.Length(), - providerBranch.c_str()); + providerVersionBranch.c_str()); return info.Env().Undefined(); } @@ -788,7 +788,7 @@ Napi::Value PactffiVerifierBrokerSourceWithSelectors(const Napi::CallbackInfo& i bool enablePending = info[5].As().Value(); std::string includeWipPactsSince = info[6].As().Utf8Value(); Napi::Array providerTags = info[7].As(); - std::string providerBranch = info[8].As().Utf8Value(); + std::string providerVersionBranch = info[8].As().Utf8Value(); Napi::Array consumerVersionSelectors = info[9].As(); Napi::Array consumerVersionTags = info[10].As(); @@ -805,7 +805,7 @@ Napi::Value PactffiVerifierBrokerSourceWithSelectors(const Napi::CallbackInfo& i includeWipPactsSince.c_str(), &cProviderTags[0], providerTags.Length(), - providerBranch.c_str(), + providerVersionBranch.c_str(), &cConsumerVersionSelectors[0], consumerVersionSelectors.Length(), &cConsumerVersionTags[0], diff --git a/src/ffi/types.ts b/src/ffi/types.ts index 313adf43..f8aa737f 100644 --- a/src/ffi/types.ts +++ b/src/ffi/types.ts @@ -319,7 +319,7 @@ export type FfiVerificationFunctions = { providerVersion: string, buildUrl: string, providerTags: string[], - providerBranch: string + providerVersionBranch: string ): void; pactffiVerifierSetConsumerFilters( handle: FfiVerifierHandle, @@ -342,7 +342,6 @@ export type FfiVerificationFunctions = { password: string, token: string ): void; - // pactffiVerifierBrokerSource(handle: FfiVerifierHandle): void; pactffiVerifierBrokerSourceWithSelectors( handle: FfiVerifierHandle, url: string, @@ -352,7 +351,7 @@ export type FfiVerificationFunctions = { enablePending: boolean, includeWipPactsSince: string, providerTags: string[], - providerBranch: string, + providerVersionBranch: string, consumerVersionSelectors: string[], consumerVersionTags: string[] ): void; diff --git a/src/verifier/argumentMapper/arguments.ts b/src/verifier/argumentMapper/arguments.ts index a28bc179..e4821081 100644 --- a/src/verifier/argumentMapper/arguments.ts +++ b/src/verifier/argumentMapper/arguments.ts @@ -137,7 +137,7 @@ export const ffiFnMapping: FnMapping< opts.enablePending || false, opts.includeWipPactsSince || '', opts.providerVersionTags || [], - opts.providerBranch || '', + opts.providerVersionBranch || opts.providerBranch || '', opts.consumerVersionSelectors ? objArrayToStringArray(opts.consumerVersionSelectors) : [], @@ -220,7 +220,7 @@ export const ffiFnMapping: FnMapping< options.providerVersion, options.buildUrl || '', options.providerVersionTags || [], - options.providerBranch || '' + options.providerVersionBranch || options.providerBranch || '' ); return { status: FnValidationStatus.SUCCESS }; } diff --git a/src/verifier/types.ts b/src/verifier/types.ts index e2636b52..3ae94626 100644 --- a/src/verifier/types.ts +++ b/src/verifier/types.ts @@ -28,7 +28,7 @@ export interface VerifierOptions { pactBrokerToken?: string; consumerVersionTags?: string[]; providerVersionTags?: string[]; - providerBranch?: string; + providerVersionBranch?: string; providerStatesSetupUrl?: string; providerStatesSetupTeardown?: boolean; providerStatesSetupBody?: boolean; @@ -43,6 +43,10 @@ export interface VerifierOptions { buildUrl?: string; customProviderHeaders?: CustomHeaders | string[]; consumerFilters?: string[]; + /** + * @deprecated use providerVersionBranch instead + */ + providerBranch?: string; } /** These are the deprecated verifier options, removed prior to this verison, diff --git a/src/verifier/validateOptions.spec.ts b/src/verifier/validateOptions.spec.ts index b11d2e0a..c0683910 100644 --- a/src/verifier/validateOptions.spec.ts +++ b/src/verifier/validateOptions.spec.ts @@ -351,7 +351,7 @@ describe('Verifier argument validator', () => { }); }); - context.only('when given customProviderHeaders', () => { + context('when given customProviderHeaders', () => { context('using the object notation', () => { it('should pass through to the Pact Verifier', () => { expect(() => @@ -385,4 +385,15 @@ describe('Verifier argument validator', () => { }); }); }); + + context('when given providerBranch', () => { + it('should not throw an error', () => { + expect(() => + validateOptions({ + providerBaseUrl: 'http://localhost', + providerVersionBranch: 'blah', + }) + ).to.not.throw(Error); + }); + }); }); diff --git a/src/verifier/validateOptions.ts b/src/verifier/validateOptions.ts index 50301ff8..43b75e3a 100644 --- a/src/verifier/validateOptions.ts +++ b/src/verifier/validateOptions.ts @@ -16,6 +16,15 @@ export const deprecatedFunction = return true; }; +export const deprecatedBy = + (preferredOption: string) => + () => + (_: any, property: string): boolean => { + logger.warn(`${property} is deprecated, use ${preferredOption} instead`); + + return true; + }; + export const incompatibleWith = (keys: (keyof InternalPactVerifierOptions)[]) => (options: InternalPactVerifierOptions) => @@ -214,7 +223,11 @@ export const validationRules: ArgumentValidationRules Date: Sat, 13 Aug 2022 13:19:19 +0000 Subject: [PATCH 4/4] chore(release): 13.7.5 --- CHANGELOG.md | 8 ++++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba76c1f3..1557ebee 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [13.7.5](https://github.com/pact-foundation/pact-js-core/compare/v13.7.4...v13.7.5) (2022-08-13) + + +### Fixes and Improvements + +* restore providerVersionBranch ([c155f94](https://github.com/pact-foundation/pact-js-core/commit/c155f943a5bb9beb33c2b1fab35f4d8a7cfb7d24)) +* support customProviderHeaders as a string[] ([171cfd0](https://github.com/pact-foundation/pact-js-core/commit/171cfd0478406eb6126d437c4e45d0b4968afbea)) + ### [13.7.4](https://github.com/pact-foundation/pact-js-core/compare/v13.7.3...v13.7.4) (2022-08-13) diff --git a/package-lock.json b/package-lock.json index 167b5770..35b3cec3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@pact-foundation/pact-core", - "version": "13.7.4", + "version": "13.7.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@pact-foundation/pact-core", - "version": "13.7.4", + "version": "13.7.5", "cpu": [ "x64", "ia32", diff --git a/package.json b/package.json index 82f1e1cf..98058007 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@pact-foundation/pact-core", - "version": "13.7.4", + "version": "13.7.5", "description": "Core of @pact-foundation/pact. You almost certainly don't want to depend on this directly.", "main": "src/index.js", "homepage": "https://github.com/pact-foundation/pact-js-core#readme",