Skip to content

Commit f4a71be

Browse files
authored
Merge branch 'main' into refactor-telemetry
2 parents 7e8c416 + 291be07 commit f4a71be

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "bugfix",
3+
"description" : "Amazon Q /test: Test generation fails for files outside the project"
4+
}

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
512512
AmazonqTelemetry.utgGenerateTests(
513513
cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
514514
hasUserPromptSupplied = session.hasUserPromptSupplied,
515+
isFileInWorkspace = true,
515516
isSupportedLanguage = true,
516517
credentialStartUrl = getStartUrl(project),
517518
jobGroup = session.testGenerationJobGroupName,

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.intellij.openapi.application.ApplicationManager
1212
import com.intellij.openapi.fileEditor.FileDocumentManager
1313
import com.intellij.openapi.fileEditor.FileEditorManager
1414
import com.intellij.openapi.project.Project
15+
import com.intellij.openapi.project.guessProjectDir
1516
import com.intellij.openapi.vfs.LocalFileSystem
1617
import com.intellij.openapi.vfs.VirtualFile
1718
import com.intellij.openapi.vfs.VirtualFileManager
@@ -158,6 +159,10 @@ class CodeTestChatController(
158159
// check if IDE has active file open, yes return (fileName and filePath) else return null
159160
val project = context.project
160161
val fileInfo = checkActiveFileInIDE(project, message) ?: return
162+
val projectRoot = Path.of(
163+
project.basePath ?: project.guessProjectDir()?.path
164+
?: error("Cannot guess base directory for project ${project.name}")
165+
)
161166
session.programmingLanguage = fileInfo.fileLanguage
162167
if (session.isGeneratingTests === true) {
163168
return
@@ -185,7 +190,8 @@ class CodeTestChatController(
185190
message.tabId,
186191
false
187192
)
188-
if (isLanguageSupported(fileInfo.fileLanguage.languageId)) {
193+
val supported = fileInfo.filePath.startsWith(projectRoot.toString()) && isLanguageSupported(fileInfo.fileLanguage.languageId)
194+
if (supported) {
189195
// Send Capability card to chat
190196
codeTestChatHelper.addNewMessage(
191197
CodeTestChatMessageContent(informationCard = true, message = null, type = ChatMessageType.Answer, canBeVoted = false),
@@ -231,9 +237,15 @@ class CodeTestChatController(
231237
}
232238
.build()
233239

234-
val messageContent = "<span style=\"color: #EE9D28;\">&#9888;<b> ${fileInfo.fileLanguage.languageId} is not a " +
235-
"language I support specialized unit test generation for at the moment.</b><br></span>The languages " +
236-
"I support now are Python and Java. I can still provide examples, instructions and code suggestions."
240+
val messageContent = if (fileInfo.filePath.startsWith(projectRoot.toString())) {
241+
"<span style=\"color: #EE9D28;\">&#9888;<b> ${fileInfo.fileLanguage.languageId} is not a " +
242+
"language I support specialized unit test generation for at the moment.</b><br></span>The languages " +
243+
"I support now are Python and Java. I can still provide examples, instructions and code suggestions."
244+
} else {
245+
"<span style=\"color: #EE9D28;\">&#9888;<b> I can't generate tests for ${fileInfo.fileName}" +
246+
" because it's outside the project directory.</b><br></span> " +
247+
"I can still provide examples, instructions and code suggestions."
248+
}
237249

238250
codeTestChatHelper.addNewMessage(
239251
CodeTestChatMessageContent(
@@ -288,7 +300,8 @@ class CodeTestChatController(
288300
AmazonqTelemetry.utgGenerateTests(
289301
cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
290302
hasUserPromptSupplied = session.hasUserPromptSupplied,
291-
isSupportedLanguage = false,
303+
isFileInWorkspace = fileInfo.filePath.startsWith(projectRoot.toString()),
304+
isSupportedLanguage = isLanguageSupported(fileInfo.fileLanguage.languageId),
292305
credentialStartUrl = getStartUrl(project),
293306
result = MetricResult.Succeeded,
294307
perfClientLatency = (Instant.now().toEpochMilli() - session.startTimeOfTestGeneration),
@@ -590,6 +603,7 @@ class CodeTestChatController(
590603
AmazonqTelemetry.utgGenerateTests(
591604
cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
592605
hasUserPromptSupplied = session.hasUserPromptSupplied,
606+
isFileInWorkspace = true,
593607
isSupportedLanguage = true,
594608
credentialStartUrl = getStartUrl(project = context.project),
595609
jobGroup = session.testGenerationJobGroupName,
@@ -785,6 +799,7 @@ class CodeTestChatController(
785799
AmazonqTelemetry.utgGenerateTests(
786800
cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
787801
hasUserPromptSupplied = session.hasUserPromptSupplied,
802+
isFileInWorkspace = true,
788803
isSupportedLanguage = true,
789804
credentialStartUrl = getStartUrl(project = context.project),
790805
jobGroup = session.testGenerationJobGroupName,

0 commit comments

Comments
 (0)