From 1d853380e7163945918c9999c54a55a611db1477 Mon Sep 17 00:00:00 2001 From: "asamuzaK (Kazz)" Date: Sat, 26 Oct 2024 10:35:17 +0900 Subject: [PATCH] Use Map() --- modules/constant.js | 3 + modules/main.js | 100 ++++++++++++---------- test/main.test.js | 200 ++++++++++++++++++++++++++++---------------- 3 files changed, 188 insertions(+), 115 deletions(-) diff --git a/modules/constant.js b/modules/constant.js index 7113392..bd6e982 100644 --- a/modules/constant.js +++ b/modules/constant.js @@ -3,6 +3,7 @@ */ /* shared constants */ +export const CMD_ARGS = 'cmdArgs'; export const CMD_BROWSER = '-b, --browser '; export const CMD_BROWSER_DESC = 'specify the browser'; export const CMD_CONFIG_PATH = '-c, --config-path '; @@ -24,6 +25,7 @@ export const EDITOR_CONFIG_FILE = 'editorconfig.json'; export const EDITOR_CONFIG_GET = 'getEditorConfig'; export const EDITOR_CONFIG_RES = 'resEditorConfig'; export const EDITOR_CONFIG_TS = 'editorConfigTimestamp'; +export const EDITOR_NAME = 'editorName'; export const EDITOR_PATH = 'editorPath'; export const EXT_CHROME_ID = 'chrome-extension://koghhpkkcndhhclklnnnhcpkkplfkgoi/'; @@ -36,6 +38,7 @@ export const HOST_VERSION_CHECK = 'checkHostVersion'; export const LABEL = 'withExEditor'; export const LOCAL_FILE_VIEW = 'viewLocalFile'; export const MODE_EDIT = 'modeEditText'; +export const PLACEHOLDER = 'placeholder'; export const PROCESS_CHILD = 'childProcess'; export const TMP_FILES = 'tmpFiles'; export const TMP_FILES_PB = 'tmpFilesPb'; diff --git a/modules/main.js b/modules/main.js index adfbfe9..e716011 100644 --- a/modules/main.js +++ b/modules/main.js @@ -20,10 +20,10 @@ import { version as hostVersion } from './version.js'; /* constants */ import { - EDITOR_CONFIG_FILE, EDITOR_CONFIG_GET, EDITOR_CONFIG_RES, EDITOR_CONFIG_TS, - FILE_WATCH, HOST, HOST_VERSION, HOST_VERSION_CHECK, LABEL, - LOCAL_FILE_VIEW, MODE_EDIT, PROCESS_CHILD, - TMP_FILES, TMP_FILES_PB, TMP_FILES_PB_REMOVE, TMP_FILE_CREATE, + CMD_ARGS, EDITOR_CONFIG_FILE, EDITOR_CONFIG_GET, EDITOR_CONFIG_RES, + EDITOR_CONFIG_TS, EDITOR_NAME, EDITOR_PATH, FILE_WATCH, HOST, HOST_VERSION, + HOST_VERSION_CHECK, LABEL, LOCAL_FILE_VIEW, MODE_EDIT, PLACEHOLDER, + PROCESS_CHILD, TMP_FILES, TMP_FILES_PB, TMP_FILES_PB_REMOVE, TMP_FILE_CREATE, TMP_FILE_DATA_PORT, TMP_FILE_DATA_REMOVE, TMP_FILE_GET, TMP_FILE_PLACEHOLDER, TMP_FILE_RES } from './constant.js'; @@ -39,12 +39,7 @@ const TMPDIR_FILES = path.join(TMPDIR_APP, TMP_FILES); const TMPDIR_FILES_PB = path.join(TMPDIR_APP, TMP_FILES_PB); /* editor config */ -export const editorConfig = { - editorName: '', - editorPath: '', - cmdArgs: '', - hasPlaceholder: false -}; +export const editorConfig = new Map(); /* output */ /** @@ -103,13 +98,16 @@ export const exportAppStatus = async () => /** * export editor config * @param {string} data - editor config - * @param {string} editorConfigPath - editor config file path + * @param {string} configFile - editor config file path * @returns {Promise.} - writeStdout() */ -export const exportEditorConfig = async (data, editorConfigPath) => { +export const exportEditorConfig = async (data, configFile) => { if (!isString(data)) { throw new TypeError(`Expected String but got ${getType(data)}.`); } + if (!isString(configFile)) { + throw new TypeError(`Expected String but got ${getType(configFile)}.`); + } let func; data = data && JSON.parse(data); if (isObjectNotEmpty(data)) { @@ -124,32 +122,42 @@ export const exportEditorConfig = async (data, editorConfigPath) => { } } } - const editorName = getFileNameFromFilePath(parsedPath); - const executable = isExecutable(parsedPath); - const timestamp = await getFileTimestamp(editorConfigPath); - const reg = - new RegExp(`\\$(?:${TMP_FILE_PLACEHOLDER}|{${TMP_FILE_PLACEHOLDER}})`); - const keys = Object.keys(editorConfig); - for (const key of keys) { - const value = data[key]; - if (key === 'editorPath') { - editorConfig[key] = value; - } else if (key === 'cmdArgs') { - editorConfig[key] = value; - editorConfig.hasPlaceholder = reg.test(value); + if (isFile(parsedPath)) { + const editorName = getFileNameFromFilePath(parsedPath); + const executable = isExecutable(parsedPath); + const timestamp = await getFileTimestamp(configFile); + const reg = + new RegExp(`\\$(?:${TMP_FILE_PLACEHOLDER}|{${TMP_FILE_PLACEHOLDER}})`); + const keys = [EDITOR_PATH, CMD_ARGS]; + for (const key of keys) { + const value = data[key]; + editorConfig.set(key, value); + if (key === CMD_ARGS) { + editorConfig.set(PLACEHOLDER, reg.test(value)); + } } + editorConfig.set(EDITOR_NAME, editorName); + const msg = { + [EDITOR_CONFIG_RES]: { + editorName, + executable, + [EDITOR_CONFIG_TS]: timestamp + } + }; + func = writeStdout(msg); + } else { + const msg = { + [EDITOR_CONFIG_RES]: null + }; + func = writeStdout(msg); } - editorConfig.editorName = editorName; + } else { const msg = { - [EDITOR_CONFIG_RES]: { - editorName, - executable, - [EDITOR_CONFIG_TS]: timestamp - } + [EDITOR_CONFIG_RES]: null }; func = writeStdout(msg); } - return func || null; + return func; }; /** @@ -245,7 +253,7 @@ export const handleChildProcessErr = e => { */ export const handleChildProcessClose = code => { if (Number.isInteger(code)) { - const { editorName } = editorConfig; + const editorName = editorConfig.get(EDITOR_NAME); const msg = new Output().encode( hostMsg(`${editorName} close all stdio with code ${code}`, 'close') ); @@ -260,7 +268,7 @@ export const handleChildProcessClose = code => { */ export const handleChildProcessExit = code => { if (Number.isInteger(code)) { - const { editorName } = editorConfig; + const editorName = editorConfig.get(EDITOR_NAME); const msg = new Output().encode( hostMsg(`${editorName} exited with code ${code}`, 'exit') ); @@ -302,10 +310,13 @@ export const handleChildProcessStdout = data => { * @param {string} app - app path * @returns {Promise.} - child process */ -export const execChildProcess = async (file, app = editorConfig.editorPath) => { +export const execChildProcess = async (file, app) => { if (!isFile(file)) { throw new Error(`No such file: ${file}`); } + if (!app) { + app = editorConfig.get(EDITOR_PATH); + } if (/\$\{\w+\}|\$\w+/.test(app)) { const envVars = app.match(/\$\{\w+\}|\$\w+/g); for (const envVar of envVars) { @@ -318,7 +329,8 @@ export const execChildProcess = async (file, app = editorConfig.editorPath) => { if (!isExecutable(app)) { throw new Error('Application is not executable.'); } - const { cmdArgs, hasPlaceholder } = editorConfig; + const cmdArgs = editorConfig.get(CMD_ARGS); + const hasPlaceholder = editorConfig.get(PLACEHOLDER); const opt = { cwd: null, encoding: CHAR, @@ -609,19 +621,19 @@ export const removeTmpFileData = async (data = {}) => { /* local files */ /** * get editor config - * @param {string} editorConfigPath - editor config file path + * @param {string} configFile - editor config file path * @returns {Promise.} - results of each handler */ -export const getEditorConfig = async editorConfigPath => { +export const getEditorConfig = async configFile => { const func = []; - if (isFile(editorConfigPath)) { - const data = await readFile(editorConfigPath, { + if (isFile(configFile)) { + const data = await readFile(configFile, { encoding: CHAR, flag: 'r' }); - func.push(exportEditorConfig(data, editorConfigPath)); + func.push(exportEditorConfig(data, configFile)); } else { func.push( - writeStdout(hostMsg(`No such file: ${editorConfigPath}`, 'warn')), + writeStdout(hostMsg(`No such file: ${configFile}`, 'warn')), writeStdout({ [EDITOR_CONFIG_RES]: null }) ); } @@ -675,8 +687,8 @@ export const handleMsg = async msg => { for (const [key, value] of items) { switch (key) { case EDITOR_CONFIG_GET: { - const editorConfigPath = path.resolve('.', EDITOR_CONFIG_FILE); - func.push(getEditorConfig(editorConfigPath)); + const configFile = path.resolve('.', EDITOR_CONFIG_FILE); + func.push(getEditorConfig(configFile)); break; } case HOST_VERSION_CHECK: diff --git a/test/main.test.js b/test/main.test.js index 218b075..13292ef 100644 --- a/test/main.test.js +++ b/test/main.test.js @@ -32,9 +32,10 @@ import { /* constants */ import { - EDITOR_CONFIG_FILE, EDITOR_CONFIG_GET, EDITOR_CONFIG_RES, EDITOR_CONFIG_TS, - FILE_WATCH, HOST_VERSION, HOST_VERSION_CHECK, LABEL, LOCAL_FILE_VIEW, - MODE_EDIT, TMP_FILES, TMP_FILES_PB, TMP_FILES_PB_REMOVE, TMP_FILE_CREATE, + CMD_ARGS, EDITOR_CONFIG_FILE, EDITOR_CONFIG_GET, EDITOR_CONFIG_RES, + EDITOR_CONFIG_TS, EDITOR_NAME, EDITOR_PATH, FILE_WATCH, HOST_VERSION, + HOST_VERSION_CHECK, LABEL, LOCAL_FILE_VIEW, MODE_EDIT, PLACEHOLDER, + TMP_FILES, TMP_FILES_PB, TMP_FILES_PB_REMOVE, TMP_FILE_CREATE, TMP_FILE_DATA_PORT, TMP_FILE_DATA_REMOVE, TMP_FILE_GET, TMP_FILE_RES } from '../modules/constant.js'; const APP = `${process.pid}`; @@ -159,14 +160,10 @@ describe('exportAppStatus', () => { describe('exportEditorConfig', () => { beforeEach(() => { - editorConfig.editorPath = ''; - editorConfig.cmdArgs = ''; - editorConfig.hasPlaceholder = false; + editorConfig.clear(); }); afterEach(() => { - editorConfig.editorPath = ''; - editorConfig.cmdArgs = ''; - editorConfig.hasPlaceholder = false; + editorConfig.clear(); }); it('should throw', async () => { @@ -178,10 +175,30 @@ describe('exportEditorConfig', () => { it('should throw', async () => { await exportEditorConfig('{foo:bar}').catch(e => { - assert.instanceOf(e, Error); + assert.instanceOf(e, TypeError); + assert.strictEqual(e.message, 'Expected String but got Undefined.'); }); }); + it('should call function', async () => { + const stubWrite = sinon.stub(process.stdout, 'write').callsFake(buf => buf); + const stubErrWrite = + sinon.stub(process.stderr, 'write').callsFake(buf => buf); + const editorConfigPath = path.resolve('test', 'file', 'editorconfig.json'); + const msg = new Output().encode({ + [EDITOR_CONFIG_RES]: null + }); + const res = await exportEditorConfig('{}', editorConfigPath); + const { calledOnce: writeCalled } = stubWrite; + const { called: errWriteCalled } = stubErrWrite; + stubWrite.restore(); + stubErrWrite.restore(); + assert.isTrue(writeCalled); + assert.isFalse(errWriteCalled); + assert.strictEqual(editorConfig.size, 0); + assert.deepEqual(res, msg); + }); + it('should call function', async () => { const stubWrite = sinon.stub(process.stdout, 'write').callsFake(buf => buf); const stubErrWrite = @@ -212,14 +229,16 @@ describe('exportEditorConfig', () => { stubErrWrite.restore(); assert.isTrue(writeCalled); assert.isFalse(errWriteCalled); - assert.strictEqual(editorConfig.editorName, 'test'); - assert.strictEqual(editorConfig.editorPath, editorPath); - assert.deepEqual(editorConfig.cmdArgs, ['--foo', '--bar']); - assert.isFalse(editorConfig.hasPlaceholder); + assert.strictEqual(editorConfig.size, 4); + assert.strictEqual(editorConfig.get(EDITOR_NAME), 'test'); + assert.strictEqual(editorConfig.get(EDITOR_PATH), editorPath); + assert.deepEqual(editorConfig.get(CMD_ARGS), ['--foo', '--bar']); + assert.isFalse(editorConfig.get(PLACEHOLDER)); assert.deepEqual(res, msg); }); it('should call function', async () => { + console.log(editorConfig); const stubWrite = sinon.stub(process.stdout, 'write').callsFake(buf => buf); const stubErrWrite = sinon.stub(process.stderr, 'write').callsFake(buf => buf); @@ -252,10 +271,43 @@ describe('exportEditorConfig', () => { stubErrWrite.restore(); assert.isTrue(writeCalled); assert.isFalse(errWriteCalled); - assert.strictEqual(editorConfig.editorName, 'test'); - assert.strictEqual(editorConfig.editorPath, envVarPath); - assert.deepEqual(editorConfig.cmdArgs, ['--foo', '--bar']); - assert.isFalse(editorConfig.hasPlaceholder); + assert.strictEqual(editorConfig.size, 4); + assert.strictEqual(editorConfig.get(EDITOR_NAME), 'test'); + assert.strictEqual(editorConfig.get(EDITOR_PATH), envVarPath); + assert.deepEqual(editorConfig.get(CMD_ARGS), ['--foo', '--bar']); + assert.isFalse(editorConfig.get(PLACEHOLDER)); + assert.deepEqual(res, msg); + }); + + it('should call function', async () => { + const stubWrite = sinon.stub(process.stdout, 'write').callsFake(buf => buf); + const stubErrWrite = + sinon.stub(process.stderr, 'write').callsFake(buf => buf); + const editorConfigPath = path.resolve('test', 'file', 'editorconfig.json'); + const app = IS_WIN ? 'test.cmd' : 'test.sh'; + const editorPath = path.resolve('test', 'file', app); + if (!IS_WIN) { + fs.chmodSync(editorPath, PERM_APP); + } + const envVarPath = path.resolve('${TEST}', 'file', app); + process.env.TEST = 'foo'; + const editorConfigData = { + editorPath: envVarPath, + cmdArgs: ['--foo', '--bar'] + }; + const value = `${JSON.stringify(editorConfigData)}\n`; + const msg = new Output().encode({ + [EDITOR_CONFIG_RES]: null + }); + const res = await exportEditorConfig(value, editorConfigPath); + const { calledOnce: writeCalled } = stubWrite; + const { called: errWriteCalled } = stubErrWrite; + delete process.env.TEST; + stubWrite.restore(); + stubErrWrite.restore(); + assert.isTrue(writeCalled); + assert.isFalse(errWriteCalled); + assert.strictEqual(editorConfig.size, 0); assert.deepEqual(res, msg); }); @@ -289,10 +341,11 @@ describe('exportEditorConfig', () => { stubErrWrite.restore(); assert.isTrue(writeCalled); assert.isFalse(errWriteCalled); - assert.strictEqual(editorConfig.editorName, 'test'); - assert.strictEqual(editorConfig.editorPath, editorPath); - assert.deepEqual(editorConfig.cmdArgs, ['--foo', '$file']); - assert.isTrue(editorConfig.hasPlaceholder); + assert.strictEqual(editorConfig.size, 4); + assert.strictEqual(editorConfig.get(EDITOR_NAME), 'test'); + assert.strictEqual(editorConfig.get(EDITOR_PATH), editorPath); + assert.deepEqual(editorConfig.get(CMD_ARGS), ['--foo', '$file']); + assert.isTrue(editorConfig.get(PLACEHOLDER)); assert.deepEqual(res, msg); }); @@ -326,10 +379,11 @@ describe('exportEditorConfig', () => { stubErrWrite.restore(); assert.isTrue(writeCalled); assert.isFalse(errWriteCalled); - assert.strictEqual(editorConfig.editorName, 'test'); - assert.strictEqual(editorConfig.editorPath, editorPath); - assert.deepEqual(editorConfig.cmdArgs, ['--foo="bar baz"']); - assert.isFalse(editorConfig.hasPlaceholder); + assert.strictEqual(editorConfig.size, 4); + assert.strictEqual(editorConfig.get(EDITOR_NAME), 'test'); + assert.strictEqual(editorConfig.get(EDITOR_PATH), editorPath); + assert.deepEqual(editorConfig.get(CMD_ARGS), ['--foo="bar baz"']); + assert.isFalse(editorConfig.get(PLACEHOLDER)); assert.deepEqual(res, msg); }); @@ -363,10 +417,11 @@ describe('exportEditorConfig', () => { stubErrWrite.restore(); assert.isTrue(writeCalled); assert.isFalse(errWriteCalled); - assert.strictEqual(editorConfig.editorName, 'test'); - assert.strictEqual(editorConfig.editorPath, editorPath); - assert.deepEqual(editorConfig.cmdArgs, ['--foo', '${file}']); - assert.isTrue(editorConfig.hasPlaceholder); + assert.strictEqual(editorConfig.size, 4); + assert.strictEqual(editorConfig.get(EDITOR_NAME), 'test'); + assert.strictEqual(editorConfig.get(EDITOR_PATH), editorPath); + assert.deepEqual(editorConfig.get(CMD_ARGS), ['--foo', '${file}']); + assert.isTrue(editorConfig.get(PLACEHOLDER)); assert.deepEqual(res, msg); }); }); @@ -712,10 +767,10 @@ describe('handleChildProcessErr', () => { describe('handleChildProcessClose', () => { beforeEach(() => { - editorConfig.editorPath = ''; + editorConfig.clear(); }); afterEach(() => { - editorConfig.editorPath = ''; + editorConfig.clear(); }); it('should not call function', async () => { @@ -731,7 +786,7 @@ describe('handleChildProcessClose', () => { const stubWrite = sinon.stub(process.stdout, 'write').callsFake(buf => { info = buf; }); - editorConfig.editorName = 'foo'; + editorConfig.set(EDITOR_NAME, 'foo'); const msg = new Output().encode({ withexeditorhost: { message: 'foo close all stdio with code 0', @@ -750,7 +805,7 @@ describe('handleChildProcessClose', () => { const stubWrite = sinon.stub(process.stdout, 'write').callsFake(buf => { info = buf; }); - editorConfig.editorName = 'foo'; + editorConfig.set(EDITOR_NAME, 'foo'); const msg = new Output().encode({ withexeditorhost: { message: 'foo close all stdio with code 1', @@ -767,10 +822,10 @@ describe('handleChildProcessClose', () => { describe('handleChildProcessExit', () => { beforeEach(() => { - editorConfig.editorName = ''; + editorConfig.clear(); }); afterEach(() => { - editorConfig.editorName = ''; + editorConfig.clear(); }); it('should not call function', async () => { @@ -786,7 +841,7 @@ describe('handleChildProcessExit', () => { const stubWrite = sinon.stub(process.stdout, 'write').callsFake(buf => { info = buf; }); - editorConfig.editorName = 'foo'; + editorConfig.set(EDITOR_NAME, 'foo'); const msg = new Output().encode({ withexeditorhost: { message: 'foo exited with code 0', @@ -805,7 +860,7 @@ describe('handleChildProcessExit', () => { const stubWrite = sinon.stub(process.stdout, 'write').callsFake(buf => { info = buf; }); - editorConfig.editorName = 'foo'; + editorConfig.set(EDITOR_NAME, 'foo'); const msg = new Output().encode({ withexeditorhost: { message: 'foo exited with code 1', @@ -878,14 +933,10 @@ describe('handleChildProcessStdout', () => { describe('execChildProcess', () => { beforeEach(() => { - editorConfig.editorPath = ''; - editorConfig.cmdArgs = ''; - editorConfig.hasPlaceholder = false; + editorConfig.clear(); }); afterEach(() => { - editorConfig.editorPath = ''; - editorConfig.cmdArgs = ''; - editorConfig.hasPlaceholder = false; + editorConfig.clear(); }); it('should throw', async () => { @@ -1010,7 +1061,7 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = ''; + editorConfig.set(CMD_ARGS, ''); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -1040,7 +1091,7 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = 'foo bar'; + editorConfig.set(CMD_ARGS, 'foo bar'); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -1070,7 +1121,7 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = ['foo', 'bar']; + editorConfig.set(CMD_ARGS, ['foo', 'bar']); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -1100,7 +1151,7 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = 'foo bar'; + editorConfig.set(CMD_ARGS, 'foo bar'); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -1130,8 +1181,8 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = 'foo ${file} bar'; - editorConfig.hasPlaceholder = true; + editorConfig.set(CMD_ARGS, 'foo ${file} bar'); + editorConfig.set(PLACEHOLDER, true); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -1161,8 +1212,8 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = '"foo ${file}" bar'; - editorConfig.hasPlaceholder = true; + editorConfig.set(CMD_ARGS, '"foo ${file}" bar'); + editorConfig.set(PLACEHOLDER, true); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -1192,8 +1243,8 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = ['foo ${file}', 'bar']; - editorConfig.hasPlaceholder = true; + editorConfig.set(CMD_ARGS, ['foo ${file}', 'bar']); + editorConfig.set(PLACEHOLDER, true); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -1223,8 +1274,8 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = '"foo ${file}" bar'; - editorConfig.hasPlaceholder = true; + editorConfig.set(CMD_ARGS, '"foo ${file}" bar'); + editorConfig.set(PLACEHOLDER, true); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -1254,8 +1305,8 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = 'foo bar="baz ${file}"'; - editorConfig.hasPlaceholder = true; + editorConfig.set(CMD_ARGS, 'foo bar="baz ${file}"'); + editorConfig.set(PLACEHOLDER, true); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -1285,8 +1336,8 @@ describe('execChildProcess', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.cmdArgs = 'foo bar="baz ${file}"'; - editorConfig.hasPlaceholder = true; + editorConfig.set(CMD_ARGS, 'foo bar="baz ${file}"'); + editorConfig.set(PLACEHOLDER, true); const res = await execChildProcess(filePath, editorPath); const { called: writeCalled } = stubWrite; const { args: spawnArgs, calledOnce: spawnCalled } = stubSpawn; @@ -2246,18 +2297,25 @@ describe('getEditorConfig', () => { }); it('should call function', async () => { + const stubWrite = sinon.stub(process.stdout, 'write').callsFake(buf => buf); const editorConfigPath = path.resolve('test', 'file', 'editorconfig.json'); + const msg = new Output().encode({ + [EDITOR_CONFIG_RES]: null + }); const res = await getEditorConfig(editorConfigPath); - assert.deepEqual(res, [null]); + const { called: writeCalled } = stubWrite; + stubWrite.restore(); + assert.isTrue(writeCalled); + assert.deepEqual(res, [msg]); }); }); describe('viewLocalFile', () => { beforeEach(() => { - editorConfig.editorPath = ''; + editorConfig.clear(); }); afterEach(() => { - editorConfig.editorPath = ''; + editorConfig.clear(); }); it('should throw', async () => { @@ -2303,7 +2361,7 @@ describe('viewLocalFile', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.editorPath = editorPath; + editorConfig.set(EDITOR_PATH, editorPath); const res = await viewLocalFile(fileUrl); const { called: writeCalled } = stubWrite; const { calledOnce: spawnCalled } = stubSpawn; @@ -2333,7 +2391,7 @@ describe('viewLocalFile', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.editorPath = editorPath; + editorConfig.set(EDITOR_PATH, editorPath); const res = await viewLocalFile(fileUrl); const { called: writeCalled } = stubWrite; const { calledOnce: spawnCalled } = stubSpawn; @@ -2347,10 +2405,10 @@ describe('viewLocalFile', () => { describe('handleCreatedTmpFile', () => { beforeEach(() => { - editorConfig.editorPath = ''; + editorConfig.clear(); }); afterEach(() => { - editorConfig.editorPath = ''; + editorConfig.clear(); }); it('should get empty array', async () => { @@ -2393,7 +2451,7 @@ describe('handleCreatedTmpFile', () => { data } }); - editorConfig.editorPath = editorPath; + editorConfig.set(EDITOR_PATH, editorPath); const res = await handleCreatedTmpFile(obj); const { calledOnce: writeCalled } = stubWrite; const { calledOnce: spawnCalled } = stubSpawn; @@ -2411,12 +2469,12 @@ describe('handleMsg', () => { const globalDispatcher = getGlobalDispatcher(); const mockAgent = new MockAgent(); beforeEach(() => { - editorConfig.editorPath = ''; + editorConfig.clear(); setGlobalDispatcher(mockAgent); mockAgent.disableNetConnect(); }); afterEach(() => { - editorConfig.editorPath = ''; + editorConfig.clear(); mockAgent.enableNetConnect(); setGlobalDispatcher(globalDispatcher); }); @@ -2552,7 +2610,7 @@ describe('handleMsg', () => { if (!IS_WIN) { fs.chmodSync(editorPath, PERM_APP); } - editorConfig.editorPath = editorPath; + editorConfig.set(EDITOR_PATH, editorPath); const [res] = await handleMsg({ [LOCAL_FILE_VIEW]: fileUrl });