Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Serialize {enablePending: false} to --no-enable-pending #356

Merged
merged 3 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/spawn/arguments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,17 @@ describe('Pact Util Spec', () => {
expect(result.length).to.be.equal(3);
expect(result[0]).to.be.equal('http://idontexist');
});
it('should accept mapping as function', () => {
const result = argsHelper.toArgumentsArray(
{
enablePending: false,
},
{
enablePending: (enablePending: boolean) =>
enablePending ? ['--enable-pending'] : ['--no-enable-pending'],
}
);
expect(result).to.eql(['--no-enable-pending']);
});
});
});
18 changes: 13 additions & 5 deletions src/spawn/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export type CliVerbOptions = {
cliVerb: string;
};

export interface ArgumentMappings {
[key: string]: Mapping;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Mapping = string | ((val: any) => string[]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to turn off the lint warnings - perhaps this could be unknown?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we change to unknown here, we need annotate the mapping function argument type with unknown,

let argMapping: ArgumentMappings = {
    enablePending: (enablePending: unknown) => [enablePending ? '--enable-pending' : '--no-enable-pending']
}

or do a explicit cast:

let argMapping = {
    enablePending: (enablePending: boolean) => [enablePending ? '--enable-pending' : '--no-enable-pending']
} as ArgumentMappings;

Is that ok?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'd rather not do an explicit cast unless we have to, as (like any) it's turning off the type checking. Maybe we can bring in an implicit type parameter or something - I'll have a play.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right - so the type checking is already pretty nobbled by this point, because of the way the rest of the mapper works (there are also already some explicit casts in the branches above). This is one of the reasons we rewrote the mapper for the native verifier in the master branch.

I think unknown is probably the better way- I'll push a commit that does that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I prefer unknown, because really we don't know what the type will be at that point. If we did rewrite this mapper from scratch, we'd want to do it in a way that the type information was retained. I think this is an artefact of this originally being pure JS)


export type SpawnArgument =
| CanDeployOptions
| MessageOptions
Expand All @@ -34,11 +41,12 @@ const valFor = (v: SpawnArgument): Array<string> => {
return v !== PACT_NODE_NO_VALUE ? [`${v}`] : [];
};

const mapFor = (mapping: string, v: string): Array<string> =>
mapping === DEFAULT_ARG ? valFor(v) : [mapping].concat(valFor(v));
const mapFor = (mapping: Mapping, v: string): Array<string> =>
mapping === DEFAULT_ARG ? valFor(v) :
typeof mapping === 'string' ? [mapping].concat(valFor(v)): mapping(v);

const convertValue = (
mapping: string,
mapping: Mapping,
v: SpawnArgument | Array<SpawnArgument>
): Array<string> => {
if (mapping && (v || typeof v === 'boolean')) {
Expand All @@ -54,7 +62,7 @@ const convertValue = (
export class Arguments {
public toArgumentsArray(
args: SpawnArguments,
mappings: { [id: string]: string }
mappings: ArgumentMappings
): string[] {
return _.chain(args instanceof Array ? args : [args])
.map((x: SpawnArguments) => this.createArgumentsFromObject(x, mappings))
Expand All @@ -64,7 +72,7 @@ export class Arguments {

private createArgumentsFromObject(
args: SpawnArguments,
mappings: { [id: string]: string }
mappings: ArgumentMappings
): string[] {
return _.chain(args)
.reduce(
Expand Down
3 changes: 2 additions & 1 deletion src/spawn/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as path from 'path';
import logger from '../logger';
import pactEnvironment from '../pact-environment';
import argsHelper, { SpawnArguments, DEFAULT_ARG } from './arguments';
import { ArgumentMappings } from '.';
flappyBug marked this conversation as resolved.
Show resolved Hide resolved

export class Spawn {
public get cwd(): string {
Expand All @@ -14,7 +15,7 @@ export class Spawn {
public spawnBinary(
command: string,
args: SpawnArguments = {},
argMapping: { [id: string]: string } = {}
argMapping: ArgumentMappings = {}
): ChildProcess {
const envVars = JSON.parse(JSON.stringify(process.env)); // Create copy of environment variables
// Remove environment variable if there
Expand Down
2 changes: 1 addition & 1 deletion src/verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Verifier {
publishVerificationResult: '--publish-verification-results',
providerVersion: '--provider-app-version',
provider: '--provider',
enablePending: '--enable-pending',
enablePending: (enablePending: boolean) => [enablePending ? '--enable-pending' : '--no-enable-pending'],
customProviderHeaders: '--custom-provider-header',
verbose: '--verbose',
includeWipPactsSince: '--include-wip-pacts-since',
Expand Down