Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor testing #203

Merged
merged 4 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -49,15 +49,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(lines.stream().collect(Collectors.joining("\n")));
}

@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 +66,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 +79,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 +89,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 +100,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 +112,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 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
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,31 @@ public void createTask() {
@Test(expected = GradleException.class)
public void testLineEndingsCheckFail() throws IOException {
checkTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
checkTask.setTarget(Collections.singleton(createTestFile("testFile", "\r\n")));
checkTask.setTarget(Collections.singleton(setFile("testFile").toContent("\r\n")));
checkTask.execute();
}

@Test
public void testLineEndingsCheckPass() throws IOException {
checkTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
checkTask.setTarget(Collections.singleton(createTestFile("testFile", "\n")));
checkTask.setTarget(Collections.singleton(setFile("testFile").toContent("\n")));
checkTask.execute();
}

@Test
public void testLineEndingsApply() throws IOException {
File testFile = createTestFile("testFile", "\r\n");
File testFile = setFile("testFile").toContent("\r\n");

applyTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
applyTask.setTarget(Collections.singleton(testFile));
applyTask.execute();

assertFileContent("\n", testFile);
assertFile(testFile).hasContent("\n");
}

@Test
public void testStepCheckFail() throws IOException {
File testFile = createTestFile("testFile", "apple");
File testFile = setFile("testFile").toContent("apple");
checkTask.setTarget(Collections.singleton(testFile));

checkTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
Expand All @@ -81,28 +81,28 @@ public void testStepCheckFail() throws IOException {
" +aple");
Assertions.assertThatThrownBy(() -> checkTask.execute()).hasStackTraceContaining(diff);

assertFileContent("apple", testFile);
assertFile(testFile).hasContent("apple");
}

@Test
public void testStepCheckPass() throws IOException {
File testFile = createTestFile("testFile", "aple");
File testFile = setFile("testFile").toContent("aple");
checkTask.setTarget(Collections.singleton(testFile));

checkTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
checkTask.execute();

assertFileContent("aple", testFile);
assertFile(testFile).hasContent("aple");
}

@Test
public void testStepApply() throws IOException {
File testFile = createTestFile("testFile", "apple");
File testFile = setFile("testFile").toContent("apple");
applyTask.setTarget(Collections.singleton(testFile));

applyTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));
applyTask.execute();

assertFileContent("aple", testFile);
assertFile(testFile).hasContent("aple");
}
}
Loading