gpt4 book ai didi

gradle - 在 IDEA 中运行测试时,不是由 Gradle TestKit 创建的 plugin-under-test-metadata.properties

转载 作者:行者123 更新时间:2023-12-04 03:10:43 25 4
gpt4 key购买 nike

我正在使用 Gradle 3.3 并尝试使用 JUnit 和 Gradle TestKit 测试自定义插件。在插件的 build.gradle我有

version '0.1'

apply plugin: 'groovy'
apply plugin: 'java-gradle-plugin'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
compile project(':codegen-core')
compile localGroovy()
testCompile 'junit:junit:4.12'
}

测试是
package com.huawei.odmf.codegen.gradle

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

import static org.junit.Assert.*

class TestOdmfCodegenPlugin {

@Rule
public final TemporaryFolder testProjectDir = new TemporaryFolder()

private File buildFile
private File assetsDir

@Before
void setUp() {
buildFile = testProjectDir.newFile("build.gradle")
assetsDir = testProjectDir.newFolder("src", "main", "assets")
}

@Test
void testPlugin() {
buildFile << """
plugins {
id 'com.huawei.odmf'
}

apply plugin: 'com.android.application'

odmf {
modelFile 'odmf.xml'
}
"""

BuildResult result = GradleRunner.create().
withProjectDir(testProjectDir.root).
withArguments(OdmfCodegenPlugin.taskName).
withPluginClasspath().
build()

// assertions
}
}

src/main/resources/META-INF/gradle-plugins/com.huawei.odmf.properties我有
implementation-class=com.huawei.odmf.codegen.gradle.OdmfCodegenPlugin

根据 https://docs.gradle.org/current/userguide/test_kit.html#sub:test-kit-automatic-classpath-injection,这似乎是所有需要的和 automaticClasspathInjectionQuickstart样本。

但是,此测试在 withPluginClasspath() 处失败(编辑:在 IDEA 中运行时;它从命令行工作)具有以下堆栈跟踪(据我所知, plugin-under-test-metadata.properties 应该由 java-gradle-plugin 自动创建):
org.gradle.testkit.runner.InvalidPluginMetadataException: Test runtime classpath does not contain plugin metadata file 'plugin-under-test-metadata.properties'

at org.gradle.testkit.runner.internal.PluginUnderTestMetadataReading.readImplementationClasspath(PluginUnderTestMetadataReading.java:44)
at org.gradle.testkit.runner.internal.PluginUnderTestMetadataReading.readImplementationClasspath(PluginUnderTestMetadataReading.java:37)
at org.gradle.testkit.runner.internal.DefaultGradleRunner.withPluginClasspath(DefaultGradleRunner.java:146)
at org.gradle.testkit.runner.internal.DefaultGradleRunner$withPluginClasspath$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at com.huawei.odmf.codegen.gradle.TestOdmfCodegenPlugin.testPlugin(TestOdmfCodegenPlugin.groovy:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我错过了什么?

最佳答案

plugins {
id "org.jetbrains.gradle.plugin.idea-ext" version "0.4.2"
}

task fixIdeaPluginClasspath {
doFirst {
configure(tasks.pluginUnderTestMetadata) {
def ideaClassesPath = project.buildDir.toPath().resolveSibling("out").resolve("production")
def newClasspath = pluginClasspath as List
newClasspath.add(0, ideaClassesPath)
pluginClasspath.setFrom(newClasspath)
}
}
}
pluginUnderTestMetadata.mustRunAfter(fixIdeaPluginClasspath)

idea.project.settings {
taskTriggers {
beforeBuild fixIdeaPluginClasspath, pluginUnderTestMetadata
}
}

这适用于 IDEA 2019.1(也可能适用于早期版本)。

这利用了 JetBrains' own gradle plugin for configuring IDEA settings执行两个 pluginUnderTestMetadata和自定义 fixIdeaPluginClasspath在每次构建之前(后者只会在 IDEA 中运行,而不是在运行原生 gradle 时)。

第一个任务-- pluginUnderTestMetadata -- 确保创建属性文件,并且也由 native Gradle 执行。

第二个任务-- fixIdeaPluginClasspath -- 修复了 IDEA 如何执行测试的另一个错误: pluginUnderTestMetadata 生成的类路径将只包含对 "$projectDir/build" 的引用目录,这不是 IDEA 输出其编译类的地方;因此,您不会看到在由 IDEA 编译的插件代码中所做的更改,而只会看到由 native gradle 编译的代码。然后它所做的是将 IDEA 类目录添加到类路径中。起初我也尝试删除 "$projectDir/build"引用,但 gradle 不喜欢它提示插件命名空间问题(对我来说太巫毒了)。

感谢 @krzychu为指出 pluginUnderTestMetadata (在较早答案的评论中)。

关于gradle - 在 IDEA 中运行测试时,不是由 Gradle TestKit 创建的 plugin-under-test-metadata.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44679007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com