Skip to content

Commit

Permalink
Refactored ResourceHarness to a fluent API that avoids newline ambigu…
Browse files Browse the repository at this point in the history
…ities.
  • Loading branch information
nedtwigg committed Feb 12, 2018
1 parent 7958dcc commit 993390d
Show file tree
Hide file tree
Showing 31 changed files with 237 additions and 343 deletions.
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", 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", 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", 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("??", 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("µ", 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("??", read("test.java"));
Assert.assertEquals("A", 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");
}
}
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 FreshMarkExtensionTest extends GradleIntegrationTest {
@Test
public void integration() throws IOException {
write("build.gradle",
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'java'",
Expand All @@ -37,11 +36,8 @@ public void integration() throws IOException {
" }",
" }",
"}");
String unformatted = getTestResource("freshmark/FreshMarkUnformatted.test");
write("README.md", unformatted);
setFile("README.md").toResource("freshmark/FreshMarkUnformatted.test");
gradleRunner().withArguments("spotlessApply").build();
String result = read("README.md");
String formatted = getTestResource("freshmark/FreshMarkFormatted.test");
Assert.assertEquals(formatted, result);
assertFile("README.md").sameAsResource("freshmark/FreshMarkFormatted.test");
}
}
Loading

0 comments on commit 993390d

Please sign in to comment.