Skip to content

Commit

Permalink
Merge pull request #395 from pact-foundation/fix/reinstate-breaking-c…
Browse files Browse the repository at this point in the history
…hanges

Fix/reinstate breaking changes
  • Loading branch information
mefellows authored Aug 13, 2022
2 parents 9c259c3 + c155f94 commit 3dda0fb
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 35 deletions.
8 changes: 4 additions & 4 deletions native/provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,14 @@ Napi::Value PactffiVerifierSetPublishOptions(const Napi::CallbackInfo& info) {
std::string providerVersion = info[1].As<Napi::String>().Utf8Value();
std::string buildUrl = info[2].As<Napi::String>().Utf8Value();
Napi::Array providerTagsRaw = info[3].As<Napi::Array>();
std::string providerBranch = info[4].As<Napi::String>().Utf8Value();
std::string providerVersionBranch = info[4].As<Napi::String>().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();
}
Expand Down Expand Up @@ -788,7 +788,7 @@ Napi::Value PactffiVerifierBrokerSourceWithSelectors(const Napi::CallbackInfo& i
bool enablePending = info[5].As<Napi::Boolean>().Value();
std::string includeWipPactsSince = info[6].As<Napi::String>().Utf8Value();
Napi::Array providerTags = info[7].As<Napi::Array>();
std::string providerBranch = info[8].As<Napi::String>().Utf8Value();
std::string providerVersionBranch = info[8].As<Napi::String>().Utf8Value();
Napi::Array consumerVersionSelectors = info[9].As<Napi::Array>();
Napi::Array consumerVersionTags = info[10].As<Napi::Array>();

Expand All @@ -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],
Expand Down
5 changes: 2 additions & 3 deletions src/ffi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export type FfiVerificationFunctions = {
providerVersion: string,
buildUrl: string,
providerTags: string[],
providerBranch: string
providerVersionBranch: string
): void;
pactffiVerifierSetConsumerFilters(
handle: FfiVerifierHandle,
Expand All @@ -342,7 +342,6 @@ export type FfiVerificationFunctions = {
password: string,
token: string
): void;
// pactffiVerifierBrokerSource(handle: FfiVerifierHandle): void;
pactffiVerifierBrokerSourceWithSelectors(
handle: FfiVerifierHandle,
url: string,
Expand All @@ -352,7 +351,7 @@ export type FfiVerificationFunctions = {
enablePending: boolean,
includeWipPactsSince: string,
providerTags: string[],
providerBranch: string,
providerVersionBranch: string,
consumerVersionSelectors: string[],
consumerVersionTags: string[]
): void;
Expand Down
35 changes: 27 additions & 8 deletions src/verifier/argumentMapper/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand Down Expand Up @@ -118,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)
: [],
Expand All @@ -139,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 ||
Expand Down Expand Up @@ -201,7 +220,7 @@ export const ffiFnMapping: FnMapping<
options.providerVersion,
options.buildUrl || '',
options.providerVersionTags || [],
options.providerBranch || ''
options.providerVersionBranch || options.providerBranch || ''
);
return { status: FnValidationStatus.SUCCESS };
}
Expand Down
4 changes: 2 additions & 2 deletions src/verifier/argumentMapper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type FnObject = {
};

export type FnMapping<T extends FnObject, O> = {
[Key in keyof T]: FnArgumentMapping<Key, T, O>;
[Key in keyof T]: FnArgumentMapping<O>;
};

export enum FnValidationStatus {
Expand All @@ -26,7 +26,7 @@ export type FnValidationResult = {
messages?: string[];
};

export type FnArgumentMapping<K extends keyof T, T extends FnObject, O> = {
export type FnArgumentMapping<O> = {
validateAndExecute: (
ffi: Ffi,
handle: FfiHandle,
Expand Down
8 changes: 6 additions & 2 deletions src/verifier/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface VerifierOptions {
pactBrokerToken?: string;
consumerVersionTags?: string[];
providerVersionTags?: string[];
providerBranch?: string;
providerVersionBranch?: string;
providerStatesSetupUrl?: string;
providerStatesSetupTeardown?: boolean;
providerStatesSetupBody?: boolean;
Expand All @@ -41,8 +41,12 @@ export interface VerifierOptions {
logLevel?: LogLevel;
disableSslVerification?: boolean;
buildUrl?: string;
customProviderHeaders?: CustomHeaders;
customProviderHeaders?: CustomHeaders | string[];
consumerFilters?: string[];
/**
* @deprecated use providerVersionBranch instead
*/
providerBranch?: string;
}

/** These are the deprecated verifier options, removed prior to this verison,
Expand Down
41 changes: 38 additions & 3 deletions src/verifier/validateOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,47 @@ describe('Verifier argument validator', () => {
});
});

context('when given customProviderHeaders that are defined', () => {
it('should pass through to the Pact Verifier', () => {
context('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);
});
});
});
});

context('when given providerBranch', () => {
it('should not throw an error', () => {
expect(() =>
validateOptions({
providerBaseUrl: 'http://localhost',
customProviderHeaders: { my: 'header' },
providerVersionBranch: 'blah',
})
).to.not.throw(Error);
});
Expand Down
45 changes: 33 additions & 12 deletions src/verifier/validateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ import {
} from './types';

export const deprecatedFunction =
(_: InternalPactVerifierOptions) =>
() =>
(_: any, property: string): boolean => {
logger.warn(`${property} is deprecated and no longer has any effect`);

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) =>
Expand Down Expand Up @@ -70,15 +79,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;
Expand All @@ -93,8 +99,7 @@ const logLevelValidator =
};

const consumerVersionSelectorValidator =
(options: InternalPactVerifierOptions) =>
(l: LogLevel): boolean => {
(options: InternalPactVerifierOptions) => (): boolean => {
if (
options.consumerVersionSelectors &&
Array.isArray(options.consumerVersionSelectors)
Expand Down Expand Up @@ -139,8 +144,7 @@ const consumerVersionSelectorValidator =
};

const consumerVersionTagsValidator =
(options: InternalPactVerifierOptions) =>
(l: LogLevel): boolean => {
(options: InternalPactVerifierOptions) => (): boolean => {
if (options.consumerVersionTags) {
if (
!checkTypes.string(options.consumerVersionTags) &&
Expand All @@ -165,6 +169,19 @@ 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<T> = {
[Key in keyof T]-?: ((options: T) => AssertFunction)[];
};
Expand All @@ -175,7 +192,7 @@ export const validationRules: ArgumentValidationRules<InternalPactVerifierOption
buildUrl: [wrapCheckType(checkTypes.assert.nonEmptyString)],
consumerVersionSelectors: [consumerVersionSelectorValidator],
consumerVersionTags: [consumerVersionTagsValidator],
customProviderHeaders: [wrapCheckType(checkTypes.assert.nonEmptyObject)],
customProviderHeaders: [customProviderHeadersValidator],
disableSslVerification: [wrapCheckType(checkTypes.assert.boolean)],
enablePending: [wrapCheckType(checkTypes.assert.boolean)],
format: [deprecatedFunction],
Expand Down Expand Up @@ -206,7 +223,11 @@ export const validationRules: ArgumentValidationRules<InternalPactVerifierOption
incompatibleWith(['pactBrokerUsername', 'pactBrokerPassword']),
],
providerVersionTags: [wrapCheckType(checkTypes.assert.nonEmptyString)],
providerBranch: [wrapCheckType(checkTypes.assert.nonEmptyString)],
providerBranch: [
wrapCheckType(checkTypes.assert.nonEmptyString),
deprecatedBy('providerVersionBranch'),
],
providerVersionBranch: [wrapCheckType(checkTypes.assert.nonEmptyString)],
providerStatesSetupUrl: [wrapCheckType(checkTypes.assert.nonEmptyString)],
providerStatesSetupTeardown: [wrapCheckType(checkTypes.assert.boolean)],
providerStatesSetupBody: [wrapCheckType(checkTypes.assert.boolean)],
Expand Down
2 changes: 1 addition & 1 deletion standalone/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3dda0fb

Please sign in to comment.