Skip to content

Commit

Permalink
Support case insensitive enum for yaml recipes (openrewrite#3264)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 authored May 22, 2023
1 parent 4874f67 commit a403c17
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public YamlResourceLoader(InputStream yamlInput, URI source, Properties properti

mapper = JsonMapper.builder()
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
.build()
.registerModule(new ParameterNamesModule())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class AppendToTextFile extends Recipe {
+ "Subsequent instances of this recipe in the same Rewrite execution will always append.",
valid = {"continue", "replace", "leave"},
required = false)
@Nullable String existingFileStrategy;
@Nullable Strategy existingFileStrategy;
public enum Strategy { CONTINUE, REPLACE, LEAVE }

private final static String CREATED_THIS_EXECUTION_MESSAGE_KEY = "AppendToTextFile.CreatedThisExecution";
Expand All @@ -81,7 +81,7 @@ protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx)
relativeFileName,
content + maybeNewline,
preamble != null ? preamble + maybeNewline : "",
existingFileStrategy != null ? Strategy.valueOf(existingFileStrategy.toUpperCase()) : Strategy.LEAVE);
existingFileStrategy != null ? existingFileStrategy : Strategy.LEAVE);
}

private List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,29 @@ void method() {
assertThat(descriptorExamples).containsExactlyElementsOf(recipes.iterator().next().getExamples());
});
}

@Test
void caseInsensitiveEnums() {
rewriteRun(
spec -> spec.recipeFromYaml(
//language=yml
"""
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.gradle.testCaseInsensitiveEnumInYaml
displayName: test Enum in yaml
description: test Enum in yaml.
recipeList:
- org.openrewrite.text.AppendToTextFile:
relativeFileName: "file.txt"
content: " World!"
preamble: "preamble"
appendNewline : false
existingFileStrategy: "Continue"
""",
"org.openrewrite.gradle.testCaseInsensitiveEnumInYaml"
),
text("Hello", "Hello World!")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AppendToTextFileTest implements RewriteTest {
@Test
void createsFileIfNeeded() {
rewriteRun(
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", "preamble", true, "leave")),
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", "preamble", true, AppendToTextFile.Strategy.LEAVE)),
text(
null,
"""
Expand All @@ -45,7 +45,7 @@ void createsFileIfNeeded() {

@Test
void createsFileIfNeededWithMultipleInstances() {
Supplier<Recipe> r = () -> new AppendToTextFile("file.txt", "content", "preamble", true, "leave");
Supplier<Recipe> r = () -> new AppendToTextFile("file.txt", "content", "preamble", true, AppendToTextFile.Strategy.LEAVE);
rewriteRun(
spec -> spec.recipe(r.get().doNext(r.get())),
text(
Expand All @@ -64,7 +64,7 @@ void createsFileIfNeededWithMultipleInstances() {
@Test
void replacesFileIfRequested() {
rewriteRun(
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", "preamble", true, "replace")),
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", "preamble", true, AppendToTextFile.Strategy.REPLACE)),
text(
"""
existing
Expand All @@ -81,7 +81,7 @@ void replacesFileIfRequested() {
@Test
void continuesFileIfRequested() {
rewriteRun(
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", "preamble", true, "continue")),
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", "preamble", true, AppendToTextFile.Strategy.CONTINUE)),
text(
"""
existing
Expand All @@ -98,7 +98,7 @@ void continuesFileIfRequested() {
@Test
void leavesFileIfRequested() {
rewriteRun(
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", "preamble", true, "leave")),
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", "preamble", true, AppendToTextFile.Strategy.LEAVE)),
text("existing", spec -> spec.path("file.txt"))
);
}
Expand All @@ -107,8 +107,8 @@ void leavesFileIfRequested() {
void multipleInstancesCanAppend() {
rewriteRun(
spec -> spec.recipe(
new AppendToTextFile("file.txt", "content", "preamble", true, "replace")
.doNext(new AppendToTextFile("file.txt", "content", "preamble", true, "replace"))),
new AppendToTextFile("file.txt", "content", "preamble", true, AppendToTextFile.Strategy.REPLACE)
.doNext(new AppendToTextFile("file.txt", "content", "preamble", true, AppendToTextFile.Strategy.REPLACE))),
text(
"existing",
"""
Expand All @@ -124,7 +124,7 @@ void multipleInstancesCanAppend() {
@Test
void noLeadingNewlineIfNoPreamble() {
rewriteRun(
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", null, true, "replace")),
spec -> spec.recipe(new AppendToTextFile("file.txt", "content", null, true, AppendToTextFile.Strategy.REPLACE)),
text(
"""
existing
Expand All @@ -140,8 +140,8 @@ void noLeadingNewlineIfNoPreamble() {
@Test
void multipleFiles() {
rewriteRun(
spec -> spec.recipe(new AppendToTextFile("file1.txt", "content1", "preamble1", true, "replace")
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, "replace"))),
spec -> spec.recipe(new AppendToTextFile("file1.txt", "content1", "preamble1", true, AppendToTextFile.Strategy.REPLACE)
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, AppendToTextFile.Strategy.REPLACE))),
text(
"existing1",
"""
Expand All @@ -166,8 +166,8 @@ void multipleFiles() {
void missingExpectedGeneratedFiles() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
rewriteRun(
spec -> spec.recipe(new AppendToTextFile("file1.txt", "content1", "preamble1", true, "replace")
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, "replace"))),
spec -> spec.recipe(new AppendToTextFile("file1.txt", "content1", "preamble1", true, AppendToTextFile.Strategy.REPLACE)
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, AppendToTextFile.Strategy.REPLACE))),
text(
"existing2",
"""
Expand All @@ -183,8 +183,8 @@ void missingExpectedGeneratedFiles() {
@Test
void changeAndCreatedFilesIfNeeded() {
rewriteRun(
spec -> spec.recipe(new AppendToTextFile("file1.txt", "content1", "preamble1", true, "replace")
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, "replace"))),
spec -> spec.recipe(new AppendToTextFile("file1.txt", "content1", "preamble1", true, AppendToTextFile.Strategy.REPLACE)
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, AppendToTextFile.Strategy.REPLACE))),
text(
"existing2",
"""
Expand All @@ -207,10 +207,10 @@ void changeAndCreatedFilesIfNeeded() {
void multipleInstancesOnMultipleFiles() {
rewriteRun(
spec -> spec.recipe(
new AppendToTextFile("file1.txt", "content1", "preamble1", true, "replace")
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, "replace"))
.doNext(new AppendToTextFile("file1.txt", "content1", "preamble1", true, "replace"))
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, "replace"))),
new AppendToTextFile("file1.txt", "content1", "preamble1", true, AppendToTextFile.Strategy.REPLACE)
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, AppendToTextFile.Strategy.REPLACE))
.doNext(new AppendToTextFile("file1.txt", "content1", "preamble1", true, AppendToTextFile.Strategy.REPLACE))
.doNext(new AppendToTextFile("file2.txt", "content2", "preamble2", true, AppendToTextFile.Strategy.REPLACE))),
text(
"existing1",
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class AddGradleEnterprise extends Recipe {
"When omitted scans will be published only when the `--scan` option is passed to the build.",
required = false,
valid = {"always", "failure"},
example = "true")
example = "always")
@Nullable
PublishCriteria publishCriteria;

Expand Down

0 comments on commit a403c17

Please sign in to comment.