From 2553b5485bb82a7fd03a427902e85527c177f6ef Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Fri, 22 Sep 2023 17:26:43 -0700 Subject: [PATCH] Fix #231 non-source code files in subprojects parsed multiple times with incorrect paths --- .../gradle/isolated/DefaultProjectParser.java | 25 +++++++++++-------- .../org/openrewrite/gradle/RewriteRunTest.kt | 21 +++++++++++++--- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java b/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java index 8afe696f9..0442d6566 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java +++ b/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java @@ -268,7 +268,7 @@ private static StringBuilder repeat(int repeat) { public Collection listSources() { // Use a sorted collection so that gradle input detection isn't thrown off by ordering - Set result = new TreeSet<>(omniParser(emptySet()).acceptedPaths(project.getProjectDir().toPath())); + Set result = new TreeSet<>(omniParser(emptySet()).build().acceptedPaths(baseDir, project.getProjectDir().toPath())); //noinspection deprecation JavaPluginConvention javaConvention = project.getConvention().findPlugin(JavaPluginConvention.class); if (javaConvention != null) { @@ -292,6 +292,7 @@ public void dryRun(Path reportPath, boolean dumpGcActivity, Consumer heapMetrics.bindTo(meterRegistry); new JvmMemoryMetrics().bindTo(meterRegistry); + //noinspection deprecation File rewriteBuildDir = new File(project.getBuildDir(), "/rewrite"); if (rewriteBuildDir.exists() || rewriteBuildDir.mkdirs()) { File rewriteGcLog = new File(rewriteBuildDir, "rewrite-gc.csv"); @@ -715,6 +716,7 @@ public Stream parse(Project subproject, Set alreadyParsed, Exe .map(Supplier::get) .flatMap(jp -> jp.parse(javaPaths, baseDir, ctx)) .map(cu -> { + //noinspection deprecation if (isExcluded(exclusions, cu.getSourcePath()) || cu.getSourcePath().startsWith(baseDir.relativize(subproject.getBuildDir().toPath()))) { return null; @@ -804,12 +806,13 @@ public Stream parse(Project subproject, Set alreadyParsed, Exe for (File resourcesDir : sourceSet.getResources().getSourceDirectories()) { if (resourcesDir.exists()) { - OmniParser omniParser = omniParser(alreadyParsed); - List accepted = omniParser.acceptedPaths(resourcesDir.toPath()); + OmniParser omniParser = omniParser(alreadyParsed).build(); + List accepted = omniParser.acceptedPaths(baseDir, resourcesDir.toPath()); sourceSetSourceFiles = Stream.concat( sourceSetSourceFiles, - omniParser.parse(accepted, resourcesDir.toPath(), new InMemoryExecutionContext()) + omniParser.parse(accepted, baseDir, new InMemoryExecutionContext()) ); + alreadyParsed.addAll(accepted); sourceSetSize += accepted.size(); } } @@ -936,13 +939,14 @@ private SourceFileStream parseGradleFiles( protected SourceFileStream parseNonProjectResources(Project subproject, Set alreadyParsed, ExecutionContext ctx, List projectProvenance, Stream sourceFiles) { //Collect any additional yaml/properties/xml files that are NOT already in a source set. - OmniParser omniParser = omniParser(alreadyParsed); - List accepted = omniParser.acceptedPaths(subproject.getProjectDir().toPath()); - return SourceFileStream.build("", s -> { - }).concat(omniParser.parse(accepted, subproject.getProjectDir().toPath(), ctx), accepted.size()); + OmniParser omniParser = omniParser(alreadyParsed) + .build(); + List accepted = omniParser.acceptedPaths(baseDir, subproject.getProjectDir().toPath()); + return SourceFileStream.build("", s -> {}) + .concat(omniParser.parse(accepted, baseDir, ctx), accepted.size()); } - private OmniParser omniParser(Set alreadyParsed) { + private OmniParser.Builder omniParser(Set alreadyParsed) { return OmniParser.builder( OmniParser.RESOURCE_PARSERS, PlainTextParser.builder() @@ -952,8 +956,7 @@ private OmniParser omniParser(Set alreadyParsed) { ) .exclusionMatchers(pathMatchers(baseDir, mergeExclusions(project, baseDir, extension))) .exclusions(alreadyParsed) - .sizeThresholdMb(extension.getSizeThresholdMb()) - .build(); + .sizeThresholdMb(extension.getSizeThresholdMb()); } private static Collection mergeExclusions(Project project, Path baseDir, RewriteExtension extension) { diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt index 41cd37646..fd3052a85 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt @@ -156,12 +156,12 @@ class RewriteRunTest : RewritePluginTest { } @Test - fun `rewriteRun applies built-in AutoFormat to a multi-project build`( + fun `rewriteRun applies recipe to a multi-project build`( @TempDir projectDir: File ) { val bTestClassExpected = """ package com.foo; - + import org.junit.Test; public class BTestClass { @@ -171,6 +171,15 @@ class RewriteRunTest : RewritePluginTest { } """.trimIndent() gradleProject(projectDir) { + rewriteYaml(""" + type: specs.openrewrite.org/v1beta/recipe + name: org.openrewrite.FormatAndAddProperty + recipeList: + - org.openrewrite.java.format.AutoFormat + - org.openrewrite.properties.ChangePropertyKey: + oldPropertyKey: foo + newPropertyKey: bar + """) buildGradle(""" plugins { id("org.openrewrite.rewrite") @@ -178,7 +187,7 @@ class RewriteRunTest : RewritePluginTest { } rewrite { - activeRecipe("org.openrewrite.java.format.AutoFormat") + activeRecipe("org.openrewrite.FormatAndAddProperty") exclusion("**/BTestClass.java") } @@ -216,6 +225,7 @@ class RewriteRunTest : RewritePluginTest { public void passes() { } } """) + propertiesFile("test.properties", "foo=baz\n") } } subproject("b") { @@ -231,7 +241,7 @@ class RewriteRunTest : RewritePluginTest { //language=java val aTestClassExpected = """ package com.foo; - + import org.junit.Test; public class ATestClass { @@ -245,6 +255,9 @@ class RewriteRunTest : RewritePluginTest { assertThat(aTestClassFile.readText()).isEqualTo(aTestClassExpected) val bTestClassFile = File(projectDir, "b/src/test/java/com/foo/BTestClass.java") assertThat(bTestClassFile.readText()).isEqualTo(bTestClassExpected) + + val propertiesFile = File(projectDir, "a/src/test/resources/test.properties") + assertThat(propertiesFile.readText()).isEqualTo("bar=baz\n") } @Suppress("ClassInitializerMayBeStatic", "StatementWithEmptyBody", "ConstantConditions")