From 48422d59f5fdf9a7fccc06318cbae650cfc700af Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Fri, 26 Jul 2024 13:45:22 +0200
Subject: [PATCH 1/6] fix: `CreateYaml` does not work with precondition
When a precondition is present and matched, an empty file is generated but the content is not added.
```
expected: "content: yes"
but was: ""
```
---
.../openrewrite/yaml/CreateYamlFileTest.java | 59 +++++++++++++++++++
1 file changed, 59 insertions(+)
diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
index d78f2cd0e25..e131c638f65 100644
--- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
+++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
@@ -208,4 +208,63 @@ void shouldUseFileContentsWhenContentsAndContentsUrlNotNull() {
)
);
}
+
+ @Test
+ void shouldCreateYamlFromYamlRecipe() {
+ rewriteRun(spec -> spec.recipeFromYaml("""
+ ---
+ type: specs.openrewrite.org/v1beta/recipe
+ name: org.openrewrite.CreateYamlPrecondition
+ displayName: Create yaml file with precondition
+ description: Create a yaml file with a precondition.
+ recipeList:
+ - org.openrewrite.yaml.CreateYamlFile:
+ relativeFileName: created.yml
+ overwriteExisting: false
+ fileContents: |
+ content: yes
+ """, "org.openrewrite.CreateYamlPrecondition"),
+ yaml("""
+ foo: bar
+ """, spec -> spec.path("precondition.yml")),
+ yaml(
+ null,
+ """
+ content: yes
+ """,
+ spec -> spec.path("created.yml")
+ )
+ );
+ }
+
+ @Test
+ void shouldCreateYamlFromYamlRecipeWithPrecondition() {
+ rewriteRun(spec -> spec.recipeFromYaml("""
+ ---
+ type: specs.openrewrite.org/v1beta/recipe
+ name: org.openrewrite.CreateYamlPrecondition
+ displayName: Create yaml file with precondition
+ description: Create a yaml file with a precondition.
+ preconditions:
+ - org.openrewrite.FindSourceFiles:
+ filePattern: "**/precondition.yml"
+ recipeList:
+ - org.openrewrite.yaml.CreateYamlFile:
+ relativeFileName: created.yml
+ overwriteExisting: false
+ fileContents: |
+ content: yes
+ """, "org.openrewrite.CreateYamlPrecondition"),
+ yaml("""
+ foo: bar
+ """, spec -> spec.path("precondition.yml")),
+ yaml(
+ null,
+ """
+ content: yes
+ """,
+ spec -> spec.path("created.yml")
+ )
+ );
+ }
}
From 9cc4849b1b773d61e72a0c185ab3cda29e289321 Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Fri, 26 Jul 2024 14:39:35 +0200
Subject: [PATCH 2/6] the fix
---
.../org/openrewrite/yaml/CreateYamlFile.java | 21 +++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java
index 4d9309a8597..6bbbef3d4a9 100644
--- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java
+++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java
@@ -87,7 +87,7 @@ public TreeVisitor, ExecutionContext> getScanner(AtomicBoolean shouldCreate) {
@Override
public Collection generate(AtomicBoolean shouldCreate, ExecutionContext ctx) {
if (shouldCreate.get()) {
- return YamlParser.builder().build().parse("")
+ return YamlParser.builder().build().parse(getYamlContents(ctx))
.map(brandNewFile -> (SourceFile) brandNewFile.withSourcePath(Paths.get(relativeFileName)))
.collect(Collectors.toList());
}
@@ -98,13 +98,12 @@ public Collection generate(AtomicBoolean shouldCreate, ExecutionCont
public TreeVisitor, ExecutionContext> getVisitor(AtomicBoolean created) {
Path path = Paths.get(relativeFileName);
return new YamlVisitor() {
+
@Override
public Yaml visitDocuments(Yaml.Documents documents, ExecutionContext ctx) {
- if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.equals(documents.getSourcePath())) {
- @Language("yml") String yamlContents = fileContents;
- if (yamlContents == null && fileContentsUrl != null) {
- yamlContents = Remote.builder(path, URI.create(fileContentsUrl)).build().printAll(ctx);
- }
+ if (Boolean.TRUE.equals(overwriteExisting) && path.equals(documents.getSourcePath())) {
+ @Language("yml")
+ String yamlContents = getYamlContents(ctx);
if (StringUtils.isBlank(yamlContents)) {
return documents.withDocuments(emptyList());
}
@@ -125,4 +124,14 @@ public Yaml visitDocuments(Yaml.Documents documents, ExecutionContext ctx) {
}
};
}
+
+ @Language("yml")
+ @Nullable
+ private String getYamlContents(ExecutionContext ctx) {
+ @Language("yml") String yamlContents = fileContents;
+ if (yamlContents == null && fileContentsUrl != null) {
+ yamlContents = Remote.builder(Paths.get(relativeFileName), URI.create(fileContentsUrl)).build().printAll(ctx);
+ }
+ return yamlContents;
+ }
}
From f7b9f4b35b6b9c1da602d06375fed2cb0de5cd60 Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Fri, 26 Jul 2024 14:42:41 +0200
Subject: [PATCH 3/6] fix other test
---
.../src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
index e131c638f65..3b38540ab2d 100644
--- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
+++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
@@ -120,7 +120,7 @@ void shouldAddAnotherFile() {
rewriteRun(
spec -> spec.recipe(new CreateYamlFile(
"test/test-file-2.yaml",
- null,
+ "",
null,
true
)),
From d125aa78bcf02791d4862136a453c077697964b0 Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Fri, 26 Jul 2024 14:43:03 +0200
Subject: [PATCH 4/6] Update
rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
---
.../src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
index 3b38540ab2d..ff75c9ca15e 100644
--- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
+++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
@@ -255,7 +255,8 @@ void shouldCreateYamlFromYamlRecipeWithPrecondition() {
fileContents: |
content: yes
""", "org.openrewrite.CreateYamlPrecondition"),
- yaml("""
+ yaml(
+ """
foo: bar
""", spec -> spec.path("precondition.yml")),
yaml(
From dad83ec556ec4c5fb2e05e9fecbe16e9051d93dd Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Fri, 26 Jul 2024 14:43:10 +0200
Subject: [PATCH 5/6] Update
rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
---
.../src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
index ff75c9ca15e..f590b822b8a 100644
--- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
+++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/CreateYamlFileTest.java
@@ -224,7 +224,8 @@ void shouldCreateYamlFromYamlRecipe() {
fileContents: |
content: yes
""", "org.openrewrite.CreateYamlPrecondition"),
- yaml("""
+ yaml(
+ """
foo: bar
""", spec -> spec.path("precondition.yml")),
yaml(
From 3862417a0848fd8094bfcae3add84852d7eaf660 Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Fri, 26 Jul 2024 15:05:10 +0200
Subject: [PATCH 6/6] generate empty if null
---
.../src/main/java/org/openrewrite/yaml/CreateYamlFile.java | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java
index 6bbbef3d4a9..4ef52c58ddd 100644
--- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java
+++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/CreateYamlFile.java
@@ -126,12 +126,11 @@ public Yaml visitDocuments(Yaml.Documents documents, ExecutionContext ctx) {
}
@Language("yml")
- @Nullable
private String getYamlContents(ExecutionContext ctx) {
@Language("yml") String yamlContents = fileContents;
if (yamlContents == null && fileContentsUrl != null) {
yamlContents = Remote.builder(Paths.get(relativeFileName), URI.create(fileContentsUrl)).build().printAll(ctx);
}
- return yamlContents;
+ return yamlContents == null ? "" : yamlContents;
}
}