From b9403027e474feca0f971b0170f5229fcdcbab99 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 8 Dec 2024 10:20:03 -0800 Subject: [PATCH 1/3] Make the Gradle testing methods public, to allow easier parameterized testing. --- .../spotless/ConfigurationCacheTest.java | 2 +- .../gradle/spotless/GitRatchetGradleTest.java | 2 +- .../spotless/GradleIntegrationHarness.java | 24 +++++++------- .../diffplug/spotless/ResourceHarness.java | 32 +++++++++---------- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java index 0306f087ac..e76585fe54 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java @@ -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); diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java index d63efd9e57..e4c1d31615 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java @@ -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); } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java index 3621c2a0f6..100bdefc50 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java @@ -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; @@ -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 subpathsToInclude) { + public String getContents(Predicate subpathsToInclude) { return StringPrinter.buildString(printer -> Errors.rethrow().run(() -> iterateFiles(subpathsToInclude, (subpath, file) -> { printer.println("### " + subpath + " ###"); try { @@ -152,18 +152,18 @@ protected String getContents(Predicate subpathsToInclude) { } /** Dumps the filtered file listing of the folder to the console. */ - protected String listFiles(Predicate subpathsToInclude) { + public String listFiles(Predicate 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 subpathsToInclude, BiConsumer consumer) { + public void iterateFiles(Predicate subpathsToInclude, BiConsumer consumer) { TreeDef treeDef = TreeDef.forFile(Errors.rethrow()); List files = TreeStream.depthFirst(treeDef, rootFolder()) .filter(File::isFile) @@ -180,20 +180,20 @@ protected void iterateFiles(Predicate subpathsToInclude, BiConsumer outcomes(BuildResult build, TaskOutcome outcome) { + public static List outcomes(BuildResult build, TaskOutcome outcome) { return build.taskPaths(outcome).stream() .filter(s -> !s.equals(":spotlessInternalRegisterDependencies")) .collect(Collectors.toList()); } - protected static List outcomes(BuildResult build) { + public static List outcomes(BuildResult build) { return build.getTasks().stream() .filter(t -> !t.getPath().equals(":spotlessInternalRegisterDependencies")) .collect(Collectors.toList()); diff --git a/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java b/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java index 93d08ea157..8d5cdff79f 100644 --- a/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java @@ -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); @@ -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 listTestResources(String path) throws IOException { + public List listTestResources(String path) throws IOException { // add leading slash if required, otherwise resources won't be found if (!path.startsWith("/")) { path = path + "/"; @@ -102,19 +102,19 @@ protected List 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)) { @@ -124,7 +124,7 @@ 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 resourceUrl = getTestResourceUrl(filename); if (resourceUrl.isPresent()) { return ThrowingEx.get(() -> LineEnding.toUnix(Resources.toString(resourceUrl.get(), StandardCharsets.UTF_8))); @@ -132,7 +132,7 @@ protected static String getTestResource(String filename) { throw new IllegalArgumentException("No such resource " + filename); } - protected static boolean existsTestResource(String filename) { + public static boolean existsTestResource(String filename) { return getTestResourceUrl(filename).isPresent(); } @@ -142,7 +142,7 @@ private static Optional 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 createTestFiles(String... filenames) { + public List createTestFiles(String... filenames) { List files = new ArrayList<>(filenames.length); for (String filename : filenames) { files.add(createTestFile(filename)); @@ -151,7 +151,7 @@ protected List 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()); } @@ -159,7 +159,7 @@ protected File createTestFile(String filename) { * 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 fileContentsProcessor) { + public File createTestFile(String filename, UnaryOperator fileContentsProcessor) { int lastSlash = filename.lastIndexOf('/'); String name = lastSlash >= 0 ? filename.substring(lastSlash) : filename; File file = newFile(name); @@ -169,12 +169,12 @@ protected File createTestFile(String filename, UnaryOperator 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); } @@ -207,7 +207,7 @@ public void matches(Consumer> conditions) } } - protected WriteAsserter setFile(String path) { + public WriteAsserter setFile(String path) { return new WriteAsserter(newFile(path)); } From 04f6bf044adde76624923ee996100e656540a9d7 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 8 Dec 2024 10:23:27 -0800 Subject: [PATCH 2/3] Fix. --- .../gradle/spotless/BumpThisNumberIfACustomStepChangesTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BumpThisNumberIfACustomStepChangesTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BumpThisNumberIfACustomStepChangesTest.java index 28351bd9b7..25a6b18bca 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BumpThisNumberIfACustomStepChangesTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BumpThisNumberIfACustomStepChangesTest.java @@ -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"); } From 77379b4f5ab88531e951accbcf7d14fd98baf80c Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 8 Dec 2024 10:51:43 -0800 Subject: [PATCH 3/3] Temporarily disable test that needs npm repo b/c it's down right now. --- .../gradle/spotless/NpmTestsWithoutNpmInstallationTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NpmTestsWithoutNpmInstallationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NpmTestsWithoutNpmInstallationTest.java index d01d1ab006..50ea620778 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NpmTestsWithoutNpmInstallationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NpmTestsWithoutNpmInstallationTest.java @@ -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. @@ -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