diff options
Diffstat (limited to 'sonar-css-plugin/src')
4 files changed, 122 insertions, 2 deletions
| diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssPlugin.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssPlugin.java index cd98f25..5300947 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssPlugin.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssPlugin.java @@ -53,6 +53,7 @@ public class CssPlugin implements Plugin {        CssRuleSensor.class,        StylelintCommandProvider.class,        StylelintReportSensor.class, +      MinifiedFilesFilter.class,        PropertyDefinition.builder(FILE_SUFFIXES_KEY)          .defaultValue(FILE_SUFFIXES_DEFVALUE) diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/MinifiedFilesFilter.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/MinifiedFilesFilter.java new file mode 100644 index 0000000..87dff1a --- /dev/null +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/MinifiedFilesFilter.java @@ -0,0 +1,62 @@ +/* + * SonarCSS + * Copyright (C) 2018-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. + */ +package org.sonar.css.plugin; + +import java.io.IOException; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.InputFileFilter; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; + +public class MinifiedFilesFilter implements InputFileFilter { + +  private static final int AVERAGE_LINE_LENGTH_THRESHOLD = 200; + +  private static final Logger LOG = Loggers.get(MinifiedFilesFilter.class); + +  @Override +  public boolean accept(InputFile file) { +    if (!CssLanguage.KEY.equals(file.language())) { +      return true; +    } + +    try { +      boolean isMinified = hasMinifiedFileName(file) || hasExcessiveAverageLineLength(file); +      if (isMinified) { +        LOG.debug("File [" + file.uri() + "] looks like a minified file and will not be analyzed"); +      } +      return !isMinified; + +    } catch (IOException e) { +      throw new IllegalStateException("Failed to read input file", e); +    } +  } + +  private static boolean hasMinifiedFileName(InputFile file) { +    String fileName = file.filename(); +    return fileName.endsWith("-min.css") || fileName.endsWith(".min.css"); +  } + +  private static boolean hasExcessiveAverageLineLength(InputFile file) throws IOException { +    int averageLineLength = file.contents().length() / file.lines(); +    return averageLineLength > AVERAGE_LINE_LENGTH_THRESHOLD; +  } + +} diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssPluginTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssPluginTest.java index 7dfe16b..21a944a 100644 --- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssPluginTest.java +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssPluginTest.java @@ -36,7 +36,7 @@ public class CssPluginTest {      Plugin.Context context = new Plugin.Context(runtime);      Plugin underTest = new CssPlugin();      underTest.define(context); -    assertThat(context.getExtensions()).hasSize(10); +    assertThat(context.getExtensions()).hasSize(11);    }    @Test @@ -45,6 +45,6 @@ public class CssPluginTest {      Plugin.Context context = new Plugin.Context(runtime);      Plugin underTest = new CssPlugin();      underTest.define(context); -    assertThat(context.getExtensions()).hasSize(11); +    assertThat(context.getExtensions()).hasSize(12);    }  } diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MinifiedFilesFilterTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MinifiedFilesFilterTest.java new file mode 100644 index 0000000..fc069e8 --- /dev/null +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MinifiedFilesFilterTest.java @@ -0,0 +1,57 @@ +/* + * SonarCSS + * Copyright (C) 2018-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. + */ +package org.sonar.css.plugin; + +import org.junit.Test; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MinifiedFilesFilterTest { + +  private static final MinifiedFilesFilter MINIFIED_FILES_FILTER = new MinifiedFilesFilter(); + +  @Test +  public void should_exclude_by_name() throws Exception { +    DefaultInputFile jsFile = TestInputFileBuilder.create("", "foo.min.css") +      .setLanguage("css") +      .setContents("short content") +      .build(); +    assertThat(MINIFIED_FILES_FILTER.accept(jsFile)).isFalse(); +  } + +  @Test +  public void should_keep_other_lang() throws Exception { +    DefaultInputFile jsFile = TestInputFileBuilder.create("", "foo.min.css").setLanguage("js").build(); +    assertThat(MINIFIED_FILES_FILTER.accept(jsFile)).isTrue(); +  } + +  @Test +  public void should_exclude_by_content() throws Exception { +    String longContent = new String(new char[500]).replace("\0", "a") + "\n"; + +    DefaultInputFile jsFile = TestInputFileBuilder.create("", "foo.css") +      .setLanguage("css") +      .setContents(longContent) +      .build(); +    assertThat(MINIFIED_FILES_FILTER.accept(jsFile)).isFalse(); +  } +} | 
