diff options
| author | Elena Vilchik | 2018-06-27 15:22:56 +0200 | 
|---|---|---|
| committer | GitHub | 2018-06-27 15:22:56 +0200 | 
| commit | cea9681a811f5bced88e443d5fda14ba15396343 (patch) | |
| tree | 3746b822ead44cf60de9336e6454770710c76960 | |
| parent | bde3e646ffe1b1ba1b513cac042f5c1e379da4ff (diff) | |
| download | sonar-css-cea9681a811f5bced88e443d5fda14ba15396343.tar.bz2 | |
Exclude minified files (#93)
9 files changed, 179 insertions, 4 deletions
| diff --git a/its/plugin/projects/minified-project/src/normal.css b/its/plugin/projects/minified-project/src/normal.css new file mode 100644 index 0000000..60f1eab --- /dev/null +++ b/its/plugin/projects/minified-project/src/normal.css @@ -0,0 +1,3 @@ +body { +  color: red; +} diff --git a/its/plugin/projects/minified-project/src/normal.min.css b/its/plugin/projects/minified-project/src/normal.min.css new file mode 100644 index 0000000..d0e3407 --- /dev/null +++ b/its/plugin/projects/minified-project/src/normal.min.css @@ -0,0 +1,4 @@ +/* content doesn't matter as file excluded based on filename */ +body { +  color: red; +} diff --git a/its/plugin/projects/minified-project/src/oneline.css b/its/plugin/projects/minified-project/src/oneline.css new file mode 100644 index 0000000..072f35f --- /dev/null +++ b/its/plugin/projects/minified-project/src/oneline.css @@ -0,0 +1 @@ +/* Styles */ @import url(extra.css);ul li{list-style:square;margin:2em 20% 15px 0}#content{-webkit-font-smoothing:antialiased;background:url(img/gradient.png);background:linear-gradient(to bottom,red,rgba(255,0,0,0))}@media only screen and (min-width:35em){#content{width:50%}}.item+.item,.item~.item{margin-left:-15px}#buttons .lotsofcontent,#buttons>*,input[type=submit]{border-radius:.3rem}a:focus,a:hover{color:green}blockquote::after,blockquote::before{content:"\201d"} diff --git a/its/plugin/src/test/java/org/sonar/css/its/MinifiedTest.java b/its/plugin/src/test/java/org/sonar/css/its/MinifiedTest.java new file mode 100644 index 0000000..91580bc --- /dev/null +++ b/its/plugin/src/test/java/org/sonar/css/its/MinifiedTest.java @@ -0,0 +1,47 @@ +/* + * 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.its; + +import com.sonar.orchestrator.Orchestrator; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.css.its.Tests.getProjectMeasureAsDouble; + +public class MinifiedTest { + +  private static String PROJECT_KEY = "minified-project"; + +  @ClassRule +  public static Orchestrator orchestrator = Tests.ORCHESTRATOR; + +  @BeforeClass +  public static void prepare() { +    orchestrator.executeBuild(Tests.createScanner(PROJECT_KEY)); +  } + +  @Test +  public void test() { +    assertThat(getProjectMeasureAsDouble("files", PROJECT_KEY)).isEqualTo(1); +  } + +} diff --git a/its/plugin/src/test/java/org/sonar/css/its/Tests.java b/its/plugin/src/test/java/org/sonar/css/its/Tests.java index d94a633..df30c9f 100644 --- a/its/plugin/src/test/java/org/sonar/css/its/Tests.java +++ b/its/plugin/src/test/java/org/sonar/css/its/Tests.java @@ -29,7 +29,6 @@ import java.util.List;  import org.junit.ClassRule;  import org.junit.runner.RunWith;  import org.junit.runners.Suite; -import org.sonar.css.plugin.StylelintReport;  import org.sonarqube.ws.Measures.ComponentWsResponse;  import org.sonarqube.ws.Measures.Measure;  import org.sonarqube.ws.client.HttpConnector; @@ -41,7 +40,8 @@ import org.sonarqube.ws.client.measures.ComponentRequest;  @Suite.SuiteClasses({    MetricsTest.class,    IssuesTest.class, -  StylelintReportTest.class +  StylelintReportTest.class, +  MinifiedTest.class  })  public class Tests { 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(); +  } +} | 
