Skip to content

Commit

Permalink
Convert rewrite-gradle to java/RewriteTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Nov 8, 2022
1 parent ccfb12e commit 175a358
Show file tree
Hide file tree
Showing 70 changed files with 3,132 additions and 2,823 deletions.
14 changes: 14 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/remote/Remote.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ static Builder builder(UUID id, Path sourcePath, Markers markers, URI uri) {
return new Builder(id, sourcePath, markers, uri);
}

@Override
default <P> TreeVisitor<?, PrintOutputCapture<P>> printer(Cursor cursor) {
return new TreeVisitor<Tree, PrintOutputCapture<P>>() {
@Override
public Tree visitSourceFile(SourceFile sourceFile, PrintOutputCapture<P> p) {
ExecutionContext ctx = p.getContext() instanceof ExecutionContext ? (ExecutionContext) p.getContext() :
new InMemoryExecutionContext();
HttpSender sender = HttpSenderExecutionContextView.view(ctx).getHttpSender();
p.out.append(StringUtils.readFully(getInputStream(sender), StandardCharsets.UTF_8));
return sourceFile;
}
};
}

class Builder {
protected final UUID id;
protected final Path sourcePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.With;
import org.apache.tools.ant.taskdefs.Exec;
import org.intellij.lang.annotations.Language;
import org.openrewrite.FileAttributes;
import org.openrewrite.PathUtils;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.ipc.http.HttpSender;
import org.openrewrite.marker.Markers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public AddGradleWrapper(String version, String distribution) {

@Override
protected TreeVisitor<?, ExecutionContext> getApplicableTest() {
return new HasSourcePath<>("regex", ".+\\.gradle(\\.kts)?$");
return new IsBuildGradle<>();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static SourceSpecs buildGradle(@Language("groovy") @Nullable String befor
}

public static SourceSpecs buildGradle(@Language("groovy") @Nullable String before, Consumer<SourceSpec<G.CompilationUnit>> spec) {
SourceSpec<G.CompilationUnit> gradle = new SourceSpec<>(G.CompilationUnit.class, "gradle", gradleParser, before, null);
SourceSpec<G.CompilationUnit> gradle = new SourceSpec<>(G.CompilationUnit.class, "gradle", gradleParser, before, (String) null);
gradle.path(Paths.get("build.gradle"));
spec.accept(gradle);
return gradle;
Expand All @@ -65,7 +65,7 @@ public static SourceSpecs settingsGradle(@Language("groovy") @Nullable String be
}

public static SourceSpecs settingsGradle(@Language("groovy") @Nullable String before, Consumer<SourceSpec<G.CompilationUnit>> spec) {
SourceSpec<G.CompilationUnit> gradle = new SourceSpec<>(G.CompilationUnit.class, "gradle", gradleParser, before, null);
SourceSpec<G.CompilationUnit> gradle = new SourceSpec<>(G.CompilationUnit.class, "gradle", gradleParser, before, (String) null);
gradle.path(Paths.get("settings.gradle"));
spec.accept(gradle);
return gradle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
public class IsBuildGradle<P> extends JavaIsoVisitor<P> {
@Override
public JavaSourceFile visitJavaSourceFile(JavaSourceFile cu, P p) {
if (cu.getSourcePath().toString().endsWith(".gradle") &&
!cu.getSourcePath().toString().endsWith("settings.gradle")) {
if ((cu.getSourcePath().toString().endsWith(".gradle") ||
cu.getSourcePath().toString().endsWith(".gradle.kts")) &&
!(cu.getSourcePath().toString().endsWith("settings.gradle") ||
cu.getSourcePath().toString().endsWith("settings.gradle.kts"))) {
return SearchResult.found(cu);
}
return super.visitJavaSourceFile(cu, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.marker.SearchResult;

import java.nio.file.Paths;

public class IsSettingsGradle<P> extends JavaIsoVisitor<P> {

@Override
public JavaSourceFile visitJavaSourceFile(JavaSourceFile cu, P p) {
if (cu.getSourcePath().toString().endsWith("settings.gradle")) {
if (cu.getSourcePath().toString().endsWith("settings.gradle") ||
cu.getSourcePath().toString().endsWith("settings.gradle.kts")) {
return SearchResult.found(cu);
}
return super.visitJavaSourceFile(cu, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
import org.openrewrite.quark.Quark;
import org.openrewrite.text.PlainText;

import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.List;

import static java.util.Objects.requireNonNull;
import static org.openrewrite.PathUtils.equalIgnoringSeparators;
import static org.openrewrite.gradle.util.GradleWrapper.*;

Expand Down Expand Up @@ -102,7 +104,7 @@ public Properties visitEntry(Properties.Entry entry, ExecutionContext context) {
return entry;
}

GradleWrapper gradleWrapper = validate(context).getValue();
GradleWrapper gradleWrapper = requireNonNull(validate(context).getValue());

// Typical example: https://services.gradle.org/distributions/gradle-7.4-all.zip
String currentDistributionUrl = entry.getValue().getText();
Expand All @@ -122,15 +124,17 @@ protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx)
List<SourceFile> sourceFileList = ListUtils.map(before, sourceFile -> {
if (sourceFile instanceof PlainText && equalIgnoringSeparators(sourceFile.getSourcePath(), WRAPPER_SCRIPT_LOCATION)) {
PlainText gradlew = (PlainText) setExecutable(sourceFile);
String gradlewText = StringUtils.readFully(UpdateGradleWrapper.class.getResourceAsStream("/gradlew"), sourceFile.getCharset());
String gradlewText = StringUtils.readFully(requireNonNull(UpdateGradleWrapper.class.getResourceAsStream("/gradlew")),
sourceFile.getCharset() == null ? StandardCharsets.UTF_8 : sourceFile.getCharset());
if (!gradlewText.equals(gradlew.getText())) {
gradlew = gradlew.withText(gradlewText);
}
return gradlew;
}
if (sourceFile instanceof PlainText && equalIgnoringSeparators(sourceFile.getSourcePath(), WRAPPER_BATCH_LOCATION)) {
PlainText gradlewBat = (PlainText) setExecutable(sourceFile);
String gradlewBatText = StringUtils.readFully(UpdateGradleWrapper.class.getResourceAsStream("/gradlew.bat"), sourceFile.getCharset());
String gradlewBatText = StringUtils.readFully(requireNonNull(UpdateGradleWrapper.class.getResourceAsStream("/gradlew.bat")),
sourceFile.getCharset() == null ? StandardCharsets.UTF_8 : sourceFile.getCharset());
if (!gradlewBatText.equals(gradlewBat.getText())) {
gradlewBat = gradlewBat.withText(gradlewBatText);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Copyright 2022 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.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.properties.Assertions.properties;
import static org.openrewrite.test.SourceSpecs.other;

class ActivateStyleTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ActivateStyle("org.openrewrite.java.IntelliJ", true))
.typeValidationOptions(TypeValidation.none());
}

@Test
void addToRewriteDsl() {
rewriteRun(
buildGradle(
"""
plugins {
id("java")
id("org.openrewrite.rewrite")
}
rewrite {
activeRecipe("org.openrewrite.java.format.AutoFormat")
}
""",
"""
plugins {
id("java")
id("org.openrewrite.rewrite")
}
rewrite {
activeRecipe("org.openrewrite.java.format.AutoFormat")
activeStyle("org.openrewrite.java.IntelliJ")
}
"""
)
);
}

@Test
void addToRewriteDslExistingStyle() {
rewriteRun(
spec -> spec.recipe(new ActivateStyle("org.openrewrite.java.IntelliJ", false)),
buildGradle(
"""
plugins {
id("java")
id("org.openrewrite.rewrite")
}
rewrite {
activeRecipe("org.openrewrite.java.format.AutoFormat")
activeStyle("otherStyle")
}
""",
"""
plugins {
id("java")
id("org.openrewrite.rewrite")
}
rewrite {
activeRecipe("org.openrewrite.java.format.AutoFormat")
activeStyle("otherStyle", "org.openrewrite.java.IntelliJ")
}
"""
)
);
}

@Test
void addToRewriteDslOverwriteStyle() {
rewriteRun(
buildGradle(
"""
plugins {
id("java")
id("org.openrewrite.rewrite")
}
rewrite {
activeRecipe("org.openrewrite.java.format.AutoFormat")
activeStyle("com.your.Style")
}
""",
"""
plugins {
id("java")
id("org.openrewrite.rewrite")
}
rewrite {
activeRecipe("org.openrewrite.java.format.AutoFormat")
activeStyle("org.openrewrite.java.IntelliJ")
}
"""
)
);
}

@Test
void addToProperties() {
rewriteRun(
other("", spec -> spec.path(".gradle.kts")),
properties(
"""
org.gradle.someProp=true
""",
"""
org.gradle.someProp=true
systemProp.rewrite.activeStyles=org.openrewrite.java.IntelliJ
""",
spec -> spec.path("gradle.properties")
)
);
}

@Test
void addToPropertiesStyles() {
rewriteRun(
spec -> spec.recipe(new ActivateStyle("org.openrewrite.java.IntelliJ", false)),
other("", spec -> spec.path(".gradle.kts")),
properties(
"""
org.gradle.someProp=true
systemProp.rewrite.activeStyles=org.openrewrite.java.Other
""",
"""
org.gradle.someProp=true
systemProp.rewrite.activeStyles=org.openrewrite.java.Other,org.openrewrite.java.IntelliJ
""",
spec -> spec.path("gradle.properties")
)
);
}

@Test
void overwritePropertiesStyles() {
rewriteRun(
other("", spec -> spec.path(".gradle.kts")),
properties(
"""
org.gradle.someProp=true
systemProp.rewrite.activeStyles=org.openrewrite.java.Other
""",
"""
org.gradle.someProp=true
systemProp.rewrite.activeStyles=org.openrewrite.java.IntelliJ
""",
spec -> spec.path("gradle.properties")
)
);
}
}
Loading

0 comments on commit 175a358

Please sign in to comment.