unit-mesh / auto-dev

🧙‍AutoDev: The AI-powered coding wizard(AI 驱动编程助手) with multilingual support 🌐, auto code generation 🏗️, and a helpful bug-slaying assistant 🐞! Customizable prompts 🎨 and a magic Auto Dev/Testing/Document/Agent feature 🧪 included! 🚀
https://ide.unitmesh.cc/
Mozilla Public License 2.0
2.72k stars 314 forks source link

test gen error: can't create test file #152

Closed iptton closed 4 months ago

iptton commented 4 months ago

when generating test for file in a submodule of gradle project , error occurred:

java.lang.Throwable: Source file is not in the src/main/java directory: /{my computer}/MyProject/SubModule/src/main/java/pack/age/to/file
    at com.intellij.openapi.diagnostic.Logger.error(Logger.java:376)
    at cc.unitmesh.kotlin.provider.KotlinAutoTestService.findOrCreateTestFile(KotlinAutoTestService.kt:57)
    at cc.unitmesh.devti.intentions.action.task.TestCodeGenTask.run(TestCodeGenTask.kt:61)
his progress indicator (测试此代码 1247087534: running=true; canceled=false; task=cc.unitmesh.devti.intentions.action.task.TestCodeGenTask@2efacf7a) is indeterminate, this may lead to visual inconsistency. Please call setIndeterminate(false) before you start progress. class com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
java.lang.IllegalStateException
    at com.intellij.openapi.progress.util.AbstractProgressIndicatorBase.setFraction(AbstractProgressIndicatorBase.java:211)
    at com.intellij.openapi.progress.util.AbstractProgressIndicatorExBase.setFraction(AbstractProgressIndicatorExBase.java:119)
    at com.intellij.openapi.progress.util.ProgressWindow.setFraction(ProgressWindow.java:333)
    at cc.unitmesh.devti.intentions.action.task.TestCodeGenTask.run(TestCodeGenTask.kt:57)
java.lang.Throwable: Failed to create test file for: KtFile: MyFileName.kt
    at com.intellij.openapi.diagnostic.Logger.error(Logger.java:376)
    at cc.unitmesh.devti.intentions.action.task.TestCodeGenTask.run(TestCodeGenTask.kt:72)
phodal commented 4 months ago

目前好像是 setIndeterminate 的问题?

上一个版本好像已经修了:https://github.com/unit-mesh/auto-dev/commit/5f139d94bb16ce62fc283499e7e374457b3c7f74#diff-39ca5a13ebc19bf3e4bf11e9d76cba80ff2bba9e5cdc944a3150f5e46ea3ac38L46

iptton commented 4 months ago

目前好像是 setIndeterminate 的问题?

上一个版本好像已经修了:5f139d9#diff-39ca5a13ebc19bf3e4bf11e9d76cba80ff2bba9e5cdc944a3150f5e46ea3ac38L46

我本地是 1.8.0-241 ,setIndeterminate 只是个 Info ,不是错误。 bug 表现是测试文件不会被创建出来

phodal commented 4 months ago

之前的行为好像也是这样的 —— 目前应该是 241 之后的 JetBrains API 变化引起的,可以更新到 release 里的版本试试

iptton commented 4 months ago

Source file is not in the src/main/java directory: /{my computer}/MyProject/SubModule/src/main/java/pack/age/to/file

这外错误有点问题吧?打出来的日志明显路径里就有 src/main/java 呀

phodal commented 4 months ago

有两个问题

  1. KotlinAutoTestService.kt:57 写的是 src/main/java 应该改为 src/main/kotlin
  2. Java 的测试不应该触发到 Kotlin 的测试生成才对。
iptton commented 4 months ago

Source file is not in the src/main/java directory: /{my computer}/MyProject/SubModule/src/main/java/pack/age/to/file

这外错误有点问题吧?打出来的日志明显路径里就有 src/main/java 呀

        if (!parentDirPath?.contains("/main/kotlin/")!!) {
            log.error("Source file is not in the src/main/java directory: $parentDirPath")
            return null
        }

现实中的项目,kotlin 还是 java 代码,都有可能会放到 src/main/java 或 src/main/kotin 里...

phodal commented 4 months ago

嗯,理论上正确的方式应该是去 Gradle 的 sourceset 拿,所以在 IDE 中会有一个 popup window 来让用户选择。

这里有一个对应的官方实现:

https://github.com/JetBrains/intellij-community/blob/74b6531f2c636b893e26e2192d99ec25e5882b3f/platform/lang-impl/src/com/intellij/testIntegration/TestFinderHelper.java#L29-L36

Java 实现:

https://github.com/JetBrains/intellij-community/blob/74b6531f2c636b893e26e2192d99ec25e5882b3f/java/java-impl/src/com/intellij/testIntegration/JavaTestFinder.java#L38-L50