Skip to content

Commit

Permalink
fix: CreateYaml does not work the same as CreateTextFile with pre…
Browse files Browse the repository at this point in the history
…condition (#4360)

* 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: ""
```

* the fix

* fix other test

* 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>

* 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>

* generate empty if null

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
pstreef and github-actions[bot] authored Jul 26, 2024
1 parent 84f5858 commit f28b1d7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public TreeVisitor<?, ExecutionContext> getScanner(AtomicBoolean shouldCreate) {
@Override
public Collection<SourceFile> 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());
}
Expand All @@ -98,13 +98,12 @@ public Collection<SourceFile> generate(AtomicBoolean shouldCreate, ExecutionCont
public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean created) {
Path path = Paths.get(relativeFileName);
return new YamlVisitor<ExecutionContext>() {

@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());
}
Expand All @@ -125,4 +124,13 @@ public Yaml visitDocuments(Yaml.Documents documents, ExecutionContext ctx) {
}
};
}

@Language("yml")
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 == null ? "" : yamlContents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void shouldAddAnotherFile() {
rewriteRun(
spec -> spec.recipe(new CreateYamlFile(
"test/test-file-2.yaml",
null,
"",
null,
true
)),
Expand Down Expand Up @@ -208,4 +208,65 @@ 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")
)
);
}
}

0 comments on commit f28b1d7

Please sign in to comment.