From 2a5df741db43d5198ec7ca44af4d33a69400b3df Mon Sep 17 00:00:00 2001 From: David RACODON Date: Sat, 18 Jul 2015 23:02:08 +0200 Subject: [PATCH] Issue 5 - Do not rely anymore on Puppet Lint to track trailing whitespaces --- .../sonarqube/puppet/checks/CheckList.java | 1 + .../checks/TrailingWhitespaceCheck.java | 76 +++++++++++++++++++ .../org/sonar/l10n/pp/rules/puppet/S1131.html | 1 + .../checks/TrailingWhitespaceCheckSpec.groovy | 48 ++++++++++++ .../resources/checks/trailingWhitespace.pp | 6 ++ .../iadams/sonarqube/puppet/pplint/rules.xml | 5 +- 6 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 puppet-checks/src/main/java/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheck.java create mode 100644 puppet-checks/src/main/resources/org/sonar/l10n/pp/rules/puppet/S1131.html create mode 100644 puppet-checks/src/test/groovy/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheckSpec.groovy create mode 100644 puppet-checks/src/test/resources/checks/trailingWhitespace.pp diff --git a/puppet-checks/src/main/java/com/iadams/sonarqube/puppet/checks/CheckList.java b/puppet-checks/src/main/java/com/iadams/sonarqube/puppet/checks/CheckList.java index 4e9b2701..3764a537 100644 --- a/puppet-checks/src/main/java/com/iadams/sonarqube/puppet/checks/CheckList.java +++ b/puppet-checks/src/main/java/com/iadams/sonarqube/puppet/checks/CheckList.java @@ -44,6 +44,7 @@ public static List getChecks() { LineLengthCheck.class, ParsingErrorCheck.class, QuotedBooleanCheck.class, + TrailingWhitespaceCheck.class, UserResourceLiteralNameCheck.class, UserResourcePasswordNotSetCheck.class, XPathCheck.class diff --git a/puppet-checks/src/main/java/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheck.java b/puppet-checks/src/main/java/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheck.java new file mode 100644 index 00000000..5e8c2c10 --- /dev/null +++ b/puppet-checks/src/main/java/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheck.java @@ -0,0 +1,76 @@ +/** + * Sonar Puppet Plugin + * The MIT License (MIT) + * + * Copyright (c) 2015 Iain Adams + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.iadams.sonarqube.puppet.checks; + +import com.google.common.io.Files; +import com.iadams.sonarqube.puppet.CharsetAwareVisitor; +import com.sonar.sslr.api.AstNode; +import org.sonar.api.server.rule.RulesDefinition; +import org.sonar.api.utils.SonarException; +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonar.squidbridge.annotations.SqaleConstantRemediation; +import org.sonar.squidbridge.annotations.SqaleSubCharacteristic; +import org.sonar.squidbridge.checks.SquidCheck; +import org.sonar.sslr.parser.LexerlessGrammar; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.List; +import java.util.regex.Pattern; + +@Rule( + key = "S1131", + name = "Lines should not end with trailing whitespaces", + priority = Priority.MINOR, + tags = {Tags.CONVENTION}) +@SqaleSubCharacteristic(RulesDefinition.SubCharacteristics.READABILITY) +@SqaleConstantRemediation("1min") +public class TrailingWhitespaceCheck extends SquidCheck implements CharsetAwareVisitor { + + private static final String WHITESPACE = "\\t\\u000B\\f\\u0020\\u00A0\\uFEFF\\p{Zs}"; + private Charset charset; + + @Override + public void setCharset(Charset charset) { + this.charset = charset; + } + + @Override + public void visitFile(AstNode astNode) { + List lines; + try { + lines = Files.readLines(getContext().getFile(), charset); + } catch (IOException e) { + throw new SonarException(e); + } + for (int i = 0; i < lines.size(); i++) { + String line = lines.get(i); + if (line.length() > 0 && Pattern.matches("[" + WHITESPACE + "]", line.subSequence(line.length() - 1, line.length()))) { + getContext().createLineViolation(this, "Remove the useless trailing whitespaces at the end of this line.", i + 1); + } + } + } +} diff --git a/puppet-checks/src/main/resources/org/sonar/l10n/pp/rules/puppet/S1131.html b/puppet-checks/src/main/resources/org/sonar/l10n/pp/rules/puppet/S1131.html new file mode 100644 index 00000000..1ebb45b4 --- /dev/null +++ b/puppet-checks/src/main/resources/org/sonar/l10n/pp/rules/puppet/S1131.html @@ -0,0 +1 @@ +

Trailing whitespaces are simply useless and should not stay in code. They may generate noise when comparing different versions of the same file. If you encounter issues from this rule, this probably means that you are not using an automated code formatter - which you should if you have the opportunity to do so.

diff --git a/puppet-checks/src/test/groovy/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheckSpec.groovy b/puppet-checks/src/test/groovy/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheckSpec.groovy new file mode 100644 index 00000000..4ebfd017 --- /dev/null +++ b/puppet-checks/src/test/groovy/com/iadams/sonarqube/puppet/checks/TrailingWhitespaceCheckSpec.groovy @@ -0,0 +1,48 @@ +/* + * Sonar Puppet Plugin + * The MIT License (MIT) + * + * Copyright (c) 2015 Iain Adams + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.iadams.sonarqube.puppet.checks + +import com.iadams.sonarqube.puppet.PuppetAstScanner +import org.sonar.squidbridge.api.SourceFile +import org.sonar.squidbridge.checks.CheckMessagesVerifier +import spock.lang.Specification + +class TrailingWhitespaceCheckSpec extends Specification { + + def "validate rule"() { + given: + TrailingWhitespaceCheck check = new TrailingWhitespaceCheck(); + + SourceFile file = PuppetAstScanner.scanSingleFile(new File("src/test/resources/checks/trailingWhitespace.pp"), check); + + expect: + CheckMessagesVerifier.verify(file.getCheckMessages()) + .next().atLine(2).withMessage("Remove the useless trailing whitespaces at the end of this line.") + .next().atLine(3).withMessage("Remove the useless trailing whitespaces at the end of this line.") + .next().atLine(4).withMessage("Remove the useless trailing whitespaces at the end of this line.") + .next().atLine(6).withMessage("Remove the useless trailing whitespaces at the end of this line.") + .noMore(); + } +} diff --git a/puppet-checks/src/test/resources/checks/trailingWhitespace.pp b/puppet-checks/src/test/resources/checks/trailingWhitespace.pp new file mode 100644 index 00000000..6224b82f --- /dev/null +++ b/puppet-checks/src/test/resources/checks/trailingWhitespace.pp @@ -0,0 +1,6 @@ +notice('hello') +notice('hello') +notice('hello') +notice('hello') +/* Comments */ +/* Comments */ diff --git a/sonar-puppet-plugin/src/main/resources/com/iadams/sonarqube/puppet/pplint/rules.xml b/sonar-puppet-plugin/src/main/resources/com/iadams/sonarqube/puppet/pplint/rules.xml index b2bbe5ac..4ecaa8e1 100644 --- a/sonar-puppet-plugin/src/main/resources/com/iadams/sonarqube/puppet/pplint/rules.xml +++ b/sonar-puppet-plugin/src/main/resources/com/iadams/sonarqube/puppet/pplint/rules.xml @@ -51,9 +51,12 @@ file { '/tmp/foo': trailing_whitespace - + This rule is deprecated, use S1131 instead.

+ ]]>
MINOR + DEPRECATED 80chars