diff --git a/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json b/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json
new file mode 100644
index 00000000000..50b175ba9dd
--- /dev/null
+++ b/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json
@@ -0,0 +1,4 @@
+{
+ "type" : "bugfix",
+ "description" : "Amazon Q /test: Test generation fails for files outside the project"
+}
\ No newline at end of file
diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt
index b2913d66285..4db756a73ee 100644
--- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt
+++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt
@@ -512,6 +512,7 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
AmazonqTelemetry.utgGenerateTests(
cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
hasUserPromptSupplied = session.hasUserPromptSupplied,
+ isFileInWorkspace = true,
isSupportedLanguage = true,
credentialStartUrl = getStartUrl(project),
jobGroup = session.testGenerationJobGroupName,
diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt
index 7736f42f3f5..50d1cc266d2 100644
--- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt
+++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt
@@ -12,6 +12,7 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
+import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
@@ -158,6 +159,10 @@ class CodeTestChatController(
// check if IDE has active file open, yes return (fileName and filePath) else return null
val project = context.project
val fileInfo = checkActiveFileInIDE(project, message) ?: return
+ val projectRoot = Path.of(
+ project.basePath ?: project.guessProjectDir()?.path
+ ?: error("Cannot guess base directory for project ${project.name}")
+ )
session.programmingLanguage = fileInfo.fileLanguage
if (session.isGeneratingTests === true) {
return
@@ -185,7 +190,8 @@ class CodeTestChatController(
message.tabId,
false
)
- if (isLanguageSupported(fileInfo.fileLanguage.languageId)) {
+ val supported = fileInfo.filePath.startsWith(projectRoot.toString()) && isLanguageSupported(fileInfo.fileLanguage.languageId)
+ if (supported) {
// Send Capability card to chat
codeTestChatHelper.addNewMessage(
CodeTestChatMessageContent(informationCard = true, message = null, type = ChatMessageType.Answer, canBeVoted = false),
@@ -231,9 +237,15 @@ class CodeTestChatController(
}
.build()
- val messageContent = "⚠ ${fileInfo.fileLanguage.languageId} is not a " +
- "language I support specialized unit test generation for at the moment.
The languages " +
- "I support now are Python and Java. I can still provide examples, instructions and code suggestions."
+ val messageContent = if (fileInfo.filePath.startsWith(projectRoot.toString())) {
+ "⚠ ${fileInfo.fileLanguage.languageId} is not a " +
+ "language I support specialized unit test generation for at the moment.
The languages " +
+ "I support now are Python and Java. I can still provide examples, instructions and code suggestions."
+ } else {
+ "⚠ I can't generate tests for ${fileInfo.fileName}" +
+ " because it's outside the project directory.
" +
+ "I can still provide examples, instructions and code suggestions."
+ }
codeTestChatHelper.addNewMessage(
CodeTestChatMessageContent(
@@ -288,7 +300,8 @@ class CodeTestChatController(
AmazonqTelemetry.utgGenerateTests(
cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
hasUserPromptSupplied = session.hasUserPromptSupplied,
- isSupportedLanguage = false,
+ isFileInWorkspace = fileInfo.filePath.startsWith(projectRoot.toString()),
+ isSupportedLanguage = isLanguageSupported(fileInfo.fileLanguage.languageId),
credentialStartUrl = getStartUrl(project),
result = MetricResult.Succeeded,
perfClientLatency = (Instant.now().toEpochMilli() - session.startTimeOfTestGeneration),
@@ -590,6 +603,7 @@ class CodeTestChatController(
AmazonqTelemetry.utgGenerateTests(
cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
hasUserPromptSupplied = session.hasUserPromptSupplied,
+ isFileInWorkspace = true,
isSupportedLanguage = true,
credentialStartUrl = getStartUrl(project = context.project),
jobGroup = session.testGenerationJobGroupName,
@@ -785,6 +799,7 @@ class CodeTestChatController(
AmazonqTelemetry.utgGenerateTests(
cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
hasUserPromptSupplied = session.hasUserPromptSupplied,
+ isFileInWorkspace = true,
isSupportedLanguage = true,
credentialStartUrl = getStartUrl(project = context.project),
jobGroup = session.testGenerationJobGroupName,
diff --git a/plugins/core/jetbrains-community/resources/telemetryOverride.json b/plugins/core/jetbrains-community/resources/telemetryOverride.json
index 5637baaa16e..1901f917406 100644
--- a/plugins/core/jetbrains-community/resources/telemetryOverride.json
+++ b/plugins/core/jetbrains-community/resources/telemetryOverride.json
@@ -215,6 +215,11 @@
"type": "boolean",
"description": "True if user selected code snippet as input else false"
},
+ {
+ "name": "isFileInWorkspace",
+ "type": "boolean",
+ "description": "Indicate if the file is in the current workspace."
+ },
{
"name": "isSupportedLanguage",
"type": "boolean",
@@ -573,6 +578,9 @@
"type": "isCodeBlockSelected",
"required": false
},
+ {
+ "type": "isFileInWorkspace"
+ },
{
"type": "isSupportedLanguage"
},