Skip to content

Commit

Permalink
Fix resources exclusion in a multi-module project with git repo (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
radoslaw-panuszewski authored Jan 8, 2024
1 parent 038268d commit 441bdb6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
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")
}
rewrite {
Expand Down

0 comments on commit 441bdb6

Please sign in to comment.