Path: blob/main/extensions/copilot/src/extension/intents/node/testIntent/testPromptUtil.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*--------------------------------------------------------------------------------------------*/45import { IWorkspaceService } from '../../../../platform/workspace/common/workspaceService';6import * as path from '../../../../util/vs/base/common/path';7import { URI } from '../../../../util/vs/base/common/uri';8import { ChatVariablesCollection } from '../../../prompt/common/chatVariablesCollection';9import { IDocumentContext } from '../../../prompt/node/documentContext';101112interface TestGenUserQueryParams {13workspaceService: IWorkspaceService;14chatVariables: ChatVariablesCollection;15userQuery: string;16testFileToWriteTo: URI;17testedSymbolIdentifier: string | undefined;18context: IDocumentContext;19}2021export function formatRequestAndUserQuery({ workspaceService, chatVariables, userQuery, testFileToWriteTo, testedSymbolIdentifier, context }: TestGenUserQueryParams) {2223const testTarget = testedSymbolIdentifier ? `\`${testedSymbolIdentifier}\`` : `my code`;2425const rewrittenMessage = chatVariables.substituteVariablesWithReferences(userQuery);2627const pathToTestFile = relativeToWorkspace(workspaceService, testFileToWriteTo.path);2829const requestAndUserQueryParts: string[] = [];3031requestAndUserQueryParts.push(`Please, generate tests for ${testTarget}.`);3233if (pathToTestFile !== null) {34let locationMessage = `The tests will be placed in \`${pathToTestFile}\``;35locationMessage += (pathToTestFile.includes('/')36? '.'37: ` located in the same directory as \`${relativeToWorkspace(workspaceService, context.document.uri.path)}\`.`38);39requestAndUserQueryParts.push(locationMessage);40requestAndUserQueryParts.push('Generate tests accordingly.');41}4243requestAndUserQueryParts.push(rewrittenMessage);4445const requestAndUserQuery = requestAndUserQueryParts.filter(s => s !== '').join(' ').trim();4647return requestAndUserQuery;48}4950/**51* @return undefined if no workspace contains given path52*/53export function relativeToWorkspace(workspaceService: IWorkspaceService, absPath: string): string | null {5455const workspaceOfTestFile = workspaceService.getWorkspaceFolders().find(folder => absPath.startsWith(folder.path));5657if (workspaceOfTestFile === undefined) {58return null;59}6061const relPath = path.relative(workspaceOfTestFile.path, absPath);6263// Convert the path separator to be platform-independent64const relPathPosix = relPath.split(path.sep).join('/');6566return relPathPosix;67}686970