From 0a3d54202f310595f8286038ff7592eab7e3ed53 Mon Sep 17 00:00:00 2001 From: Matthias Balke Date: Fri, 16 Feb 2018 09:17:34 +0100 Subject: [PATCH 1/7] add support for missing generic steps (closes #207) add format section for custom formats --- .gitattributes | 3 + README.md | 20 +++--- plugin-maven/README.md | 54 ++++++++++++++++ .../spotless/maven/AbstractSpotlessMojo.java | 9 ++- .../maven/generic/EndWithNewline.java | 29 +++++++++ .../spotless/maven/generic/Format.java | 55 ++++++++++++++++ .../spotless/maven/generic/Indent.java | 63 +++++++++++++++++++ .../spotless/maven/generic/Replace.java | 28 +++++++++ .../spotless/maven/generic/ReplaceRegex.java | 28 +++++++++ .../maven/generic/TrimTrailingWhitespace.java | 29 +++++++++ .../diffplug/spotless/maven/java/Java.java | 8 +-- .../diffplug/spotless/maven/scala/Scala.java | 8 +-- .../spotless/maven/MavenIntegrationTest.java | 25 ++++++-- .../maven/generic/EndWithNewlineTest.java | 47 ++++++++++++++ .../spotless/maven/generic/IndentTest.java | 58 +++++++++++++++++ .../maven/generic/LicenseHeaderTest.java | 28 ++++++++- .../maven/generic/LineEndingsTest.java | 56 +++++++++++++++++ .../maven/generic/ReplaceRegexTest.java | 42 +++++++++++++ .../spotless/maven/generic/ReplaceTest.java | 42 +++++++++++++ .../generic/TrimTrailingWhitespaceTest.java | 39 ++++++++++++ .../resources/lineEndings/JavaCode-UNIX.test | 5 ++ .../lineEndings/JavaCode-WINDOWS.test | 5 ++ .../main/resources/newline/HasNewline.test | 28 +++++++++ .../resources/newline/MissingNewline.test | 28 +++++++++ .../resources/replace/JavaCodeFormatted.test | 5 ++ .../replace/JavaCodeUnformatted.test | 5 ++ .../trailingWhitespace/JavaCodeFormated.test | 6 ++ .../JavaCodeUnformated.test | 6 ++ 28 files changed, 732 insertions(+), 27 deletions(-) create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EndWithNewline.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java create mode 100644 testlib/src/main/resources/lineEndings/JavaCode-UNIX.test create mode 100644 testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test create mode 100644 testlib/src/main/resources/newline/HasNewline.test create mode 100644 testlib/src/main/resources/newline/MissingNewline.test create mode 100644 testlib/src/main/resources/replace/JavaCodeFormatted.test create mode 100644 testlib/src/main/resources/replace/JavaCodeUnformatted.test create mode 100644 testlib/src/main/resources/trailingWhitespace/JavaCodeFormated.test create mode 100644 testlib/src/main/resources/trailingWhitespace/JavaCodeUnformated.test diff --git a/.gitattributes b/.gitattributes index e6141d8894..7c3cb92a42 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,6 @@ * text eol=lf *.png binary *.jar binary + +# for newline tests this file needs crlf +testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test eol=crlf diff --git a/README.md b/README.md index 38aa599cfc..b230f876b8 100644 --- a/README.md +++ b/README.md @@ -32,12 +32,12 @@ function extra(className) { return '| [`' + className + '`](lib-extra/src/main/j output = [ '| Feature / FormatterStep | [plugin-gradle](plugin-gradle/README.md) | [plugin-maven](plugin-maven/README.md) | [(Your build tool here)](CONTRIBUTING.md#how-to-add-a-new-plugin-for-a-build-system) |', '| --------------------------------------------- | ------------- | ------------ | --------|', -lib('generic.EndWithNewlineStep') +'{{yes}} | {{no}} | {{no}} |', -lib('generic.IndentStep') +'{{yes}} | {{no}} | {{no}} |', +lib('generic.EndWithNewlineStep') +'{{yes}} | {{yes}} | {{no}} |', +lib('generic.IndentStep') +'{{yes}} | {{yes}} | {{no}} |', lib('generic.LicenseHeaderStep') +'{{yes}} | {{yes}} | {{no}} |', -lib('generic.ReplaceRegexStep') +'{{yes}} | {{no}} | {{no}} |', -lib('generic.ReplaceStep') +'{{yes}} | {{no}} | {{no}} |', -lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{no}} | {{no}} |', +lib('generic.ReplaceRegexStep') +'{{yes}} | {{yes}} | {{no}} |', +lib('generic.ReplaceStep') +'{{yes}} | {{yes}} | {{no}} |', +lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{yes}} | {{no}} |', extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{no}} | {{no}} |', lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}} | {{no}} |', lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{no}} |', @@ -55,12 +55,12 @@ lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} --> | Feature / FormatterStep | [plugin-gradle](plugin-gradle/README.md) | [plugin-maven](plugin-maven/README.md) | [(Your build tool here)](CONTRIBUTING.md#how-to-add-a-new-plugin-for-a-build-system) | | --------------------------------------------- | ------------- | ------------ | --------| -| [`generic.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :white_large_square: | :white_large_square: | -| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :white_large_square: | :white_large_square: | +| [`generic.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :+1: | :white_large_square: | +| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :+1: | :white_large_square: | | [`generic.LicenseHeaderStep`](lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java) | :+1: | :+1: | :white_large_square: | -| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :white_large_square: | :white_large_square: | -| [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :white_large_square: | :white_large_square: | -| [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :white_large_square: | :white_large_square: | +| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :+1: | :white_large_square: | +| [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :+1: | :white_large_square: | +| [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :+1: | :white_large_square: | | [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: | | [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :+1: | :white_large_square: | | [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :white_large_square: | diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 342b878115..b9d12bc119 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -115,6 +115,60 @@ By default, all files matching `src/main/java/**/*.java` and `src/test/java/**/* ``` + + +## Applying to custom sources + +By default, no Ant-Style include patterns are defined. Each element under `` is a step, and they will be applied in the order specified. Every step is optional, and they will be applied in the order specified. + +```xml + + + + + src/**/resources/**/*.properties + + + + + /* Licensed under Apache-2.0 */ + ${basedir}/license-header + + # + + + + + + + + + true + true + + 4 + + + + + + + + Say Hello to Mars + World + Mars + + + + + Say Hello to Mars from Regex + (Hello) W[a-z]{3}d + $1 Mars + + + +``` + ## Line endings and encodings (invisible stuff) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index 04fb5001cf..33bf1a7061 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -27,7 +27,6 @@ import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.plexus.util.FileUtils; @@ -38,6 +37,7 @@ import com.diffplug.spotless.Formatter; import com.diffplug.spotless.LineEnding; import com.diffplug.spotless.Provisioner; +import com.diffplug.spotless.maven.generic.Format; import com.diffplug.spotless.maven.generic.LicenseHeader; import com.diffplug.spotless.maven.java.Java; import com.diffplug.spotless.maven.scala.Scala; @@ -71,6 +71,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter private LicenseHeader licenseHeader; + @Parameter + private Format format; + @Parameter private Java java; @@ -80,7 +83,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { protected abstract void process(List files, Formatter formatter) throws MojoExecutionException; @Override - public final void execute() throws MojoExecutionException, MojoFailureException { + public final void execute() throws MojoExecutionException { List formatterFactories = getFormatterFactories(); for (FormatterFactory formatterFactory : formatterFactories) { @@ -127,7 +130,7 @@ private FormatterConfig getFormatterConfig() { } private List getFormatterFactories() { - return Stream.of(java, scala) + return Stream.of(format, java, scala) .filter(Objects::nonNull) .collect(toList()); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EndWithNewline.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EndWithNewline.java new file mode 100644 index 0000000000..4f042962a4 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EndWithNewline.java @@ -0,0 +1,29 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.EndWithNewlineStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class EndWithNewline implements FormatterStepFactory { + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + return EndWithNewlineStep.create(); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java new file mode 100644 index 0000000000..10f1ba505c --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import java.util.Collections; +import java.util.Set; + +import com.diffplug.spotless.maven.FormatterFactory; + +public class Format extends FormatterFactory { + + @Override + public Set defaultIncludes() { + return Collections.emptySet(); + } + + @Override + public String licenseHeaderDelimiter() { + // do not specify a default delimiter + return null; + } + + public void addEndWithNewline(EndWithNewline endWithNewline) { + addStepFactory(endWithNewline); + } + + public void addIndent(Indent indent) { + addStepFactory(indent); + } + + public void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) { + addStepFactory(trimTrailingWhitespace); + } + + public void addReplace(Replace replace) { + addStepFactory(replace); + } + + public void addReplaceRegex(ReplaceRegex replaceRegex) { + addStepFactory(replaceRegex); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java new file mode 100644 index 0000000000..bda26c4603 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java @@ -0,0 +1,63 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.apache.maven.plugins.annotations.Parameter; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.IndentStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class Indent implements FormatterStepFactory { + + private static final int DEFAULT_NUM_SPACES_PER_TAB = 4; + + @Parameter + private boolean withTabs; + + @Parameter + private boolean withSpaces; + + @Parameter + private Integer spacesPerTab; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + System.out.println("withTabs: " + withTabs); + System.out.println("withSpaces: " + withSpaces); + System.out.println("spacesPerTab: " + spacesPerTab); + + int amountOfSpacesPerTab = DEFAULT_NUM_SPACES_PER_TAB; + if (spacesPerTab != null) { + amountOfSpacesPerTab = spacesPerTab; + } + + if (withSpaces && withTabs) { + throw new IllegalArgumentException("Must specify exactly one of 'withSpaces' or 'withTabs'."); + } + + if (!withTabs && !withSpaces) { + return IndentStep.create(IndentStep.Type.SPACE, amountOfSpacesPerTab); + } else { + if (withSpaces) { + return IndentStep.create(IndentStep.Type.SPACE, amountOfSpacesPerTab); + } else { + return IndentStep.create(IndentStep.Type.TAB, amountOfSpacesPerTab); + } + } + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java new file mode 100644 index 0000000000..2967bf86ee --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java @@ -0,0 +1,28 @@ +package com.diffplug.spotless.maven.generic; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.ReplaceStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; +import org.apache.maven.plugins.annotations.Parameter; + +public class Replace implements FormatterStepFactory { + + @Parameter + private String name; + + @Parameter + private String search; + + @Parameter + private String replacement; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + if (name == null || search == null ||replacement == null) { + throw new IllegalArgumentException("Must specify 'name', 'search' and 'replacement'."); + } + + return ReplaceStep.create(name, search, replacement); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java new file mode 100644 index 0000000000..c4c8e88fca --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java @@ -0,0 +1,28 @@ +package com.diffplug.spotless.maven.generic; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.ReplaceRegexStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; +import org.apache.maven.plugins.annotations.Parameter; + +public class ReplaceRegex implements FormatterStepFactory { + + @Parameter + private String name; + + @Parameter + private String searchRegex; + + @Parameter + private String replacement; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + if (name == null || searchRegex == null ||replacement == null) { + throw new IllegalArgumentException("Must specify 'name', 'searchRegex' and 'replacement'."); + } + + return ReplaceRegexStep.create(name, searchRegex, replacement); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java new file mode 100644 index 0000000000..3f2a4bdbeb --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java @@ -0,0 +1,29 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.ReplaceRegexStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class TrimTrailingWhitespace implements FormatterStepFactory { + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + return ReplaceRegexStep.create("trimTrailingWhitespace", "[ \t]+$", ""); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java index f15444f75f..c680cc0ffa 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java @@ -15,14 +15,14 @@ */ package com.diffplug.spotless.maven.java; -import static com.diffplug.common.collect.Sets.newHashSet; -import static java.util.Collections.unmodifiableSet; +import com.diffplug.spotless.maven.generic.Format; import java.util.Set; -import com.diffplug.spotless.maven.FormatterFactory; +import static com.diffplug.common.collect.Sets.newHashSet; +import static java.util.Collections.unmodifiableSet; -public class Java extends FormatterFactory { +public class Java extends Format { private static final Set DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/java/**/*.java", "src/test/java/**/*.java")); private static final String LICENSE_HEADER_DELIMITER = "package "; diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java index 0f2034e883..4138784649 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java @@ -15,14 +15,14 @@ */ package com.diffplug.spotless.maven.scala; -import static com.diffplug.common.collect.Sets.newHashSet; -import static java.util.Collections.unmodifiableSet; +import com.diffplug.spotless.maven.generic.Format; import java.util.Set; -import com.diffplug.spotless.maven.FormatterFactory; +import static com.diffplug.common.collect.Sets.newHashSet; +import static java.util.Collections.unmodifiableSet; -public class Scala extends FormatterFactory { +public class Scala extends Format { private static final Set DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/scala/**/*.scala", "src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc")); private static final String LICENSE_HEADER_DELIMITER = "package "; diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java index 524c03c29a..9547113794 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java @@ -79,6 +79,10 @@ private File copy(String path) throws IOException { return target.toFile(); } + protected void writePomWithFormatSteps(String... steps) throws IOException { + writePom(groupWithSteps("format", including("src/**/java/**/*.java"), steps)); + } + protected void writePomWithJavaSteps(String... steps) throws IOException { writePom(groupWithSteps("java", steps)); } @@ -137,11 +141,24 @@ private static String getSystemProperty(String name) { return value; } + private static String[] groupWithSteps(String group, String[] includes, String... steps) { + String[] result = new String[steps.length + includes.length + 2]; + result[0] = "<" + group + ">"; + System.arraycopy(includes, 0, result, 1, includes.length); + System.arraycopy(steps, 0, result, includes.length + 1, steps.length); + result[result.length - 1] = ""; + return result; + } + private static String[] groupWithSteps(String group, String... steps) { - String[] result = new String[steps.length + 2]; - result[0] = '<' + group + '>'; - System.arraycopy(steps, 0, result, 1, steps.length); - result[result.length - 1] = "'; + return groupWithSteps(group, new String[]{}, steps); + } + + private static String[] including(String... include) { + String[] result = new String[include.length + 2]; + result[0] = ""; + System.arraycopy(include, 0, result, 1, include.length); + result[result.length - 1] = ""; return result; } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java new file mode 100644 index 0000000000..5a19d61f21 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; + +public class EndWithNewlineTest extends MavenIntegrationTest { + + @Test + public void fromContent() throws Exception { + writePomWithFormatSteps( + "", + ""); + runTest(); + } + + @Test + public void fromContentWithSelfclosingTag() throws Exception { + writePomWithFormatSteps( + ""); + runTest(); + } + + private void runTest() throws Exception { + setFile("src/main/java/test.java").toResource("newline/MissingNewline.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + String actual = read("src/main/java/test.java"); + assertThat(actual).isEqualTo(getTestResource("newline/HasNewline.test")); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java new file mode 100644 index 0000000000..f9a060ba01 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; + +public class IndentTest extends MavenIntegrationTest { + + @Test + public void fromContentToTabs() throws Exception { + writePomWithFormatSteps( + "", + " true", + ""); + runToTabTest(); + } + + @Test + public void fromContentToSpaces() throws Exception { + writePomWithFormatSteps( + "", + " true", + ""); + runToSpacesTest(); + } + + private void runToTabTest() throws Exception { + runTest("indent/IndentedWithSpace.test", "indent/IndentedWithTab.test"); + } + + private void runToSpacesTest() throws Exception { + runTest("indent/IndentedWithTab.test", "indent/IndentedWithSpace.test"); + } + + private void runTest(String source, String target) throws Exception { + setFile("src/main/java/test.java").toResource(source); + mavenRunner().withArguments("spotless:apply").runNoError(); + String actual = read("src/main/java/test.java"); + assertThat(actual).isEqualTo(getTestResource(target)); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java index c0e7f784d2..728829c237 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java @@ -25,7 +25,7 @@ public class LicenseHeaderTest extends MavenIntegrationTest { private static final String KEY_LICENSE = "license/TestLicense"; @Test - public void fromFile() throws Exception { + public void fromFileJava() throws Exception { setFile("license.txt").toResource(KEY_LICENSE); writePomWithJavaSteps( "", @@ -35,7 +35,7 @@ public void fromFile() throws Exception { } @Test - public void fromContent() throws Exception { + public void fromContentJava() throws Exception { writePomWithJavaSteps( "", " ", @@ -57,6 +57,30 @@ public void fromFileGlobal() throws Exception { runTest(); } + @Test + public void fromFileFormat() throws Exception { + setFile("license.txt").toResource(KEY_LICENSE); + writePomWithFormatSteps( + "", + " ${basedir}/license.txt", + " package", + ""); + runTest(); + } + + @Test + public void fromContentFormat() throws Exception { + writePomWithFormatSteps( + "", + " ", + "// If you can't trust a man's word", + "// Does it help to have it in writing?", + " ", + " package", + ""); + runTest(); + } + private void runTest() throws Exception { setFile("src/main/java/test.java").toResource("license/MissingLicense.test"); mavenRunner().withArguments("spotless:apply").runNoError(); diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java new file mode 100644 index 0000000000..fc6f9afa80 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; + +public class LineEndingsTest extends MavenIntegrationTest { + + @Test + public void fromContentToWindows() throws Exception { + writePomWithFormatSteps( + "WINDOWS" + ); + runToWindowsTest(); + } + + @Test + public void fromContentToUnix() throws Exception { + writePomWithFormatSteps( + "UNIX" + ); + runToUnixTest(); + } + + private void runToWindowsTest() throws Exception { + runTest("lineEndings/JavaCode-UNIX.test", "lineEndings/JavaCode-WINDOWS.test"); + } + + private void runToUnixTest() throws Exception { + runTest("lineEndings/JavaCode-WINDOWS.test", "lineEndings/JavaCode-UNIX.test"); + } + + private void runTest(String source, String target) throws Exception { + setFile("src/main/java/test.java").toResource(source); + mavenRunner().withArguments("spotless:apply").runNoError(); + String actual = read("src/main/java/test.java"); + assertThat(actual).isEqualTo(getTestResource(target)); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java new file mode 100644 index 0000000000..444c66bcee --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import com.diffplug.spotless.maven.MavenIntegrationTest; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ReplaceRegexTest extends MavenIntegrationTest { + + @Test + public void fromContent() throws Exception { + writePomWithFormatSteps( + "", + " Greetings to Mars", + " (hello) w[a-z]{3}d", + " $1 mars", + ""); + runTest(); + } + + private void runTest() throws Exception { + setFile("src/main/java/test.java").toResource("replace/JavaCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + String actual = read("src/main/java/test.java"); + assertThat(actual).isEqualTo(getTestResource("replace/JavaCodeFormatted.test")); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java new file mode 100644 index 0000000000..5f0524a97f --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import com.diffplug.spotless.maven.MavenIntegrationTest; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ReplaceTest extends MavenIntegrationTest { + + @Test + public void fromContent() throws Exception { + writePomWithFormatSteps( + "", + " Greetings to Mars", + " world", + " mars", + ""); + runTest(); + } + + private void runTest() throws Exception { + setFile("src/main/java/test.java").toResource("replace/JavaCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + String actual = read("src/main/java/test.java"); + assertThat(actual).isEqualTo(getTestResource("replace/JavaCodeFormatted.test")); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java new file mode 100644 index 0000000000..5f9557fc10 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import com.diffplug.spotless.maven.MavenIntegrationTest; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TrimTrailingWhitespaceTest extends MavenIntegrationTest { + + @Test + public void fromContentToTabs() throws Exception { + writePomWithFormatSteps( + ""); + runTest(); + } + + private void runTest() throws Exception { + setFile("src/main/java/test.java").toResource("trailingWhitespace/JavaCodeUnformated.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + String actual = read("src/main/java/test.java"); + assertThat(actual).isEqualTo(getTestResource("trailingWhitespace/JavaCodeFormated.test")); + } + +} diff --git a/testlib/src/main/resources/lineEndings/JavaCode-UNIX.test b/testlib/src/main/resources/lineEndings/JavaCode-UNIX.test new file mode 100644 index 0000000000..fc8b92a1ea --- /dev/null +++ b/testlib/src/main/resources/lineEndings/JavaCode-UNIX.test @@ -0,0 +1,5 @@ +public class Java { + public static void main(String[] args) { + System.out.println("hello"); + } +} \ No newline at end of file diff --git a/testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test b/testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test new file mode 100644 index 0000000000..fc8b92a1ea --- /dev/null +++ b/testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test @@ -0,0 +1,5 @@ +public class Java { + public static void main(String[] args) { + System.out.println("hello"); + } +} \ No newline at end of file diff --git a/testlib/src/main/resources/newline/HasNewline.test b/testlib/src/main/resources/newline/HasNewline.test new file mode 100644 index 0000000000..297c16a214 --- /dev/null +++ b/testlib/src/main/resources/newline/HasNewline.test @@ -0,0 +1,28 @@ +package com.acme; + +import java.util.function.Function; + + +public class Java8Test { + public void doStuff() throws Exception { + Function example = Integer::parseInt; + example.andThen(val -> { + return val + 2; + } ); + SimpleEnum val = SimpleEnum.A; + switch (val) { + case A: + break; + case B: + break; + case C: + break; + default: + throw new Exception(); + } + } + + public enum SimpleEnum { + A, B, C; + } +} diff --git a/testlib/src/main/resources/newline/MissingNewline.test b/testlib/src/main/resources/newline/MissingNewline.test new file mode 100644 index 0000000000..abdfa489f3 --- /dev/null +++ b/testlib/src/main/resources/newline/MissingNewline.test @@ -0,0 +1,28 @@ +package com.acme; + +import java.util.function.Function; + + +public class Java8Test { + public void doStuff() throws Exception { + Function example = Integer::parseInt; + example.andThen(val -> { + return val + 2; + } ); + SimpleEnum val = SimpleEnum.A; + switch (val) { + case A: + break; + case B: + break; + case C: + break; + default: + throw new Exception(); + } + } + + public enum SimpleEnum { + A, B, C; + } +} \ No newline at end of file diff --git a/testlib/src/main/resources/replace/JavaCodeFormatted.test b/testlib/src/main/resources/replace/JavaCodeFormatted.test new file mode 100644 index 0000000000..533e74479f --- /dev/null +++ b/testlib/src/main/resources/replace/JavaCodeFormatted.test @@ -0,0 +1,5 @@ +public class Java { + public static void main(String[] args) { + System.out.println("hello mars"); + } +} diff --git a/testlib/src/main/resources/replace/JavaCodeUnformatted.test b/testlib/src/main/resources/replace/JavaCodeUnformatted.test new file mode 100644 index 0000000000..5cab0798ef --- /dev/null +++ b/testlib/src/main/resources/replace/JavaCodeUnformatted.test @@ -0,0 +1,5 @@ +public class Java { + public static void main(String[] args) { + System.out.println("hello world"); + } +} diff --git a/testlib/src/main/resources/trailingWhitespace/JavaCodeFormated.test b/testlib/src/main/resources/trailingWhitespace/JavaCodeFormated.test new file mode 100644 index 0000000000..feedbb4ea5 --- /dev/null +++ b/testlib/src/main/resources/trailingWhitespace/JavaCodeFormated.test @@ -0,0 +1,6 @@ + +public class Java { + public static void main(String[] args) { + System.out.println("hello"); + } +} diff --git a/testlib/src/main/resources/trailingWhitespace/JavaCodeUnformated.test b/testlib/src/main/resources/trailingWhitespace/JavaCodeUnformated.test new file mode 100644 index 0000000000..feedbb4ea5 --- /dev/null +++ b/testlib/src/main/resources/trailingWhitespace/JavaCodeUnformated.test @@ -0,0 +1,6 @@ + +public class Java { + public static void main(String[] args) { + System.out.println("hello"); + } +} From 98bfe1e14096d64bd7aa7efc776293341698e825 Mon Sep 17 00:00:00 2001 From: Matthias Balke Date: Thu, 22 Feb 2018 18:26:03 +0100 Subject: [PATCH 2/7] reformated code --- .../spotless/maven/generic/Replace.java | 20 +++++++++++++++++-- .../spotless/maven/generic/ReplaceRegex.java | 20 +++++++++++++++++-- .../diffplug/spotless/maven/java/Java.java | 6 +++--- .../diffplug/spotless/maven/scala/Scala.java | 6 +++--- .../maven/generic/LineEndingsTest.java | 6 ++---- .../maven/generic/ReplaceRegexTest.java | 5 +++-- .../spotless/maven/generic/ReplaceTest.java | 5 +++-- .../generic/TrimTrailingWhitespaceTest.java | 5 +++-- 8 files changed, 53 insertions(+), 20 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java index 2967bf86ee..947ceee8e2 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java @@ -1,10 +1,26 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.diffplug.spotless.maven.generic; +import org.apache.maven.plugins.annotations.Parameter; + import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.generic.ReplaceStep; import com.diffplug.spotless.maven.FormatterStepConfig; import com.diffplug.spotless.maven.FormatterStepFactory; -import org.apache.maven.plugins.annotations.Parameter; public class Replace implements FormatterStepFactory { @@ -19,7 +35,7 @@ public class Replace implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { - if (name == null || search == null ||replacement == null) { + if (name == null || search == null || replacement == null) { throw new IllegalArgumentException("Must specify 'name', 'search' and 'replacement'."); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java index c4c8e88fca..243f14220e 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java @@ -1,10 +1,26 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.diffplug.spotless.maven.generic; +import org.apache.maven.plugins.annotations.Parameter; + import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.generic.ReplaceRegexStep; import com.diffplug.spotless.maven.FormatterStepConfig; import com.diffplug.spotless.maven.FormatterStepFactory; -import org.apache.maven.plugins.annotations.Parameter; public class ReplaceRegex implements FormatterStepFactory { @@ -19,7 +35,7 @@ public class ReplaceRegex implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { - if (name == null || searchRegex == null ||replacement == null) { + if (name == null || searchRegex == null || replacement == null) { throw new IllegalArgumentException("Must specify 'name', 'searchRegex' and 'replacement'."); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java index c680cc0ffa..a8c0078fb9 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java @@ -15,12 +15,12 @@ */ package com.diffplug.spotless.maven.java; -import com.diffplug.spotless.maven.generic.Format; +import static com.diffplug.common.collect.Sets.newHashSet; +import static java.util.Collections.unmodifiableSet; import java.util.Set; -import static com.diffplug.common.collect.Sets.newHashSet; -import static java.util.Collections.unmodifiableSet; +import com.diffplug.spotless.maven.generic.Format; public class Java extends Format { private static final Set DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/java/**/*.java", diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java index 4138784649..dbaada1cad 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java @@ -15,12 +15,12 @@ */ package com.diffplug.spotless.maven.scala; -import com.diffplug.spotless.maven.generic.Format; +import static com.diffplug.common.collect.Sets.newHashSet; +import static java.util.Collections.unmodifiableSet; import java.util.Set; -import static com.diffplug.common.collect.Sets.newHashSet; -import static java.util.Collections.unmodifiableSet; +import com.diffplug.spotless.maven.generic.Format; public class Scala extends Format { private static final Set DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/scala/**/*.scala", diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java index fc6f9afa80..03303519c0 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java @@ -26,16 +26,14 @@ public class LineEndingsTest extends MavenIntegrationTest { @Test public void fromContentToWindows() throws Exception { writePomWithFormatSteps( - "WINDOWS" - ); + "WINDOWS"); runToWindowsTest(); } @Test public void fromContentToUnix() throws Exception { writePomWithFormatSteps( - "UNIX" - ); + "UNIX"); runToUnixTest(); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java index 444c66bcee..91f4a8f5f2 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java @@ -15,10 +15,11 @@ */ package com.diffplug.spotless.maven.generic; -import com.diffplug.spotless.maven.MavenIntegrationTest; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; +import com.diffplug.spotless.maven.MavenIntegrationTest; public class ReplaceRegexTest extends MavenIntegrationTest { diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java index 5f0524a97f..4444919ae6 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java @@ -15,10 +15,11 @@ */ package com.diffplug.spotless.maven.generic; -import com.diffplug.spotless.maven.MavenIntegrationTest; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; +import com.diffplug.spotless.maven.MavenIntegrationTest; public class ReplaceTest extends MavenIntegrationTest { diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java index 5f9557fc10..16800f40d8 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java @@ -15,10 +15,11 @@ */ package com.diffplug.spotless.maven.generic; -import com.diffplug.spotless.maven.MavenIntegrationTest; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; +import com.diffplug.spotless.maven.MavenIntegrationTest; public class TrimTrailingWhitespaceTest extends MavenIntegrationTest { From 0ab7ca09f45cceae312ce245276b1c8b42c79387 Mon Sep 17 00:00:00 2001 From: Matthias Balke Date: Thu, 22 Feb 2018 19:32:08 +0100 Subject: [PATCH 3/7] fixed review findings --- .gitattributes | 3 -- .../diffplug/spotless/generic/IndentStep.java | 12 ++++++++ .../gradle/spotless/FormatExtension.java | 4 +-- plugin-maven/README.md | 6 ++-- .../spotless/maven/generic/Indent.java | 30 +++++++------------ .../maven/generic/EndWithNewlineTest.java | 9 +++--- .../spotless/maven/generic/IndentTest.java | 12 ++++---- .../maven/generic/LineEndingsTest.java | 22 +++++++++----- .../main/resources/newline/HasNewline.test | 28 ----------------- .../resources/newline/MissingNewline.test | 28 ----------------- 10 files changed, 50 insertions(+), 104 deletions(-) delete mode 100644 testlib/src/main/resources/newline/HasNewline.test delete mode 100644 testlib/src/main/resources/newline/MissingNewline.test diff --git a/.gitattributes b/.gitattributes index 7c3cb92a42..e6141d8894 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,6 +1,3 @@ * text eol=lf *.png binary *.jar binary - -# for newline tests this file needs crlf -testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test eol=crlf diff --git a/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java b/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java index 2ccdccefc5..e50a45b82e 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java @@ -23,6 +23,9 @@ /** Simple step which checks for consistent indentation characters. */ public final class IndentStep { + + private static final int DEFAULT_NUM_SPACES_PER_TAB = 4; + // prevent direct instantiation private IndentStep() {} @@ -33,6 +36,11 @@ private T tabSpace(T tab, T space) { return this == TAB ? tab : space; } + /** Creates a step which will indent with the given type of whitespace, converting between tabs and spaces at the default ratio. */ + public FormatterStep create() { + return IndentStep.create(this, defaultNumSpacesPerTab()); + } + /** Synonym for {@link IndentStep#create(Type, int)}. */ public FormatterStep create(int numSpacesPerTab) { return IndentStep.create(this, numSpacesPerTab); @@ -128,4 +136,8 @@ String format(String raw) { private static boolean isSpaceOrTab(char c) { return c == ' ' || c == '\t'; } + + public static int defaultNumSpacesPerTab() { + return DEFAULT_NUM_SPACES_PER_TAB; + } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index 77d5259881..7071dbd6ee 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -332,7 +332,7 @@ public void indentWithSpaces(int numSpacesPerTab) { /** Ensures that the files are indented using spaces. */ public void indentWithSpaces() { - indentWithSpaces(4); + addStep(IndentStep.Type.SPACE.create()); } /** Ensures that the files are indented using tabs. */ @@ -342,7 +342,7 @@ public void indentWithTabs(int tabToSpaces) { /** Ensures that the files are indented using tabs. */ public void indentWithTabs() { - indentWithTabs(4); + addStep(IndentStep.Type.TAB.create()); } abstract class LicenseHeaderConfig { diff --git a/plugin-maven/README.md b/plugin-maven/README.md index b9d12bc119..4223d6c091 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -142,9 +142,9 @@ By default, no Ant-Style include patterns are defined. Each element under ` - - true - true + + true + true 4 diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java index bda26c4603..1d1b6925e8 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java @@ -24,40 +24,30 @@ public class Indent implements FormatterStepFactory { - private static final int DEFAULT_NUM_SPACES_PER_TAB = 4; - @Parameter - private boolean withTabs; + private boolean tabs; @Parameter - private boolean withSpaces; + private boolean spaces; @Parameter private Integer spacesPerTab; @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { - System.out.println("withTabs: " + withTabs); - System.out.println("withSpaces: " + withSpaces); - System.out.println("spacesPerTab: " + spacesPerTab); - - int amountOfSpacesPerTab = DEFAULT_NUM_SPACES_PER_TAB; - if (spacesPerTab != null) { - amountOfSpacesPerTab = spacesPerTab; - } - if (withSpaces && withTabs) { - throw new IllegalArgumentException("Must specify exactly one of 'withSpaces' or 'withTabs'."); + if (spacesPerTab == null) { + spacesPerTab = IndentStep.defaultNumSpacesPerTab(); } - if (!withTabs && !withSpaces) { - return IndentStep.create(IndentStep.Type.SPACE, amountOfSpacesPerTab); - } else { - if (withSpaces) { - return IndentStep.create(IndentStep.Type.SPACE, amountOfSpacesPerTab); + if (spaces ^ tabs) { + if (spaces) { + return IndentStep.create(IndentStep.Type.SPACE, spacesPerTab); } else { - return IndentStep.create(IndentStep.Type.TAB, amountOfSpacesPerTab); + return IndentStep.create(IndentStep.Type.TAB, spacesPerTab); } + } else { + throw new IllegalArgumentException("Must specify exactly one of 'spaces' or 'tabs'."); } } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java index 5a19d61f21..2215b8899f 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.generic; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; import com.diffplug.spotless.maven.MavenIntegrationTest; @@ -39,9 +37,10 @@ public void fromContentWithSelfclosingTag() throws Exception { } private void runTest() throws Exception { - setFile("src/main/java/test.java").toResource("newline/MissingNewline.test"); + String noTrailingNewline = "public class Java {}"; + String hasTrailingNewline = noTrailingNewline + "\n"; + setFile("src/main/java/test.java").toContent(noTrailingNewline); mavenRunner().withArguments("spotless:apply").runNoError(); - String actual = read("src/main/java/test.java"); - assertThat(actual).isEqualTo(getTestResource("newline/HasNewline.test")); + assertFile("src/main/java/test.java").hasContent(hasTrailingNewline); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java index f9a060ba01..915c66c59f 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.generic; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; import com.diffplug.spotless.maven.MavenIntegrationTest; @@ -27,7 +25,7 @@ public class IndentTest extends MavenIntegrationTest { public void fromContentToTabs() throws Exception { writePomWithFormatSteps( "", - " true", + " true", ""); runToTabTest(); } @@ -36,7 +34,7 @@ public void fromContentToTabs() throws Exception { public void fromContentToSpaces() throws Exception { writePomWithFormatSteps( "", - " true", + " true", ""); runToSpacesTest(); } @@ -50,9 +48,9 @@ private void runToSpacesTest() throws Exception { } private void runTest(String source, String target) throws Exception { - setFile("src/main/java/test.java").toResource(source); + String path = "src/main/java/test.java"; + setFile(path).toResource(source); mavenRunner().withArguments("spotless:apply").runNoError(); - String actual = read("src/main/java/test.java"); - assertThat(actual).isEqualTo(getTestResource(target)); + assertFile(path).sameAsResource(target); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java index 03303519c0..8d23527975 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.generic; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; import com.diffplug.spotless.maven.MavenIntegrationTest; @@ -38,17 +36,25 @@ public void fromContentToUnix() throws Exception { } private void runToWindowsTest() throws Exception { - runTest("lineEndings/JavaCode-UNIX.test", "lineEndings/JavaCode-WINDOWS.test"); + runTest(getClassWithLineEndings("\n"), getClassWithLineEndings("\r\n")); } private void runToUnixTest() throws Exception { - runTest("lineEndings/JavaCode-WINDOWS.test", "lineEndings/JavaCode-UNIX.test"); + runTest(getClassWithLineEndings("\r\n"), getClassWithLineEndings("\n")); } - private void runTest(String source, String target) throws Exception { - setFile("src/main/java/test.java").toResource(source); + private void runTest(String sourceContent, String targetContent) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toContent(sourceContent); mavenRunner().withArguments("spotless:apply").runNoError(); - String actual = read("src/main/java/test.java"); - assertThat(actual).isEqualTo(getTestResource(target)); + assertFile(path).hasContent(targetContent); + } + + private String getClassWithLineEndings(String lineEnding) { + return "public class Java {" + lineEnding + + " public static void main(String[] args) {" + lineEnding + + " System.out.println(\"hello\");" + lineEnding + + " }" + lineEnding + + "}"; } } diff --git a/testlib/src/main/resources/newline/HasNewline.test b/testlib/src/main/resources/newline/HasNewline.test deleted file mode 100644 index 297c16a214..0000000000 --- a/testlib/src/main/resources/newline/HasNewline.test +++ /dev/null @@ -1,28 +0,0 @@ -package com.acme; - -import java.util.function.Function; - - -public class Java8Test { - public void doStuff() throws Exception { - Function example = Integer::parseInt; - example.andThen(val -> { - return val + 2; - } ); - SimpleEnum val = SimpleEnum.A; - switch (val) { - case A: - break; - case B: - break; - case C: - break; - default: - throw new Exception(); - } - } - - public enum SimpleEnum { - A, B, C; - } -} diff --git a/testlib/src/main/resources/newline/MissingNewline.test b/testlib/src/main/resources/newline/MissingNewline.test deleted file mode 100644 index abdfa489f3..0000000000 --- a/testlib/src/main/resources/newline/MissingNewline.test +++ /dev/null @@ -1,28 +0,0 @@ -package com.acme; - -import java.util.function.Function; - - -public class Java8Test { - public void doStuff() throws Exception { - Function example = Integer::parseInt; - example.andThen(val -> { - return val + 2; - } ); - SimpleEnum val = SimpleEnum.A; - switch (val) { - case A: - break; - case B: - break; - case C: - break; - default: - throw new Exception(); - } - } - - public enum SimpleEnum { - A, B, C; - } -} \ No newline at end of file From 23a1d1b01073966e7df08880db2b8b4e9e5b5acf Mon Sep 17 00:00:00 2001 From: Matthias Balke Date: Thu, 22 Feb 2018 20:03:23 +0100 Subject: [PATCH 4/7] use assertFile instead of assertThat.isEqualTo for all tests --- .../maven/generic/LicenseHeaderTest.java | 8 +++----- .../spotless/maven/generic/ReplaceRegexTest.java | 8 +++----- .../spotless/maven/generic/ReplaceTest.java | 8 +++----- .../generic/TrimTrailingWhitespaceTest.java | 8 +++----- .../maven/java/EclipseFormatStepTest.java | 5 +++-- .../maven/java/GoogleJavaFormatTest.java | 13 ++++++++----- .../spotless/maven/java/ImportOrderTest.java | 5 +++-- .../maven/java/RemoveUnusedImportsStepTest.java | 5 +++-- .../spotless/maven/scala/ScalafmtTest.java | 16 ++++++++++------ .../resources/lineEndings/JavaCode-UNIX.test | 5 ----- .../resources/lineEndings/JavaCode-WINDOWS.test | 5 ----- 11 files changed, 39 insertions(+), 47 deletions(-) delete mode 100644 testlib/src/main/resources/lineEndings/JavaCode-UNIX.test delete mode 100644 testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java index 728829c237..957bbd2e5c 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.generic; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; import com.diffplug.spotless.maven.MavenIntegrationTest; @@ -82,9 +80,9 @@ public void fromContentFormat() throws Exception { } private void runTest() throws Exception { - setFile("src/main/java/test.java").toResource("license/MissingLicense.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("license/MissingLicense.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - String actual = read("src/main/java/test.java"); - assertThat(actual).isEqualTo(getTestResource("license/HasLicense.test")); + assertFile(path).sameAsResource("license/HasLicense.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java index 91f4a8f5f2..c408e9e892 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.generic; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; import com.diffplug.spotless.maven.MavenIntegrationTest; @@ -35,9 +33,9 @@ public void fromContent() throws Exception { } private void runTest() throws Exception { - setFile("src/main/java/test.java").toResource("replace/JavaCodeUnformatted.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("replace/JavaCodeUnformatted.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - String actual = read("src/main/java/test.java"); - assertThat(actual).isEqualTo(getTestResource("replace/JavaCodeFormatted.test")); + assertFile(path).sameAsResource("replace/JavaCodeFormatted.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java index 4444919ae6..78e2d30668 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.generic; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; import com.diffplug.spotless.maven.MavenIntegrationTest; @@ -35,9 +33,9 @@ public void fromContent() throws Exception { } private void runTest() throws Exception { - setFile("src/main/java/test.java").toResource("replace/JavaCodeUnformatted.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("replace/JavaCodeUnformatted.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - String actual = read("src/main/java/test.java"); - assertThat(actual).isEqualTo(getTestResource("replace/JavaCodeFormatted.test")); + assertFile(path).sameAsResource("replace/JavaCodeFormatted.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java index 16800f40d8..28dd8530af 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.generic; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; import com.diffplug.spotless.maven.MavenIntegrationTest; @@ -31,10 +29,10 @@ public void fromContentToTabs() throws Exception { } private void runTest() throws Exception { - setFile("src/main/java/test.java").toResource("trailingWhitespace/JavaCodeUnformated.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("trailingWhitespace/JavaCodeUnformated.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - String actual = read("src/main/java/test.java"); - assertThat(actual).isEqualTo(getTestResource("trailingWhitespace/JavaCodeFormated.test")); + assertFile(path).sameAsResource("trailingWhitespace/JavaCodeFormated.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/EclipseFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/EclipseFormatStepTest.java index f97dc2d664..af9f06e15d 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/EclipseFormatStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/EclipseFormatStepTest.java @@ -30,8 +30,9 @@ public void testEclipse() throws Exception { ""); setFile("formatter.xml").toResource("java/eclipse/formatter.xml"); - setFile("src/main/java/test.java").toResource("java/eclipse/JavaCodeUnformatted.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("java/eclipse/JavaCodeUnformatted.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/eclipse/JavaCodeFormatted.test"); + assertFile(path).sameAsResource("java/eclipse/JavaCodeFormatted.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java index e32fde7e00..137cf4c7fc 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java @@ -27,9 +27,7 @@ public void specificVersionDefaultStyle() throws Exception { " 1.2", ""); - setFile("src/main/java/test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/googlejavaformat/JavaCodeFormatted.test"); + runTest("java/googlejavaformat/JavaCodeFormatted.test"); } @Test @@ -40,8 +38,13 @@ public void specificVersionSpecificStyle() throws Exception { " ", ""); - setFile("src/main/java/test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test"); + runTest("java/googlejavaformat/JavaCodeFormattedAOSP.test"); + } + + private void runTest(String targetResource) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toResource("java/googlejavaformat/JavaCodeUnformatted.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/googlejavaformat/JavaCodeFormattedAOSP.test"); + assertFile(path).sameAsResource(targetResource); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ImportOrderTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ImportOrderTest.java index 99a5bb219d..aa9db93793 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ImportOrderTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ImportOrderTest.java @@ -40,8 +40,9 @@ public void order() throws Exception { } private void runTest() throws Exception { - setFile("src/main/java/test.java").toResource("java/importsorter/JavaCodeUnsortedImports.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("java/importsorter/JavaCodeUnsortedImports.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/importsorter/JavaCodeSortedImports.test"); + assertFile(path).sameAsResource("java/importsorter/JavaCodeSortedImports.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java index 9ef2134582..4cad34ef90 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java @@ -25,8 +25,9 @@ public class RemoveUnusedImportsStepTest extends MavenIntegrationTest { public void testRemoveUnusedInports() throws Exception { writePomWithJavaSteps(""); - setFile("src/main/java/test.java").toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test"); + assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java index d070a92f0f..50be37addb 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java @@ -24,21 +24,25 @@ public class ScalafmtTest extends MavenIntegrationTest { public void testScalafmtWithDefaultConfig() throws Exception { writePomWithScalaSteps(""); - setFile("src/main/scala/test.scala").toResource("scala/scalafmt/basic.dirty"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/scala/test.scala").sameAsResource("scala/scalafmt/basic.clean"); + runTest("scala/scalafmt/basic.clean"); } @Test public void testScalafmtWithCustomConfig() throws Exception { + setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt.conf"); + writePomWithScalaSteps( "", " ${project.basedir}/scalafmt.conf", ""); - setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt.conf"); - setFile("src/main/scala/test.scala").toResource("scala/scalafmt/basic.dirty"); + runTest("scala/scalafmt/basic.cleanWithCustomConf"); + } + + private void runTest(String s) throws Exception { + String path = "src/main/scala/test.scala"; + setFile(path).toResource("scala/scalafmt/basic.dirty"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/scala/test.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf"); + assertFile(path).sameAsResource(s); } } diff --git a/testlib/src/main/resources/lineEndings/JavaCode-UNIX.test b/testlib/src/main/resources/lineEndings/JavaCode-UNIX.test deleted file mode 100644 index fc8b92a1ea..0000000000 --- a/testlib/src/main/resources/lineEndings/JavaCode-UNIX.test +++ /dev/null @@ -1,5 +0,0 @@ -public class Java { - public static void main(String[] args) { - System.out.println("hello"); - } -} \ No newline at end of file diff --git a/testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test b/testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test deleted file mode 100644 index fc8b92a1ea..0000000000 --- a/testlib/src/main/resources/lineEndings/JavaCode-WINDOWS.test +++ /dev/null @@ -1,5 +0,0 @@ -public class Java { - public static void main(String[] args) { - System.out.println("hello"); - } -} \ No newline at end of file From 2256c732d5dc61c2351914e5111c72038c90c607 Mon Sep 17 00:00:00 2001 From: Matthias Balke Date: Thu, 22 Feb 2018 20:40:01 +0100 Subject: [PATCH 5/7] fixed review findings --- .../spotless/maven/generic/ReplaceRegexTest.java | 9 +++++---- .../diffplug/spotless/maven/generic/ReplaceTest.java | 12 ++++++------ .../maven/generic/TrimTrailingWhitespaceTest.java | 11 +++++++---- .../main/resources/replace/JavaCodeFormatted.test | 5 ----- .../main/resources/replace/JavaCodeUnformatted.test | 5 ----- .../trailingWhitespace/JavaCodeFormated.test | 6 ------ .../trailingWhitespace/JavaCodeUnformated.test | 6 ------ 7 files changed, 18 insertions(+), 36 deletions(-) delete mode 100644 testlib/src/main/resources/replace/JavaCodeFormatted.test delete mode 100644 testlib/src/main/resources/replace/JavaCodeUnformatted.test delete mode 100644 testlib/src/main/resources/trailingWhitespace/JavaCodeFormated.test delete mode 100644 testlib/src/main/resources/trailingWhitespace/JavaCodeUnformated.test diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java index c408e9e892..204b0e57a4 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java @@ -29,13 +29,14 @@ public void fromContent() throws Exception { " (hello) w[a-z]{3}d", " $1 mars", ""); - runTest(); + + runTest("hello world", "hello mars"); } - private void runTest() throws Exception { + private void runTest(String sourceContent, String targetContent) throws Exception { String path = "src/main/java/test.java"; - setFile(path).toResource("replace/JavaCodeUnformatted.test"); + setFile(path).toContent(sourceContent); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("replace/JavaCodeFormatted.test"); + assertFile(path).hasContent(targetContent); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java index 78e2d30668..04ae5ae6d0 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java @@ -26,16 +26,16 @@ public void fromContent() throws Exception { writePomWithFormatSteps( "", " Greetings to Mars", - " world", - " mars", + " World", + " Mars", ""); - runTest(); + runTest("Hello World", "Hello Mars"); } - private void runTest() throws Exception { + private void runTest(String sourceContent, String targetContent) throws Exception { String path = "src/main/java/test.java"; - setFile(path).toResource("replace/JavaCodeUnformatted.test"); + setFile(path).toContent(sourceContent); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("replace/JavaCodeFormatted.test"); + assertFile(path).hasContent(targetContent); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java index 28dd8530af..b8f930cf12 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java @@ -25,14 +25,17 @@ public class TrimTrailingWhitespaceTest extends MavenIntegrationTest { public void fromContentToTabs() throws Exception { writePomWithFormatSteps( ""); - runTest(); + + String target = "This line ends with whitespaces"; + String source = target + " "; + runTest(source, target); } - private void runTest() throws Exception { + private void runTest(String sourceContent, String targetContent) throws Exception { String path = "src/main/java/test.java"; - setFile(path).toResource("trailingWhitespace/JavaCodeUnformated.test"); + setFile(path).toContent(sourceContent); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("trailingWhitespace/JavaCodeFormated.test"); + assertFile(path).hasContent(targetContent); } } diff --git a/testlib/src/main/resources/replace/JavaCodeFormatted.test b/testlib/src/main/resources/replace/JavaCodeFormatted.test deleted file mode 100644 index 533e74479f..0000000000 --- a/testlib/src/main/resources/replace/JavaCodeFormatted.test +++ /dev/null @@ -1,5 +0,0 @@ -public class Java { - public static void main(String[] args) { - System.out.println("hello mars"); - } -} diff --git a/testlib/src/main/resources/replace/JavaCodeUnformatted.test b/testlib/src/main/resources/replace/JavaCodeUnformatted.test deleted file mode 100644 index 5cab0798ef..0000000000 --- a/testlib/src/main/resources/replace/JavaCodeUnformatted.test +++ /dev/null @@ -1,5 +0,0 @@ -public class Java { - public static void main(String[] args) { - System.out.println("hello world"); - } -} diff --git a/testlib/src/main/resources/trailingWhitespace/JavaCodeFormated.test b/testlib/src/main/resources/trailingWhitespace/JavaCodeFormated.test deleted file mode 100644 index feedbb4ea5..0000000000 --- a/testlib/src/main/resources/trailingWhitespace/JavaCodeFormated.test +++ /dev/null @@ -1,6 +0,0 @@ - -public class Java { - public static void main(String[] args) { - System.out.println("hello"); - } -} diff --git a/testlib/src/main/resources/trailingWhitespace/JavaCodeUnformated.test b/testlib/src/main/resources/trailingWhitespace/JavaCodeUnformated.test deleted file mode 100644 index feedbb4ea5..0000000000 --- a/testlib/src/main/resources/trailingWhitespace/JavaCodeUnformated.test +++ /dev/null @@ -1,6 +0,0 @@ - -public class Java { - public static void main(String[] args) { - System.out.println("hello"); - } -} From b2eee6fc5722223708f83cb6b7cb77ecfd456ccf Mon Sep 17 00:00:00 2001 From: Matthias Balke Date: Fri, 23 Feb 2018 09:35:21 +0100 Subject: [PATCH 6/7] fixed code duplication --- .../spotless/maven/generic/TrimTrailingWhitespace.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java index 3f2a4bdbeb..3993f29705 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java @@ -16,7 +16,7 @@ package com.diffplug.spotless.maven.generic; import com.diffplug.spotless.FormatterStep; -import com.diffplug.spotless.generic.ReplaceRegexStep; +import com.diffplug.spotless.generic.TrimTrailingWhitespaceStep; import com.diffplug.spotless.maven.FormatterStepConfig; import com.diffplug.spotless.maven.FormatterStepFactory; @@ -24,6 +24,6 @@ public class TrimTrailingWhitespace implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { - return ReplaceRegexStep.create("trimTrailingWhitespace", "[ \t]+$", ""); + return TrimTrailingWhitespaceStep.create(); } } From 904761234d72712e7409ca1c73d5c105ff59f8b5 Mon Sep 17 00:00:00 2001 From: Matthias Balke Date: Mon, 26 Feb 2018 07:53:57 +0100 Subject: [PATCH 7/7] updated changelog --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index f4c2adf4ec..ed78ff047c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,8 @@ You might be looking for: ### Version 1.11.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/)) +* Added generic format support for maven-plugin ([#209](https://github.com/diffplug/spotless/pull/209)) + ### Version 1.10.0 - February 15th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.10.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.10.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra))) * LicenseHeaderStep now supports customizing the year range separator in copyright notices. ([#199](https://github.com/diffplug/spotless/pull/199))