Path: blob/main/extensions/markdown-language-features/src/test/documentLink.test.ts
3292 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as assert from 'assert';6import 'mocha';7import * as vscode from 'vscode';8import { joinLines } from './util';910const testFileA = workspaceFile('a.md');1112const debug = false;1314function debugLog(...args: any[]) {15if (debug) {16console.log(...args);17}18}1920function workspaceFile(...segments: string[]) {21return vscode.Uri.joinPath(vscode.workspace.workspaceFolders![0].uri, ...segments);22}2324async function getLinksForFile(file: vscode.Uri): Promise<vscode.DocumentLink[]> {25debugLog('getting links', file.toString(), Date.now());26const r = (await vscode.commands.executeCommand<vscode.DocumentLink[]>('vscode.executeLinkProvider', file, /*linkResolveCount*/ 100))!;27debugLog('got links', file.toString(), Date.now());28return r;29}3031(vscode.env.uiKind === vscode.UIKind.Web ? suite.skip : suite)('Markdown Document links', () => {3233setup(async () => {34// the tests make the assumption that link providers are already registered35await vscode.extensions.getExtension('vscode.markdown-language-features')!.activate();36});3738teardown(async () => {39await vscode.commands.executeCommand('workbench.action.closeAllEditors');40});4142test('Should navigate to markdown file', async () => {43await withFileContents(testFileA, '[b](b.md)');4445const [link] = await getLinksForFile(testFileA);46await executeLink(link);4748assertActiveDocumentUri(workspaceFile('b.md'));49});5051test('Should navigate to markdown file with leading ./', async () => {52await withFileContents(testFileA, '[b](./b.md)');5354const [link] = await getLinksForFile(testFileA);55await executeLink(link);5657assertActiveDocumentUri(workspaceFile('b.md'));58});5960test('Should navigate to markdown file with leading /', async () => {61await withFileContents(testFileA, '[b](./b.md)');6263const [link] = await getLinksForFile(testFileA);64await executeLink(link);6566assertActiveDocumentUri(workspaceFile('b.md'));67});6869test('Should navigate to markdown file without file extension', async () => {70await withFileContents(testFileA, '[b](b)');7172const [link] = await getLinksForFile(testFileA);73await executeLink(link);7475assertActiveDocumentUri(workspaceFile('b.md'));76});7778test('Should navigate to markdown file in directory', async () => {79await withFileContents(testFileA, '[b](sub/c)');8081const [link] = await getLinksForFile(testFileA);82await executeLink(link);8384assertActiveDocumentUri(workspaceFile('sub', 'c.md'));85});8687test('Should navigate to fragment by title in file', async () => {88await withFileContents(testFileA, '[b](sub/c#second)');8990const [link] = await getLinksForFile(testFileA);91await executeLink(link);9293assertActiveDocumentUri(workspaceFile('sub', 'c.md'));94assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 1);95});9697test('Should navigate to fragment by line', async () => {98await withFileContents(testFileA, '[b](sub/c#L2)');99100const [link] = await getLinksForFile(testFileA);101await executeLink(link);102103assertActiveDocumentUri(workspaceFile('sub', 'c.md'));104assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 1);105});106107test('Should navigate to line number within non-md file', async () => {108await withFileContents(testFileA, '[b](sub/foo.txt#L3)');109110const [link] = await getLinksForFile(testFileA);111await executeLink(link);112113assertActiveDocumentUri(workspaceFile('sub', 'foo.txt'));114assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 2);115});116117test('Should navigate to fragment within current file', async () => {118await withFileContents(testFileA, joinLines(119'[](a#header)',120'[](#header)',121'# Header'));122123const links = await getLinksForFile(testFileA);124{125await executeLink(links[0]);126assertActiveDocumentUri(workspaceFile('a.md'));127assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 2);128}129{130await executeLink(links[1]);131assertActiveDocumentUri(workspaceFile('a.md'));132assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 2);133}134});135136test.skip('Should navigate to fragment within current untitled file', async () => { // TODO: skip for now for ls migration137const testFile = workspaceFile('x.md').with({ scheme: 'untitled' });138await withFileContents(testFile, joinLines(139'[](#second)',140'# Second'));141142const [link] = await getLinksForFile(testFile);143await executeLink(link);144145assertActiveDocumentUri(testFile);146assert.strictEqual(vscode.window.activeTextEditor!.selection.start.line, 1);147});148});149150151function assertActiveDocumentUri(expectedUri: vscode.Uri) {152assert.strictEqual(153vscode.window.activeTextEditor!.document.uri.fsPath,154expectedUri.fsPath155);156}157158async function withFileContents(file: vscode.Uri, contents: string): Promise<void> {159debugLog('openTextDocument', file.toString(), Date.now());160const document = await vscode.workspace.openTextDocument(file);161debugLog('showTextDocument', file.toString(), Date.now());162const editor = await vscode.window.showTextDocument(document);163debugLog('editTextDocument', file.toString(), Date.now());164await editor.edit(edit => {165edit.replace(new vscode.Range(0, 0, 1000, 0), contents);166});167debugLog('opened done', vscode.window.activeTextEditor?.document.toString(), Date.now());168}169170async function executeLink(link: vscode.DocumentLink) {171debugLog('executingLink', link.target?.toString(), Date.now());172173await vscode.commands.executeCommand('vscode.open', link.target!);174debugLog('executedLink', vscode.window.activeTextEditor?.document.toString(), Date.now());175}176177178