Skip to content

Commit d3e153e

Browse files
Merge pull request #93 from browserstack/fix/LOC-7659-local-args-multiple-values
[LOC-7659] fix(setup-local): keep every value of multi-value local-args flags
2 parents 024b083 + 930cbdb commit d3e153e

8 files changed

Lines changed: 202 additions & 343 deletions

File tree

‎setup-local/dist/index.js‎

Lines changed: 66 additions & 294 deletions
Large diffs are not rendered by default.

‎setup-local/package-lock.json‎

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎setup-local/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"@actions/github": "^6.0.0",
3838
"@actions/io": "^1.0.2",
3939
"@actions/tool-cache": "^1.6.0",
40-
"minimist": "^1.2.6",
4140
"uuid": "^8.3.0"
4241
},
4342
"devDependencies": {

‎setup-local/src/actionInput/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ActionInput {
7777
* @returns {{
7878
* accessKey: String,
7979
* localTesting: String,
80-
* localArgs: String,
80+
* localArgs: Array<String>,
8181
* localIdentifier: String,
8282
* localLoggingLevel: Number
8383
* }}

‎setup-local/src/actionInput/inputValidator.js‎

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const core = require('@actions/core');
22
const { v4: uuidv4 } = require('uuid');
3-
const parseArgs = require('minimist');
43
const constants = require('../../config/constants');
54

65
const {
@@ -84,28 +83,70 @@ class InputValidator {
8483
return inputLocalIdentifier.split(/\s+/).join('-');
8584
}
8685

86+
/**
87+
* Splits a string into tokens on whitespace, keeping single/double quoted
88+
* sections together (the quotes themselves are removed).
89+
* @param {String} argString Raw args string
90+
* @returns {Array<String>} Tokens
91+
*/
92+
static _tokenizeArgs(argString) {
93+
const tokens = [];
94+
let current = '';
95+
let inToken = false;
96+
let quoteChar = null;
97+
98+
for (const char of argString) {
99+
if (quoteChar) {
100+
if (char === quoteChar) {
101+
quoteChar = null;
102+
} else {
103+
current += char;
104+
}
105+
} else if (char === '"' || char === "'") {
106+
quoteChar = char;
107+
inToken = true;
108+
} else if (/\s/.test(char)) {
109+
if (inToken) {
110+
tokens.push(current);
111+
current = '';
112+
inToken = false;
113+
}
114+
} else {
115+
current += char;
116+
inToken = true;
117+
}
118+
}
119+
if (inToken) tokens.push(current);
120+
121+
return tokens;
122+
}
123+
87124
/**
88125
* Validates the local-args input. Removes any args which might conflict with
89126
* the input args taken from the action input for the Local Binary.
127+
* Every value following a flag is preserved, so flags taking multiple values
128+
* (e.g. --include-hosts host1 host2) are passed to the binary intact.
90129
* @param {String} inputLocalArgs Action input for 'local-args'
91-
* @returns {String} Parsed args
130+
* @returns {Array<String>} Parsed args, one element per argv entry
92131
*/
93132
static validateLocalArgs(inputLocalArgs) {
94-
const parsedArgs = parseArgs(inputLocalArgs.split(/\s+/));
133+
if (!inputLocalArgs) return [];
95134

96-
delete parsedArgs._;
97-
RESTRICTED_LOCAL_ARGS.forEach((arg) => {
98-
delete parsedArgs[arg];
99-
});
135+
const tokens = InputValidator._tokenizeArgs(inputLocalArgs);
136+
const isFlag = (token) => /^-{1,2}[^-\d]/.test(token);
100137

101-
let parsedArgsString = '';
102-
for (const [key, value] of Object.entries(parsedArgs)) {
103-
const argKey = key.length === 1 ? `-${key}` : `--${key}`;
104-
const argValue = value === true ? '' : value;
105-
parsedArgsString += `${argKey} ${argValue} `;
106-
}
138+
const keptTokens = [];
139+
// Tokens before the first flag have no flag to belong to and are dropped.
140+
let keepCurrentGroup = false;
141+
tokens.forEach((token) => {
142+
if (isFlag(token)) {
143+
const flagName = token.replace(/^-{1,2}/, '').split('=')[0];
144+
keepCurrentGroup = !RESTRICTED_LOCAL_ARGS.includes(flagName);
145+
}
146+
if (keepCurrentGroup) keptTokens.push(token);
147+
});
107148

108-
return parsedArgsString;
149+
return keptTokens;
109150
}
110151
}
111152

‎setup-local/src/binaryControl.js‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,30 @@ class BinaryControl {
9898
localTesting: binaryAction,
9999
} = this.stateForBinary;
100100

101-
let argsString = `--key ${key} --only-automate --ci-plugin GitHubAction `;
101+
// Built as an argv array (not a command string) so that values containing
102+
// spaces/quotes/backslashes reach the binary verbatim, without re-parsing.
103+
const args = ['--key', key, '--only-automate', '--ci-plugin', 'GitHubAction'];
102104

103105
switch (binaryAction) {
104106
case LOCAL_TESTING.START: {
105-
if (localArgs) argsString += `${localArgs} `;
106-
if (localIdentifier) argsString += `--local-identifier ${localIdentifier} `;
107+
if (localArgs && localArgs.length) args.push(...localArgs);
108+
if (localIdentifier) args.push('--local-identifier', localIdentifier);
107109
if (verbose) {
108110
this._generateLogFileMetadata();
109-
argsString += `--verbose ${verbose} --log-file ${this.logFilePath} `;
111+
args.push('--verbose', `${verbose}`, '--log-file', this.logFilePath);
110112
}
111113
break;
112114
}
113115
case LOCAL_TESTING.STOP: {
114-
if (localIdentifier) argsString += `--local-identifier ${localIdentifier} `;
116+
if (localIdentifier) args.push('--local-identifier', localIdentifier);
115117
break;
116118
}
117119
default: {
118120
throw Error('Invalid Binary Action');
119121
}
120122
}
121123

122-
this.binaryArgs = argsString;
124+
this.binaryArgs = args;
123125
}
124126

125127
/**
@@ -130,8 +132,8 @@ class BinaryControl {
130132
let triggerOutput = '';
131133
let triggerError = '';
132134
await exec.exec(
133-
`${LOCAL_BINARY_NAME} ${this.binaryArgs} --daemon ${operation}`,
134-
[],
135+
LOCAL_BINARY_NAME,
136+
[...this.binaryArgs, '--daemon', operation],
135137
{
136138
listeners: {
137139
stdout: (data) => {

‎setup-local/test/actionInput/inputValidator.test.js‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,50 @@ describe('InputValidator class to validate individual fields of the action input
7979
context('Validates local args', () => {
8080
it('Removes the restricted/not-alowed args from the local-args input and returns the string', () => {
8181
const inputLocalArgs = '--key someKey --proxy-host hostname --someOtherKey someValue -z --daemon start --ci-plugin someName -k anotherKey --only-automate --log-file some/path/ --verbose level --local-identifier someIdentifier';
82-
const expectedLocalArgs = '--proxy-host hostname --someOtherKey someValue -z ';
83-
expect(InputValidator.validateLocalArgs(inputLocalArgs)).to.eq(expectedLocalArgs);
82+
const expectedLocalArgs = ['--proxy-host', 'hostname', '--someOtherKey', 'someValue', '-z'];
83+
expect(InputValidator.validateLocalArgs(inputLocalArgs)).to.deep.eq(expectedLocalArgs);
84+
});
85+
86+
['', null, undefined].forEach((value) => {
87+
it(`Returns an empty array if the input is :${JSON.stringify(value)}`, () => {
88+
expect(InputValidator.validateLocalArgs(value)).to.deep.eq([]);
89+
});
90+
});
91+
92+
it('Keeps every value of a flag that takes multiple values (quoted)', () => {
93+
const inputLocalArgs = '--include-hosts "hostA.example.com:443" "hostB.example.com:443" --force-local';
94+
expect(InputValidator.validateLocalArgs(inputLocalArgs)).to.deep.eq([
95+
'--include-hosts', 'hostA.example.com:443', 'hostB.example.com:443', '--force-local',
96+
]);
97+
});
98+
99+
it('Keeps every value of a flag that takes multiple values (unquoted, multi-line)', () => {
100+
const inputLocalArgs = '--include-hosts hostA.example.com\n hostB.example.com\n--exclude-hosts hostC hostD';
101+
expect(InputValidator.validateLocalArgs(inputLocalArgs)).to.deep.eq([
102+
'--include-hosts', 'hostA.example.com', 'hostB.example.com', '--exclude-hosts', 'hostC', 'hostD',
103+
]);
104+
});
105+
106+
it('Removes the values of restricted args even when they have multiple values', () => {
107+
const inputLocalArgs = '--include-hosts hostA hostB --verbose 3 extra --local-identifier=abc --key=secret --proxy-port 8080';
108+
expect(InputValidator.validateLocalArgs(inputLocalArgs)).to.deep.eq([
109+
'--include-hosts', 'hostA', 'hostB', '--proxy-port', '8080',
110+
]);
111+
});
112+
113+
it('Drops tokens that appear before the first flag', () => {
114+
expect(InputValidator.validateLocalArgs('stray value --force-local')).to.deep.eq(['--force-local']);
115+
});
116+
117+
it('Keeps negative numbers as values rather than treating them as flags', () => {
118+
expect(InputValidator.validateLocalArgs('--some-flag -1 --other')).to.deep.eq(['--some-flag', '-1', '--other']);
119+
});
120+
121+
it('Keeps quoted values with spaces, quotes and backslashes as single verbatim args', () => {
122+
const inputLocalArgs = `--folder "C:\\my dir\\" --name 'say "hi"' --other "it's"`;
123+
expect(InputValidator.validateLocalArgs(inputLocalArgs)).to.deep.eq([
124+
'--folder', 'C:\\my dir\\', '--name', 'say "hi"', '--other', "it's",
125+
]);
84126
});
85127
});
86128
});

‎setup-local/test/binaryControl.test.js‎

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('Binary Control Operations', () => {
199199
it('with localArgs, localIdentifier, localLoggingLevel', () => {
200200
const stateForBinary = {
201201
accessKey: 'someKey',
202-
localArgs: '--arg1 val1 --arg2 val2',
202+
localArgs: ['--arg1', 'val1', '--arg2', 'val2'],
203203
localIdentifier: 'someIdentifier',
204204
localLoggingLevel: 1,
205205
localTesting: 'start',
@@ -214,95 +214,99 @@ describe('Binary Control Operations', () => {
214214
os.platform(),
215215
'BrowserStackLocal_someJobName_now.log',
216216
);
217-
const expectedFinalArgs = `--key someKey --only-automate --ci-plugin GitHubAction --arg1 val1 --arg2 val2 --local-identifier someIdentifier --verbose 1 --log-file ${expectedLogFilePath} `;
217+
const expectedFinalArgs = [
218+
'--key', 'someKey', '--only-automate', '--ci-plugin', 'GitHubAction',
219+
'--arg1', 'val1', '--arg2', 'val2',
220+
'--local-identifier', 'someIdentifier', '--verbose', '1', '--log-file', expectedLogFilePath,
221+
];
218222
const binaryControl = new BinaryControl(stateForBinary);
219223
binaryControl._generateArgsForBinary();
220-
expect(binaryControl.binaryArgs).to.eq(expectedFinalArgs);
224+
expect(binaryControl.binaryArgs).to.deep.eq(expectedFinalArgs);
221225
});
222226

223227
it('with localArgs, localIdentifier', () => {
224228
const stateForBinary = {
225229
accessKey: 'someKey',
226-
localArgs: '--arg1 val1 --arg2 val2',
230+
localArgs: ['--arg1', 'val1', '--arg2', 'val2'],
227231
localIdentifier: 'someIdentifier',
228232
localLoggingLevel: 0,
229233
localTesting: 'start',
230234
};
231235

232-
const expectedFinalArgs = `--key someKey --only-automate --ci-plugin GitHubAction --arg1 val1 --arg2 val2 --local-identifier someIdentifier `;
236+
const expectedFinalArgs = ['--key', 'someKey', '--only-automate', '--ci-plugin', 'GitHubAction', '--arg1', 'val1', '--arg2', 'val2', '--local-identifier', 'someIdentifier'];
233237
const binaryControl = new BinaryControl(stateForBinary);
234238
binaryControl._generateArgsForBinary();
235-
expect(binaryControl.binaryArgs).to.eq(expectedFinalArgs);
239+
expect(binaryControl.binaryArgs).to.deep.eq(expectedFinalArgs);
236240
});
237241

238242
it('with localArgs', () => {
239243
const stateForBinary = {
240244
accessKey: 'someKey',
241-
localArgs: '--arg1 val1 --arg2 val2',
245+
localArgs: ['--arg1', 'val1', '--arg2', 'val2'],
242246
localIdentifier: '',
243247
localLoggingLevel: 0,
244248
localTesting: 'start',
245249
};
246250

247-
const expectedFinalArgs = `--key someKey --only-automate --ci-plugin GitHubAction --arg1 val1 --arg2 val2 `;
251+
const expectedFinalArgs = ['--key', 'someKey', '--only-automate', '--ci-plugin', 'GitHubAction', '--arg1', 'val1', '--arg2', 'val2'];
248252
const binaryControl = new BinaryControl(stateForBinary);
249253
binaryControl._generateArgsForBinary();
250-
expect(binaryControl.binaryArgs).to.eq(expectedFinalArgs);
254+
expect(binaryControl.binaryArgs).to.deep.eq(expectedFinalArgs);
251255
});
252256

253257
it('with the default args', () => {
254258
const stateForBinary = {
255259
accessKey: 'someKey',
256-
localArgs: '',
260+
localArgs: [],
257261
localIdentifier: '',
258262
localLoggingLevel: 0,
259263
localTesting: 'start',
260264
};
261265

262-
const expectedFinalArgs = `--key someKey --only-automate --ci-plugin GitHubAction `;
266+
const expectedFinalArgs = ['--key', 'someKey', '--only-automate', '--ci-plugin', 'GitHubAction'];
263267
const binaryControl = new BinaryControl(stateForBinary);
264268
binaryControl._generateArgsForBinary();
265-
expect(binaryControl.binaryArgs).to.eq(expectedFinalArgs);
269+
expect(binaryControl.binaryArgs).to.deep.eq(expectedFinalArgs);
266270
});
267271
});
268272

269273
context('Stop operation', () => {
270274
it('with localIdentifier', () => {
271275
const stateForBinary = {
272276
accessKey: 'someKey',
273-
localArgs: '',
277+
localArgs: [],
274278
localIdentifier: 'someIdentifier',
275279
localLoggingLevel: 0,
276280
localTesting: 'stop',
277281
};
278282

279-
const expectedFinalArgs = `--key someKey --only-automate --ci-plugin GitHubAction --local-identifier someIdentifier `;
283+
const expectedFinalArgs = ['--key', 'someKey', '--only-automate', '--ci-plugin', 'GitHubAction', '--local-identifier', 'someIdentifier'];
280284
const binaryControl = new BinaryControl(stateForBinary);
281285
binaryControl._generateArgsForBinary();
282-
expect(binaryControl.binaryArgs).to.eq(expectedFinalArgs);
286+
expect(binaryControl.binaryArgs).to.deep.eq(expectedFinalArgs);
283287
});
284288

285289
it('without localIdentifier', () => {
286290
const stateForBinary = {
287291
accessKey: 'someKey',
288-
localArgs: '',
292+
localArgs: [],
289293
localIdentifier: '',
290294
localLoggingLevel: 0,
291295
localTesting: 'stop',
292296
};
293297

294-
const expectedFinalArgs = `--key someKey --only-automate --ci-plugin GitHubAction `;
298+
const expectedFinalArgs = ['--key', 'someKey', '--only-automate', '--ci-plugin', 'GitHubAction'];
295299
const binaryControl = new BinaryControl(stateForBinary);
296300
binaryControl._generateArgsForBinary();
297-
expect(binaryControl.binaryArgs).to.eq(expectedFinalArgs);
301+
expect(binaryControl.binaryArgs).to.deep.eq(expectedFinalArgs);
298302
});
299303
});
300304

301305
context('Invalid Operation', () => {
302306
it('Throws error in case its not start/stop', () => {
303307
const stateForBinary = {
304308
accessKey: 'someKey',
305-
localArgs: '',
309+
localArgs: [],
306310
localIdentifier: 'someIdentifier',
307311
localLoggingLevel: 0,
308312
localTesting: 'someOperation',
@@ -322,7 +326,7 @@ describe('Binary Control Operations', () => {
322326
beforeEach(() => {
323327
sinon.stub(exec, 'exec');
324328
binaryControl = new BinaryControl();
325-
binaryControl.binaryArgs = 'someArgs ';
329+
binaryControl.binaryArgs = ['someArgs'];
326330
});
327331

328332
afterEach(() => {
@@ -331,7 +335,7 @@ describe('Binary Control Operations', () => {
331335

332336
it('Start Operation', async () => {
333337
const response = await binaryControl._triggerBinary(LOCAL_TESTING.START);
334-
sinon.assert.calledWith(exec.exec, `${LOCAL_BINARY_NAME} someArgs --daemon start`);
338+
sinon.assert.calledWith(exec.exec, LOCAL_BINARY_NAME, ['someArgs', '--daemon', 'start']);
335339
expect(response).to.eql({
336340
output: '',
337341
error: '',
@@ -340,7 +344,7 @@ describe('Binary Control Operations', () => {
340344

341345
it('Stop Operation', async () => {
342346
const response = await binaryControl._triggerBinary(LOCAL_TESTING.STOP);
343-
sinon.assert.calledWith(exec.exec, `${LOCAL_BINARY_NAME} someArgs --daemon stop`);
347+
sinon.assert.calledWith(exec.exec, LOCAL_BINARY_NAME, ['someArgs', '--daemon', 'stop']);
344348
expect(response).to.eql({
345349
output: '',
346350
error: '',

0 commit comments

Comments
 (0)