Skip to content

Commit

Permalink
Update spawn args (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed May 14, 2024
1 parent ce9b582 commit e5b3036
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
7 changes: 4 additions & 3 deletions modules/child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import childProcess from 'node:child_process';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import { parse, quote } from 'shell-quote';
import { parse as parseCmd } from 'shell-quote';
import { escapeChar, getType, isString } from './common.js';
import { isExecutable, isFile } from './file-util.js';

Expand Down Expand Up @@ -143,9 +143,10 @@ export class ChildProcess {
constructor(cmd, args, opt) {
this.#cmd = isString(cmd) ? cmd : null;
if (Array.isArray(args)) {
this.#args = parse(quote(args));
args = new CmdArgs(args).toString();
this.#args = parseCmd(args);
} else if (args && isString(args)) {
this.#args = parse(args);
this.#args = parseCmd(args);
} else {
this.#args = [];
}
Expand Down
44 changes: 44 additions & 0 deletions test/child-process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,5 +446,49 @@ describe('ChildProcess', () => {
stub.restore();
assert.isTrue(calledOnce);
});

it('should spawn with file path as last argument', async () => {
const app = path.resolve(IS_WIN
? path.join('test', 'file', 'test.cmd')
: path.join('test', 'file', 'test.sh'));
const arg = ['-a', '--b="c d\\e"'];
const file = path.resolve(path.join('test', 'file', 'test.txt'));
await fs.chmodSync(app, PERM_EXEC);
await fs.chmodSync(file, PERM_FILE);
const stub = sinon.stub(childProcess, 'spawn').callsFake((...args) => {
const [, cmdArgs] = args;
const [arg1, arg2, arg3] = cmdArgs;
assert.strictEqual(cmdArgs.length, 3);
assert.strictEqual(arg1, '-a');
assert.strictEqual(arg2, '--b="c d\\e"');
assert.strictEqual(arg3, file);
});
await new ChildProcess(app, arg).spawn(file);
const { calledOnce } = stub;
stub.restore();
assert.isTrue(calledOnce);
});

it('should spawn with file path as last argument', async () => {
const app = path.resolve(IS_WIN
? path.join('test', 'file', 'test.cmd')
: path.join('test', 'file', 'test.sh'));
const arg = '-a --b="c d\\e"';
const file = path.resolve(path.join('test', 'file', 'test.txt'));
await fs.chmodSync(app, PERM_EXEC);
await fs.chmodSync(file, PERM_FILE);
const stub = sinon.stub(childProcess, 'spawn').callsFake((...args) => {
const [, cmdArgs] = args;
const [arg1, arg2, arg3] = cmdArgs;
assert.strictEqual(cmdArgs.length, 3);
assert.strictEqual(arg1, '-a');
assert.strictEqual(arg2, '--b=c d\\e');
assert.strictEqual(arg3, file);
});
await new ChildProcess(app, arg).spawn(file);
const { calledOnce } = stub;
stub.restore();
assert.isTrue(calledOnce);
});
});
});

0 comments on commit e5b3036

Please sign in to comment.