radarsh / gradle-test-logger-plugin

A Gradle plugin for printing beautiful logs on the console while running tests
Apache License 2.0
836 stars 37 forks source link

Can it work as a Gradle init script [Yes!] ? #253

Open patkujawa-wf opened 2 years ago

patkujawa-wf commented 2 years ago

Is it possible to use this plugin as an init script?

I use https://github.com/ben-manes/gradle-versions-plugin#using-a-gradle-init-script for all my projects, which is very convenient and allows me to keep it out of each repo. I'd like to do the same for gradle-test-logger-plugin, but I'm running into "Plugin with id 'com.adarshr.test-logger' not found." when I mimic the instructions.

I've created a $HOME/.gradle/init.d/gradle-test-logger-plugin.gradle (similar to my current $HOME/.gradle/init.d/add-versions-plugin.gradle that's working) with these contents:

initscript {
    repositories {
        gradlePluginPortal()
        // maven {
        //     url 'https://plugins.gradle.org/m2/'
        // }
    }
    dependencies {
        classpath 'com.adarshr:gradle-test-logger-plugin:+'
    }
}

allprojects {
    apply plugin: 'com.adarshr.test-logger'
}

I've tried a few variations of repositories and using buildscript instead of initscript, but all end up with the same Plugin with id 'com.adarshr.test-logger' not found. failure.

patkujawa-wf commented 2 years ago

Another example which works until I can get this sweet plugin to work is in https://stackoverflow.com/questions/45856846/how-can-i-share-build-code-script-for-all-my-gradle-projects-not-just-subprojec

allprojects {
    tasks.withType(Test) {
        testLogging {
            events "passed", "skipped", "failed", "standardOut", "standardError"
        }
    }
}
radarsh commented 2 years ago

That's odd but there is nothing different about the way the plugin is published. Have you tried running with info or debug log level turned on to perhaps get some clue? Could you also try with a specific version such as 3.1.0 instead of the + wildcard to see if it works?

patkujawa-wf commented 2 years ago

Aha, it was an issue with trying to use the plugin id, which apparently isn't supported in init scripts.

This seems to have worked:

initscript {
    repositories {
        gradlePluginPortal()
    }
    dependencies {
        classpath 'com.adarshr:gradle-test-logger-plugin:+'
    }
}

allprojects {
    apply plugin: com.adarshr.gradle.testlogger.TestLoggerPlugin
}

https://github.com/radarsh/gradle-test-logger-plugin/blob/develop/src/main/groovy/com/adarshr/gradle/testlogger/TestLoggerPlugin.groovy

patkujawa-wf commented 2 years ago

Whoops, I guess the work isn't done. Now I get this exception over and over when I run my tests:

Failed to notify test listener.
org.gradle.internal.event.ListenerNotificationException: Failed to notify test listener.
...
Caused by: java.lang.AbstractMethodError: groovy/lang/GroovyObject.getProperty(Ljava/lang/String;)Ljava/lang/Object;
        at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:190)
        at groovy.lang.Closure.getPropertyTryThese(Closure.java:313)
        at groovy.lang.Closure.getPropertyOwnerFirst(Closure.java:307)
        at groovy.lang.Closure.getProperty(Closure.java:296)
        at com.adarshr.gradle.testlogger.logger.SequentialTestLogger$_afterTest_closure2.doCall(SequentialTestLogger.groovy:39)
        at com.adarshr.gradle.testlogger.logger.SequentialTestLogger$_afterTest_closure2.call(SequentialTestLogger.groovy)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2330)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2315)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2356)
        at com.adarshr.gradle.testlogger.logger.SequentialTestLogger.afterTest(SequentialTestLogger.groovy:36)
        at com.adarshr.gradle.testlogger.logger.TestLoggerAdapter.afterTest(TestLoggerAdapter.groovy:81)
        at com.adarshr.gradle.testlogger.logger.TestLoggerWrapper.afterTest(TestLoggerWrapper.groovy:25)
        at jdk.internal.reflect.GeneratedMethodAccessor368.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
        at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
        at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:43)
        at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:245)
        at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:157)
        at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:61)
        ... 41 more

Perhaps something to do with being written in groovy? I can test with the normal way of using this plugin and see if that also fails similarly.

patkujawa-wf commented 2 years ago

I get the same exception when using the plugin in my project's build.gradle, so perhaps just something with my setup?

disrupted commented 2 years ago

@patkujawa-wf same here

radarsh commented 2 years ago

The exception you are seeing is mostly as a result of https://github.com/radarsh/gradle-test-logger-plugin/issues/241#issuecomment-1060628271. There is nothing that can be done about that if you are using Gradle < 7 and plugin version > 3.1.0 I'm afraid.