From 368b25366c3415d3f938b1e7a3b8096d4477ae99 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 13 Nov 2023 10:51:34 +0100 Subject: [PATCH] CreateTextFile should override files of different type (#3682) * CreateTextFile should override files of different type * Address review comments to override any file --- .../org/openrewrite/text/CreateTextFile.java | 28 +++++++++++++----- rewrite-test/build.gradle.kts | 2 ++ .../openrewrite/text/CreateTextFileTest.java | 29 +++++++++++++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java b/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java index 2e3ecba140b..f218d03aba9 100644 --- a/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java +++ b/rewrite-core/src/main/java/org/openrewrite/text/CreateTextFile.java @@ -71,7 +71,7 @@ public TreeVisitor getScanner(AtomicBoolean shouldCreate) { @Override public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { SourceFile sourceFile = (SourceFile) requireNonNull(tree); - if (path.toString().equals(sourceFile.getSourcePath().toString())) { + if (path.equals(sourceFile.getSourcePath())) { shouldCreate.set(false); } return sourceFile; @@ -81,7 +81,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { @Override public Collection generate(AtomicBoolean shouldCreate, ExecutionContext ctx) { - if(shouldCreate.get()) { + if (shouldCreate.get()) { return PlainTextParser.builder().build().parse(fileContents) .map(brandNewFile -> (SourceFile) brandNewFile.withSourcePath(Paths.get(relativeFileName))) .collect(Collectors.toList()); @@ -92,13 +92,27 @@ public Collection generate(AtomicBoolean shouldCreate, ExecutionCont @Override public TreeVisitor getVisitor(AtomicBoolean created) { Path path = Paths.get(relativeFileName); - return new PlainTextVisitor() { + return new TreeVisitor() { @Override - public PlainText visitText(PlainText text, ExecutionContext ctx) { - if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.toString().equals(text.getSourcePath().toString())) { - return text.withText(fileContents); + public SourceFile visit(@Nullable Tree tree, ExecutionContext executionContext) { + SourceFile sourceFile = (SourceFile) requireNonNull(tree); + if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.equals(sourceFile.getSourcePath())) { + if (sourceFile instanceof PlainText) { + return ((PlainText) sourceFile).withText(fileContents); + } + PlainText plainText = PlainText.builder() + .id(sourceFile.getId()) + .sourcePath(sourceFile.getSourcePath()) + .fileAttributes(sourceFile.getFileAttributes()) + .charsetBomMarked(sourceFile.isCharsetBomMarked()) + .text(fileContents) + .build(); + if (sourceFile.getCharset() != null) { + return plainText.withCharset(sourceFile.getCharset()); + } + return plainText; } - return text; + return sourceFile; } }; } diff --git a/rewrite-test/build.gradle.kts b/rewrite-test/build.gradle.kts index e7f48222158..dc57aa0ed8a 100644 --- a/rewrite-test/build.gradle.kts +++ b/rewrite-test/build.gradle.kts @@ -12,4 +12,6 @@ dependencies { implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-csv") implementation("org.slf4j:slf4j-api:1.7.36") implementation("org.eclipse.jgit:org.eclipse.jgit:5.13.+") + + testImplementation(project(":rewrite-groovy")) } diff --git a/rewrite-test/src/test/java/org/openrewrite/text/CreateTextFileTest.java b/rewrite-test/src/test/java/org/openrewrite/text/CreateTextFileTest.java index 4205b722046..a3dfe9a3b08 100644 --- a/rewrite-test/src/test/java/org/openrewrite/text/CreateTextFileTest.java +++ b/rewrite-test/src/test/java/org/openrewrite/text/CreateTextFileTest.java @@ -17,8 +17,10 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; +import org.openrewrite.Issue; import org.openrewrite.test.RewriteTest; +import static org.openrewrite.groovy.Assertions.groovy; import static org.openrewrite.test.SourceSpecs.text; class CreateTextFileTest implements RewriteTest { @@ -97,4 +99,31 @@ void shouldAddAnotherFile() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-jenkins/issues/52") + void shouldOverrideDifferentSourceFileType() { + String after = """ + /* + See the documentation for more options: + https://github.com/jenkins-infra/pipeline-library/ + */ + buildPlugin( + useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests + configurations: [ + [platform: 'linux', jdk: 21], + [platform: 'windows', jdk: 17], + ])"""; + rewriteRun( + spec -> spec.recipe(new CreateTextFile(after, "Jenkinsfile", true)), + groovy( + """ + #!groovy + buildPlugin() + """, + after, + spec -> spec.path("Jenkinsfile") + ) + ); + } }