Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added gradle recipe to remove enableFeaturePreview method #4191

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.gradle;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.java.tree.J;

@Value
@EqualsAndHashCode(callSuper = false)
public class RemoveEnableFeaturePreview extends Recipe {

@Option(displayName = "The feature preview name",
description = "The name of the feature preview to remove.",
example = "ONE_LOCKFILE_PER_PROJECT")
String previewFeatureName;

@Override
public String getDisplayName() {
return "Remove an enabled Gradle preview feature";
}

@Override
public String getDescription() {
return "Remove an enabled Gradle preview feature from `settings.gradle`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new IsSettingsGradle<>(), new GroovyIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if ("enableFeaturePreview".equals(method.getSimpleName()) &&
method.getArguments().size() == 1 &&
J.Literal.isLiteralValue(method.getArguments().get(0), previewFeatureName)) {
return null;
}
return method;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.gradle;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.gradle.Assertions.settingsGradle;

class RemoveEnableFeaturePreviewTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RemoveEnableFeaturePreview("ONE_LOCKFILE_PER_PROJECT"));
}

@Test
void singleQuotes() {
//language=gradle
rewriteRun(
settingsGradle(
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}

rootProject.name = 'merge-service'
enableFeaturePreview('ONE_LOCKFILE_PER_PROJECT')
""",
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}

rootProject.name = 'merge-service'
"""
)
);
}

@Test
void doubleQuotes() {
//language=gradle
rewriteRun(
settingsGradle(
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}

rootProject.name = 'merge-service'
enableFeaturePreview("ONE_LOCKFILE_PER_PROJECT")

include 'service'
""",
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}

rootProject.name = 'merge-service'

include 'service'
"""
)
);
}

@Nested
class NoChange {
@Test
void differentFeature() {
//language=gradle
rewriteRun(
settingsGradle(
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}

enableFeaturePreview("DIFFERENT_FEATURE")

rootProject.name = 'merge-service'

include 'service'
"""
)
);
}

@Test
void noArgument() {
//language=gradle
rewriteRun(
settingsGradle(
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}

enableFeaturePreview()

rootProject.name = 'merge-service'

include 'service'
"""
)
);
}

timtebeek marked this conversation as resolved.
Show resolved Hide resolved
@Test
void nullArgument() {
//language=gradle
rewriteRun(
settingsGradle(
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}

enableFeaturePreview(null)

rootProject.name = 'merge-service'

include 'service'
"""
)
);
}
}
}