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

Issue/fix gradle filter #3257

Merged
merged 4 commits into from
Oct 23, 2022
Merged

Issue/fix gradle filter #3257

merged 4 commits into from
Oct 23, 2022

Conversation

myuwono
Copy link
Contributor

@myuwono myuwono commented Oct 22, 2022

In this PR

Patterns that works

tested against gradle 7.5.1, using both filters defined in build.gradle.kts and command line:

  • gradle test --tests "SomeTest"
  • gradle test --tests "*Test"
  • gradle test --tests "io.package.*"
  • gradle test --tests "io.package"
  • gradle test --tests "io.package.SomeTest"
  • gradle test --tests "io.package.SomeTest.first level context*"
  • gradle test --tests "io.package.SomeTest.*"
  • gradle test --tests "io.*.SomeTest"
  • gradle test --tests "io.*.SomeTest.*"
  • gradle test --tests "SomeTest.first level context*"
  • gradle test --tests "*.first level context*"

Pattern that does NOT work

In this PR I decided to omit the support for nested context / test matching. It's arguably quite involved so I'd reserve it for future contribution. Kotest support lazy test registration within nested context. Gradle test filter does not natively work nicely with kotest. In order to make it work we need to think of a way to recursively apply partial context-search as we dive deeper into the contexts. I.e. These patterns DOES NOT work yet:

  • gradle test --tests "SomeTest.first level context -- should assert this test description"
  • gradle test --tests "SomeTest.first level context -- nested context -- *"
  • gradle test --tests "SomeTest.first level context -- nested context -- yet another nested context -- *"

Mechanism

Gradle supplies a pattern string which corresponds to a well-formed regex object. This can be directly usable for kotest.

  • A* becomes \QA\E.*
  • A*Test becomes \QA\E.*\QTest\E
  • io.*.A*Test becomes \Qio.\E.*\Q.A\E.*\QTest\E
  • io.*.A*Test.AccountDetails* becomes \Qio.\E.*\Q.A\E.*\QTest.AccountDetails\E.*
  • io.*.A*Test.some test context* becomes \Qio.\E.*\Q.A\E.*\QTest.some test context\E.*

Within the code we get this pattern string and directly translate them into a regex object to be used against the path / classname / packagename as appropriate.

@@ -52,6 +59,12 @@ object GradlePostDiscoveryFilterExtractor {
return field.get(obj) as List<Any>
}

private fun buildScriptIncludePatterns(obj: Any): List<Any> {
val field = obj::class.java.getDeclaredField("buildScriptIncludePatterns")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added support for filters defined in build script

Copy link
Member

Choose a reason for hiding this comment

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

Are there build script exclude patterns as well? Not saying we need to fix it now, but could be flagged up as a potential future contribution

val isPackageMatched by lazy { doesNotContainUppercase && packagePath.matches(laxRegexPattern) } // io.kotest
val isPackageWithDotMatched by lazy { doesNotContainUppercase && "$packagePath.".matches(laxRegexPattern) } // io.kotest.*

return isSimpleClassMatch ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sksamuel @Kantis this is the change

@Kantis Kantis added this to the 5.5.2 milestone Oct 23, 2022
@@ -52,6 +59,12 @@ object GradlePostDiscoveryFilterExtractor {
return field.get(obj) as List<Any>
}

private fun buildScriptIncludePatterns(obj: Any): List<Any> {
val field = obj::class.java.getDeclaredField("buildScriptIncludePatterns")
Copy link
Member

Choose a reason for hiding this comment

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

Are there build script exclude patterns as well? Not saying we need to fix it now, but could be flagged up as a potential future contribution

Comment on lines +14 to +25
withData(
nameFn = { filters -> "should be INCLUDED when evaluating $filters" },
listOf("\\QGradleClassMethodRegexTestFilterTest\\E"),
listOf(".*\\QthodRegexTestFilterTest\\E"),
listOf(".*\\QTest\\E"),
listOf("\\Qio.kotest.runner.junit.platform.gradle.GradleClassMethodRegexTestFilterTest\\E"),
listOf(".*\\Q.platform.gradle.GradleClassMethodRegexTestFilterTest\\E"),
listOf(".*\\Qorm.gradle.GradleClassMethodRegexTestFilterTest\\E")
) { filters ->
GradleClassMethodRegexTestFilter(filters).filter(spec) shouldBe TestFilterResult.Include
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Nice :)

@Kantis Kantis merged commit 5db3e46 into master Oct 23, 2022
@Kantis Kantis deleted the issue/fix-gradle-filter branch October 23, 2022 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gradle test regex are incorrectly applied Kotest should respect test filter { } defined in gradle.build.kts
2 participants