-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not filter out advice to add to testImplementation if there i…
…s conflicting advice to downgrade from implementation.
- Loading branch information
1 parent
97cb447
commit fcab399
Showing
4 changed files
with
141 additions
and
10 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/functionalTest/groovy/com/autonomousapps/jvm/RuntimeOnlySpec.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,34 @@ | ||
package com.autonomousapps.jvm | ||
|
||
import com.autonomousapps.jvm.projects.ImplRuntimeTestImplConfusionProject | ||
import com.autonomousapps.utils.Colors | ||
|
||
import static com.autonomousapps.utils.Runner.build | ||
import static com.google.common.truth.Truth.assertThat | ||
|
||
final class RuntimeOnlySpec extends AbstractJvmSpec { | ||
|
||
def "don't suggest implementation to runtimeOnly when used for testImplementation (#gradleVersion)"() { | ||
given: | ||
def project = new ImplRuntimeTestImplConfusionProject() | ||
gradleProject = project.gradleProject | ||
when: | ||
def result = build( | ||
gradleVersion, gradleProject.rootDir, | ||
'buildHealth', | ||
':lib:reason', '--id', ImplRuntimeTestImplConfusionProject.SPARK_SQL_ID, | ||
) | ||
then: 'advice is correct' | ||
assertThat(project.actualBuildHealth()).containsExactlyElementsIn(project.expectedBuildHealth) | ||
and: 'reason makes sense' | ||
def output = Colors.decolorize(result.output) | ||
assertThat(output).contains('You have been advised to change this dependency to \'runtimeOnly\' from \'implementation\'.') | ||
assertThat(output).contains('There is no advice regarding this dependency.') | ||
where: | ||
gradleVersion << gradleVersions() | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...nalTest/groovy/com/autonomousapps/jvm/projects/ImplRuntimeTestImplConfusionProject.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,89 @@ | ||
package com.autonomousapps.jvm.projects | ||
|
||
import com.autonomousapps.AbstractProject | ||
import com.autonomousapps.kit.GradleProject | ||
import com.autonomousapps.kit.Source | ||
import com.autonomousapps.model.Advice | ||
import com.autonomousapps.model.ProjectAdvice | ||
|
||
import static com.autonomousapps.AdviceHelper.actualProjectAdvice | ||
import static com.autonomousapps.AdviceHelper.emptyProjectAdviceFor | ||
import static com.autonomousapps.AdviceHelper.moduleCoordinates | ||
import static com.autonomousapps.AdviceHelper.projectAdviceForDependencies | ||
import static com.autonomousapps.kit.gradle.Dependency.implementation | ||
|
||
/** | ||
* A dependency declared on {@code implementation} is visible to {@code testImplementation}, and so we don't typically | ||
* advise to add a declaration on the latter. However, in the case where the dependency is not used in main source, and | ||
* that dependency also provides runtime capabilities (such as a service loader), the algorithm must not suggest moving | ||
* the declaration to {@code runtimeOnly} without also adding it to {@code testImplementation} (since the former is not | ||
* visible to the latter). So, this is a case where a single declaration, combined with two usages (runtimeOnly and | ||
* testImplementation), must result in two pieces of advice: <strong>change</strong> {@code implementation} -> | ||
* {@code runtimeOnly} and <strong>add</strong> to {@code testImplementation}. | ||
* | ||
* @see <a href="https://docs.gradle.org/current/userguide/java_plugin.html#resolvable_configurations">Java configurations</a> | ||
*/ | ||
final class ImplRuntimeTestImplConfusionProject extends AbstractProject { | ||
|
||
final GradleProject gradleProject | ||
|
||
static final SPARK_SQL_ID = "org.apache.spark:spark-sql_2.12" | ||
private final spark = implementation("$SPARK_SQL_ID:3.5.0") | ||
|
||
ImplRuntimeTestImplConfusionProject() { | ||
this.gradleProject = build() | ||
} | ||
|
||
private GradleProject build() { | ||
return newGradleProjectBuilder() | ||
.withSubproject('lib') { s -> | ||
s.sources = SOURCES | ||
s.withBuildScript { bs -> | ||
bs.plugins = javaLibrary | ||
bs.dependencies(spark) | ||
} | ||
} | ||
.write() | ||
} | ||
|
||
private static final List<Source> SOURCES = [ | ||
Source.java( | ||
'''\ | ||
package com.example.lib; | ||
public class Lib {} | ||
''' | ||
) | ||
.withPath('com.example.lib', 'Lib') | ||
.build(), | ||
Source.java( | ||
'''\ | ||
package com.example.lib; | ||
import org.apache.spark.sql.SparkSession; | ||
public class LibTest { | ||
private void test() { | ||
SparkSession.builder().getOrCreate(); | ||
} | ||
} | ||
''' | ||
) | ||
.withPath('com.example.lib', 'LibTest') | ||
.withSourceSet('test') | ||
.build(), | ||
] | ||
|
||
Set<ProjectAdvice> actualBuildHealth() { | ||
return actualProjectAdvice(gradleProject) | ||
} | ||
|
||
private final Set<Advice> libAdvice = [ | ||
Advice.ofChange(moduleCoordinates(spark), 'implementation', 'runtimeOnly'), | ||
Advice.ofAdd(moduleCoordinates(spark), 'testImplementation'), | ||
] | ||
|
||
final Set<ProjectAdvice> expectedBuildHealth = [ | ||
projectAdviceForDependencies(':lib', libAdvice), | ||
] | ||
} |
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
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