Skip to content

Commit f5ed158

Browse files
authored
Fix for Test generation active file info file path check on windows. (#5242)
1 parent 25443ba commit f5ed158

File tree

1 file changed

+11
-8
lines changed
  • plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller

1 file changed

+11
-8
lines changed

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import software.aws.toolkits.resources.message
8989
import software.aws.toolkits.telemetry.AmazonqTelemetry
9090
import software.aws.toolkits.telemetry.MetricResult
9191
import software.aws.toolkits.telemetry.UiTelemetry
92+
import java.io.File
9293
import java.nio.file.Files
9394
import java.nio.file.Path
9495
import java.nio.file.Paths
@@ -159,10 +160,6 @@ class CodeTestChatController(
159160
// check if IDE has active file open, yes return (fileName and filePath) else return null
160161
val project = context.project
161162
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-
)
166163
session.programmingLanguage = fileInfo.fileLanguage
167164
if (session.isGeneratingTests === true) {
168165
return
@@ -190,8 +187,7 @@ class CodeTestChatController(
190187
message.tabId,
191188
false
192189
)
193-
val supported = fileInfo.filePath.startsWith(projectRoot.toString()) && isLanguageSupported(fileInfo.fileLanguage.languageId)
194-
if (supported) {
190+
if (fileInfo.fileInWorkspace && isLanguageSupported(fileInfo.fileLanguage.languageId)) {
195191
// Send Capability card to chat
196192
codeTestChatHelper.addNewMessage(
197193
CodeTestChatMessageContent(informationCard = true, message = null, type = ChatMessageType.Answer, canBeVoted = false),
@@ -237,7 +233,7 @@ class CodeTestChatController(
237233
}
238234
.build()
239235

240-
val messageContent = if (fileInfo.filePath.startsWith(projectRoot.toString())) {
236+
val messageContent = if (fileInfo.fileInWorkspace) {
241237
"<span style=\"color: #EE9D28;\">&#9888;<b> ${fileInfo.fileLanguage.languageId} is not a " +
242238
"language I support specialized unit test generation for at the moment.</b><br></span>The languages " +
243239
"I support now are Python and Java. I can still provide examples, instructions and code suggestions."
@@ -300,7 +296,7 @@ class CodeTestChatController(
300296
AmazonqTelemetry.utgGenerateTests(
301297
cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
302298
hasUserPromptSupplied = session.hasUserPromptSupplied,
303-
isFileInWorkspace = fileInfo.filePath.startsWith(projectRoot.toString()),
299+
isFileInWorkspace = fileInfo.fileInWorkspace,
304300
isSupportedLanguage = isLanguageSupported(fileInfo.fileLanguage.languageId),
305301
credentialStartUrl = getStartUrl(project),
306302
result = MetricResult.Succeeded,
@@ -1129,6 +1125,7 @@ class CodeTestChatController(
11291125
val filePath: String,
11301126
val fileName: String,
11311127
val fileLanguage: CodeWhispererProgrammingLanguage,
1128+
val fileInWorkspace: Boolean = true,
11321129
)
11331130

11341131
private suspend fun updateUIState() {
@@ -1160,6 +1157,9 @@ class CodeTestChatController(
11601157
val fileEditorManager = FileEditorManager.getInstance(project)
11611158
val activeEditor = fileEditorManager.selectedEditor
11621159
val activeFile = fileEditorManager.selectedFiles.firstOrNull()
1160+
val projectRoot = project.basePath?.let { Path.of(it) }?.toFile()?.toVirtualFile() ?: run {
1161+
project.guessProjectDir() ?: error("Cannot guess base directory for project ${project.name}")
1162+
}
11631163

11641164
if (activeEditor == null || activeFile == null) {
11651165
handleInvalidFileState(message.tabId)
@@ -1174,6 +1174,7 @@ class CodeTestChatController(
11741174
filePath = activeFile.path,
11751175
fileName = activeFile.name,
11761176
fileLanguage = programmingLanguage,
1177+
fileInWorkspace = activeFile.path.startsWith(projectRoot.path)
11771178
)
11781179
} catch (e: Exception) {
11791180
LOG.debug { "Error checking active file: $e" }
@@ -1187,6 +1188,8 @@ class CodeTestChatController(
11871188
}
11881189
}
11891190

1191+
private fun File.toVirtualFile() = LocalFileSystem.getInstance().findFileByIoFile(this)
1192+
11901193
/* UTG Tab Chat input use cases:
11911194
* 1. If User exits the flow and want to start a new generate unit test cycle.
11921195
* 2. If User clicks on Modify build command option and can enter the build command from chat input

0 commit comments

Comments
 (0)