Skip to content

Commit 31799e1

Browse files
authored
Code review clarification: withType<Test> (#18092)
1 parent 53c4ab3 commit 31799e1

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

.github/agents/knowledge/gradle-conventions.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ module it extends) imports or instantiates Testcontainers types (`GenericContain
188188
rely solely on the presence of an `org.testcontainers:*` dependency in `build.gradle.kts`,
189189
because the dependency may only be used by some suites in the module.
190190

191-
## Prefer `withType<Test>().configureEach` (when multiple test tasks exist)
191+
## Prefer `withType<Test>().configureEach` (ONLY when multiple test tasks exist)
192192

193193
When a module has custom test tasks (e.g., `testStableSemconv`), system properties and JVM
194194
args that apply to **all** test tasks should be set once in a `withType<Test>().configureEach`
@@ -197,13 +197,24 @@ block, not repeated on each individual task.
197197
If a property or JVM arg is moved into `withType<Test>().configureEach`, remove any now-redundant
198198
copies from individual tasks unless a task intentionally overrides the shared value.
199199

200-
When there is only one test task, `tasks.test { ... }` is fine — do not convert it to
201-
`withType<Test>().configureEach` and do not flag it.
200+
When the module's `build.gradle.kts` does not explicitly register additional `Test` tasks,
201+
`tasks.test { ... }` is fine — **do not** convert it to `withType<Test>().configureEach` and
202+
do not flag it.
202203

203-
**How to spot violations:** If `build.gradle.kts` has both a `test { ... }` block and a
204-
custom test task (e.g., `val testStableSemconv by registering(Test::class)`), check whether
205-
any `systemProperty(...)` or `jvmArgs(...)` calls appear inside the `test { }` block that
206-
should apply to all tasks. If so, move them to `withType<Test>().configureEach`.
204+
**`latestDepTest` does not count as a second test task for this rule.** It is registered
205+
implicitly by the convention plugin when `testLatestDeps` is set, and it inherits the
206+
configuration of `tasks.test`. A module with only a `tasks.test { ... }` block and no
207+
`by registering(Test::class)` declarations is a single-test-task module — leave it alone
208+
even if `testLatestDeps = true`.
209+
210+
Only consider converting to `withType<Test>().configureEach` when the **same
211+
`build.gradle.kts`** explicitly registers one or more additional `Test` tasks via
212+
`val foo by registering(Test::class)`.
213+
214+
**How to spot violations:** If `build.gradle.kts` has both a `test { ... }` block and an
215+
explicit `by registering(Test::class)` custom test task (e.g., `testStableSemconv`), check
216+
whether any `systemProperty(...)` or `jvmArgs(...)` calls appear inside the `test { }` block
217+
that should apply to all tasks. If so, move them to `withType<Test>().configureEach`.
207218

208219
```kotlin
209220
tasks {
@@ -232,7 +243,8 @@ review**. Only verify correctness when they are already present.
232243

233244
When already present, verify:
234245

235-
- `collectMetadata` is in `withType<Test>().configureEach` (or `tasks.test` if only one test
236-
task) — never on individual tasks.
246+
- `collectMetadata` is in `withType<Test>().configureEach` (or `tasks.test` if the module
247+
does not explicitly register additional `Test` tasks — `latestDepTest` does not count) —
248+
never on individual tasks.
237249
- `metadataConfig` is on each non-default task, not on the default `test` task.
238250
- The `metadataConfig` value matches at least one of the jvmArgs configured in the task

0 commit comments

Comments
 (0)