Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added detecting cucumber support to pts scripts #948

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Capture {
'net.jqwik.engine.JqwikTestEngine' : 'jqwik',
'com.tngtech.archunit.junit.ArchUnitTestEngine' : 'archunit',
'co.helmethair.scalatest.ScalatestEngine' : 'scalatest',
'io.kotest.runner.junit.platform.KotestJunitPlatformTestEngine' : 'kotest-runner'
'io.kotest.runner.junit.platform.KotestJunitPlatformTestEngine' : 'kotest-runner',
'io.cucumber.junit.platform.engine.CucumberTestEngine' : 'cucumber-junit-platform'
]
private Logger logger
private BuildScanExtension buildScanApi
Expand All @@ -48,7 +49,7 @@ class Capture {
if (t.getTestFramework().getClass().getName() == 'org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestFramework') {
def engines = testEngines(t)
buildScanApi.value("${t.identityPath}#engines", "${engines}")
if (!engines.isEmpty() && engines.stream().allMatch { e -> supportedEngines.containsKey(e) }) {
if (ptsSupported(t, engines)) {
buildScanApi.value("${t.identityPath}#pts", 'SUPPORTED')
} else {
buildScanApi.value("${t.identityPath}#pts", 'ENGINES_NOT_ALL_SUPPORTED')
Expand All @@ -58,6 +59,25 @@ class Capture {
}
}

boolean ptsSupported(Test t, Set<String> engines) {
if (cucumberUsed(t) && !t.project.plugins.hasPlugin('com.gradle.cucumber.companion')) {
// If cucumber is used without companion it's not supported.
return false
} else if (!engines.isEmpty() && engines.stream().allMatch { e -> supportedEngines.containsKey(e) }) {
return true
} else {
return false
}
}

boolean cucumberUsed(Test t) {
return t.project.configurations.stream().filter { it.canBeResolved }.any {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the testImplementation always a resolvable dependency? I'm wondering if there are cases where this check would just return false or true incorrectly if it isn't resolvable.

Copy link
Member Author

@ribafish ribafish Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the code will check all configurations. As for if testImplementation is always resolvable...I'm not sure, but if it's not resolvable, then how it would work (the project setup itself) without it being resolved?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think testImplementation is resolvable in the same way that implementation is not resolvable. They just declare a group of dependencies. Can you please check?

This is a useful blog post that someone in our slack linked to that covers this topic.
https://dev.to/autonomousapps/configuration-roles-and-the-blogging-industrial-complex-21mn

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, however, at the end, in order for a dependency to be used it will need to be resolved in one of the configurations. The implementation I made will check all resolvable implementations for cucumber. Or am I missing something?

Note: I decided not to use the task.classpath, as that's a file collection and getting the group id from jars is more complicated than this, as well as not as exact (for example for shaded dependencies).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method iterating through the dependencies of testImplementation? y/n? You can debug this if needed with some printlines (or tagging).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, for a simple cucumber test project it's checking

annotationProcessor
compileClasspath
cucumberRuntime
runtimeClasspath
testAnnotationProcessor
testCompileClasspath
testRuntimeClasspath

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I think we only need to check the test configurations. For example, if the compileClasspath contains cucumber then it isn't necessarily being used in a test (perhaps we're building a cucumber based plugin or framework).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok since there isn't a good way to detect only the test configuration and we assume that this script will be used with a solutions engineer present this is okay.

it.resolvedConfiguration.resolvedArtifacts.any {
it.moduleVersion.id.group == 'io.cucumber'
}
}
}

Set<String> testEngines(Test t) {
try {
Stream<String> engines = t.classpath.files.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ class Capture(val api: BuildScanExtension, val logger: Logger) {
"net.jqwik.engine.JqwikTestEngine" to "jqwik",
"com.tngtech.archunit.junit.ArchUnitTestEngine" to "archunit",
"co.helmethair.scalatest.ScalatestEngine" to "scalatest",
"io.kotest.runner.junit.platform.KotestJunitPlatformTestEngine" to "kotest-runner"
"io.kotest.runner.junit.platform.KotestJunitPlatformTestEngine" to "kotest-runner",
"io.cucumber.junit.platform.engine.CucumberTestEngine" to "cucumber-junit-platform"
)

fun capturePts(t: Test) {
if (t.getTestFramework()::class.java.name == "org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestFramework") {
val engines = testEngines(t)
api.value("${t.identityPath}#engines", "${engines}")
if (!engines.isEmpty() && engines.stream().allMatch { e -> supportedEngines.containsKey(e) }) {
if (ptsSupported(t, engines)) {
api.value("${t.identityPath}#pts", "SUPPORTED")
} else {
api.value("${t.identityPath}#pts", "ENGINES_NOT_ALL_SUPPORTED")
Expand All @@ -54,6 +55,25 @@ class Capture(val api: BuildScanExtension, val logger: Logger) {
}
}

private fun ptsSupported(t: Test, engines: Set<String>): Boolean {
return if (cucumberUsed(t) && !t.project.plugins.hasPlugin("com.gradle.cucumber.companion")) {
// If cucumber is used without companion it's not supported.
false
} else if (!engines.isEmpty() && engines.stream().allMatch { e -> supportedEngines.containsKey(e) }) {
true
} else {
false
}
}

private fun cucumberUsed(t: Test): Boolean {
return t.project.configurations.filter { it.isCanBeResolved }.any {
it.resolvedConfiguration.resolvedArtifacts.any {
it.moduleVersion.id.group == "io.cucumber"
}
}
}

private fun testEngines(t: Test): Set<String> {
try {
var engines = t.classpath.files.stream().filter { f -> f.name.endsWith(".jar") }
Expand Down