Skip to content

Commit

Permalink
fix(dotnet): update handling of extraParameters to be compatible with…
Browse files Browse the repository at this point in the history
… spawn (#403)
  • Loading branch information
AgentEnder authored Mar 25, 2022
1 parent 84bde4c commit 65f0c48
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
3 changes: 0 additions & 3 deletions packages/dotnet/.babelrc

This file was deleted.

68 changes: 68 additions & 0 deletions packages/dotnet/src/lib/core/dotnet.client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { DotNetClient } from './dotnet.client';
import { mockDotnetFactory } from './dotnet.factory';
import * as cp from 'child_process';

describe('dotnet client', () => {
describe('publish', () => {
describe('extra parameters', () => {
const dotnetClient = new DotNetClient(mockDotnetFactory());

let spawnSyncSpy: jest.SpyInstance;

beforeEach(() => {
spawnSyncSpy = jest
.spyOn(cp, 'spawnSync')
.mockImplementation(jest.fn());
});

afterEach(() => {
jest.resetAllMocks();
});

it('should handle multiple parameters', () => {
dotnetClient.publish(
'my-project',
undefined,
undefined,
'--flag --other-flag',
);
expect(spawnSyncSpy.mock.calls[0][1]).toContain('--flag');
expect(spawnSyncSpy.mock.calls[0][1]).toContain('--other-flag');
expect(spawnSyncSpy).toHaveBeenCalledTimes(1);
});

it('should handle multiple parameters with quotations', () => {
dotnetClient.publish(
'my-project',
undefined,
undefined,
`--flag \\p:"my project"`,
);
expect(spawnSyncSpy.mock.calls[0][1]).toContain('--flag');
expect(spawnSyncSpy.mock.calls[0][1]).toContain('\\p:"my project"');
expect(spawnSyncSpy).toHaveBeenCalledTimes(1);
});

it('should handle several parameters', () => {
dotnetClient.publish(
'my-project',
undefined,
undefined,
`--self-contained=false /p:CopyOutputSymbolsToPublishDirectory=false /p:Version=2022.03.25.1 /p:VersionAssembly=2022.03.25.1 /p:Name:"My Project"`,
);
expect(spawnSyncSpy.mock.calls[0][1]).toMatchInlineSnapshot(`
Array [
"publish",
"\\"my-project\\"",
"--self-contained=false",
"/p:CopyOutputSymbolsToPublishDirectory=false",
"/p:Version=2022.03.25.1",
"/p:VersionAssembly=2022.03.25.1",
"/p:Name:\\"My Project\\"",
]
`);
expect(spawnSyncSpy).toHaveBeenCalledTimes(1);
});
});
});
});
10 changes: 9 additions & 1 deletion packages/dotnet/src/lib/core/dotnet.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export class DotNetClient {
params.push(`-p:PublishProfile=${publishProfile}`);
}
if (extraParameters) {
params.push(`${extraParameters}`);
const matches = extraParameters.match(EXTRA_PARAMS_REGEX);
params.push(...(matches as RegExpMatchArray));
}
return this.logAndExecute(params);
}
Expand Down Expand Up @@ -195,3 +196,10 @@ export class DotNetClient {
});
}
}

/**
* Regular Expression for Parsing Extra Params before sending to spawn / exec
* First part of expression matches parameters such as --flag="my answer"
* Second part of expression matches parameters such as --flag=my_answer
*/
const EXTRA_PARAMS_REGEX = /\S*".+?"|\S+/g;

0 comments on commit 65f0c48

Please sign in to comment.