Skip to content

Commit

Permalink
Merge pull request #59 from chali/FixSourceSetsInheritance
Browse files Browse the repository at this point in the history
Properly inherit outpus of parent source sets
  • Loading branch information
chali authored Jan 23, 2020
2 parents 52d708d + 5905f80 commit 9143c9b
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/main/groovy/nebula/plugin/responsible/NebulaFacetPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,26 @@ class NebulaFacetPlugin implements Plugin<Project> {
SourceSetContainer sourceSets = javaConvention.sourceSets
sourceSets.create(set.name) { SourceSet sourceSet ->
//our new source set needs to see compiled classes from its parent
sourceSet.compileClasspath += parentSourceSet.output
//the parent can be also inheriting so we need to extract all the output from previous parents
//e.g smokeTest inherits from test which inherits from main and we need to see classes from main
extractAllOutputs(parentSourceSet.compileClasspath).each {
sourceSet.compileClasspath += it
}
Set<Object> compileClasspath = extractAllOutputs(parentSourceSet.compileClasspath)
compileClasspath.add(parentSourceSet.output)
compileClasspath.add(sourceSet.compileClasspath)
//we are using from to create ConfigurableFileCollection so if we keep inhering from created facets we can
//still extract chain of output from all parents
sourceSet.compileClasspath = project.objects.fileCollection().from(compileClasspath as Object[])
//runtime classpath of parent already has parent output so we don't need to explicitly add it
extractAllOutputs(parentSourceSet.runtimeClasspath).each {
sourceSet.runtimeClasspath += it
}
Set<Object> runtimeClasspath = extractAllOutputs(parentSourceSet.runtimeClasspath)
runtimeClasspath.add(sourceSet.runtimeClasspath)
sourceSet.runtimeClasspath = project.objects.fileCollection().from(runtimeClasspath as Object[])
}
}

private static Set<SourceSetOutput> extractAllOutputs(FileCollection classpath) {
private static Set<Object> extractAllOutputs(FileCollection classpath) {
if (classpath instanceof ConfigurableFileCollection) {
(classpath as ConfigurableFileCollection).from.findAll { it instanceof SourceSetOutput} as Set<SourceSetOutput>
(classpath as ConfigurableFileCollection).from.findAll { it instanceof SourceSetOutput} as Set<Object>
} else {
Collections.emptySet()
new HashSet<Object>()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,140 @@ ${applyPlugin(NebulaFacetPlugin)}
result.standardOutput.contains(":functionalTestClasses SKIPPED")
}

def 'makes sure we can have a parent source set with was created by facet plugin'() {
createFile('src/test/resources/application.yml') << """
spring:
application.name: myapp
logging:
level:
org.springframework.web: DEBUG
"""

createFile('src/test/resources/bootstrap.yml') << """
spring:
application.name: myapp
logging:
level:
org.springframework.web: DEBUG
"""
createFile('src/main/java/com/netflix/Application.java') << """
package com.netflix;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
}
"""



createFile('src/specializedSmokeTest/java/com/netflix/HelloTest.java') << """
package com.netflix;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloTest {
@LocalServerPort
private int port;
private URL base;
@Autowired
private TestRestTemplate template;
@Before
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
}
@Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(),
String.class);
assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
}
}
"""


buildFile << """
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
${applyPlugin(NebulaFacetPlugin)}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("junit:junit")
}
facets {
smokeTest {
parentSourceSet = 'test'
}
specializedSmokeTest {
parentSourceSet = 'smokeTest'
}
}
"""

when:
def result = runTasksSuccessfully( 'specializedSmokeTest' )

then:
result.wasExecuted(':specializedSmokeTest')
}

}

0 comments on commit 9143c9b

Please # to comment.