|
| 1 | +import { expect } from 'chai'; |
| 2 | +import path from 'path'; |
| 3 | +import { promises as fs } from 'fs'; |
| 4 | +import { promisify } from 'util'; |
| 5 | +import rimraf from 'rimraf'; |
| 6 | + |
| 7 | +import { eventually } from '../../../testing/eventually'; |
| 8 | +import { useTmpdir, fakeExternalEditor, setTemporaryHomeDirectory } from './repl-helpers'; |
| 9 | +import { TestShell } from './test-shell'; |
| 10 | + |
| 11 | +describe('external editor e2e', () => { |
| 12 | + const tmpdir = useTmpdir(); |
| 13 | + let homedir: string; |
| 14 | + let env: Record<string, string>; |
| 15 | + let shell: TestShell; |
| 16 | + |
| 17 | + beforeEach(async() => { |
| 18 | + const homeInfo = setTemporaryHomeDirectory(); |
| 19 | + |
| 20 | + homedir = homeInfo.homedir; |
| 21 | + env = homeInfo.env; |
| 22 | + |
| 23 | + shell = TestShell.start({ |
| 24 | + args: [ '--nodb' ], |
| 25 | + forceTerminal: true, |
| 26 | + env |
| 27 | + }); |
| 28 | + |
| 29 | + await shell.waitForPrompt(); |
| 30 | + shell.assertNoErrors(); |
| 31 | + |
| 32 | + // make nyc happy when spawning npm below |
| 33 | + await fs.mkdir(path.join(tmpdir.path, '.mongodb', '.nyc_output', 'processinfo'), { recursive: true }); |
| 34 | + await fs.mkdir(path.join(tmpdir.path, 'mongodb', '.nyc_output', 'processinfo'), { recursive: true }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(async() => { |
| 38 | + await TestShell.killall.call(this); |
| 39 | + try { |
| 40 | + await promisify(rimraf)(homedir); |
| 41 | + } catch (err) { |
| 42 | + // On Windows in CI, this can fail with EPERM for some reason. |
| 43 | + // If it does, just log the error instead of failing all tests. |
| 44 | + console.error('Could not remove fake home directory:', err); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns a modified identifier for fn', async() => { |
| 49 | + const shellOriginalInput = "const fn = function () { console.log(111); }; edit('fn')"; |
| 50 | + const editorOutput = `function () { |
| 51 | + console.log(222); |
| 52 | + };`; |
| 53 | + const shellModifiedInput = 'fn = function () { console.log(222); };'; |
| 54 | + const editor = await fakeExternalEditor(editorOutput); |
| 55 | + const result = await shell.executeLine(`config.set("editor", ${JSON.stringify(editor)});`); |
| 56 | + |
| 57 | + expect(result).to.include('"editor" has been changed'); |
| 58 | + shell.writeInputLine(shellOriginalInput); |
| 59 | + await eventually(() => { |
| 60 | + shell.assertContainsOutput(shellModifiedInput); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns a modified identifier for var', async() => { |
| 65 | + const shellOriginalInput = "const myVar = '111'; edit('myVar')"; |
| 66 | + const editorOutput = "const myVar = '222';"; |
| 67 | + const shellModifiedInput = "myVar = '222';"; |
| 68 | + const editor = await fakeExternalEditor(editorOutput); |
| 69 | + const result = await shell.executeLine(`config.set("editor", ${JSON.stringify(editor)});`); |
| 70 | + |
| 71 | + expect(result).to.include('"editor" has been changed'); |
| 72 | + shell.writeInputLine(shellOriginalInput); |
| 73 | + await eventually(() => { |
| 74 | + shell.assertContainsOutput(shellModifiedInput); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns a modified identifier for a.b.c', async() => { |
| 79 | + const shellOriginalInput = "const myObj = { field: { child: 'string value' } }; edit('myObj')"; |
| 80 | + const editorOutput = `const myObj = { |
| 81 | + field: { |
| 82 | + child: 'new value' |
| 83 | + } |
| 84 | + };`; |
| 85 | + const shellModifiedInput = "myObj = { field: { child: 'new value' } };"; |
| 86 | + const editor = await fakeExternalEditor(editorOutput); |
| 87 | + const result = await shell.executeLine(`config.set("editor", ${JSON.stringify(editor)});`); |
| 88 | + |
| 89 | + expect(result).to.include('"editor" has been changed'); |
| 90 | + shell.writeInputLine(shellOriginalInput); |
| 91 | + await eventually(() => { |
| 92 | + shell.assertContainsOutput(shellModifiedInput); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('returns an error when editor exits with exitCode 1', async() => { |
| 97 | + const shellOriginalInput = 'edit function() {}'; |
| 98 | + const editor = await fakeExternalEditor(); |
| 99 | + const result = await shell.executeLine(`config.set("editor", ${JSON.stringify(editor)});`); |
| 100 | + await shell.executeLine('config.set("showStackTraces", true); print(process.env.PATH);'); |
| 101 | + |
| 102 | + expect(result).to.include('"editor" has been changed'); |
| 103 | + shell.writeInputLine(shellOriginalInput); |
| 104 | + await eventually(() => { |
| 105 | + shell.assertContainsError('failed with an exit code 1'); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('opens an empty editor', async() => { |
| 110 | + const output = ''; |
| 111 | + const editor = await fakeExternalEditor(output); |
| 112 | + const result = await shell.executeLine(`config.set("editor", ${JSON.stringify(editor)});`); |
| 113 | + |
| 114 | + expect(result).to.include('"editor" has been changed'); |
| 115 | + shell.writeInputLine('edit'); |
| 116 | + await eventually(() => { |
| 117 | + shell.assertContainsOutput(output); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments