-
-
Notifications
You must be signed in to change notification settings - Fork 124
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
fix: compileOnly dependencies are not visible to the test compile classpath. #1362
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...alTest/groovy/com/autonomousapps/jvm/projects/CompileOnlyTestImplementationProject.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.autonomousapps.jvm.projects | ||
|
||
import com.autonomousapps.AbstractProject | ||
import com.autonomousapps.kit.GradleProject | ||
import com.autonomousapps.kit.Source | ||
import com.autonomousapps.model.ProjectAdvice | ||
|
||
import static com.autonomousapps.AdviceHelper.actualProjectAdvice | ||
import static com.autonomousapps.AdviceHelper.emptyProjectAdviceFor | ||
import static com.autonomousapps.kit.gradle.dependencies.Dependencies.commonsCollections | ||
|
||
/** | ||
* The {@code testImplementation} configuration does not extend from the {@code compileOnly} configuration. So, it is | ||
* inaccurate to suggest removing {@code testImplementation} dependencies just because they're on the compile classpath. | ||
* | ||
* @see <a href="https://docs.gradle.org/current/userguide/java_plugin.html#resolvable_configurations">Java configurations</a> | ||
*/ | ||
final class CompileOnlyTestImplementationProject extends AbstractProject { | ||
|
||
final GradleProject gradleProject | ||
|
||
CompileOnlyTestImplementationProject() { | ||
this.gradleProject = build() | ||
} | ||
|
||
private GradleProject build() { | ||
return newGradleProjectBuilder() | ||
.withSubproject('lib') { s -> | ||
s.sources = SOURCES | ||
s.withBuildScript { bs -> | ||
bs.plugins = javaLibrary | ||
bs.dependencies = [ | ||
// These two configurations are independent! | ||
commonsCollections('compileOnly'), | ||
commonsCollections('testImplementation'), | ||
] | ||
} | ||
} | ||
.write() | ||
} | ||
|
||
private static final List<Source> SOURCES = [ | ||
Source.java( | ||
'''\ | ||
package com.example.lib; | ||
|
||
import org.apache.commons.collections4.Bag; | ||
import org.apache.commons.collections4.bag.HashBag; | ||
|
||
public class Lib { | ||
private Bag<String> newBag() { | ||
return new HashBag<>(); | ||
} | ||
} | ||
''' | ||
) | ||
.withPath('com.example.lib', 'Lib') | ||
.build(), | ||
Source.java( | ||
'''\ | ||
package com.example.lib; | ||
|
||
import org.apache.commons.collections4.Bag; | ||
import org.apache.commons.collections4.bag.HashBag; | ||
|
||
public class LibTest { | ||
private void test() { | ||
Bag<String> bag = new HashBag<>(); | ||
} | ||
} | ||
''' | ||
) | ||
.withPath('com.example.lib', 'LibTest') | ||
.withSourceSet('test') | ||
.build(), | ||
] | ||
|
||
Set<ProjectAdvice> actualBuildHealth() { | ||
return actualProjectAdvice(gradleProject) | ||
} | ||
|
||
final Set<ProjectAdvice> expectedBuildHealth = [ | ||
emptyProjectAdviceFor(':lib'), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,19 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
package com.autonomousapps.model.declaration.internal | ||
|
||
import com.autonomousapps.internal.utils.reallyAll | ||
import com.autonomousapps.model.internal.intermediates.Usage | ||
import com.squareup.moshi.JsonClass | ||
|
||
/** Standard user-facing dependency buckets (such as **implementation** and **api**), [variant][Variant]-agnostic. */ | ||
/** | ||
* Standard user-facing dependency buckets (such as **implementation** and **api**), | ||
* [variant][com.autonomousapps.model.declaration.Variant]-agnostic. | ||
*/ | ||
@JsonClass(generateAdapter = false) | ||
internal enum class Bucket(val value: String) { | ||
API("api"), | ||
IMPL("implementation"), | ||
|
||
// These configurations go into the compileOnly bucket: '...CompileOny', '...CompileOnlyApi', 'providedCompile' | ||
COMPILE_ONLY("compileOnly"), | ||
RUNTIME_ONLY("runtimeOnly"), | ||
|
@@ -37,9 +43,30 @@ internal enum class Bucket(val value: String) { | |
} | ||
|
||
/** | ||
* [Declarations][Declaration] in these buckets are visible from [SourceSetKind.MAIN] to [SourceSetKind.TEST] and | ||
* [SourceSetKind.ANDROID_TEST]. This is necessary for correct advice relating to test source. | ||
* [Declarations][Declaration] in these buckets are visible from | ||
* [SourceSetKind.MAIN][com.autonomousapps.model.declaration.SourceSetKind.MAIN] to | ||
* [SourceSetKind.TEST][com.autonomousapps.model.declaration.SourceSetKind.TEST] and | ||
* [SourceSetKind.ANDROID_TEST][com.autonomousapps.model.declaration.SourceSetKind.ANDROID_TEST]. This is necessary | ||
* for correct advice relating to test source. | ||
* | ||
* TODO(tsr): wait, is this actually true for ANNOTATION_PROCESSOR as well? That seems like an error. Oh, maybe it | ||
* was true for an older version of Gradle? | ||
*/ | ||
val VISIBLE_TO_TEST_SOURCE = listOf(API, IMPL, ANNOTATION_PROCESSOR) | ||
private val VISIBLE_TO_TEST_SOURCE = listOf(API, IMPL, ANNOTATION_PROCESSOR) | ||
|
||
/** | ||
* A dependency is visible from main to test source iff it is in the correct bucket ([VISIBLE_TO_TEST_SOURCE]) _and_ | ||
* if it is declared on either the [API] or [IMPL] configurations. | ||
* | ||
* Note that the `compileOnly` configuration _is not_ visible to the `testImplementation` configuration. | ||
* | ||
* @see <a href="https://docs.gradle.org/current/userguide/java_plugin.html#resolvable_configurations.">Java configurations</a> | ||
*/ | ||
fun isVisibleToTestSource(usages: Set<Usage>, declarations: Set<Declaration>): Boolean { | ||
return usages.reallyAll { usage -> | ||
VISIBLE_TO_TEST_SOURCE.any { it == usage.bucket } | ||
&& declarations.any { API.matches(it) || IMPL.matches(it) } | ||
} | ||
} | ||
Comment on lines
+65
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fix. |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ internal class StandardTransform( | |
{ it.variant.kind == SourceSetKind.MAIN }, | ||
{ it.variant.kind == SourceSetKind.TEST }, | ||
{ it.variant.kind == SourceSetKind.ANDROID_TEST }, | ||
{ it.variant.kind == SourceSetKind.CUSTOM_JVM } | ||
{ it.variant.kind == SourceSetKind.CUSTOM_JVM }, | ||
) | ||
|
||
val hasCustomSourceSets = hasCustomSourceSets(usages) | ||
|
@@ -51,17 +51,15 @@ internal class StandardTransform( | |
{ it.variant(supportedSourceSets, hasCustomSourceSets)?.kind == SourceSetKind.MAIN }, | ||
{ it.variant(supportedSourceSets, hasCustomSourceSets)?.kind == SourceSetKind.TEST }, | ||
{ it.variant(supportedSourceSets, hasCustomSourceSets)?.kind == SourceSetKind.ANDROID_TEST }, | ||
{ it.variant(supportedSourceSets, hasCustomSourceSets)?.kind == SourceSetKind.CUSTOM_JVM } | ||
{ it.variant(supportedSourceSets, hasCustomSourceSets)?.kind == SourceSetKind.CUSTOM_JVM }, | ||
) | ||
|
||
/* | ||
* Main usages. | ||
*/ | ||
|
||
val singleVariant = mainUsages.size == 1 | ||
val isMainVisibleDownstream = mainUsages.reallyAll { usage -> | ||
Bucket.VISIBLE_TO_TEST_SOURCE.any { it == usage.bucket } | ||
} | ||
val isMainVisibleDownstream = Bucket.isVisibleToTestSource(mainUsages, mainDeclarations) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fix. |
||
|
||
mainUsages = reduceUsages(mainUsages) | ||
computeAdvice(advice, mainUsages, mainDeclarations, singleVariant) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix.