|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | + |
| 4 | +import { getAndUpdateModeHandler } from '../../extension'; |
| 5 | +import * as t from '../testUtils'; |
| 6 | +import { GrepCommand } from '../../src/cmd_line/commands/grep'; |
| 7 | +import { Pattern, SearchDirection } from '../../src/vimscript/pattern'; |
| 8 | +import { Mode } from '../../src/mode/mode'; |
| 9 | + |
| 10 | +// This will go into the exCommandParse test |
| 11 | +// function exParseTest(input: string, parsed: ExCommand) { |
| 12 | +// test(input, () => { |
| 13 | +// const { command } = exCommandParser.tryParse(input); |
| 14 | +// assert.deepStrictEqual(command, parsed); |
| 15 | +// }); |
| 16 | +// } |
| 17 | + |
| 18 | +// suite('grep', () => { |
| 19 | +// const pattern = Pattern.parser({ direction: SearchDirection.Forward, delimiter: '/' }); |
| 20 | +// exParseTest(':vimgrep "t*st" foo.txt', |
| 21 | +// new GrepCommand({ |
| 22 | +// pattern: pattern.tryParse('t*st'), |
| 23 | +// files: ['foo.txt'], |
| 24 | +// }), |
| 25 | +// ); |
| 26 | +// }); |
| 27 | + |
| 28 | +function grep(pattern: Pattern, files: string[]): GrepCommand { |
| 29 | + return new GrepCommand({ pattern, files }); |
| 30 | +} |
| 31 | + |
| 32 | +suite('Basic grep command', () => { |
| 33 | + setup(t.setupWorkspace); |
| 34 | + suiteTeardown(t.cleanUpWorkspace); |
| 35 | + test('GrepCommand parses correctly', async () => { |
| 36 | + await vscode.commands.executeCommand('workbench.action.files.newUntitledFile'); |
| 37 | + const pattern = Pattern.parser({ direction: SearchDirection.Backward, delimiter: '/' }); |
| 38 | + const command = grep(pattern.tryParse('t*st'), ['Untitled-1']); |
| 39 | + assert.deepStrictEqual(command.arguments, { |
| 40 | + pattern: pattern.tryParse('t*st'), |
| 41 | + files: ['Untitled-1'], |
| 42 | + }); |
| 43 | + }); |
| 44 | + // when you search.action.focusNextSearchResult , it will enter the file in visual mode for some reason, we can test whether it is in visual mode or not after running that command |
| 45 | + // that only happens if the search panel is not open already |
| 46 | + // if the search panel is open, it will be in normal mode |
| 47 | + // it will also be in normal mode if you run vimgrep from another file |
| 48 | + test('GrepCommand executes correctly', async () => { |
| 49 | + // Untitled-1 |
| 50 | + await vscode.commands.executeCommand('workbench.action.files.newUntitledFile'); |
| 51 | + const editor = vscode.window.activeTextEditor; |
| 52 | + if (editor) { |
| 53 | + await editor.edit((editBuilder) => { |
| 54 | + editBuilder.insert( |
| 55 | + new vscode.Position(0, 0), |
| 56 | + 'this is a test\nanother t*st line\nno match here\n', |
| 57 | + ); |
| 58 | + }); |
| 59 | + // Because of the save confirmation dialog, it will timeout |
| 60 | + // await editor.document.save() |
| 61 | + } |
| 62 | + // Untitled-2 |
| 63 | + await vscode.commands.executeCommand('workbench.action.files.newUntitledFile'); |
| 64 | + const modeHandler = await getAndUpdateModeHandler(); |
| 65 | + const pattern = Pattern.parser({ direction: SearchDirection.Backward, delimiter: '/' }); |
| 66 | + const command = grep(pattern.tryParse('t*st'), ['Untitled-1']); |
| 67 | + await command.execute(); |
| 68 | + await vscode.commands.executeCommand('search.action.focusNextSearchResult'); |
| 69 | + // Assert that the active editor is Untitled-1 |
| 70 | + const activeEditor = vscode.window.activeTextEditor; |
| 71 | + assert.ok(activeEditor, 'There should be an active editor'); |
| 72 | + assert.strictEqual( |
| 73 | + activeEditor?.document.fileName.endsWith('Untitled-1'), |
| 74 | + true, |
| 75 | + 'Active editor should be Untitled-1 after grep', |
| 76 | + ); |
| 77 | + assert.ok(modeHandler, 'modeHandler should be defined'); |
| 78 | + assert.notStrictEqual( |
| 79 | + modeHandler.vimState, |
| 80 | + Mode.Visual, |
| 81 | + 'Should not be in visual mode after grep', |
| 82 | + ); |
| 83 | + }); |
| 84 | +}); |
0 commit comments