|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | + |
| 4 | +import { SourceMapStore, encodeSourceMapSourceUri, OutputChannelLogger } from 'vscode-sourcemap-helper'; |
| 5 | +// import { SourceMapStore } from '../src/SourceMapStore'; |
| 6 | +import { createServer, stopServer } from './server'; |
| 7 | +// import { encodeSourceMapSourceUri } from '../src/UriHelper'; |
| 8 | +import { SourceMapConsumer } from 'source-map'; |
| 9 | +// import { OutputChannelLogger } from '../src/Logger'; |
| 10 | + |
| 11 | +export async function closeFileIfOpen(file: vscode.Uri) : Promise<void> { |
| 12 | + const tabs: vscode.Tab[] = vscode.window.tabGroups.all.map(tg => tg.tabs).flat(); |
| 13 | + const index = tabs.findIndex(tab => tab.input instanceof vscode.TabInputText && tab.input.uri.path === file.path); |
| 14 | + if (index !== -1) { |
| 15 | + await vscode.window.tabGroups.close(tabs[index]); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function getSourceMapConsumerSources(sourceMap: SourceMapConsumer) { |
| 20 | + const sources = []; |
| 21 | + sourceMap.eachMapping(item => { |
| 22 | + if (sources.includes(item.source)) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + sources.push(item.source); |
| 27 | + }); |
| 28 | + |
| 29 | + return sources; |
| 30 | +} |
| 31 | + |
| 32 | +async function testOpenSourceFilesFor(generatedFileWithMap: vscode.Uri) { |
| 33 | + const document = await vscode.workspace.openTextDocument(generatedFileWithMap); |
| 34 | + const outputChannel = vscode.window.createOutputChannel('Source maps'); |
| 35 | + const logger = new OutputChannelLogger(outputChannel); |
| 36 | + const sms = new SourceMapStore(logger); |
| 37 | + const mapping = await sms.getForDocument(document); |
| 38 | + const sources = getSourceMapConsumerSources(mapping.sourceMap); |
| 39 | + |
| 40 | + for (let source of sources) { |
| 41 | + const sourceUri = encodeSourceMapSourceUri(mapping.sourceMapUri, source); |
| 42 | + |
| 43 | + await assert.doesNotReject(async () => { |
| 44 | + const sourceDocument = await vscode.workspace.openTextDocument(sourceUri); |
| 45 | + const sourceEditor = await vscode.window.showTextDocument(sourceDocument); |
| 46 | + |
| 47 | + await closeFileIfOpen(sourceUri); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +suite("SourceMapStore", () => { |
| 54 | + |
| 55 | + suiteSetup(async () => { |
| 56 | + // wait for activation |
| 57 | + |
| 58 | + // perhaps, can call activate from here? if it checks the singleton |
| 59 | + |
| 60 | + // await extensionActivated; |
| 61 | + }); |
| 62 | + |
| 63 | + test("flat: Open source files", async () => { |
| 64 | + const workspaceUri = vscode.workspace.workspaceFolders[0].uri; |
| 65 | + const minJsUri = vscode.Uri.joinPath(workspaceUri, "flat/min.js") |
| 66 | + await testOpenSourceFilesFor(minJsUri); |
| 67 | + // assert.equal(workspaceUri.toString(true) + "/flat", mapping.sourceMapBaseUri.toString(true)); |
| 68 | + }); |
| 69 | + |
| 70 | + test("flat_no_sourceRoot: Open source files", async () => { |
| 71 | + const workspaceUri = vscode.workspace.workspaceFolders[0].uri; |
| 72 | + const minJsUri = vscode.Uri.joinPath(workspaceUri, "flat_no_sourceRoot/min.js") |
| 73 | + await testOpenSourceFilesFor(minJsUri); |
| 74 | + // assert.equal(workspaceUri.toString(true) + "/flat", mapping.sourceMapBaseUri.toString(true)); |
| 75 | + }); |
| 76 | + |
| 77 | + test("with_src_out: Open source files", async () => { |
| 78 | + const workspaceUri = vscode.workspace.workspaceFolders[0].uri; |
| 79 | + const minJsUri = vscode.Uri.joinPath(workspaceUri, "with_src_out/out/min.js") |
| 80 | + await testOpenSourceFilesFor(minJsUri); |
| 81 | + // assert.equal(workspaceUri.toString(true) + "/flat", mapping.sourceMapBaseUri.toString(true)); |
| 82 | + }); |
| 83 | + |
| 84 | + test("with_src_out_no_sourceRoot: Open source files", async () => { |
| 85 | + const workspaceUri = vscode.workspace.workspaceFolders[0].uri; |
| 86 | + const minJsUri = vscode.Uri.joinPath(workspaceUri, "with_src_out_no_sourceRoot/out/min.js") |
| 87 | + await testOpenSourceFilesFor(minJsUri); |
| 88 | + }); |
| 89 | + |
| 90 | + test("inline: Open source files", async () => { |
| 91 | + const workspaceUri = vscode.workspace.workspaceFolders[0].uri; |
| 92 | + const minJsUri = vscode.Uri.joinPath(workspaceUri, "inline/min.js") |
| 93 | + await testOpenSourceFilesFor(minJsUri); |
| 94 | + }); |
| 95 | + |
| 96 | + test("flat via http", async () => { |
| 97 | + const httpRootUri = vscode.workspace.workspaceFolders[0].uri; |
| 98 | + const server = await createServer("localhost", 8809, httpRootUri.fsPath); |
| 99 | + |
| 100 | + const minJsUri = vscode.Uri.parse("sourcemap-http://localhost:8809/flat/min.js"); |
| 101 | + // const minJsUri = encodeSourceMapSourceUri(vscode.Uri.parse("null:null"), vscode.Uri.parse("http://localhost:8809/flat/min.js")); |
| 102 | + await testOpenSourceFilesFor(minJsUri); |
| 103 | + |
| 104 | + await stopServer(server); |
| 105 | + }); |
| 106 | + |
| 107 | + test("flat_no_sourceRoot via http", async () => { |
| 108 | + const httpRootUri = vscode.workspace.workspaceFolders[0].uri; |
| 109 | + const server = await createServer("localhost", 8809, httpRootUri.fsPath); |
| 110 | + |
| 111 | + const minJsUri = vscode.Uri.parse("sourcemap-http://localhost:8809/flat_no_sourceRoot/min.js"); |
| 112 | + // const minJsUri = encodeSourceMapSourceUri(vscode.Uri.parse("null:null"), vscode.Uri.parse("http://localhost:8809/flat_no_sourceRoot/min.js")); |
| 113 | + await testOpenSourceFilesFor(minJsUri); |
| 114 | + |
| 115 | + await stopServer(server); |
| 116 | + }); |
| 117 | + |
| 118 | + test("remote via http", async () => { |
| 119 | + const httpRootUri = vscode.workspace.workspaceFolders[0].uri; |
| 120 | + const server = await createServer("localhost", 8809, httpRootUri.fsPath); |
| 121 | + |
| 122 | + const minJsUri = vscode.Uri.parse("sourcemap-http://localhost:8809/remote/min.js"); |
| 123 | + // const minJsUri = encodeSourceMapSourceUri(vscode.Uri.parse("null:null"), vscode.Uri.parse("http://localhost:8809/remote/min.js")); |
| 124 | + await testOpenSourceFilesFor(minJsUri); |
| 125 | + |
| 126 | + await stopServer(server); |
| 127 | + }); |
| 128 | + |
| 129 | +}); |
0 commit comments