Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/intents/node/testIntent/testPromptUtil.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
6
import { IWorkspaceService } from '../../../../platform/workspace/common/workspaceService';
7
import * as path from '../../../../util/vs/base/common/path';
8
import { URI } from '../../../../util/vs/base/common/uri';
9
import { ChatVariablesCollection } from '../../../prompt/common/chatVariablesCollection';
10
import { IDocumentContext } from '../../../prompt/node/documentContext';
11
12
13
interface TestGenUserQueryParams {
14
workspaceService: IWorkspaceService;
15
chatVariables: ChatVariablesCollection;
16
userQuery: string;
17
testFileToWriteTo: URI;
18
testedSymbolIdentifier: string | undefined;
19
context: IDocumentContext;
20
}
21
22
export function formatRequestAndUserQuery({ workspaceService, chatVariables, userQuery, testFileToWriteTo, testedSymbolIdentifier, context }: TestGenUserQueryParams) {
23
24
const testTarget = testedSymbolIdentifier ? `\`${testedSymbolIdentifier}\`` : `my code`;
25
26
const rewrittenMessage = chatVariables.substituteVariablesWithReferences(userQuery);
27
28
const pathToTestFile = relativeToWorkspace(workspaceService, testFileToWriteTo.path);
29
30
const requestAndUserQueryParts: string[] = [];
31
32
requestAndUserQueryParts.push(`Please, generate tests for ${testTarget}.`);
33
34
if (pathToTestFile !== null) {
35
let locationMessage = `The tests will be placed in \`${pathToTestFile}\``;
36
locationMessage += (pathToTestFile.includes('/')
37
? '.'
38
: ` located in the same directory as \`${relativeToWorkspace(workspaceService, context.document.uri.path)}\`.`
39
);
40
requestAndUserQueryParts.push(locationMessage);
41
requestAndUserQueryParts.push('Generate tests accordingly.');
42
}
43
44
requestAndUserQueryParts.push(rewrittenMessage);
45
46
const requestAndUserQuery = requestAndUserQueryParts.filter(s => s !== '').join(' ').trim();
47
48
return requestAndUserQuery;
49
}
50
51
/**
52
* @return undefined if no workspace contains given path
53
*/
54
export function relativeToWorkspace(workspaceService: IWorkspaceService, absPath: string): string | null {
55
56
const workspaceOfTestFile = workspaceService.getWorkspaceFolders().find(folder => absPath.startsWith(folder.path));
57
58
if (workspaceOfTestFile === undefined) {
59
return null;
60
}
61
62
const relPath = path.relative(workspaceOfTestFile.path, absPath);
63
64
// Convert the path separator to be platform-independent
65
const relPathPosix = relPath.split(path.sep).join('/');
66
67
return relPathPosix;
68
}
69
70