Path: blob/main/extensions/copilot/src/platform/notebook/test/node/utils.ts
13405 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*--------------------------------------------------------------------------------------------*/4import * as fs from 'fs';5import * as path from '../../../../util/vs/base/common/path';6import type * as vscode from 'vscode';7import { BaseAlternativeNotebookContentProvider } from '../../../../platform/notebook/common/alternativeContentProvider';8import { SimulationWorkspace } from '../../../../platform/test/node/simulationWorkspace';9import { ExtHostNotebookDocumentData } from '../../../../util/common/test/shims/notebookDocument';10import { Uri } from '../../../../vscodeTypes';1112export function loadFile(data: FixtureData): Promise<ITestFile>;13export function loadFile(data: Omit<FixtureData, 'filePath'> & { fileName: string; fileContents: string }): Promise<'not_supported'>;14export async function loadFile(data: FixtureData | (Omit<FixtureData, 'filePath'> & { fileName: string; fileContents: string })): Promise<ITestFile | 'not_supported'> {15if ('fileName' in data) { return 'not_supported'; }16const contents = (await fs.promises.readFile(data.filePath)).toString();17return {18contents,19filePath: data.filePath,20formattingOptions: undefined,21};22}2324interface FixtureData {25filePath: string;26}2728export type RelativeFilePath<T extends string> = string & { baseDir?: T };2930export function fixture(relativePath: RelativeFilePath<'$dir/fixtures'>): string {31const filePath = path.join(__dirname, 'fixtures', relativePath);32return filePath;33}34export function getAlternativeNotebookSnapshotPath(data: ITestFile, extension: string): string {35return addSecondaryExtension(data.filePath, [extension]);36}3738function addSecondaryExtension(filePath: string, extensions: string[]): string {39return filePath + '.' + extensions.join('.');40}414243export function docPathInFixture(pathWithinFixturesDir: string, type: 'summarized' | 'selection') {44const dirname = path.dirname(pathWithinFixturesDir);45const basename = path.basename(pathWithinFixturesDir);46const basenameByDots = basename.split('.');47basenameByDots.splice(basenameByDots.length - 1, 0, type);48const docBasename = basenameByDots.join('.');49const docPathWithinFixturesDir = path.join(dirname, docBasename);50return path.join(__dirname, 'fixtures', docPathWithinFixturesDir);51}5253interface ITestFile {54contents: string;55filePath: string;56formattingOptions?: vscode.FormattingOptions;57}58export async function generateAlternativeContent(59filePromise: ITestFile | Promise<ITestFile>,60contentProvider: BaseAlternativeNotebookContentProvider,61): Promise<{ content: string; notebook: vscode.NotebookDocument }> {62const notebook = await loadNotebook(filePromise);6364const content = contentProvider.getAlternativeDocument(notebook).getText();65return { content, notebook };66}6768export async function loadNotebook(filePromise: ITestFile | Promise<ITestFile>, simulationWorkspace?: SimulationWorkspace) {69const file = await filePromise;70const uri = Uri.file(file.filePath);71return file.filePath.endsWith('.ipynb') ? ExtHostNotebookDocumentData.createJupyterNotebook(uri, file.contents, simulationWorkspace).document :72ExtHostNotebookDocumentData.createGithubIssuesNotebook(uri, file.contents, simulationWorkspace).document;73}7475