|
| 1 | +import { fail } from 'assert'; |
| 2 | +import { commands, Selection, window, workspace } from 'vscode'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as assert from 'assert'; |
| 5 | +import { expect } from 'chai'; |
| 6 | +import { tmpDir } from '../../helpers'; |
| 7 | +import * as fs from 'fs-extra'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Integration tests for queries |
| 11 | + */ |
| 12 | +describe('SourceMap', function() { |
| 13 | + this.timeout(20000); |
| 14 | + |
| 15 | + it('should jump to QL code', async () => { |
| 16 | + try { |
| 17 | + const root = workspace.workspaceFolders![0].uri.fsPath; |
| 18 | + const srcFiles = { |
| 19 | + summary: path.join(root, 'log-summary', 'evaluator-log.summary'), |
| 20 | + summaryMap: path.join(root, 'log-summary', 'evaluator-log.summary.map') |
| 21 | + }; |
| 22 | + // We need to modify the source map so that its paths point to the actual location of the |
| 23 | + // workspace root on this machine. We'll copy the summary and its source map to a temp |
| 24 | + // directory, modify the source map their, and open that summary. |
| 25 | + const tempFiles = await copyFilesToTempDirectory(srcFiles); |
| 26 | + |
| 27 | + // The checked-in sourcemap has placeholders of the form `${root}`, which we need to replace |
| 28 | + // with the actual root directory. |
| 29 | + const mapText = await fs.readFile(tempFiles.summaryMap, 'utf-8'); |
| 30 | + // Always use forward slashes, since they work everywhere. |
| 31 | + const slashRoot = root.replaceAll('\\', '/'); |
| 32 | + const newMapText = mapText.replaceAll('${root}', slashRoot); |
| 33 | + await fs.writeFile(tempFiles.summaryMap, newMapText); |
| 34 | + |
| 35 | + const summaryDocument = await workspace.openTextDocument(tempFiles.summary); |
| 36 | + assert(summaryDocument.languageId === 'ql-summary'); |
| 37 | + const summaryEditor = await window.showTextDocument(summaryDocument); |
| 38 | + summaryEditor.selection = new Selection(356, 10, 356, 10); |
| 39 | + await commands.executeCommand('codeQL.gotoQL'); |
| 40 | + |
| 41 | + const newEditor = window.activeTextEditor; |
| 42 | + expect(newEditor).to.be.not.undefined; |
| 43 | + const newDocument = newEditor!.document; |
| 44 | + expect(path.basename(newDocument.fileName)).to.equal('Namespace.qll'); |
| 45 | + const newSelection = newEditor!.selection; |
| 46 | + expect(newSelection.start.line).to.equal(60); |
| 47 | + expect(newSelection.start.character).to.equal(2); |
| 48 | + } catch (e) { |
| 49 | + console.error('Test Failed'); |
| 50 | + fail(e as Error); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + async function copyFilesToTempDirectory<T extends Record<string, string>>(files: T): Promise<T> { |
| 55 | + const tempDir = path.join(tmpDir.name, 'log-summary'); |
| 56 | + await fs.ensureDir(tempDir); |
| 57 | + const result: Record<string, string> = {}; |
| 58 | + for (const key in files) { |
| 59 | + const srcPath = files[key]; |
| 60 | + const destPath = path.join(tempDir, path.basename(srcPath)); |
| 61 | + await fs.copy(srcPath, destPath); |
| 62 | + result[key] = destPath; |
| 63 | + } |
| 64 | + |
| 65 | + return result as T; |
| 66 | + } |
| 67 | +}); |
0 commit comments