Path: blob/main/extensions/copilot/test/simulation/inlineEdit/fileLoading.ts
13394 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 { readFileSync } from 'fs';6import { join } from 'path/posix';7import { getFixturesDir } from '../stestUtil';89/**10* Rewrites fileName.[ext].txt to fileName.[ext] if no virtualFileName is given!11*12* This function allows [tools](https://github.com/microsoft/vscode-ts-file-path-support/tree/main) to inline/extract the file content.13*/1415type FileInfo = { filePath: string | { fullPath: string; pathWithinFixturesDir?: string } } | {/** Relative */ fileName: string; fileContents: string };1617export interface ILoadedFile {18fileContents: string;19fileName?: string; // relative20filePath?: string; // absolute21pathWithinFixturesDir?: string; // relative22}2324/**25* This function allows [tools](https://github.com/microsoft/vscode-ts-file-path-support/tree/main) to inline/extract the file content.26*/27export function loadFile(data: FileInfo): ILoadedFile {28let fileName: string | undefined = undefined;29let filePath: string | undefined = undefined;30let pathWithinFixturesDir: string | undefined = undefined;31let fileContents: string;3233if ('fileContents' in data) {34fileName = data.fileName;35fileContents = data.fileContents;36} else {37if (typeof data.filePath === 'string') {38filePath = data.filePath;39filePath = filePath;40} else {41filePath = data.filePath.fullPath;42pathWithinFixturesDir = data.filePath.pathWithinFixturesDir;43}4445fileContents = readFileSync(filePath, 'utf8');46}4748return { fileContents, fileName, filePath, pathWithinFixturesDir };49}5051export function inlineEditsFixture(pathWithinFixturesDir: RelativeFilePath<'$dir/../fixtures/inlineEdits'>): { fullPath: string; pathWithinFixturesDir: string } {52const fullPath = join(getFixturesDir(), 'inlineEdits', pathWithinFixturesDir);53return {54fullPath,55pathWithinFixturesDir,56};57}5859/** See https://github.com/microsoft/vscode-ts-file-path-support */60type RelativeFilePath<T extends string> = string & { baseDir?: T }; export interface IInlineEditBaseFile {61fileContents: string;6263/** Relative or absolute */64virtualFileName: string;65}666768