Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/notebook/test/node/utils.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import * as fs from 'fs';
6
import * as path from '../../../../util/vs/base/common/path';
7
import type * as vscode from 'vscode';
8
import { BaseAlternativeNotebookContentProvider } from '../../../../platform/notebook/common/alternativeContentProvider';
9
import { SimulationWorkspace } from '../../../../platform/test/node/simulationWorkspace';
10
import { ExtHostNotebookDocumentData } from '../../../../util/common/test/shims/notebookDocument';
11
import { Uri } from '../../../../vscodeTypes';
12
13
export function loadFile(data: FixtureData): Promise<ITestFile>;
14
export function loadFile(data: Omit<FixtureData, 'filePath'> & { fileName: string; fileContents: string }): Promise<'not_supported'>;
15
export async function loadFile(data: FixtureData | (Omit<FixtureData, 'filePath'> & { fileName: string; fileContents: string })): Promise<ITestFile | 'not_supported'> {
16
if ('fileName' in data) { return 'not_supported'; }
17
const contents = (await fs.promises.readFile(data.filePath)).toString();
18
return {
19
contents,
20
filePath: data.filePath,
21
formattingOptions: undefined,
22
};
23
}
24
25
interface FixtureData {
26
filePath: string;
27
}
28
29
export type RelativeFilePath<T extends string> = string & { baseDir?: T };
30
31
export function fixture(relativePath: RelativeFilePath<'$dir/fixtures'>): string {
32
const filePath = path.join(__dirname, 'fixtures', relativePath);
33
return filePath;
34
}
35
export function getAlternativeNotebookSnapshotPath(data: ITestFile, extension: string): string {
36
return addSecondaryExtension(data.filePath, [extension]);
37
}
38
39
function addSecondaryExtension(filePath: string, extensions: string[]): string {
40
return filePath + '.' + extensions.join('.');
41
}
42
43
44
export function docPathInFixture(pathWithinFixturesDir: string, type: 'summarized' | 'selection') {
45
const dirname = path.dirname(pathWithinFixturesDir);
46
const basename = path.basename(pathWithinFixturesDir);
47
const basenameByDots = basename.split('.');
48
basenameByDots.splice(basenameByDots.length - 1, 0, type);
49
const docBasename = basenameByDots.join('.');
50
const docPathWithinFixturesDir = path.join(dirname, docBasename);
51
return path.join(__dirname, 'fixtures', docPathWithinFixturesDir);
52
}
53
54
interface ITestFile {
55
contents: string;
56
filePath: string;
57
formattingOptions?: vscode.FormattingOptions;
58
}
59
export async function generateAlternativeContent(
60
filePromise: ITestFile | Promise<ITestFile>,
61
contentProvider: BaseAlternativeNotebookContentProvider,
62
): Promise<{ content: string; notebook: vscode.NotebookDocument }> {
63
const notebook = await loadNotebook(filePromise);
64
65
const content = contentProvider.getAlternativeDocument(notebook).getText();
66
return { content, notebook };
67
}
68
69
export async function loadNotebook(filePromise: ITestFile | Promise<ITestFile>, simulationWorkspace?: SimulationWorkspace) {
70
const file = await filePromise;
71
const uri = Uri.file(file.filePath);
72
return file.filePath.endsWith('.ipynb') ? ExtHostNotebookDocumentData.createJupyterNotebook(uri, file.contents, simulationWorkspace).document :
73
ExtHostNotebookDocumentData.createGithubIssuesNotebook(uri, file.contents, simulationWorkspace).document;
74
}
75