Skip to content

Commit f9ca3d4

Browse files
authored
feat(codewhisperer): more timeout checks when generating unit tests #3678
1 parent ea7bcd1 commit f9ca3d4

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/codewhisperer/util/supplementalContext/utgUtils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function fetchSupplementalContextForTest(
5959
// TODO (Metrics): 1. Total number of calls to fetchSupplementalContextForTest
6060
throwIfCancelled(cancellationToken)
6161

62-
let crossSourceFile = await findSourceFileByName(editor, languageConfig)
62+
let crossSourceFile = await findSourceFileByName(editor, languageConfig, cancellationToken)
6363
if (crossSourceFile) {
6464
// TODO (Metrics): 2. Success count for fetchSourceFileByName (find source file by name)
6565
return generateSupplementalContextFromFocalFile(crossSourceFile, cancellationToken)
@@ -102,7 +102,13 @@ async function findSourceFileByContent(
102102
): Promise<string | undefined> {
103103
const testFileContent = fs.readFileSync(editor.document.fileName, 'utf-8')
104104
const testElementList = extractFunctions(testFileContent, languageConfig.functionExtractionPattern)
105+
106+
throwIfCancelled(cancellationToken)
107+
105108
testElementList.push(...extractClasses(editor.document.fileName, languageConfig.classExtractionPattern))
109+
110+
throwIfCancelled(cancellationToken)
111+
106112
let sourceFilePath: string | undefined = undefined
107113
let maxMatchCount = 0
108114

@@ -113,6 +119,8 @@ async function findSourceFileByContent(
113119

114120
const relevantFilePaths = await getRelevantUtgFiles(editor)
115121

122+
throwIfCancelled(cancellationToken)
123+
116124
// TODO (Metrics):Add metrics for relevantFilePaths length
117125
relevantFilePaths.forEach(filePath => {
118126
throwIfCancelled(cancellationToken)
@@ -144,7 +152,8 @@ async function getRelevantUtgFiles(editor: vscode.TextEditor): Promise<string[]>
144152

145153
async function findSourceFileByName(
146154
editor: vscode.TextEditor,
147-
languageConfig: utgLanguageConfig
155+
languageConfig: utgLanguageConfig,
156+
cancellationToken: vscode.CancellationToken
148157
): Promise<string | undefined> {
149158
const uri = editor.document.uri
150159
const workspaceFolder = vscode.workspace.getWorkspaceFolder(uri)
@@ -157,6 +166,8 @@ async function findSourceFileByName(
157166
basenameSuffix = match[1] || match[2]
158167
}
159168

169+
throwIfCancelled(cancellationToken)
170+
160171
// Assuming the convention of using similar path structure for test and src files.
161172
const dirPath = path.dirname(editor.document.uri.fsPath)
162173
let newPath = ''
@@ -174,12 +185,16 @@ async function findSourceFileByName(
174185
return newPath
175186
}
176187

188+
throwIfCancelled(cancellationToken)
189+
177190
// TODO: vscode.workspace.findFiles is preferred but doesn't seems to be working for now.
178191
// TODO: Enable this later.
179192
//const sourceFiles =
180193
// await vscode.workspace.findFiles(`${projectPath}/**/${basenameSuffix}${languageConfig.extension}`);
181194
const sourceFiles = await globPromise(`${projectPath}/**/${basenameSuffix}${languageConfig.extension}`)
182195

196+
throwIfCancelled(cancellationToken)
197+
183198
if (sourceFiles.length > 0) {
184199
return sourceFiles[0]
185200
}

0 commit comments

Comments
 (0)