Skip to content

Commit 81fe435

Browse files
committed
fix for /test for files outside project scope.
1 parent 05d7054 commit 81fe435

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
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: Fix to redirect /test to generate tests in chat for external files out of project scope."
4+
}

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

Lines changed: 16 additions & 4 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
@@ -157,6 +158,10 @@ class CodeTestChatController(
157158
// check if IDE has active file open, yes return (fileName and filePath) else return null
158159
val project = context.project
159160
val fileInfo = checkActiveFileInIDE(project, message) ?: return
161+
val projectRoot = Path.of(
162+
project.basePath ?: project.guessProjectDir()?.path
163+
?: error("Cannot guess base directory for project ${project.name}")
164+
)
160165
session.programmingLanguage = fileInfo.fileLanguage
161166
if (session.isGeneratingTests === true) {
162167
return
@@ -184,7 +189,8 @@ class CodeTestChatController(
184189
message.tabId,
185190
false
186191
)
187-
if (isLanguageSupported(fileInfo.fileLanguage.languageId)) {
192+
val supported = fileInfo.filePath.startsWith(projectRoot.toString()) && isLanguageSupported(fileInfo.fileLanguage.languageId)
193+
if (supported) {
188194
// Send Capability card to chat
189195
codeTestChatHelper.addNewMessage(
190196
CodeTestChatMessageContent(informationCard = true, message = null, type = ChatMessageType.Answer, canBeVoted = false),
@@ -230,9 +236,15 @@ class CodeTestChatController(
230236
}
231237
.build()
232238

233-
val messageContent = "<span style=\"color: #EE9D28;\">&#9888;<b> ${fileInfo.fileLanguage.languageId} is not a " +
234-
"language I support specialized unit test generation for at the moment.</b><br></span>The languages " +
235-
"I support now are Python and Java. I can still provide examples, instructions and code suggestions."
239+
val messageContent = if (fileInfo.filePath.startsWith(projectRoot.toString())) {
240+
"<span style=\"color: #EE9D28;\">&#9888;<b> ${fileInfo.fileLanguage.languageId} is not a " +
241+
"language I support specialized unit test generation for at the moment.</b><br></span>The languages " +
242+
"I support now are Python and Java. I can still provide examples, instructions and code suggestions."
243+
} else {
244+
"<span style=\"color: #EE9D28;\">&#9888;<b>External File Detected: ${fileInfo.fileName} \n" +
245+
" is outside the project directory.</b><br></span>" +
246+
"However, I can still help you create unit tests for ${fileInfo.fileName} here"
247+
}
236248

237249
codeTestChatHelper.addNewMessage(
238250
CodeTestChatMessageContent(

0 commit comments

Comments
 (0)