Skip to content

Commit 339d2c9

Browse files
authored
Make the Gradle testing methods public, to allow easier parameterized testing. (#2359)
2 parents dbe3d97 + 77379b4 commit 339d2c9

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BumpThisNumberIfACustomStepChangesTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private void writeContentWithBadFormatting() throws IOException {
3939
}
4040

4141
@Override
42-
protected void applyIsUpToDate(boolean upToDate) throws IOException {
42+
public void applyIsUpToDate(boolean upToDate) throws IOException {
4343
super.applyIsUpToDate(upToDate);
4444
assertFile("README.md").hasContent("abc");
4545
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
public class ConfigurationCacheTest extends GradleIntegrationHarness {
2424
@Override
25-
protected GradleRunner gradleRunner() throws IOException {
25+
public GradleRunner gradleRunner() throws IOException {
2626
setFile("gradle.properties").toContent("org.gradle.unsafe.configuration-cache=true");
2727
setFile("settings.gradle").toContent("enableFeaturePreview(\"STABLE_CONFIGURATION_CACHE\")");
2828
return super.gradleRunner().withGradleVersion(GradleVersionSupport.STABLE_CONFIGURATION_CACHE.version);

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private Git initRepo() throws IllegalStateException, GitAPIException, IOExceptio
5252
}
5353

5454
@Override
55-
protected GradleRunner gradleRunner() throws IOException {
55+
public GradleRunner gradleRunner() throws IOException {
5656
return super.gradleRunner().withGradleVersion(GradleVersionSupport.CUSTOM_STEPS.version);
5757
}
5858

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void gitAttributes() throws IOException {
121121
setFile(".gitattributes").toContent("* text eol=lf");
122122
}
123123

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

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

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

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

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

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

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

187-
protected void checkRunsThenUpToDate() throws IOException {
187+
public void checkRunsThenUpToDate() throws IOException {
188188
checkIsUpToDate(false);
189189
checkIsUpToDate(true);
190190
}
191191

192-
protected void applyIsUpToDate(boolean upToDate) throws IOException {
192+
public void applyIsUpToDate(boolean upToDate) throws IOException {
193193
taskIsUpToDate("spotlessApply", upToDate);
194194
}
195195

196-
protected void checkIsUpToDate(boolean upToDate) throws IOException {
196+
public void checkIsUpToDate(boolean upToDate) throws IOException {
197197
taskIsUpToDate("spotlessCheck", upToDate);
198198
}
199199

@@ -215,13 +215,13 @@ private void taskIsUpToDate(String task, boolean upToDate) throws IOException {
215215
}
216216
}
217217

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

224-
protected static List<BuildTask> outcomes(BuildResult build) {
224+
public static List<BuildTask> outcomes(BuildResult build) {
225225
return build.getTasks().stream()
226226
.filter(t -> !t.getPath().equals(":spotlessInternalRegisterDependencies"))
227227
.collect(Collectors.toList());

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NpmTestsWithoutNpmInstallationTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,12 @@
1717

1818
import org.assertj.core.api.Assertions;
1919
import org.gradle.testkit.runner.BuildResult;
20+
import org.junit.jupiter.api.Disabled;
2021
import org.junit.jupiter.api.Test;
2122

2223
import com.diffplug.common.base.Predicates;
2324

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

2628
@Test

testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ public class ResourceHarness {
5353
File folderDontUseDirectly;
5454

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

6060
/** Returns a new child of the root folder. */
61-
protected File newFile(String subpath) {
61+
public File newFile(String subpath) {
6262
return new File(rootFolder(), subpath);
6363
}
6464

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

105-
protected String relativeToRoot(String path) {
105+
public String relativeToRoot(String path) {
106106
return new File(path).toPath().relativize(rootFolder().toPath()).toString();
107107
}
108108

109-
protected String read(String path) throws IOException {
109+
public String read(String path) throws IOException {
110110
return read(newFile(path).toPath(), StandardCharsets.UTF_8);
111111
}
112112

113-
protected String read(Path path, Charset encoding) throws IOException {
113+
public String read(Path path, Charset encoding) throws IOException {
114114
return new String(Files.readAllBytes(path), encoding);
115115
}
116116

117-
protected void replace(String path, String toReplace, String replaceWith) throws IOException {
117+
public void replace(String path, String toReplace, String replaceWith) throws IOException {
118118
String before = read(path);
119119
String after = before.replace(toReplace, replaceWith);
120120
if (before.equals(after)) {
@@ -124,15 +124,15 @@ protected void replace(String path, String toReplace, String replaceWith) throws
124124
}
125125

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

135-
protected static boolean existsTestResource(String filename) {
135+
public static boolean existsTestResource(String filename) {
136136
return getTestResourceUrl(filename).isPresent();
137137
}
138138

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

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

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

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

171171
@CheckReturnValue
172-
protected ReadAsserter assertFile(String path) {
172+
public ReadAsserter assertFile(String path) {
173173
return new ReadAsserter(newFile(path));
174174
}
175175

176176
@CheckReturnValue
177-
protected ReadAsserter assertFile(File file) {
177+
public ReadAsserter assertFile(File file) {
178178
return new ReadAsserter(file);
179179
}
180180

@@ -207,7 +207,7 @@ public void matches(Consumer<AbstractCharSequenceAssert<?, String>> conditions)
207207
}
208208
}
209209

210-
protected WriteAsserter setFile(String path) {
210+
public WriteAsserter setFile(String path) {
211211
return new WriteAsserter(newFile(path));
212212
}
213213

0 commit comments

Comments
 (0)