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

Fix resources exclusion in a multi-module project with git repo #257

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private static StringBuilder repeat(int repeat) {

public Collection<Path> listSources() {
// Use a sorted collection so that gradle input detection isn't thrown off by ordering
Set<Path> result = new TreeSet<>(omniParser(emptySet()).acceptedPaths(baseDir, project.getProjectDir().toPath()));
Set<Path> result = new TreeSet<>(omniParser(emptySet(), project).acceptedPaths(baseDir, project.getProjectDir().toPath()));
//noinspection deprecation
JavaPluginConvention javaConvention = project.getConvention().findPlugin(JavaPluginConvention.class);
if (javaConvention != null) {
Expand Down Expand Up @@ -809,7 +809,7 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe

for (File resourcesDir : sourceSet.getResources().getSourceDirectories()) {
if (resourcesDir.exists() && !alreadyParsed.contains(resourcesDir.toPath())) {
OmniParser omniParser = omniParser(alreadyParsed);
OmniParser omniParser = omniParser(alreadyParsed, subproject);
List<Path> accepted = omniParser.acceptedPaths(baseDir, resourcesDir.toPath());
sourceSetSourceFiles = Stream.concat(
sourceSetSourceFiles,
Expand Down Expand Up @@ -958,7 +958,7 @@ private SourceFileStream parseGradleWrapperFiles(Collection<PathMatcher> exclusi
Stream<SourceFile> sourceFiles = Stream.empty();
int fileCount = 0;
if (project == project.getRootProject()) {
OmniParser omniParser = omniParser(alreadyParsed);
OmniParser omniParser = omniParser(alreadyParsed, project);
List<Path> gradleWrapperFiles = Stream.of(
"gradlew", "gradlew.bat",
"gradle/wrapper/gradle-wrapper.jar",
Expand All @@ -978,13 +978,13 @@ private SourceFileStream parseGradleWrapperFiles(Collection<PathMatcher> exclusi

protected SourceFileStream parseNonProjectResources(Project subproject, Set<Path> alreadyParsed, ExecutionContext ctx, List<Marker> projectProvenance, Stream<SourceFile> sourceFiles) {
//Collect any additional yaml/properties/xml files that are NOT already in a source set.
OmniParser omniParser = omniParser(alreadyParsed);
OmniParser omniParser = omniParser(alreadyParsed, subproject);
List<Path> accepted = omniParser.acceptedPaths(baseDir, subproject.getProjectDir().toPath());
return SourceFileStream.build("", s -> {
}).concat(omniParser.parse(accepted, baseDir, ctx), accepted.size());
}

private OmniParser omniParser(Set<Path> alreadyParsed) {
private OmniParser omniParser(Set<Path> alreadyParsed, Project project) {
return OmniParser.builder(
OmniParser.defaultResourceParsers(),
PlainTextParser.builder()
Expand Down
12 changes: 12 additions & 0 deletions plugin/src/main/kotlin/org/openrewrite/gradle/GradleProjectSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,15 @@ class GradleSourceSetSpec(
fun gradleProject(dir: File, init: GradleProjectSpec.()->Unit): GradleProjectSpec {
return GradleProjectSpec(dir).apply(init).build()
}

fun commitFilesToGitRepo(dir: File) {
exec("git init", dir)
exec("git config user.email [email protected]", dir)
exec("git config user.name TestUser", dir)
exec("git add .", dir)
exec("git commit -m \"Initial commit\"", dir)
}

private fun exec(command: String, workingDirectory: File) {
Runtime.getRuntime().exec(command, null, workingDirectory)
}
66 changes: 64 additions & 2 deletions plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import org.junit.jupiter.api.condition.DisabledIf
import org.junit.jupiter.api.condition.DisabledOnOs
import org.junit.jupiter.api.condition.OS
import org.junit.jupiter.api.io.TempDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.openrewrite.Issue
import java.io.File

Expand Down Expand Up @@ -259,6 +261,66 @@ class RewriteRunTest : RewritePluginTest {
assertThat(propertiesFile.readText()).isEqualTo("bar=baz\n")
}

@Test
fun `resources in subproject committed to git are correctly processed`(
@TempDir projectDir: File
) {
gradleProject(projectDir) {
rewriteYaml("""
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.ChangeFooToBar
recipeList:
- org.openrewrite.properties.ChangePropertyKey:
oldPropertyKey: foo
newPropertyKey: bar
""")
buildGradle("""
plugins {
id("org.openrewrite.rewrite")
id("java")
}

rewrite {
activeRecipe("org.openrewrite.ChangeFooToBar")
}

repositories {
mavenLocal()
mavenCentral()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
}
}

subprojects {
apply plugin: "java"

repositories {
mavenCentral()
}

dependencies {
implementation(project(":"))
implementation("junit:junit:4.12")
}
}
""".trimIndent())
subproject("a") {
sourceSet("main") {
propertiesFile("test.properties", "foo=baz\n")
}
}
}
commitFilesToGitRepo(projectDir)

val result = runGradle(projectDir, "rewriteRun")
val rewriteRunResult = result.task(":rewriteRun")!!
assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS)

val propertiesFile = File(projectDir, "a/src/main/resources/test.properties")
assertThat(propertiesFile.readText()).isEqualTo("bar=baz\n")
}

@Suppress("ClassInitializerMayBeStatic", "StatementWithEmptyBody", "ConstantConditions")
@Test
fun `Checkstyle configuration is applied as a style`(
Expand Down Expand Up @@ -531,7 +593,7 @@ class RewriteRunTest : RewritePluginTest {
- org.openrewrite.gradle.UpgradeDependencyVersion:
groupId: com.fasterxml.jackson.core
artifactId: jackson-core
nevVersion: 2.16.0
newVersion: 2.16.0
""")
settingsGradle("""
dependencyResolutionManagement {
Expand Down Expand Up @@ -573,7 +635,7 @@ class RewriteRunTest : RewritePluginTest {
}

dependencies {
implementation("com.fasterxml.jackson.core:jackson-core:2.16.1")
implementation("com.fasterxml.jackson.core:jackson-core:2.16.0")
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}

rewrite {
Expand Down