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

Inject services into all external module dependencies created by DefultJvmComponentDependencies #23535

Merged
merged 2 commits into from Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -130,7 +130,9 @@ private void configureModule(ClientModule clientModule, Closure configureClosure

@Override
public ExternalModuleDependency create(CharSequence dependencyNotation) {
return dependencyNotationParser.getStringNotationParser().parseNotation(dependencyNotation.toString());
ExternalModuleDependency dependency = dependencyNotationParser.getStringNotationParser().parseNotation(dependencyNotation.toString());
injectServices(dependency);
return dependency;
}

@Override
Expand All @@ -142,6 +144,7 @@ public ExternalModuleDependency create(@Nullable String group, String name, @Nul
public ExternalModuleDependency create(@Nullable String group, String name, @Nullable String version, @Nullable String classifier, @Nullable String extension) {
DefaultExternalModuleDependency dependency = instantiator.newInstance(DefaultExternalModuleDependency.class, group, name, version);
ModuleFactoryHelper.addExplicitArtifactsIfDefined(dependency, extension, classifier);
injectServices(dependency);
return dependency;
}

Expand All @@ -152,7 +155,9 @@ public FileCollectionDependency create(FileCollection fileCollection) {

@Override
public ProjectDependency create(Project project) {
return dependencyNotationParser.getProjectNotationParser().parseNotation(project);
ProjectDependency dependency = dependencyNotationParser.getProjectNotationParser().parseNotation(project);
injectServices(dependency);
return dependency;
}

// endregion
Expand Down
Expand Up @@ -782,4 +782,199 @@ class TestSuitesIntegrationTest extends AbstractIntegrationSpec {
failure.assertHasErrorOutput("Compilation failed; see the compiler error output for details.")
failure.assertHasErrorOutput("error: package org.junit does not exist")
}

@Issue("https://github.com/gradle/gradle/issues/19065")
def "test suites can add platforms using a #platformType with #format via coordinates"() {
given: "a project defining a platform"
file('platform/build.gradle') << """
plugins {
id 'java-platform'
}

group = "org.example.gradle"

dependencies {
constraints {
api 'org.assertj:assertj-core:3.22.0'
}
}
"""

and: "an application project with a test suite using the platform"
file('app/build.gradle') << """
plugins {
id 'java'
}

${mavenCentralRepository()}

testing {
suites {
test {
useJUnitJupiter()

dependencies {
implementation($expression)
implementation 'org.assertj:assertj-core'
}
}
}
}
"""
file('app/src/test/java/org/example/app/ExampleTest.java') << """
package org.example.app;

import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;

public class ExampleTest {
@Test public void testOK() {
assertThat(1 + 1).isEqualTo(2);
}
}
"""

settingsFile << """
dependencyResolutionManagement {
includeBuild("platform")
}

rootProject.name = 'example-of-platform-in-test-suites'

include("app")
"""

expect: "should be able to reference the platform without failing"
succeeds ':app:test'
def unitTestResults = new JUnitXmlTestExecutionResult(testDirectory, 'app/build/test-results/test')
unitTestResults.assertTestClassesExecuted('org.example.app.ExampleTest')

where:
format | platformType | expression
'single GAV string' | 'platform' | "platform('org.example.gradle:platform')"
'module method' | 'platform' | "platform(module('org.example.gradle', 'platform', null))"
'referencing project.dependencies' | 'platform' | "project.dependencies.platform('org.example.gradle:platform')"
'single GAV string' | 'enforcedPlatform' | "enforcedPlatform('org.example.gradle:platform')"
'module method' | 'enforcedPlatform' | "enforcedPlatform(module('org.example.gradle', 'platform', null))"
'referencing project.dependencies' | 'enforcedPlatform' | "project.dependencies.enforcedPlatform('org.example.gradle:platform')"
}

@Issue("https://github.com/gradle/gradle/issues/19065")
def "test suites can add project dependencies via coordinates"() {
given: "a project used as a dependency"
file('dep/build.gradle') << """
plugins {
id 'java-library'
}

group = "org.example.gradle"
"""
file('dep/src/main/java/org/example/dep/Dep.java') << """
package org.example.dep;
public class Dep {}
"""

and: "an application project with a test suite using a project dependency"
file('app/build.gradle') << """
plugins {
id 'java'
}

${mavenCentralRepository()}

testing {
suites {
test {
useJUnitJupiter()

dependencies {
implementation('org.example.gradle:dep')
}
}
}
}
"""
file('app/src/test/java/org/example/app/ExampleTest.java') << """
package org.example.app;

import org.junit.jupiter.api.Test;
import org.example.dep.Dep;

public class ExampleTest {
@Test public void testOK() {
new Dep();
}
}
"""

settingsFile << """
dependencyResolutionManagement {
includeBuild("dep")
}

rootProject.name = 'example-of-project-reference-in-test-suites'

include("app")
"""

expect: "should be able to reference the project without failing"
succeeds ':app:test'
def unitTestResults = new JUnitXmlTestExecutionResult(testDirectory, 'app/build/test-results/test')
unitTestResults.assertTestClassesExecuted('org.example.app.ExampleTest')
}

@Issue("https://github.com/gradle/gradle/issues/19065")
def "test suites can add self project dependency via coordinates"() {
given: "an application project with a custom test suite with a dependency on the project"
file('app/src/main/java/org/example/dep/Dep.java') << """
package org.example.dep;
public class Dep {}
"""
file('app/build.gradle') << """
plugins {
id 'java'
}

${mavenCentralRepository()}

group = "org.example.gradle"
version = "1.0"

testing {
suites {
integrationTest(JvmTestSuite) {
useJUnitJupiter()

dependencies {
implementation('org.example.gradle:app:1.0')
}
}
}
}
"""
file('app/src/integrationTest/java/org/example/app/ExampleTest.java') << """
package org.example.app;

import org.junit.jupiter.api.Test;
import org.example.dep.Dep;

public class ExampleTest {
@Test public void testOK() {
new Dep();
}
}
"""

settingsFile << """
rootProject.name = 'example-of-project-reference-in-test-suites'

include("app")
"""
executer.noDeprecationChecks()

expect: "should be able to reference the project without failing"
succeeds ':app:assemble', ':app:integrationTest'
def unitTestResults = new JUnitXmlTestExecutionResult(testDirectory, 'app/build/test-results/integrationTest')
unitTestResults.assertTestClassesExecuted('org.example.app.ExampleTest')
}
}