diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml deleted file mode 100644 index 8838461..0000000 --- a/.github/workflows/maven.yml +++ /dev/null @@ -1,65 +0,0 @@ -# -# Copyright (c) 2017 EditorConfig Maven Plugin -# project contributors as indicated by the @author tags. -# -# 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. -# - -name: Java CI - -on: [pull_request] - -jobs: - linux: - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - java: [ 8, 11, 17 ] - steps: - - uses: actions/checkout@v1 - - - name: Install Java ${{ matrix.java }} - uses: actions/setup-java@v3 - with: - distribution: temurin - java-version: ${{ matrix.java }} - - - name: Build with Maven - run: ./mvnw clean verify -B -ntp -Dinvoker.streamLogs=true - - - name: Display test results - if: failure() - run: cat target/it/rpkg-and-run-tests/build.log target/it/rpkg-and-run-tests2/build.log - - windows: - runs-on: windows-2022 - strategy: - fail-fast: false - matrix: - java: [ 8, 11, 17 ] - steps: - - uses: actions/checkout@v1 - - - name: Install Java ${{ matrix.java }} - uses: actions/setup-java@v3 - with: - distribution: temurin - java-version: ${{ matrix.java }} - - - name: Build with Maven - run: './mvnw clean verify -B -ntp "-Dinvoker.streamLogs=true"' - - - name: Display test results - if: failure() - run: 'type target\it\rpkg-and-run-tests\build.log' diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml new file mode 100644 index 0000000..294bf9e --- /dev/null +++ b/.github/workflows/verify.yml @@ -0,0 +1,78 @@ +# +# Copyright (c) 2017 EditorConfig Maven Plugin +# project contributors as indicated by the @author tags. +# +# 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. +# + +name: verify + +on: [pull_request] + +jobs: + install: + name: Install + runs-on: ubuntu-latest + steps: + + - name: Set up Java + uses: actions/setup-java@v5 + with: + java-version: 17 + distribution: 'temurin' + + - name: Checkout + uses: actions/checkout@v5 + with: + persist-credentials: false + + - name: Cache + uses: actions/cache@v4 + with: + path: ~/.m2/repository + key: pom-tuner-${{ hashFiles('**/pom.xml') }}-${{ github.sha }} + restore-keys: | + pom-tuner-${{ hashFiles('**/pom.xml') }}- + pom-tuner- + + - name: mvn install + shell: bash + run: ./mvnw install -e -B -ntp + + test: + name: Test + needs: install + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + os: [ "ubuntu-latest", "windows-latest", "macOS-latest" ] + # 17 is covered in install above + jdk: [ "8", "11", "21", "25" ] + steps: + + - uses: actions/checkout@v5 + with: + persist-credentials: false + + - uses: actions/setup-java@v5 + with: + java-version: ${{ matrix.jdk }} + distribution: 'temurin' + + - uses: actions/cache@v4 + with: + path: ~/.m2/repository + key: pom-tuner-${{ hashFiles('**/pom.xml') }}-${{ github.sha }} + + - run: ./mvnw verify -e -B -ntp -rf :editorconfig-maven-plugin-test diff --git a/.sdkmanrc b/.sdkmanrc index 1f53a9c..17091ab 100644 --- a/.sdkmanrc +++ b/.sdkmanrc @@ -1,5 +1,5 @@ # Enable auto-env through the sdkman_auto_env config # Add key=value pairs of SDKs to use below -java=8.0.472-tem +java=17.0.17-tem mvnd=1.0.3 maven=3.9.11 \ No newline at end of file diff --git a/editorconfig-maven-plugin/pom.xml b/editorconfig-maven-plugin/pom.xml deleted file mode 100644 index 5eca22f..0000000 --- a/editorconfig-maven-plugin/pom.xml +++ /dev/null @@ -1,203 +0,0 @@ - - - 4.0.0 - - org.ec4j.maven - editorconfig-maven-plugin-parent - 0.1.6-SNAPSHOT - - - editorconfig-maven-plugin - takari-maven-plugin - - EditorConfig Maven Plugin - A Maven plugin for checking whether project files comply with format rules defined in .editorconfig - files and eventually also for fixing the violations - - - 3.3.1 - - - - - - io.takari.maven.plugins - takari-plugin-testing - test - - - - io.takari.maven.plugins - takari-plugin-integration-testing - pom - test - - - - org.apache.maven - maven-plugin-api - provided - - - - org.apache.maven - maven-project - provided - - - - org.apache.maven.plugin-tools - maven-plugin-annotations - provided - - - - org.apache.maven.shared - maven-shared-utils - - - - org.ec4j.linters - editorconfig-lint-api - - - - org.ec4j.linters - editorconfig-linters - - - - org.slf4j - slf4j-api - provided - - - - junit - junit - test - - - - - - - - - org.apache.maven.plugins - maven-plugin-plugin - - - org.apache.maven.plugins - maven-plugin-report-plugin - - - - org.apache.maven.plugins - maven-project-info-reports-plugin - - - - team - ci-management - issue-management - licenses - scm - - - - - - - - - - - - site - - - - - - org.apache.maven.plugins - maven-site-plugin - ${maven-site-plugin.version} - - - default-site - site - - site - - - - - - - font - coderay - style - 2 - ${project.version} - ${project.prerequisites.maven} - ${maven.compiler.target} - - - - - - org.asciidoctor - asciidoctor-maven-plugin - ${asciidoctor-maven-plugin.version} - - - - - - org.apache.maven.plugins - maven-antrun-plugin - ${maven-antrun-plugin.version} - - - site - - run - - - - - - - - - - - - - - - - - - - - - diff --git a/editorconfig-maven-plugin/src/test/java/org/ec4j/maven/EditorConfigMojosTest.java b/editorconfig-maven-plugin/src/test/java/org/ec4j/maven/EditorConfigMojosTest.java deleted file mode 100644 index 712806e..0000000 --- a/editorconfig-maven-plugin/src/test/java/org/ec4j/maven/EditorConfigMojosTest.java +++ /dev/null @@ -1,290 +0,0 @@ -/* - * Copyright (c) 2017 EditorConfig Maven Plugin - * project contributors as indicated by the @author tags. - * - * 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 org.ec4j.maven; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import io.takari.maven.testing.TestResources; -import io.takari.maven.testing.executor.MavenExecution; -import io.takari.maven.testing.executor.MavenExecutionResult; -import io.takari.maven.testing.executor.MavenRuntime; -import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder; -import io.takari.maven.testing.executor.MavenVersions; -import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner; -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(MavenJUnitTestRunner.class) -@MavenVersions({ "3.6.3" }) -public class EditorConfigMojosTest { - - private static final Path basedir = Paths.get(System.getProperty("basedir", ".")); - - @Rule - public final TestResources resources = new TestResources(); - - private final MavenRuntime verifier; - - public EditorConfigMojosTest(MavenRuntimeBuilder runtimeBuilder) throws Exception { - final String logOpt = "-Dorg.slf4j.simpleLogger.log." + EditorConfigCheckMojo.class.getPackage().getName() + "=trace"; - final String[] opts; - if (System.getProperty("java.version").startsWith("1.7.")) { - opts = new String[] { logOpt, "-Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2" }; - } else { - opts = new String[] { logOpt }; - } - - this.verifier = runtimeBuilder // - .withCliOptions(opts) // - .build(); - } - - @Test - public void check() throws Exception { - File projDir = resources.getBasedir("defaults"); - - MavenExecution mavenExec = verifier.forProject(projDir) // - .withCliOption("-X") // debug - .withCliOption("-B") // batch - ; - - mavenExec // - .execute("clean", "verify") // - .assertLogText("[TRACE] Processing file '.editorconfig' using linter org.ec4j.linters.TextLinter") // - .assertLogText("[DEBUG] No formatting violations found in file '.editorconfig'") // - .assertLogText("[TRACE] Processing file 'pom.xml' using linter org.ec4j.linters.TextLinter") // - .assertLogText("[TRACE] Processing file 'pom.xml' using linter org.ec4j.linters.XmlLinter") // - .assertLogText("[DEBUG] No formatting violations found in file 'pom.xml'") // - .assertLogText( - "[TRACE] Processing file 'src/main/java/org/ec4j/maven/it/defaults/App.java' using linter org.ec4j.linters.TextLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[DEBUG] No formatting violations found in file 'src/main/java/org/ec4j/maven/it/defaults/App.java'" - .replace('/', File.separatorChar)) // - .assertLogText( - "[TRACE] Processing file 'src/main/resources/trailing-whitespace.txt' using linter org.ec4j.linters.TextLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[ERROR] src/main/resources/trailing-whitespace.txt@1,7: Delete 2 characters - violates trim_trailing_whitespace = true, reported by org.ec4j.linters.TextLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[TRACE] Processing file 'src/main/resources/indent.xml' using linter org.ec4j.linters.TextLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[TRACE] Processing file 'src/main/resources/indent.xml' using linter org.ec4j.linters.XmlLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[ERROR] src/main/resources/indent.xml@23,5: Delete 1 character - violates indent_style = space, indent_size = 2, reported by org.ec4j.linters.XmlLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[ERROR] src/main/resources/indent.xml@24,3: Delete 2 characters - violates indent_style = space, indent_size = 2, reported by org.ec4j.linters.XmlLinter" - .replace('/', File.separatorChar)) // - .assertLogText("[TRACE] Processing file 'README.adoc' using linter org.ec4j.linters.TextLinter") // - .assertLogText( - "[ERROR] README.adoc@2,1: Delete 2 characters - violates trim_trailing_whitespace = true, reported by org.ec4j.linters.TextLinter") // - .assertLogText("[INFO] Checked 6 files") // - .assertLogText("[INFO] BUILD FAILURE") // - .assertLogText("There are .editorconfig violations. You may want to run") // - .assertLogText(" mvn editorconfig:format") // - .assertLogText("to fix them automagically.") // - ; - } - - @Test - public void encoding() throws Exception { - File projDir = resources.getBasedir("encoding"); - - MavenExecution mavenExec = verifier.forProject(projDir) // - .withCliOption("-X") // debug - .withCliOption("-B") // batch - ; - - MavenExecutionResult result = mavenExec // - .execute("clean", "editorconfig:check") // - .assertErrorFreeLog() // - .assertLogText("[TRACE] Processing file '.editorconfig' using linter org.ec4j.linters.TextLinter") // - .assertLogText("[TRACE] Creating a Resource for path '.editorconfig' with encoding 'UTF-8'") // - .assertLogText("[DEBUG] No formatting violations found in file '.editorconfig'") // - .assertLogText( - "[TRACE] Processing file 'src/main/resources/simplelogger.properties' using linter org.ec4j.linters.TextLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[TRACE] Creating a Resource for path 'src/main/resources/simplelogger.properties' with encoding 'ISO-8859-1'" - .replace('/', File.separatorChar)) // - .assertLogText( - "[DEBUG] No formatting violations found in file 'src/main/resources/simplelogger.properties'" - .replace('/', File.separatorChar)) // - .assertLogText("[INFO] Checked 3 files") // - ; - - } - - @Test - public void format() throws Exception { - File projDir = resources.getBasedir("defaults"); - - MavenExecution mavenExec = verifier.forProject(projDir) // - .withCliOption("-X") // debug - .withCliOption("-B") // batch - ; - - MavenExecutionResult result = mavenExec // - .execute("clean", "editorconfig:format") // - .assertErrorFreeLog() // - .assertLogText("[TRACE] Processing file '.editorconfig' using linter org.ec4j.linters.TextLinter") // - .assertLogText("[DEBUG] No formatting violations found in file '.editorconfig'") // - .assertLogText("[TRACE] Processing file 'pom.xml' using linter org.ec4j.linters.TextLinter") // - .assertLogText("[TRACE] Processing file 'pom.xml' using linter org.ec4j.linters.XmlLinter") // - .assertLogText("[DEBUG] No formatting violations found in file 'pom.xml'") // - .assertLogText( - "[TRACE] Processing file 'src/main/java/org/ec4j/maven/it/defaults/App.java' using linter org.ec4j.linters.TextLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[DEBUG] No formatting violations found in file 'src/main/java/org/ec4j/maven/it/defaults/App.java'" - .replace('/', File.separatorChar)) // - .assertLogText( - "[TRACE] Processing file 'src/main/resources/trailing-whitespace.txt' using linter org.ec4j.linters.TextLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[INFO] src/main/resources/trailing-whitespace.txt@1,7: Delete 2 characters - violates trim_trailing_whitespace = true, reported by org.ec4j.linters.TextLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[TRACE] Processing file 'src/main/resources/indent.xml' using linter org.ec4j.linters.TextLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[TRACE] Processing file 'src/main/resources/indent.xml' using linter org.ec4j.linters.XmlLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[INFO] src/main/resources/indent.xml@23,5: Delete 1 character - violates indent_style = space, indent_size = 2, reported by org.ec4j.linters.XmlLinter" - .replace('/', File.separatorChar)) // - .assertLogText( - "[INFO] src/main/resources/indent.xml@24,3: Delete 2 characters - violates indent_style = space, indent_size = 2, reported by org.ec4j.linters.XmlLinter" - .replace('/', File.separatorChar)) // - .assertLogText("[TRACE] Processing file 'README.adoc' using linter org.ec4j.linters.TextLinter") // - .assertLogText( - "[INFO] README.adoc@2,1: Delete 2 characters - violates trim_trailing_whitespace = true, reported by org.ec4j.linters.TextLinter") // ; - .assertLogText("[INFO] Formatted 3 out of 6 files") // - ; - - final Path actualBaseDir = result.getBasedir().toPath(); - final Path expectedBaseDir = basedir.resolve("src/test/projects/defaults-formatted"); - assertFilesEqual(actualBaseDir, expectedBaseDir, ".editorconfig"); - assertFilesEqual(actualBaseDir, expectedBaseDir, "pom.xml"); - assertFilesEqual(actualBaseDir, expectedBaseDir, "README.adoc"); - assertFilesEqual(actualBaseDir, expectedBaseDir, "src/main/java/org/ec4j/maven/it/defaults/App.java"); - assertFilesEqual(actualBaseDir, expectedBaseDir, "src/main/resources/indent.xml"); - assertFilesEqual(actualBaseDir, expectedBaseDir, "src/main/resources/trailing-whitespace.txt"); - - } - - @Test - public void submodulesProfileless() throws Exception { - File projDir = resources.getBasedir("submodules"); - - MavenExecution mavenExec = verifier.forProject(projDir) // - .withCliOption("-B") // batch - ; - - mavenExec // - .execute("clean", "verify") // - .assertErrorFreeLog() - .assertLogText("[TRACE] Processing file 'good-1.adoc' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText( - "[TRACE] Processing file 'module-1/good-1.adoc' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText( - "[TRACE] Processing file 'module-2/bad.xml' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText("[TRACE] Processing file 'bad.xml' using linter org.ec4j.linters.TextLinter") // - ; - } - - @Test - public void submodulesParallel() throws Exception { - File projDir = resources.getBasedir("submodules"); - - MavenExecution mavenExec = verifier.forProject(projDir) // - .withCliOption("-B") // batch - .withCliOption("-T").withCliOption("2") // 2 threads - ; - - mavenExec // - .execute("clean", "verify") // - .assertErrorFreeLog() - .assertLogText("[TRACE] Processing file 'good-1.adoc' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText( - "[TRACE] Processing file 'module-1/good-1.adoc' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText( - "[TRACE] Processing file 'module-2/bad.xml' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText("[TRACE] Processing file 'bad.xml' using linter org.ec4j.linters.TextLinter") // - ; - } - - @Test - public void submodulesWithModule2() throws Exception { - File projDir = resources.getBasedir("submodules"); - - MavenExecution mavenExec = verifier.forProject(projDir) // - .withCliOption("-B") // batch - .withCliOption("-Pwith-module-2") // - ; - - mavenExec // - .execute("clean", "verify") // - .assertErrorFreeLog() - .assertLogText("[TRACE] Processing file 'good-1.adoc' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText( - "[TRACE] Processing file 'module-1/good-1.adoc' using linter org.ec4j.linters.TextLinter") // - .assertLogText("[TRACE] Processing file 'good.xml' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText( - "[TRACE] Processing file 'module-2/good.xml' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText( - "[TRACE] Processing file 'module-2/bad.xml' using linter org.ec4j.linters.TextLinter") // - .assertNoLogText("[TRACE] Processing file 'bad.xml' using linter org.ec4j.linters.TextLinter") // - ; - } - - @Test - public void excludesFile() throws Exception { - File projDir = resources.getBasedir("excludes-file"); - - MavenExecution mavenExec = verifier.forProject(projDir) // - .withCliOption("-X") // debug - .withCliOption("-B") // batch - ; - - mavenExec // - .execute("clean", "editorconfig:check") // - .assertErrorFreeLog() // - .assertLogText("[DEBUG] Using excludesFile") // - .assertLogText("ignore.txt'") // - ; - } - - private void assertFilesEqual(Path actualBaseDir, Path expectedBaseDir, String relPath) throws IOException { - final String contentActual = new String(Files.readAllBytes(actualBaseDir.resolve(relPath)), - StandardCharsets.UTF_8); - final String contentExpected = new String(Files.readAllBytes(expectedBaseDir.resolve(relPath)), - StandardCharsets.UTF_8); - Assert.assertEquals(relPath, contentExpected, contentActual); - } -} diff --git a/plugin/pom.xml b/plugin/pom.xml new file mode 100644 index 0000000..5e157c7 --- /dev/null +++ b/plugin/pom.xml @@ -0,0 +1,188 @@ + + + 4.0.0 + + org.ec4j.maven + editorconfig-maven-plugin-parent + 0.1.6-SNAPSHOT + + + editorconfig-maven-plugin + maven-plugin + + EditorConfig Maven Plugin + A Maven plugin for checking whether project files comply with + format rules defined in .editorconfig + files and eventually also for fixing the violations + + + 3.3.1 + + + + + + org.apache.maven + maven-plugin-api + provided + + + + org.apache.maven + maven-project + provided + + + + org.apache.maven.plugin-tools + maven-plugin-annotations + provided + + + + org.apache.maven.shared + maven-shared-utils + + + + org.ec4j.linters + editorconfig-lint-api + + + + org.ec4j.linters + editorconfig-linters + + + + org.slf4j + slf4j-api + provided + + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + + + org.apache.maven.plugins + maven-plugin-report-plugin + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + + + + team + ci-management + issue-management + licenses + scm + + + + + + + + + + + + site + + + + + + org.apache.maven.plugins + maven-site-plugin + ${maven-site-plugin.version} + + + default-site + site + + site + + + + + + + font + coderay + style + 2 + ${project.version} + + ${project.prerequisites.maven} + + ${maven.compiler.target} + + + + + + org.asciidoctor + asciidoctor-maven-plugin + ${asciidoctor-maven-plugin.version} + + + + + + org.apache.maven.plugins + maven-antrun-plugin + + + site + + run + + + + + + + + + + + + + + + + + + + + + diff --git a/editorconfig-maven-plugin/src/main/java/org/ec4j/maven/AbstractEditorConfigMojo.java b/plugin/src/main/java/org/ec4j/maven/AbstractEditorConfigMojo.java similarity index 88% rename from editorconfig-maven-plugin/src/main/java/org/ec4j/maven/AbstractEditorConfigMojo.java rename to plugin/src/main/java/org/ec4j/maven/AbstractEditorConfigMojo.java index 3b10a1f..b72b554 100644 --- a/editorconfig-maven-plugin/src/main/java/org/ec4j/maven/AbstractEditorConfigMojo.java +++ b/plugin/src/main/java/org/ec4j/maven/AbstractEditorConfigMojo.java @@ -24,6 +24,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; @@ -59,7 +60,7 @@ */ public abstract class AbstractEditorConfigMojo extends AbstractMojo { - private static final Logger log = LoggerFactory.getLogger(AbstractEditorConfigMojo.class); + protected final Logger log; /** * If set to {@code true}, the class path will be scanned for implementations of {@link Linter} and all @@ -68,7 +69,7 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.0.1 */ @Parameter(property = "editorconfig.addLintersFromClassPath", defaultValue = "true") - protected boolean addLintersFromClassPath; + boolean addLintersFromClassPath = true; /** * The base directory of the current Maven project. @@ -76,7 +77,7 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.0.1 */ @Parameter(defaultValue = "${project.basedir}", required = true, readonly = true) - private File basedir; + File basedir; /** The result of {@code basedir.toPath()} */ protected Path basedirPath; @@ -91,7 +92,7 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.0.1 */ @Parameter(property = "editorconfig.encoding", defaultValue = "${project.build.sourceEncoding}") - protected String encoding; + String encoding; /** * If {@code true} the default exclude patterns (that exclude binary files and other non-source code files, see @@ -101,7 +102,7 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.0.3 */ @Parameter(property = "editorconfig.excludeNonSourceFiles", defaultValue = "true") - protected boolean excludeNonSourceFiles; + boolean excludeNonSourceFiles = true; /** * File patterns to exclude from the set of files to process. The patterns are relative to the current project's @@ -110,7 +111,7 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.0.1 */ @Parameter(property = "editorconfig.excludes") - protected String[] excludes; + List excludes = new ArrayList<>(); /** * If {@code true} the Maven submodule directories of the current project will be prepended to the list of @@ -119,7 +120,7 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.0.3 */ @Parameter(property = "editorconfig.excludeSubmodules", defaultValue = "true") - protected boolean excludeSubmodules; + boolean excludeSubmodules = true; /** * If {@code true} the plugin execution will fail with an error in case no single {@code .editorconfig} property @@ -129,7 +130,7 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.0.1 */ @Parameter(property = "editorconfig.failOnNoMatchingProperties", defaultValue = "true") - protected boolean failOnNoMatchingProperties; + boolean failOnNoMatchingProperties = true; /** * File containing exclude patterns to add to any existing exclude patterns. Empty lines and lines starting with # @@ -138,7 +139,7 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.1.0 */ @Parameter(property = "editorconfig.excludesFile") - protected File excludesFile; + File excludesFile; /** * File patterns to include into the set of files to process. The patterns are relative to the current project's @@ -147,7 +148,7 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.0.1 */ @Parameter(property = "editorconfig.includes", defaultValue = "**") - protected String[] includes; + List includes = Arrays.asList("**"); /** * Set the includes and excludes for the individual {@link Linter}s @@ -155,10 +156,10 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { * @since 0.0.1 */ @Parameter - protected List linters = new ArrayList<>(); + List linters = new ArrayList<>(); @Component - public MavenProject project; + MavenProject project; /** * If {@code true} the execution of the Mojo will be skipped; otherwise the Mojo will be executed. @@ -168,13 +169,20 @@ public abstract class AbstractEditorConfigMojo extends AbstractMojo { @Parameter(property = "editorconfig.skip", defaultValue = "false") private boolean skip; + List modules = new ArrayList<>(); + public AbstractEditorConfigMojo() { + this(LoggerFactory.getLogger(AbstractEditorConfigMojo.class)); + } + + AbstractEditorConfigMojo(Logger log) { super(); + this.log = log; } private LinterRegistry buildLinterRegistry() { final LinterRegistry.Builder linterRegistryBuilder = LinterRegistry.builder() - .log(new Slf4jLintLogger(LoggerFactory.getLogger(LinterRegistry.class))); + .log(new Slf4jLintLogger(log)); if (addLintersFromClassPath) { linterRegistryBuilder.scan(getClass().getClassLoader()); @@ -218,6 +226,17 @@ public void execute() throws MojoExecutionException, MojoFailureException { } this.basedirPath = basedir.toPath(); + if (project != null) { + for (Object m : project.getModules()) { + modules.add((String) m); + } + for (Profile p : project.getModel().getProfiles()) { + for (Object m : p.getModules()) { + modules.add((String) m); + } + } + } + LinterRegistry linterRegistry = buildLinterRegistry(); final String[] includedFiles = scanIncludedFiles(); @@ -305,26 +324,10 @@ private String[] scanIncludedFiles() { } } - if (excludeSubmodules && project != null) { + if (excludeSubmodules) { { - @SuppressWarnings("unchecked") - final List modules = project.getModules(); - if (modules != null) { - for (String module : modules) { - excls.add(module + "/**"); - } - } - } - - final List profiles = project.getModel().getProfiles(); - if (profiles != null) { - for (Profile profile : profiles) { - final List modules = profile.getModules(); - if (modules != null) { - for (String module : modules) { - excls.add(module + "/**"); - } - } + for (String module : modules) { + excls.add(module + "/**"); } } @@ -335,8 +338,8 @@ private String[] scanIncludedFiles() { return scanner.getIncludedFiles(); } - static String[] appendSanitized(String[] input, Collection result) { - if (input == null || input.length == 0) { + static String[] appendSanitized(List input, Collection result) { + if (input == null || input.size() == 0) { return result.toArray(new String[0]); } diff --git a/editorconfig-maven-plugin/src/main/java/org/ec4j/maven/EditorConfigCheckMojo.java b/plugin/src/main/java/org/ec4j/maven/EditorConfigCheckMojo.java similarity index 89% rename from editorconfig-maven-plugin/src/main/java/org/ec4j/maven/EditorConfigCheckMojo.java rename to plugin/src/main/java/org/ec4j/maven/EditorConfigCheckMojo.java index 248cc3e..c7147bb 100644 --- a/editorconfig-maven-plugin/src/main/java/org/ec4j/maven/EditorConfigCheckMojo.java +++ b/plugin/src/main/java/org/ec4j/maven/EditorConfigCheckMojo.java @@ -21,7 +21,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.ec4j.lint.api.ViolationCollector; import org.ec4j.lint.api.ViolationHandler; -import org.slf4j.LoggerFactory; +import org.slf4j.Logger; /** * Checks whether files are formatted according to rules defined in {@code .editorconfig} files. If fomat violations are @@ -42,12 +42,20 @@ public class EditorConfigCheckMojo extends AbstractEditorConfigMojo { * @since 0.0.1 */ @Parameter(property = "editorconfig.failOnFormatViolation", defaultValue = "true") - private boolean failOnFormatViolation; + boolean failOnFormatViolation = true; + + public EditorConfigCheckMojo() { + super(); + } + + EditorConfigCheckMojo(Logger log) { + super(log); + } /** {@inheritDoc} */ @Override protected ViolationHandler createHandler() { return new ViolationCollector(failOnFormatViolation, "mvn editorconfig:format", - new Slf4jLintLogger(LoggerFactory.getLogger(ViolationCollector.class))); + new Slf4jLintLogger(log)); } } diff --git a/editorconfig-maven-plugin/src/main/java/org/ec4j/maven/EditorConfigFormatMojo.java b/plugin/src/main/java/org/ec4j/maven/EditorConfigFormatMojo.java similarity index 88% rename from editorconfig-maven-plugin/src/main/java/org/ec4j/maven/EditorConfigFormatMojo.java rename to plugin/src/main/java/org/ec4j/maven/EditorConfigFormatMojo.java index 4c017e5..80cca2f 100644 --- a/editorconfig-maven-plugin/src/main/java/org/ec4j/maven/EditorConfigFormatMojo.java +++ b/plugin/src/main/java/org/ec4j/maven/EditorConfigFormatMojo.java @@ -21,7 +21,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.ec4j.lint.api.FormattingHandler; import org.ec4j.lint.api.ViolationHandler; -import org.slf4j.LoggerFactory; +import org.slf4j.Logger; /** * Formats a set of files so that they comply with rules defined in {@code .editorconfig} files. @@ -40,7 +40,7 @@ public class EditorConfigFormatMojo extends AbstractEditorConfigMojo { * @since 0.0.1 */ @Parameter(property = "editorconfig.backup", defaultValue = "false") - private boolean backup; + boolean backup; /** * A suffix to append to a file name to create its backup. See also {@link #backup}. @@ -48,13 +48,21 @@ public class EditorConfigFormatMojo extends AbstractEditorConfigMojo { * @since 0.0.1 */ @Parameter(property = "editorconfig.backupSuffix", defaultValue = ".bak") - private String backupSuffix; + String backupSuffix = ".bak"; + + public EditorConfigFormatMojo() { + super(); + } + + EditorConfigFormatMojo(Logger log) { + super(log); + } /** {@inheritDoc} */ @Override protected ViolationHandler createHandler() { return new FormattingHandler(backup, backupSuffix, - new Slf4jLintLogger(LoggerFactory.getLogger(FormattingHandler.class))); + new Slf4jLintLogger(log)); } } diff --git a/editorconfig-maven-plugin/src/main/java/org/ec4j/maven/LinterConfig.java b/plugin/src/main/java/org/ec4j/maven/LinterConfig.java similarity index 100% rename from editorconfig-maven-plugin/src/main/java/org/ec4j/maven/LinterConfig.java rename to plugin/src/main/java/org/ec4j/maven/LinterConfig.java diff --git a/editorconfig-maven-plugin/src/main/java/org/ec4j/maven/Slf4jLintLogger.java b/plugin/src/main/java/org/ec4j/maven/Slf4jLintLogger.java similarity index 100% rename from editorconfig-maven-plugin/src/main/java/org/ec4j/maven/Slf4jLintLogger.java rename to plugin/src/main/java/org/ec4j/maven/Slf4jLintLogger.java diff --git a/editorconfig-maven-plugin/src/site/asciidoc/index.adoc b/plugin/src/site/asciidoc/index.adoc similarity index 100% rename from editorconfig-maven-plugin/src/site/asciidoc/index.adoc rename to plugin/src/site/asciidoc/index.adoc diff --git a/editorconfig-maven-plugin/src/site/resources/css/additional.css b/plugin/src/site/resources/css/additional.css similarity index 100% rename from editorconfig-maven-plugin/src/site/resources/css/additional.css rename to plugin/src/site/resources/css/additional.css diff --git a/editorconfig-maven-plugin/src/site/site.xml b/plugin/src/site/site.xml similarity index 100% rename from editorconfig-maven-plugin/src/site/site.xml rename to plugin/src/site/site.xml diff --git a/pom.xml b/pom.xml index 0dfcb26..beb23c1 100644 --- a/pom.xml +++ b/pom.xml @@ -16,204 +16,213 @@ limitations under the License. --> - - 4.0.0 - - org.ec4j.maven - editorconfig-maven-plugin-parent - 0.1.6-SNAPSHOT - pom - - EditorConfig Maven Plugin Parent - See editorconfig-maven-plugin - - https://github.com/ec4j/editorconfig-maven-plugin - 2017 - - - Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - - - - ppalaga - Peter Palaga - Europe/Prague - - - - - scm:git:git@github.com:ec4j/editorconfig-maven-plugin.git - scm:git:git@github.com:ec4j/editorconfig-maven-plugin.git + + 4.0.0 + + org.ec4j.maven + editorconfig-maven-plugin-parent + 0.1.6-SNAPSHOT + pom + + EditorConfig Maven Plugin Parent + See editorconfig-maven-plugin + https://github.com/ec4j/editorconfig-maven-plugin - head - - - GitHub Issues - https://github.com/ec4j/editorconfig-maven-plugin/issues - - - - ossrh - ${snapshots.repo.url} - - - ossrh - ${releases.repo.url} - - - - - editorconfig-maven-plugin - - - - - - 2.9.2 - 4.13.2 - 3.9.11 - 3.0-alpha-2 - 3.10.2 - 3.4.2 - 0.3.0 - 0.2.1 - 2.0.11 - - - 2.2.5 - 0.7.0 - 2.23.0 - 4.3 - 1.13.9 - 1.9.0 - 3.1.0 - 3.3.2 - 3.11.0 - 3.1.1 - 3.4.1 - 3.1.0 - 3.1.1 - 3.3.0 - 3.12.0 - 3.11.0 - 3.10.2 - 3.4.5 - 3.0.1 - 3.3.1 - 2.0.1 - 3.12.1 - 3.3.0 - 3.5.4 - - UTF-8 - UTF-8 - - - 1.8 - 1.8 - true - - - true - true - - - -Xdoclint:none - - - true - - - false - false - - - - - - - - org.ec4j.linters - editorconfig-linters-bom - ${ec4j-linters.version} - pom - import - - - - io.takari.maven.plugins - takari-plugin-testing - ${takari-plugin-testing.version} - - - - io.takari.maven.plugins - takari-plugin-integration-testing - ${takari-plugin-testing.version} - pom - - - - junit - junit - ${junit.version} - - - - org.apache.maven - maven-plugin-api - ${maven.version} - - - - org.apache.maven - maven-model - ${maven.version} - - - - org.apache.maven - maven-project - ${maven.version.maven-project} - - - - org.apache.maven.plugin-tools - maven-plugin-annotations - ${maven.version.plugin-tools} - - - org.apache.maven.shared - maven-shared-utils - ${maven-shared-utils.version} - - - - org.slf4j - slf4j-api - ${slf4j.version} - - - - - - - - - - - - com.mycila - license-maven-plugin - ${license-maven-plugin.version} - - - - Copyright (c) ${project.inceptionYear} EditorConfig Maven Plugin + 2017 + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + + ppalaga + Peter Palaga + Europe/Prague + + + + + scm:git:git@github.com:ec4j/editorconfig-maven-plugin.git + + scm:git:git@github.com:ec4j/editorconfig-maven-plugin.git + https://github.com/ec4j/editorconfig-maven-plugin + head + + + GitHub Issues + https://github.com/ec4j/editorconfig-maven-plugin/issues + + + + ossrh + ${snapshots.repo.url} + + + ossrh + ${releases.repo.url} + + + + + plugin + test + + + + + + 3.27.6 + 2.21.0 + 5.14.1 + 3.9.11 + 3.0-alpha-2 + 3.15.2 + 3.4.2 + 0.3.0 + 0.2.1 + 2.0.17 + + + 2.2.5 + 0.9.0 + 2.29.0 + 4.3 + 1.12.0 + 3.1.0 + 3.5.0 + 3.14.1 + 3.1.4 + 3.4.1 + 3.2.8 + 3.1.4 + 3.4.2 + 3.12.0 + 3.15.2 + 3.10.2 + 3.9.0 + 3.2.0 + 3.3.1 + 2.2.1 + 3.21.0 + 3.3.1 + 3.5.4 + + UTF-8 + UTF-8 + + + 1.8 + 1.8 + true + + + true + true + + + -Xdoclint:none + + + true + + + false + false + + + + + + + commons-io + commons-io + ${commons-io.version} + + + + org.ec4j.linters + editorconfig-linters-bom + ${ec4j-linters.version} + pom + import + + + + org.junit + junit-bom + ${junit.version} + import + pom + + + + org.apache.maven + maven-plugin-api + ${maven.version} + + + + org.apache.maven + maven-model + ${maven.version} + + + + org.apache.maven + maven-project + ${maven-project.version} + + + + org.apache.maven.plugin-tools + maven-plugin-annotations + ${maven.plugin-tools.version} + + + org.apache.maven.shared + maven-shared-utils + ${maven-shared-utils.version} + + + + org.assertj + assertj-core + ${assertj.version} + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + slf4j-simple + ${slf4j-version} + + + + + + + + + + + + com.mycila + license-maven-plugin + ${license-maven-plugin.version} + + + + Copyright (c) ${project.inceptionYear} EditorConfig Maven Plugin project contributors as indicated by the @author tags. Licensed under the Apache License, Version 2.0 (the "License"); @@ -227,378 +236,349 @@ 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. - - **/*.adoc - **/README.* - **/.gitkeep - .gitattributes/ - .mvn/wrapper/ - .sdkmanrc/ - LICENSE - mvnw - mvnw.cmd - site/** - src/main/antlr4/org/ec4j/linters/xml/XmlLexer.g4 - src/main/antlr4/org/ec4j/linters/xml/XmlParser.g4 - src/test/resources/**/*.txt - src/test/resources/**/*.good - src/test/resources/**/*.bad - src/test/projects/defaults*/**/*.txt - src/test/projects/encoding/**/*.properties - src/test/projects/excludes-file/**/*.txt - - - - - - - - check - - - - - - - io.takari.maven.plugins - takari-lifecycle-plugin - ${takari-lifecycle-plugin.version} - - - ${maven.compiler.source} - jdt - - none - - true - - - - - default-deploy - none - - deploy - - - - - - - net.revelc.code.formatter - formatter-maven-plugin - ${formatter-maven-plugin.version} - - - net.revelc.code - impsort-maven-plugin - ${impsort-maven-plugin.version} - - - - org.apache.maven.plugins - maven-clean-plugin - ${maven-clean-plugin.version} - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - - true - true - - - - - org.apache.maven.plugins - maven-deploy-plugin - ${maven-deploy-plugin.version} - - - true - - - - - org.apache.maven.plugins - maven-enforcer-plugin - ${maven-enforcer-plugin.version} - - - - org.apache.maven.plugins - maven-gpg-plugin - ${maven-gpg-plugin.version} - - - sign-artifacts - verify - - sign - - - - - - - org.apache.maven.plugins - maven-install-plugin - ${maven-install-plugin.version} - - - true - - - - - org.apache.maven.plugins - maven-jar-plugin - ${maven-jar-plugin.version} - - - true - - - - - org.apache.maven.plugins - maven-javadoc-plugin - ${maven-javadoc-plugin.version} - - - - org.apache.maven.plugins - maven-plugin-plugin - ${maven-plugin-plugin.version} - - editorconfig - - - - org.apache.maven.plugins - maven-plugin-report-plugin - ${maven-plugin-report-plugin.version} - - - - org.apache.maven.plugins - maven-project-info-reports-plugin - ${maven-project-info-reports-plugin.version} - - - - org.apache.maven.plugins - maven-release-plugin - ${maven-release-plugin.version} - - @{project.version} - package - deploy site - release - true - - - - - org.apache.maven.plugins - maven-resources-plugin - ${maven-resources-plugin.version} - - - true - - - - - org.apache.maven.plugins - maven-scm-plugin - ${maven-scm-plugin.version} - - - - org.apache.maven.plugins - maven-site-plugin - ${maven-site-plugin.version} - - - default-site - none - - - - - - org.apache.maven.plugins - maven-source-plugin - ${maven-source-plugin.version} - - - true - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - ${project.build.directory} - ${project.version} - ${project.build.sourceEncoding} - true - - - - - - org.sonatype.central - central-publishing-maven-plugin - ${central-publishing-maven-plugin.version} - - central - true - published - 7200 - - - - - - - - - - com.mycila - license-maven-plugin - - - - io.takari.maven.plugins - takari-lifecycle-plugin - true - - - - org.antlr - antlr4-maven-plugin - - - - org.apache.maven.plugins - maven-antrun-plugin - - - - org.apache.maven.plugins - maven-javadoc-plugin - - - default - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - - org.sonatype.central - central-publishing-maven-plugin - true - - - - - - - - java-11+ - - - - net.revelc.code.formatter - formatter-maven-plugin - - eclipse-format.xml - ${format.skip} - - - - format-java - - format - - process-sources - - - - - org.ec4j.core - ec4j-core-build - ${ec4j-core.version} - - - - - net.revelc.code - impsort-maven-plugin - - java.,javax. - ${format.skip} - true - - - - sort-imports - - sort - - process-sources - - - - - - - [11,) - false - - - - - release - - + + **/*.adoc + **/README.* + **/.gitkeep + .gitattributes/ + .mvn/wrapper/ + .sdkmanrc/ + LICENSE + mvnw + mvnw.cmd + site/** + src/test/resources/**/*.txt + src/test/resources/**/*.good + src/test/resources/**/*.bad + + src/test/projects/defaults*/**/*.txt + + src/test/projects/encoding/**/*.properties + + src/test/projects/excludes-file/**/*.txt + + + + + + + + check + + + + + + + net.revelc.code.formatter + formatter-maven-plugin + ${formatter-maven-plugin.version} + + + net.revelc.code + impsort-maven-plugin + ${impsort-maven-plugin.version} + + + + org.apache.maven.plugins + maven-antrun-plugin + ${maven-antrun-plugin.version} + + + + org.apache.maven.plugins + maven-clean-plugin + ${maven-clean-plugin.version} + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + + + org.apache.maven.plugins + maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven-gpg-plugin.version} + + + sign-artifacts + verify + + sign + + + + + + + org.apache.maven.plugins + maven-install-plugin + ${maven-install-plugin.version} + + + + org.apache.maven.plugins + maven-jar-plugin + ${maven-jar-plugin.version} + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + + org.apache.maven.plugins + maven-plugin-plugin + ${maven-plugin-plugin.version} + + editorconfig + + + + org.apache.maven.plugins + maven-plugin-report-plugin + ${maven-plugin-report-plugin.version} + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + ${maven-project-info-reports-plugin.version} + + + + org.apache.maven.plugins + maven-release-plugin + ${maven-release-plugin.version} + + @{project.version} + package + deploy site + release + true + + + + + org.apache.maven.plugins + maven-resources-plugin + ${maven-resources-plugin.version} + + + + org.apache.maven.plugins + maven-scm-plugin + ${maven-scm-plugin.version} + + + + org.apache.maven.plugins + maven-site-plugin + ${maven-site-plugin.version} + + + default-site + none + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + ${project.build.directory} + ${project.version} + + ${project.build.sourceEncoding} + true + + + + + + org.sonatype.central + central-publishing-maven-plugin + ${central-publishing-maven-plugin.version} + + central + true + published + 7200 + + + + + + - - org.apache.maven.plugins - maven-gpg-plugin - - - sign-artifacts - verify - - sign - - - - + + com.mycila + license-maven-plugin + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + default + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + org.sonatype.central + central-publishing-maven-plugin + true + - - - - + + + + + java-17+ + + [17,) + false + + + + + net.revelc.code.formatter + formatter-maven-plugin + + eclipse-format.xml + ${format.skip} + + + + format-java + + format + + process-sources + + + + + org.ec4j.core + ec4j-core-build + ${ec4j-core.version} + + + + + net.revelc.code + impsort-maven-plugin + + java.,javax. + ${format.skip} + true + + + + sort-imports + + sort + + process-sources + + + + + + + + + release + + + + + + org.apache.maven.plugins + maven-gpg-plugin + + + sign-artifacts + verify + + sign + + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + attach-sources + + jar + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + + + ${javadoc.opts} + false + + + + + + + + diff --git a/test/pom.xml b/test/pom.xml new file mode 100644 index 0000000..acc4049 --- /dev/null +++ b/test/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + org.ec4j.maven + editorconfig-maven-plugin-parent + 0.1.6-SNAPSHOT + + + editorconfig-maven-plugin-test + + EditorConfig Maven Plugin - Test + + + + + org.ec4j.maven + editorconfig-maven-plugin + ${project.version} + + + + commons-io + commons-io + test + + + + org.apache.maven + maven-plugin-api + test + + + + org.apache.maven + maven-project + test + + + + org.apache.maven.plugin-tools + maven-plugin-annotations + test + + + + org.slf4j + slf4j-api + test + + + org.slf4j + slf4j-api + test + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.assertj + assertj-core + test + + + + diff --git a/test/src/test/java/org/ec4j/maven/EditorConfigMojosTest.java b/test/src/test/java/org/ec4j/maven/EditorConfigMojosTest.java new file mode 100644 index 0000000..3c1e159 --- /dev/null +++ b/test/src/test/java/org/ec4j/maven/EditorConfigMojosTest.java @@ -0,0 +1,367 @@ +/* + * Copyright (c) 2017 EditorConfig Maven Plugin + * project contributors as indicated by the @author tags. + * + * 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 org.ec4j.maven; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Marker; +import org.slf4j.event.Level; +import org.slf4j.helpers.LegacyAbstractLogger; +import org.slf4j.helpers.MessageFormatter; + +public class EditorConfigMojosTest { + + private static final Path basedir = Paths.get(System.getProperty("basedir", ".")); + + @Test + public void check() throws Exception { + + final Verifier mavenExec = new Verifier<>( + "defaults", + EditorConfigCheckMojo.class, + Arrays.asList("log.txt")); + + mavenExec // + .execute() // + .assertLogText("[TRACE] Processing file '.editorconfig' using linter org.ec4j.linters.TextLinter") // + .assertLogText("[DEBUG] No formatting violations found in file '.editorconfig'") // + .assertLogText("[TRACE] Processing file 'pom.xml' using linter org.ec4j.linters.TextLinter") // + .assertLogText("[TRACE] Processing file 'pom.xml' using linter org.ec4j.linters.XmlLinter") // + .assertLogText("[DEBUG] No formatting violations found in file 'pom.xml'") // + .assertLogText( + "[TRACE] Processing file 'src/main/java/org/ec4j/maven/it/defaults/App.java' using linter org.ec4j.linters.TextLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[DEBUG] No formatting violations found in file 'src/main/java/org/ec4j/maven/it/defaults/App.java'" + .replace('/', File.separatorChar)) // + .assertLogText( + "[TRACE] Processing file 'src/main/resources/trailing-whitespace.txt' using linter org.ec4j.linters.TextLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[ERROR] src/main/resources/trailing-whitespace.txt@1,7: Delete 2 characters - violates trim_trailing_whitespace = true, reported by org.ec4j.linters.TextLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[TRACE] Processing file 'src/main/resources/indent.xml' using linter org.ec4j.linters.TextLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[TRACE] Processing file 'src/main/resources/indent.xml' using linter org.ec4j.linters.XmlLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[ERROR] src/main/resources/indent.xml@23,5: Delete 1 character - violates indent_style = space, indent_size = 2, reported by org.ec4j.linters.XmlLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[ERROR] src/main/resources/indent.xml@24,3: Delete 2 characters - violates indent_style = space, indent_size = 2, reported by org.ec4j.linters.XmlLinter" + .replace('/', File.separatorChar)) // + .assertLogText("[TRACE] Processing file 'README.adoc' using linter org.ec4j.linters.TextLinter") // + .assertLogText( + "[ERROR] README.adoc@2,1: Delete 2 characters - violates trim_trailing_whitespace = true, reported by org.ec4j.linters.TextLinter") // + .assertLogText("[INFO] Checked 6 files") // + .assertLogText("[INFO] BUILD FAILURE") // + .assertLogText("There are .editorconfig violations. You may want to run") // + .assertLogText(" mvn editorconfig:format") // + .assertLogText("to fix them automagically.") // + ; + } + + @Test + public void encoding() throws Exception { + final Verifier mavenExec = new Verifier( + "encoding", + EditorConfigCheckMojo.class, + Arrays.asList("log.txt")); + + mavenExec // + .execute() // + .assertErrorFreeLog() // + .assertLogText("[TRACE] Processing file '.editorconfig' using linter org.ec4j.linters.TextLinter") // + .assertLogText("[TRACE] Creating a Resource for path '.editorconfig' with encoding 'UTF-8'") // + .assertLogText("[DEBUG] No formatting violations found in file '.editorconfig'") // + .assertLogText( + "[TRACE] Processing file 'src/main/resources/simplelogger.properties' using linter org.ec4j.linters.TextLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[TRACE] Creating a Resource for path 'src/main/resources/simplelogger.properties' with encoding 'ISO-8859-1'" + .replace('/', File.separatorChar)) // + .assertLogText( + "[DEBUG] No formatting violations found in file 'src/main/resources/simplelogger.properties'" + .replace('/', File.separatorChar)) // + .assertLogText("[INFO] Checked 3 files") // + ; + + } + + @Test + public void format() throws Exception { + final Verifier mavenExec = new Verifier<>( + "defaults", + EditorConfigFormatMojo.class, + Arrays.asList("log.txt")); + final Path expectedBaseDir = basedir.resolve("src/test/projects/defaults-formatted"); + + mavenExec // + .execute() // + .assertErrorFreeLog() // + .assertLogText("[TRACE] Processing file '.editorconfig' using linter org.ec4j.linters.TextLinter") // + .assertLogText("[DEBUG] No formatting violations found in file '.editorconfig'") // + .assertLogText("[TRACE] Processing file 'pom.xml' using linter org.ec4j.linters.TextLinter") // + .assertLogText("[TRACE] Processing file 'pom.xml' using linter org.ec4j.linters.XmlLinter") // + .assertLogText("[DEBUG] No formatting violations found in file 'pom.xml'") // + .assertLogText( + "[TRACE] Processing file 'src/main/java/org/ec4j/maven/it/defaults/App.java' using linter org.ec4j.linters.TextLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[DEBUG] No formatting violations found in file 'src/main/java/org/ec4j/maven/it/defaults/App.java'" + .replace('/', File.separatorChar)) // + .assertLogText( + "[TRACE] Processing file 'src/main/resources/trailing-whitespace.txt' using linter org.ec4j.linters.TextLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[INFO] src/main/resources/trailing-whitespace.txt@1,7: Delete 2 characters - violates trim_trailing_whitespace = true, reported by org.ec4j.linters.TextLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[TRACE] Processing file 'src/main/resources/indent.xml' using linter org.ec4j.linters.TextLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[TRACE] Processing file 'src/main/resources/indent.xml' using linter org.ec4j.linters.XmlLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[INFO] src/main/resources/indent.xml@23,5: Delete 1 character - violates indent_style = space, indent_size = 2, reported by org.ec4j.linters.XmlLinter" + .replace('/', File.separatorChar)) // + .assertLogText( + "[INFO] src/main/resources/indent.xml@24,3: Delete 2 characters - violates indent_style = space, indent_size = 2, reported by org.ec4j.linters.XmlLinter" + .replace('/', File.separatorChar)) // + .assertLogText("[TRACE] Processing file 'README.adoc' using linter org.ec4j.linters.TextLinter") // + .assertLogText( + "[INFO] README.adoc@2,1: Delete 2 characters - violates trim_trailing_whitespace = true, reported by org.ec4j.linters.TextLinter") // ; + .assertLogText("[INFO] Formatted 3 out of 6 files") // + .assertFilesEqual(expectedBaseDir, + ".editorconfig", + "pom.xml", + "README.adoc", + "src/main/java/org/ec4j/maven/it/defaults/App.java", + "src/main/resources/indent.xml", + "src/main/resources/trailing-whitespace.txt"); + + } + + @Test + public void submodulesProfileless() throws Exception { + + { + final Verifier mavenExec = new Verifier<>( + "submodules", + EditorConfigCheckMojo.class, + Arrays.asList("log.txt")); + mavenExec.mojo.modules = Arrays.asList("module-1", "module-2"); + + mavenExec // + .execute() // + .assertErrorFreeLog() + .assertNoLogText( + "[TRACE] Processing file 'module-1/good-1.adoc' using linter org.ec4j.linters.TextLinter") // + .assertNoLogText( + "[TRACE] Processing file 'module-2/bad.xml' using linter org.ec4j.linters.TextLinter") // + .assertNoLogText("[TRACE] Processing file 'bad.xml' using linter org.ec4j.linters.TextLinter") // + ; + } + { + final Verifier mavenExec = new Verifier<>( + "submodules", + EditorConfigCheckMojo.class, + Arrays.asList("log.txt")); + + mavenExec.mojo.basedir = mavenExec.mojo.basedir.toPath().resolve("module-1").toFile(); + + mavenExec // + .execute() // + .assertErrorFreeLog() + .assertLogText("[TRACE] Processing file 'good-1.adoc' using linter org.ec4j.linters.TextLinter") // + ; + } + } + + // @Test + // public void submodulesWithModule2() throws Exception { + // final Verifier mavenExec = new Verifier<>( + // "submodules", + // EditorConfigCheckMojo.class, + // Arrays.asList("log.txt")); + // // .withCliOption("-Pwith-module-2") // + // + // mavenExec // + // .execute() // + // .assertErrorFreeLog() + // .assertLogText("[TRACE] Processing file 'good-1.adoc' using linter org.ec4j.linters.TextLinter") // + // .assertNoLogText( + // "[TRACE] Processing file 'module-1/good-1.adoc' using linter org.ec4j.linters.TextLinter") // + // .assertLogText("[TRACE] Processing file 'good.xml' using linter org.ec4j.linters.TextLinter") // + // .assertNoLogText( + // "[TRACE] Processing file 'module-2/good.xml' using linter org.ec4j.linters.TextLinter") // + // .assertNoLogText( + // "[TRACE] Processing file 'module-2/bad.xml' using linter org.ec4j.linters.TextLinter") // + // .assertNoLogText("[TRACE] Processing file 'bad.xml' using linter org.ec4j.linters.TextLinter") // + // ; + // } + + @Test + public void excludesFile() throws Exception { + final Verifier mavenExec = new Verifier<>( + "excludes-file", + EditorConfigCheckMojo.class, + Arrays.asList("log.txt")); + + Path ignoreTxt = mavenExec.mojo.basedir.toPath().resolve("ignore.txt"); + mavenExec.mojo.excludesFile = ignoreTxt.toFile(); + + mavenExec // + .execute() // + .assertErrorFreeLog() // + .assertLogText("[DEBUG] Using excludesFile '" + ignoreTxt + "'"); + } + + static class Verifier { + private final T mojo; + private final LogRecorder logger; + + public Verifier( + String projectDir, + Class mojoClass, + List excludes) throws IOException { + + final Path testingSrcDir = Paths.get("src/test/projects/" + projectDir); + final Path testingProjectDir = Paths.get("target/" + projectDir + "-" + UUID.randomUUID()); + FileUtils.copyDirectory(testingSrcDir.toFile(), testingProjectDir.toFile()); + logger = new LogRecorder(testingProjectDir); + if (mojoClass == EditorConfigCheckMojo.class) { + mojo = (T) new EditorConfigCheckMojo(logger); + } else if (mojoClass == EditorConfigFormatMojo.class) { + mojo = (T) new EditorConfigFormatMojo(logger); + } else { + throw new IllegalStateException("Unexpected mojo type " + mojoClass); + } + mojo.encoding = StandardCharsets.UTF_8.name(); + mojo.basedir = testingProjectDir.toAbsolutePath().normalize().toFile(); + mojo.excludes = excludes; + } + + public LogRecorder execute() { + try { + mojo.execute(); + } catch (MojoExecutionException e) { + logger.info("BUILD ERROR"); + for (String line : e.getMessage().split("[\r\n]+")) { + logger.messages.add(line); + } + } catch (MojoFailureException e) { + logger.info("BUILD FAILURE"); + for (String line : e.getMessage().split("[\r\n]+")) { + logger.messages.add(line); + } + } + return logger; + } + + } + + static class LogRecorder extends LegacyAbstractLogger { + + final List messages = new CopyOnWriteArrayList<>(); + + private final Path basedir; + + public LogRecorder(Path basedir) { + this.basedir = basedir; + } + + public LogRecorder assertNoLogText(String message) { + if (messages.contains(message)) { + throw new AssertionError("The log should not contain\n\n " + message + "\n\nbut contains\n\n " + + messages.stream().collect(Collectors.joining("\n ")) + "\n."); + } + return this; + } + + public LogRecorder assertErrorFreeLog() { + final String errors = messages.stream().filter(s -> s.startsWith("[ERROR] ")).collect(Collectors.joining("\n ")); + if (!errors.isEmpty()) { + throw new AssertionError("The log should not contain any errors but contains:\n\n " + + errors + "\n."); + } + return this; + } + + public LogRecorder assertFilesEqual(Path expectedDirectory, String... relativePaths) { + for (String relPath : relativePaths) { + Assertions.assertThat(basedir.resolve(relPath)).hasSameTextualContentAs(expectedDirectory.resolve(relPath)); + } + return this; + } + + public LogRecorder assertLogText(String message) { + + if (!messages.contains(message)) { + throw new AssertionError("The log should contain\n\n " + message + "\n\nbut contains only\n\n " + + messages.stream().collect(Collectors.joining("\n ")) + "\n."); + } + return this; + } + + public boolean isTraceEnabled() { + return true; + } + + public boolean isDebugEnabled() { + return true; + } + + public boolean isInfoEnabled() { + return true; + } + + public boolean isWarnEnabled() { + return true; + } + + public boolean isErrorEnabled() { + return true; + } + + protected void handleNormalizedLoggingCall(Level level, Marker marker, String msg, Object[] args, Throwable throwable) { + messages.add(String.format("[%s] %s", level, MessageFormatter.arrayFormat(msg, args).getMessage().trim())); + } + + @Override + protected String getFullyQualifiedCallerName() { + return null; + } + } + +} diff --git a/editorconfig-maven-plugin/src/test/projects/defaults-formatted/.editorconfig b/test/src/test/projects/defaults-formatted/.editorconfig similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults-formatted/.editorconfig rename to test/src/test/projects/defaults-formatted/.editorconfig diff --git a/editorconfig-maven-plugin/src/test/projects/defaults-formatted/README.adoc b/test/src/test/projects/defaults-formatted/README.adoc similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults-formatted/README.adoc rename to test/src/test/projects/defaults-formatted/README.adoc diff --git a/editorconfig-maven-plugin/src/test/projects/defaults-formatted/pom.xml b/test/src/test/projects/defaults-formatted/pom.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults-formatted/pom.xml rename to test/src/test/projects/defaults-formatted/pom.xml diff --git a/editorconfig-maven-plugin/src/test/projects/defaults-formatted/src/main/java/org/ec4j/maven/it/defaults/App.java b/test/src/test/projects/defaults-formatted/src/main/java/org/ec4j/maven/it/defaults/App.java similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults-formatted/src/main/java/org/ec4j/maven/it/defaults/App.java rename to test/src/test/projects/defaults-formatted/src/main/java/org/ec4j/maven/it/defaults/App.java diff --git a/editorconfig-maven-plugin/src/test/projects/defaults-formatted/src/main/resources/indent.xml b/test/src/test/projects/defaults-formatted/src/main/resources/indent.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults-formatted/src/main/resources/indent.xml rename to test/src/test/projects/defaults-formatted/src/main/resources/indent.xml diff --git a/editorconfig-maven-plugin/src/test/projects/defaults-formatted/src/main/resources/trailing-whitespace.txt b/test/src/test/projects/defaults-formatted/src/main/resources/trailing-whitespace.txt similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults-formatted/src/main/resources/trailing-whitespace.txt rename to test/src/test/projects/defaults-formatted/src/main/resources/trailing-whitespace.txt diff --git a/editorconfig-maven-plugin/src/test/projects/defaults/.editorconfig b/test/src/test/projects/defaults/.editorconfig similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults/.editorconfig rename to test/src/test/projects/defaults/.editorconfig diff --git a/editorconfig-maven-plugin/src/test/projects/defaults/README.adoc b/test/src/test/projects/defaults/README.adoc similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults/README.adoc rename to test/src/test/projects/defaults/README.adoc diff --git a/editorconfig-maven-plugin/src/test/projects/defaults/pom.xml b/test/src/test/projects/defaults/pom.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults/pom.xml rename to test/src/test/projects/defaults/pom.xml diff --git a/editorconfig-maven-plugin/src/test/projects/defaults/src/main/java/org/ec4j/maven/it/defaults/App.java b/test/src/test/projects/defaults/src/main/java/org/ec4j/maven/it/defaults/App.java similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults/src/main/java/org/ec4j/maven/it/defaults/App.java rename to test/src/test/projects/defaults/src/main/java/org/ec4j/maven/it/defaults/App.java diff --git a/editorconfig-maven-plugin/src/test/projects/defaults/src/main/resources/indent.xml b/test/src/test/projects/defaults/src/main/resources/indent.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults/src/main/resources/indent.xml rename to test/src/test/projects/defaults/src/main/resources/indent.xml diff --git a/editorconfig-maven-plugin/src/test/projects/defaults/src/main/resources/trailing-whitespace.txt b/test/src/test/projects/defaults/src/main/resources/trailing-whitespace.txt similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/defaults/src/main/resources/trailing-whitespace.txt rename to test/src/test/projects/defaults/src/main/resources/trailing-whitespace.txt diff --git a/editorconfig-maven-plugin/src/test/projects/encoding/.editorconfig b/test/src/test/projects/encoding/.editorconfig similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/encoding/.editorconfig rename to test/src/test/projects/encoding/.editorconfig diff --git a/editorconfig-maven-plugin/src/test/projects/encoding/pom.xml b/test/src/test/projects/encoding/pom.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/encoding/pom.xml rename to test/src/test/projects/encoding/pom.xml diff --git a/editorconfig-maven-plugin/src/test/projects/encoding/src/main/resources/simplelogger.properties b/test/src/test/projects/encoding/src/main/resources/simplelogger.properties similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/encoding/src/main/resources/simplelogger.properties rename to test/src/test/projects/encoding/src/main/resources/simplelogger.properties diff --git a/editorconfig-maven-plugin/src/test/projects/excludes-file/.editorconfig b/test/src/test/projects/excludes-file/.editorconfig similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/excludes-file/.editorconfig rename to test/src/test/projects/excludes-file/.editorconfig diff --git a/editorconfig-maven-plugin/src/test/projects/excludes-file/README.adoc b/test/src/test/projects/excludes-file/README.adoc similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/excludes-file/README.adoc rename to test/src/test/projects/excludes-file/README.adoc diff --git a/editorconfig-maven-plugin/src/test/projects/excludes-file/ignore.txt b/test/src/test/projects/excludes-file/ignore.txt similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/excludes-file/ignore.txt rename to test/src/test/projects/excludes-file/ignore.txt diff --git a/editorconfig-maven-plugin/src/test/projects/excludes-file/pom.xml b/test/src/test/projects/excludes-file/pom.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/excludes-file/pom.xml rename to test/src/test/projects/excludes-file/pom.xml diff --git a/editorconfig-maven-plugin/src/test/projects/submodules/.editorconfig b/test/src/test/projects/submodules/.editorconfig similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/submodules/.editorconfig rename to test/src/test/projects/submodules/.editorconfig diff --git a/editorconfig-maven-plugin/src/test/projects/submodules/module-1/good-1.adoc b/test/src/test/projects/submodules/module-1/good-1.adoc similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/submodules/module-1/good-1.adoc rename to test/src/test/projects/submodules/module-1/good-1.adoc diff --git a/editorconfig-maven-plugin/src/test/projects/submodules/module-1/pom.xml b/test/src/test/projects/submodules/module-1/pom.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/submodules/module-1/pom.xml rename to test/src/test/projects/submodules/module-1/pom.xml diff --git a/editorconfig-maven-plugin/src/test/projects/submodules/module-2/bad.xml b/test/src/test/projects/submodules/module-2/bad.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/submodules/module-2/bad.xml rename to test/src/test/projects/submodules/module-2/bad.xml diff --git a/editorconfig-maven-plugin/src/test/projects/submodules/module-2/good.xml b/test/src/test/projects/submodules/module-2/good.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/submodules/module-2/good.xml rename to test/src/test/projects/submodules/module-2/good.xml diff --git a/editorconfig-maven-plugin/src/test/projects/submodules/module-2/pom.xml b/test/src/test/projects/submodules/module-2/pom.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/submodules/module-2/pom.xml rename to test/src/test/projects/submodules/module-2/pom.xml diff --git a/editorconfig-maven-plugin/src/test/projects/submodules/pom.xml b/test/src/test/projects/submodules/pom.xml similarity index 100% rename from editorconfig-maven-plugin/src/test/projects/submodules/pom.xml rename to test/src/test/projects/submodules/pom.xml