Skip to content

Commit

Permalink
Merge pull request #203 from diffplug/feature/190
Browse files Browse the repository at this point in the history
Refactor testing
  • Loading branch information
nedtwigg authored Feb 12, 2018
2 parents 96b2071 + 86a13c3 commit d192b89
Show file tree
Hide file tree
Showing 145 changed files with 252 additions and 1,012 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ You might be looking for:
### Version 1.10.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/))

* LicenseHeaderStep now supports customizing the year range separator in copyright notices. ([#199](https://github.com/diffplug/spotless/pull/199))
* Breaking change to testlib - removed `ResourceHarness.write` and added `ResourceHarness.[set/assert]File` for easier-to-read tests. ([#203](https://github.com/diffplug/spotless/pull/203))

### Version 1.9.0 - February 5th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.9.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.9.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ For the folders below in monospace text, they are published on maven central at
| Folder | Description |
| ------ | ----------- |
| `lib` | Contains all of Spotless' core infrastructure and most of its `FormatterStep` - has no external dependencies. |
| `testlib` | Contains testing infrastructure, as well as all tests for `spotless-lib`, since those tests need the testing infrastructure in `testlib`. |
| `testlib` | Contains testing infrastructure and all test resources, so that they can be reused in plugin-specific integration tests. Also contains tests for `lib`. |
| `lib-extra` | Contains the optional parts of Spotless which require external dependencies. `LineEnding.GIT_ATTRIBUTES` won't work unless `lib-extra` is available. |
| `plugin-gradle` | Integrates spotless and all of its formatters into Gradle. |
| `plugin-maven` | Integrates spotless and all of its formatters into Maven. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@
public class EclipseFormatterStepTest extends ResourceHarness {
@Test
public void loadPropertiesSettings() throws Throwable {
List<File> eclipseFormatFile = createTestFiles("java/eclipse/format/formatter.properties");
List<File> eclipseFormatFile = createTestFiles("java/eclipse/formatter.properties");
StepHarness.forStep(EclipseFormatterStep.create(eclipseFormatFile, TestProvisioner.mavenCentral()))
.testResource("java/eclipse/format/JavaCodeUnformatted.test", "java/eclipse/format/JavaCodeFormatted.test");
.testResource("java/eclipse/JavaCodeUnformatted.test", "java/eclipse/JavaCodeFormatted.test");
}

@Test
public void loadXmlSettings() throws Throwable {
List<File> eclipseFormatFile = createTestFiles("java/eclipse/format/formatter.xml");
List<File> eclipseFormatFile = createTestFiles("java/eclipse/formatter.xml");
StepHarness.forStep(EclipseFormatterStep.create(eclipseFormatFile, TestProvisioner.mavenCentral()))
.testResource("java/eclipse/format/JavaCodeUnformatted.test", "java/eclipse/format/JavaCodeFormatted.test");
.testResource("java/eclipse/JavaCodeUnformatted.test", "java/eclipse/JavaCodeFormatted.test");
}

@Test
public void longLiteralProblem() throws Throwable {
String folder = "java/eclipse/format/long_literals/";
String folder = "java/eclipse/long_literals/";
List<File> eclipseFormatFile = createTestFiles(folder + "spotless.eclipseformat.xml");
StepHarness.forStep(EclipseFormatterStep.create(eclipseFormatFile, TestProvisioner.mavenCentral()))
.testResourceUnaffected(folder + "Example1.test")
Expand All @@ -53,8 +53,8 @@ public void longLiteralProblem() throws Throwable {

@Test
public void equality() throws IOException {
List<File> xmlFile = createTestFiles("java/eclipse/format/formatter.xml");
List<File> propFile = createTestFiles("java/eclipse/format/formatter.properties");
List<File> xmlFile = createTestFiles("java/eclipse/formatter.xml");
List<File> propFile = createTestFiles("java/eclipse/formatter.properties");
new SerializableEqualityTester() {
List<File> settingsFiles;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

public class BumpThisNumberIfACustomStepChangesTest extends GradleIntegrationTest {

private void writeBuildFile(String toInsert) throws IOException {
write("build.gradle",
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
Expand All @@ -39,14 +38,13 @@ private void writeBuildFile(String toInsert) throws IOException {
}

private void writeContentWithBadFormatting() throws IOException {
write("README.md", "ABC");
setFile("README.md").toContent("ABC");
}

@Override
protected void applyIsUpToDate(boolean upToDate) throws IOException {
super.applyIsUpToDate(upToDate);
String result = read("README.md");
Assert.assertEquals("abc\n", result);
assertFile("README.md").hasContent("abc");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

public class CustomLazyGroovyTest extends GradleIntegrationTest {
@Test
public void integration() throws IOException {
write("build.gradle",
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
Expand All @@ -35,9 +34,8 @@ public void integration() throws IOException {
" }",
" }",
"}");
write("README.md", "ABC");
setFile("README.md").toContent("ABC");
gradleRunner().withArguments("spotlessApply").build();
String result = read("README.md");
Assert.assertEquals("abc\n", result);
assertFile("README.md").hasContent("abc");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected String getTaskErrorMessage(SpotlessTask task) {

@Test
public void lineEndingProblem() throws Exception {
SpotlessTask task = create(createTestFile("testFile", "A\r\nB\r\nC\r\n"));
SpotlessTask task = create(setFile("testFile").toContent("A\r\nB\r\nC\r\n"));
assertTaskFailure(task,
" testFile",
" @@ -1,3 +1,3 @@",
Expand All @@ -87,7 +87,7 @@ public void lineEndingProblem() throws Exception {

@Test
public void whitespaceProblem() throws Exception {
SpotlessTask task = create(createTestFile("testFile", "A \nB\t\nC \n"));
SpotlessTask task = create(setFile("testFile").toContent("A \nB\t\nC \n"));
task.addStep(FormatterStep.createNeverUpToDate("trimTrailing", input -> {
Pattern pattern = Pattern.compile("[ \t]+$", Pattern.UNIX_LINES | Pattern.MULTILINE);
return pattern.matcher(input).replaceAll("");
Expand All @@ -106,8 +106,8 @@ public void whitespaceProblem() throws Exception {
@Test
public void multipleFiles() throws Exception {
SpotlessTask task = create(
createTestFile("A", "1\r\n2\r\n"),
createTestFile("B", "3\n4\r\n"));
setFile("A").toContent("1\r\n2\r\n"),
setFile("B").toContent("3\n4\r\n"));
assertTaskFailure(task,
" A",
" @@ -1,2 +1,2 @@",
Expand All @@ -126,7 +126,7 @@ public void multipleFiles() throws Exception {
public void manyFiles() throws Exception {
List<File> testFiles = new ArrayList<>();
for (int i = 0; i < 9 + DiffMessageFormatter.MAX_FILES_TO_LIST - 1; ++i) {
testFiles.add(createTestFile(Integer.toString(i) + ".txt", "1\r\n2\r\n"));
testFiles.add(setFile(Integer.toString(i) + ".txt").toContent("1\r\n2\r\n"));
}
SpotlessTask task = create(testFiles);
assertTaskFailure(task,
Expand Down Expand Up @@ -199,7 +199,7 @@ public void manyFiles() throws Exception {
public void manyManyFiles() throws Exception {
List<File> testFiles = new ArrayList<>();
for (int i = 0; i < 9 + DiffMessageFormatter.MAX_FILES_TO_LIST; ++i) {
testFiles.add(createTestFile(Integer.toString(i) + ".txt", "1\r\n2\r\n"));
testFiles.add(setFile(Integer.toString(i) + ".txt").toContent("1\r\n2\r\n"));
}
SpotlessTask task = create(testFiles);
assertTaskFailure(task,
Expand Down Expand Up @@ -266,7 +266,7 @@ public void longFile() throws Exception {
builder.append(i);
builder.append("\r\n");
}
SpotlessTask task = create(createTestFile("testFile", builder.toString()));
SpotlessTask task = create(setFile("testFile").toContent(builder.toString()));
assertTaskFailure(task,
" testFile",
" @@ -1,1000 +1,1000 @@",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@

import java.nio.charset.Charset;

import org.junit.Assert;
import org.junit.Test;

import com.diffplug.spotless.LineEnding;

public class EncodingTest extends GradleIntegrationTest {
@Test
public void defaultIsUtf8() throws Exception {
write("build.gradle",
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
Expand All @@ -35,14 +32,14 @@ public void defaultIsUtf8() throws Exception {
" custom 'replaceMicro', { it.replace('µ', 'A') }",
" }",
"}");
write("test.java", "µ");
setFile("test.java").toContent("µ");
gradleRunner().withArguments("spotlessApply").build();
Assert.assertEquals("A\n", read("test.java"));
assertFile("test.java").hasContent("A");
}

@Test
public void globalIsRespected() throws Exception {
write("build.gradle",
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
Expand All @@ -53,14 +50,14 @@ public void globalIsRespected() throws Exception {
" }",
" encoding 'US-ASCII'",
"}");
write("test.java", "µ");
setFile("test.java").toContent("µ");
gradleRunner().withArguments("spotlessApply").build();
Assert.assertEquals("??\n", read("test.java"));
assertFile("test.java").hasContent("??");
}

@Test
public void globalIsRespectedButCanBeOverridden() throws Exception {
write("build.gradle",
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
Expand All @@ -76,12 +73,10 @@ public void globalIsRespectedButCanBeOverridden() throws Exception {
" }",
" encoding 'US-ASCII'",
"}");
write("test.java", "µ");
write("utf32.encoded", LineEnding.UNIX, Charset.forName("UTF-32"), "µ");
Assert.assertEquals(\n", read("utf32.encoded", Charset.forName("UTF-32")));

setFile("test.java").toContent("µ");
setFile("utf32.encoded").toContent("µ", Charset.forName("UTF-32"));
gradleRunner().withArguments("spotlessApply").build();
Assert.assertEquals("??\n", read("test.java"));
Assert.assertEquals("A\n", read("utf32.encoded", Charset.forName("UTF-32")));
assertFile("test.java").hasContent("??");
assertFile("utf32.encoded").hasContent("A", Charset.forName("UTF-32"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.assertj.core.api.Assertions;
import org.gradle.testkit.runner.BuildResult;
Expand Down Expand Up @@ -49,15 +48,15 @@ private void writeBuild(String... toInsert) throws IOException {
lines.add(" }");
lines.add(" }");
lines.addAll(Arrays.asList(toInsert));
write("build.gradle", lines.toArray(new String[lines.size()]));
setFile("build.gradle").toContent(String.join("\n", lines));
}

@Test
public void passesIfNoException() throws Exception {
writeBuild(
" } // format",
"} // spotless");
write("README.md", "This code is fun.");
setFile("README.md").toContent("This code is fun.");
runWithSuccess(":spotlessMisc");
}

Expand All @@ -66,7 +65,7 @@ public void anyExceptionShouldFail() throws Exception {
writeBuild(
" } // format",
"} // spotless");
write("README.md", "This code is fubar.");
setFile("README.md").toContent("This code is fubar.");
runWithFailure(
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
"No swearing!",
Expand All @@ -79,7 +78,7 @@ public void unlessEnforceCheckIsFalse() throws Exception {
" } // format",
" enforceCheck false",
"} // spotless");
write("README.md", "This code is fubar.");
setFile("README.md").toContent("This code is fubar.");
runWithSuccess(":compileJava UP-TO-DATE");
}

Expand All @@ -89,7 +88,7 @@ public void unlessExemptedByStep() throws Exception {
" ignoreErrorForStep 'no swearing'",
" } // format",
"} // spotless");
write("README.md", "This code is fubar.");
setFile("README.md").toContent("This code is fubar.");
runWithSuccess(":spotlessMisc",
"Unable to apply step 'no swearing' to 'README.md'");
}
Expand All @@ -100,7 +99,7 @@ public void unlessExemptedByPath() throws Exception {
" ignoreErrorForPath 'README.md'",
" } // format",
"} // spotless");
write("README.md", "This code is fubar.");
setFile("README.md").toContent("This code is fubar.");
runWithSuccess(":spotlessMisc",
"Unable to apply step 'no swearing' to 'README.md'");
}
Expand All @@ -112,7 +111,7 @@ public void failsIfNeitherStepNorFileExempted() throws Exception {
" ignoreErrorForPath 'nope'",
" } // format",
"} // spotless");
write("README.md", "This code is fubar.");
setFile("README.md").toContent("This code is fubar.");
runWithFailure(
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
"No swearing!",
Expand All @@ -133,7 +132,7 @@ private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, St
String expectedToStartWith = StringPrinter.buildStringFromLines(messages).trim();
int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith);
List<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput()));
String actualStart = actualLines.subList(0, numNewlines + 1).stream().collect(Collectors.joining("\n"));
String actualStart = String.join("\n", actualLines.subList(0, numNewlines + 1));
Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith);
Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size())
.isEqualTo(result.getTasks().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public void fileTree() throws IOException {

@Test
public void absolutePathDoesntWork() throws IOException {
File someFile = write("someFolder/someFile");
File someFile = setFile("someFolder/someFile").toContent("");
File someFolder = someFile.getParentFile();
fileTree.exclude(someFolder.getAbsolutePath());
Assertions.assertThat(fileTree).containsExactlyInAnyOrder(someFile);
}

@Test
public void relativePathDoes() throws IOException {
File someFile = write("someFolder/someFile");
File someFile = setFile("someFolder/someFile").toContent("");
File someFolder = someFile.getParentFile();
fileTree.exclude(relativize(rootFolder(), someFolder));
Assertions.assertThat(fileTree).containsExactlyInAnyOrder();
Expand Down
Loading

0 comments on commit d192b89

Please sign in to comment.