Skip to content

Commit

Permalink
Update cmdArgs handling
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed May 14, 2024
1 parent 6cb33cb commit c8376a6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 10 deletions.
9 changes: 4 additions & 5 deletions modules/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getFileNameFromFilePath, getFileTimestamp, isDir, isExecutable, isFile,
removeDir, removeDirectory, readFile
} from 'web-ext-native-msg';
import { getType, quoteArg, isObjectNotEmpty, isString } from './common.js';
import { getType, isObjectNotEmpty, isString } from './common.js';
import { version as hostVersion } from './version.js';

/* constants */
Expand All @@ -41,7 +41,7 @@ const TMPDIR_FILES_PB = path.join(TMPDIR_APP, TMP_FILES_PB);
/* editor config */
export const editorConfig = {
editorPath: '',
cmdArgs: [],
cmdArgs: '',
hasPlaceholder: false
};

Expand Down Expand Up @@ -125,7 +125,7 @@ export const exportEditorConfig = async (data, editorConfigPath) => {
editorConfig[key] = value;
}
if (key === 'cmdArgs') {
editorConfig[key] = new CmdArgs(value).toArray();
editorConfig[key] = value;
editorConfig.hasPlaceholder = reg.test(value);
}
}
Expand Down Expand Up @@ -313,15 +313,14 @@ export const execChildProcess = async (file, app = editorConfig.editorPath) => {
args = new CmdArgs(cmdArgs).toArray();
}
if (hasPlaceholder) {
const [filePath] = new CmdArgs(quoteArg(file)).toArray();
const reg =
new RegExp(`\\$(?:${TMP_FILE_PLACEHOLDER}|{${TMP_FILE_PLACEHOLDER}})`);
const l = args.length;
let i = 0;
while (i < l) {
const arg = args[i];
if (reg.test(arg)) {
args.splice(i, 1, arg.replace(reg, filePath));
args.splice(i, 1, arg.replace(reg, file));
}
i++;
}
Expand Down
102 changes: 97 additions & 5 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,12 +855,12 @@ describe('handleChildProcessStdout', () => {
describe('execChildProcess', () => {
beforeEach(() => {
editorConfig.editorPath = '';
editorConfig.cmdArgs = [];
editorConfig.cmdArgs = '';
editorConfig.hasPlaceholder = false;
});
afterEach(() => {
editorConfig.editorPath = '';
editorConfig.cmdArgs = [];
editorConfig.cmdArgs = '';
editorConfig.hasPlaceholder = false;
});

Expand Down Expand Up @@ -967,6 +967,36 @@ describe('execChildProcess', () => {
assert.isObject(res);
});

it('should call function', async () => {
const stubWrite = sinon.stub(process.stderr, 'write').callsFake(buf => buf);
const stubSpawn = sinon.stub(childProcess, 'spawn').returns({
on: a => a,
stderr: {
on: a => a
},
stdout: {
on: a => a
}
});
const filePath = path.resolve('test', 'file', 'test.txt');
const app = IS_WIN ? 'test.cmd' : 'test.sh';
const editorPath = path.resolve('test', 'file', app);
if (!IS_WIN) {
fs.chmodSync(editorPath, PERM_APP);
}
editorConfig.cmdArgs = 'foo bar';
const res = await execChildProcess(filePath, editorPath);
const { called: writeCalled } = stubWrite;
const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn;
stubWrite.restore();
stubSpawn.restore();
assert.isFalse(writeCalled);
assert.isTrue(spawnCalled);
assert.strictEqual(spawnArgs[0][0], editorPath);
assert.deepEqual(spawnArgs[0][1], ['foo', 'bar', filePath]);
assert.isObject(res);
});

it('should call function', async () => {
const stubWrite = sinon.stub(process.stderr, 'write').callsFake(buf => buf);
const stubSpawn = sinon.stub(childProcess, 'spawn').returns({
Expand Down Expand Up @@ -1044,7 +1074,38 @@ describe('execChildProcess', () => {
if (!IS_WIN) {
fs.chmodSync(editorPath, PERM_APP);
}
editorConfig.cmdArgs = ['foo ${file}', 'bar'];
editorConfig.cmdArgs = 'foo ${file} bar';
editorConfig.hasPlaceholder = true;
const res = await execChildProcess(filePath, editorPath);
const { called: writeCalled } = stubWrite;
const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn;
stubWrite.restore();
stubSpawn.restore();
assert.isFalse(writeCalled);
assert.isTrue(spawnCalled);
assert.strictEqual(spawnArgs[0][0], editorPath);
assert.deepEqual(spawnArgs[0][1], ['foo', filePath, 'bar']);
assert.isObject(res);
});

it('should call function', async () => {
const stubWrite = sinon.stub(process.stderr, 'write').callsFake(buf => buf);
const stubSpawn = sinon.stub(childProcess, 'spawn').returns({
on: a => a,
stderr: {
on: a => a
},
stdout: {
on: a => a
}
});
const filePath = path.resolve('test', 'file', 'test.txt');
const app = IS_WIN ? 'test.cmd' : 'test.sh';
const editorPath = path.resolve('test', 'file', app);
if (!IS_WIN) {
fs.chmodSync(editorPath, PERM_APP);
}
editorConfig.cmdArgs = '"foo ${file}" bar';
editorConfig.hasPlaceholder = true;
const res = await execChildProcess(filePath, editorPath);
const { called: writeCalled } = stubWrite;
Expand Down Expand Up @@ -1089,6 +1150,37 @@ describe('execChildProcess', () => {
assert.isObject(res);
});

it('should call function', async () => {
const stubWrite = sinon.stub(process.stderr, 'write').callsFake(buf => buf);
const stubSpawn = sinon.stub(childProcess, 'spawn').returns({
on: a => a,
stderr: {
on: a => a
},
stdout: {
on: a => a
}
});
const filePath = path.resolve('test', 'file', 'sub dir', 'test.txt');
const app = IS_WIN ? 'test.cmd' : 'test.sh';
const editorPath = path.resolve('test', 'file', app);
if (!IS_WIN) {
fs.chmodSync(editorPath, PERM_APP);
}
editorConfig.cmdArgs = '"foo ${file}" bar';
editorConfig.hasPlaceholder = true;
const res = await execChildProcess(filePath, editorPath);
const { called: writeCalled } = stubWrite;
const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn;
stubWrite.restore();
stubSpawn.restore();
assert.isFalse(writeCalled);
assert.isTrue(spawnCalled);
assert.strictEqual(spawnArgs[0][0], editorPath);
assert.deepEqual(spawnArgs[0][1], [`foo ${filePath}`, 'bar']);
assert.isObject(res);
});

it('should call function', async () => {
const stubWrite = sinon.stub(process.stderr, 'write').callsFake(buf => buf);
const stubSpawn = sinon.stub(childProcess, 'spawn').returns({
Expand All @@ -1106,7 +1198,7 @@ describe('execChildProcess', () => {
if (!IS_WIN) {
fs.chmodSync(editorPath, PERM_APP);
}
editorConfig.cmdArgs = ['foo', 'bar=baz ${file}'];
editorConfig.cmdArgs = 'foo bar="baz ${file}"';
editorConfig.hasPlaceholder = true;
const res = await execChildProcess(filePath, editorPath);
const { called: writeCalled } = stubWrite;
Expand Down Expand Up @@ -1137,7 +1229,7 @@ describe('execChildProcess', () => {
if (!IS_WIN) {
fs.chmodSync(editorPath, PERM_APP);
}
editorConfig.cmdArgs = ['foo', 'bar=baz ${file}'];
editorConfig.cmdArgs = 'foo bar="baz ${file}"';
editorConfig.hasPlaceholder = true;
const res = await execChildProcess(filePath, editorPath);
const { called: writeCalled } = stubWrite;
Expand Down

0 comments on commit c8376a6

Please sign in to comment.