Skip to content

Commit

Permalink
Make the Gradle testing methods public, to allow easier parameterized…
Browse files Browse the repository at this point in the history
… testing. (#2359)
  • Loading branch information
nedtwigg authored Dec 8, 2024
2 parents dbe3d97 + 77379b4 commit 339d2c9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void writeContentWithBadFormatting() throws IOException {
}

@Override
protected void applyIsUpToDate(boolean upToDate) throws IOException {
public void applyIsUpToDate(boolean upToDate) throws IOException {
super.applyIsUpToDate(upToDate);
assertFile("README.md").hasContent("abc");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class ConfigurationCacheTest extends GradleIntegrationHarness {
@Override
protected GradleRunner gradleRunner() throws IOException {
public GradleRunner gradleRunner() throws IOException {
setFile("gradle.properties").toContent("org.gradle.unsafe.configuration-cache=true");
setFile("settings.gradle").toContent("enableFeaturePreview(\"STABLE_CONFIGURATION_CACHE\")");
return super.gradleRunner().withGradleVersion(GradleVersionSupport.STABLE_CONFIGURATION_CACHE.version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private Git initRepo() throws IllegalStateException, GitAPIException, IOExceptio
}

@Override
protected GradleRunner gradleRunner() throws IOException {
public GradleRunner gradleRunner() throws IOException {
return super.gradleRunner().withGradleVersion(GradleVersionSupport.CUSTOM_STEPS.version);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void gitAttributes() throws IOException {
setFile(".gitattributes").toContent("* text eol=lf");
}

protected GradleRunner gradleRunner() throws IOException {
public GradleRunner gradleRunner() throws IOException {
GradleVersionSupport version;
if (newFile("build.gradle").exists() && read("build.gradle").contains("custom")) {
version = GradleVersionSupport.CUSTOM_STEPS;
Expand All @@ -136,11 +136,11 @@ protected GradleRunner gradleRunner() throws IOException {
}

/** Dumps the complete file contents of the folder to the console. */
protected String getContents() {
public String getContents() {
return getContents(subPath -> !subPath.startsWith(".gradle"));
}

protected String getContents(Predicate<String> subpathsToInclude) {
public String getContents(Predicate<String> subpathsToInclude) {
return StringPrinter.buildString(printer -> Errors.rethrow().run(() -> iterateFiles(subpathsToInclude, (subpath, file) -> {
printer.println("### " + subpath + " ###");
try {
Expand All @@ -152,18 +152,18 @@ protected String getContents(Predicate<String> subpathsToInclude) {
}

/** Dumps the filtered file listing of the folder to the console. */
protected String listFiles(Predicate<String> subpathsToInclude) {
public String listFiles(Predicate<String> subpathsToInclude) {
return StringPrinter.buildString(printer -> iterateFiles(subpathsToInclude, (subPath, file) -> {
printer.println(subPath + " [" + getFileAttributes(file) + "]");
}));
}

/** Dumps the file listing of the folder to the console. */
protected String listFiles() {
public String listFiles() {
return listFiles(subPath -> !subPath.startsWith(".gradle"));
}

protected void iterateFiles(Predicate<String> subpathsToInclude, BiConsumer<String, File> consumer) {
public void iterateFiles(Predicate<String> subpathsToInclude, BiConsumer<String, File> consumer) {
TreeDef<File> treeDef = TreeDef.forFile(Errors.rethrow());
List<File> files = TreeStream.depthFirst(treeDef, rootFolder())
.filter(File::isFile)
Expand All @@ -180,20 +180,20 @@ protected void iterateFiles(Predicate<String> subpathsToInclude, BiConsumer<Stri
}
}

protected String getFileAttributes(File file) {
public String getFileAttributes(File file) {
return (file.canRead() ? "r" : "-") + (file.canWrite() ? "w" : "-") + (file.canExecute() ? "x" : "-");
}

protected void checkRunsThenUpToDate() throws IOException {
public void checkRunsThenUpToDate() throws IOException {
checkIsUpToDate(false);
checkIsUpToDate(true);
}

protected void applyIsUpToDate(boolean upToDate) throws IOException {
public void applyIsUpToDate(boolean upToDate) throws IOException {
taskIsUpToDate("spotlessApply", upToDate);
}

protected void checkIsUpToDate(boolean upToDate) throws IOException {
public void checkIsUpToDate(boolean upToDate) throws IOException {
taskIsUpToDate("spotlessCheck", upToDate);
}

Expand All @@ -215,13 +215,13 @@ private void taskIsUpToDate(String task, boolean upToDate) throws IOException {
}
}

protected static List<String> outcomes(BuildResult build, TaskOutcome outcome) {
public static List<String> outcomes(BuildResult build, TaskOutcome outcome) {
return build.taskPaths(outcome).stream()
.filter(s -> !s.equals(":spotlessInternalRegisterDependencies"))
.collect(Collectors.toList());
}

protected static List<BuildTask> outcomes(BuildResult build) {
public static List<BuildTask> outcomes(BuildResult build) {
return build.getTasks().stream()
.filter(t -> !t.getPath().equals(":spotlessInternalRegisterDependencies"))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,10 +17,12 @@

import org.assertj.core.api.Assertions;
import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import com.diffplug.common.base.Predicates;

@Disabled("https://status.npmjs.org/ shows npm services down on 12/8/2024, should undisable this later")
class NpmTestsWithoutNpmInstallationTest extends GradleIntegrationHarness {

@Test
Expand Down
32 changes: 16 additions & 16 deletions testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public class ResourceHarness {
File folderDontUseDirectly;

/** Returns the root folder (canonicalized to fix OS X issue) */
protected File rootFolder() {
public File rootFolder() {
return Errors.rethrow().get(() -> folderDontUseDirectly.getCanonicalFile());
}

/** Returns a new child of the root folder. */
protected File newFile(String subpath) {
public File newFile(String subpath) {
return new File(rootFolder(), subpath);
}

/** Creates and returns a new child-folder of the root folder. */
protected File newFolder(String subpath) throws IOException {
public File newFolder(String subpath) throws IOException {
File targetDir = newFile(subpath);
if (!targetDir.mkdir()) {
throw new IOException("Failed to create " + targetDir);
Expand All @@ -77,7 +77,7 @@ protected File newFolder(String subpath) throws IOException {
* @return the list of resources in that directory on the classpath, unix-style file separators
* @throws IOException
*/
protected List<String> listTestResources(String path) throws IOException {
public List<String> listTestResources(String path) throws IOException {
// add leading slash if required, otherwise resources won't be found
if (!path.startsWith("/")) {
path = path + "/";
Expand All @@ -102,19 +102,19 @@ protected List<String> listTestResources(String path) throws IOException {
return filenames;
}

protected String relativeToRoot(String path) {
public String relativeToRoot(String path) {
return new File(path).toPath().relativize(rootFolder().toPath()).toString();
}

protected String read(String path) throws IOException {
public String read(String path) throws IOException {
return read(newFile(path).toPath(), StandardCharsets.UTF_8);
}

protected String read(Path path, Charset encoding) throws IOException {
public String read(Path path, Charset encoding) throws IOException {
return new String(Files.readAllBytes(path), encoding);
}

protected void replace(String path, String toReplace, String replaceWith) throws IOException {
public void replace(String path, String toReplace, String replaceWith) throws IOException {
String before = read(path);
String after = before.replace(toReplace, replaceWith);
if (before.equals(after)) {
Expand All @@ -124,15 +124,15 @@ protected void replace(String path, String toReplace, String replaceWith) throws
}

/** Returns the contents of the given file from the src/test/resources directory. */
protected static String getTestResource(String filename) {
public static String getTestResource(String filename) {
Optional<URL> resourceUrl = getTestResourceUrl(filename);
if (resourceUrl.isPresent()) {
return ThrowingEx.get(() -> LineEnding.toUnix(Resources.toString(resourceUrl.get(), StandardCharsets.UTF_8)));
}
throw new IllegalArgumentException("No such resource " + filename);
}

protected static boolean existsTestResource(String filename) {
public static boolean existsTestResource(String filename) {
return getTestResourceUrl(filename).isPresent();
}

Expand All @@ -142,7 +142,7 @@ private static Optional<URL> getTestResourceUrl(String filename) {
}

/** Returns Files (in a temporary folder) which has the contents of the given file from the src/test/resources directory. */
protected List<File> createTestFiles(String... filenames) {
public List<File> createTestFiles(String... filenames) {
List<File> files = new ArrayList<>(filenames.length);
for (String filename : filenames) {
files.add(createTestFile(filename));
Expand All @@ -151,15 +151,15 @@ protected List<File> createTestFiles(String... filenames) {
}

/** Returns a File (in a temporary folder) which has the contents of the given file from the src/test/resources directory. */
protected File createTestFile(String filename) {
public File createTestFile(String filename) {
return createTestFile(filename, UnaryOperator.identity());
}

/**
* Returns a File (in a temporary folder) which has the contents, possibly processed, of the given file from the
* src/test/resources directory.
*/
protected File createTestFile(String filename, UnaryOperator<String> fileContentsProcessor) {
public File createTestFile(String filename, UnaryOperator<String> fileContentsProcessor) {
int lastSlash = filename.lastIndexOf('/');
String name = lastSlash >= 0 ? filename.substring(lastSlash) : filename;
File file = newFile(name);
Expand All @@ -169,12 +169,12 @@ protected File createTestFile(String filename, UnaryOperator<String> fileContent
}

@CheckReturnValue
protected ReadAsserter assertFile(String path) {
public ReadAsserter assertFile(String path) {
return new ReadAsserter(newFile(path));
}

@CheckReturnValue
protected ReadAsserter assertFile(File file) {
public ReadAsserter assertFile(File file) {
return new ReadAsserter(file);
}

Expand Down Expand Up @@ -207,7 +207,7 @@ public void matches(Consumer<AbstractCharSequenceAssert<?, String>> conditions)
}
}

protected WriteAsserter setFile(String path) {
public WriteAsserter setFile(String path) {
return new WriteAsserter(newFile(path));
}

Expand Down

0 comments on commit 339d2c9

Please sign in to comment.