aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-css-plugin/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-css-plugin/src/test')
-rw-r--r--sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssLanguageTest.java38
-rw-r--r--sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssPluginTest.java2
-rw-r--r--sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java125
-rw-r--r--sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRulesDefinitionTest.java43
-rw-r--r--sonar-css-plugin/src/test/java/org/sonar/css/plugin/SonarWayProfileTest.java44
-rw-r--r--sonar-css-plugin/src/test/java/org/sonar/css/plugin/StylelintExecutionTest.java44
-rw-r--r--sonar-css-plugin/src/test/java/org/sonar/css/plugin/TestActiveRules.java114
-rw-r--r--sonar-css-plugin/src/test/java/org/sonar/css/plugin/bundle/CssBundleHandlerTest.java55
-rw-r--r--sonar-css-plugin/src/test/resources/.DS_Storebin0 -> 8196 bytes
-rw-r--r--sonar-css-plugin/src/test/resources/bundle/.DS_Storebin0 -> 6148 bytes
-rw-r--r--sonar-css-plugin/src/test/resources/bundle/test-bundle.zipbin0 -> 185 bytes
-rw-r--r--sonar-css-plugin/src/test/resources/executables/mockStylelint.js19
12 files changed, 483 insertions, 1 deletions
diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssLanguageTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssLanguageTest.java
new file mode 100644
index 0000000..cf5c65a
--- /dev/null
+++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssLanguageTest.java
@@ -0,0 +1,38 @@
+/*
+ * 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.config.internal.MapSettings;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CssLanguageTest {
+
+ @Test
+ public void test() throws Exception {
+ MapSettings settings = new MapSettings();
+ settings.setProperty(CssPlugin.FILE_SUFFIXES_KEY, CssPlugin.FILE_SUFFIXES_DEFVALUE);
+ CssLanguage language = new CssLanguage(settings.asConfig());
+ assertThat(language.getKey()).isEqualTo("css");
+ assertThat(language.getName()).isEqualTo("CSS");
+ assertThat(language.getFileSuffixes()).containsOnly(".css", ".less", ".scss");
+ }
+}
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 33bd4dd..1cbade9 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,6 +36,6 @@ public class CssPluginTest {
Plugin.Context context = new Plugin.Context(runtime);
Plugin underTest = new CssPlugin();
underTest.define(context);
- assertThat(context.getExtensions()).hasSize(4);
+ assertThat(context.getExtensions()).hasSize(8);
}
}
diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java
new file mode 100644
index 0000000..a4d5921
--- /dev/null
+++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.InputFile.Type;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.api.batch.rule.CheckFactory;
+import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
+import org.sonar.api.batch.sensor.internal.SensorContextTester;
+import org.sonar.css.plugin.bundle.CssBundleHandler;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CssRuleSensorTest {
+
+ private static CheckFactory checkFactory = new CheckFactory(new TestActiveRules("S4647"));
+ private File BASE_DIR = new File("src/test/resources").getAbsoluteFile();
+
+ @Rule
+ public TemporaryFolder tmpDir = new TemporaryFolder();
+
+ @Test
+ public void test_descriptor() throws Exception {
+ CssRuleSensor sensor = new CssRuleSensor(new CssBundleHandler(), checkFactory, new StylelintExecution());
+ DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor();
+ sensor.describe(sensorDescriptor);
+ assertThat(sensorDescriptor.name()).isEqualTo("SonarCSS Rules");
+ assertThat(sensorDescriptor.languages()).containsOnly("css");
+ assertThat(sensorDescriptor.type()).isEqualTo(Type.MAIN);
+ }
+
+ @Test
+ public void test_execute() throws Exception {
+ SensorContextTester context = SensorContextTester.create(BASE_DIR);
+ context.fileSystem().setWorkDir(tmpDir.getRoot().toPath());
+ DefaultInputFile inputFile = createInputFile(context, "some css content\n on 2 lines", "dir/file.css");
+ TestLinterCommandProvider rulesExecution = TestLinterCommandProvider.nodeScript("/executables/mockStylelint.js", inputFile.absolutePath());
+ CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, rulesExecution);
+ sensor.execute(context);
+
+ assertThat(context.allIssues()).hasSize(1);
+ }
+
+ private static DefaultInputFile createInputFile(SensorContextTester sensorContext, String content, String relativePath) {
+ DefaultInputFile inputFile = new TestInputFileBuilder("moduleKey", relativePath)
+ .setModuleBaseDir(sensorContext.fileSystem().baseDirPath())
+ .setType(Type.MAIN)
+ .setLanguage(CssLanguage.KEY)
+ .setCharset(StandardCharsets.UTF_8)
+ .setContents(content)
+ .build();
+
+ sensorContext.fileSystem().add(inputFile);
+ return inputFile;
+ }
+
+ private static class TestLinterCommandProvider implements LinterCommandProvider {
+
+ private static String nodeExecutable = findNodeExecutable();
+
+ private String[] elements;
+
+ private static String findNodeExecutable() {
+ try {
+ String nodeFromMavenPlugin = "target/node/node";
+ Runtime.getRuntime().exec(nodeFromMavenPlugin);
+ return nodeFromMavenPlugin;
+ } catch (IOException e) {
+ return "node";
+ }
+ }
+
+ private static String resourceScript(String script) {
+ try {
+ return new File(TestLinterCommandProvider.class.getResource(script).toURI()).getAbsolutePath();
+ } catch (URISyntaxException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ static TestLinterCommandProvider nodeScript(String script, String args) {
+ TestLinterCommandProvider testRulesExecution = new TestLinterCommandProvider();
+ testRulesExecution.elements = new String[]{ nodeExecutable, resourceScript(script), args};
+ return testRulesExecution;
+ }
+
+ @Override
+ public String[] commandParts(File deployDestination, File projectBaseDir) {
+ return elements;
+ }
+ }
+
+ private static class TestBundleHandler implements BundleHandler {
+
+ @Override
+ public void deployBundle(File deployDestination) {
+ // do nothing
+ }
+ }
+}
diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRulesDefinitionTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRulesDefinitionTest.java
new file mode 100644
index 0000000..7dffb35
--- /dev/null
+++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRulesDefinitionTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.server.rule.RulesDefinition;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CssRulesDefinitionTest {
+
+ @Test
+ public void test() {
+ CssRulesDefinition rulesDefinition = new CssRulesDefinition();
+ RulesDefinition.Context context = new RulesDefinition.Context();
+ rulesDefinition.define(context);
+ RulesDefinition.Repository repository = context.repository("css");
+
+ assertThat(context.repositories()).hasSize(1);
+
+ assertThat(repository.name()).isEqualTo("SonarAnalyzer");
+ assertThat(repository.language()).isEqualTo("css");
+ assertThat(repository.isExternal()).isEqualTo(false);
+ assertThat(repository.rules()).hasSize(CssRules.getRuleClasses().size());
+ }
+}
diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/SonarWayProfileTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/SonarWayProfileTest.java
new file mode 100644
index 0000000..eba68f5
--- /dev/null
+++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/SonarWayProfileTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
+import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.Context;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SonarWayProfileTest {
+
+ @Test
+ public void should_create_sonar_way_profile() {
+ SonarWayProfile definition = new SonarWayProfile();
+ Context context = new Context();
+ definition.define(context);
+
+ BuiltInQualityProfile profile = context.profile("css", SonarWayProfile.PROFILE_NAME);
+
+ assertThat(profile.language()).isEqualTo(CssLanguage.KEY);
+ assertThat(profile.name()).isEqualTo(SonarWayProfile.PROFILE_NAME);
+ assertThat(profile.rules()).extracting("repoKey").containsOnly(CssRulesDefinition.REPOSITORY_KEY);
+ assertThat(profile.rules()).extracting("ruleKey").contains("S4647");
+ }
+
+}
diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/StylelintExecutionTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/StylelintExecutionTest.java
new file mode 100644
index 0000000..4ca78db
--- /dev/null
+++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/StylelintExecutionTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.File;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class StylelintExecutionTest {
+
+ @Test
+ public void test() throws Exception {
+ StylelintExecution stylelintExecution = new StylelintExecution();
+ File deployDestination = new File("deploy_destination");
+ File baseDir = new File("base_dir");
+ assertThat(stylelintExecution.commandParts(deployDestination, baseDir)).containsExactly(
+ "node",
+ new File(deployDestination, "css-bundle/node_modules/stylelint/bin/stylelint").getAbsolutePath(),
+ baseDir.getAbsolutePath(),
+ "--config",
+ new File(deployDestination, "css-bundle/stylelintconfig.json").getAbsolutePath(),
+ "-f",
+ "json"
+ );
+ }
+}
diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/TestActiveRules.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/TestActiveRules.java
new file mode 100644
index 0000000..76ae3d7
--- /dev/null
+++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/TestActiveRules.java
@@ -0,0 +1,114 @@
+/*
+ * 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.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import javax.annotation.CheckForNull;
+import org.sonar.api.batch.rule.ActiveRule;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.rule.RuleKey;
+
+public class TestActiveRules implements ActiveRules {
+
+ private final List<ActiveRule> activeRules;
+
+ public TestActiveRules(String... activeRules) {
+ this.activeRules = Arrays.stream(activeRules).map(TestActiveRule::new).collect(Collectors.toList());
+ }
+
+ @CheckForNull
+ @Override
+ public ActiveRule find(RuleKey ruleKey) {
+ return null;
+ }
+
+ @Override
+ public Collection<ActiveRule> findAll() {
+ return activeRules;
+ }
+
+ @Override
+ public Collection<ActiveRule> findByRepository(String repository) {
+ return activeRules;
+ }
+
+ @Override
+ public Collection<ActiveRule> findByLanguage(String language) {
+ return activeRules;
+ }
+
+ @CheckForNull
+ @Override
+ public ActiveRule findByInternalKey(String repository, String internalKey) {
+ return null;
+ }
+
+ class TestActiveRule implements ActiveRule {
+
+ final RuleKey ruleKey;
+
+ public TestActiveRule(String key) {
+ ruleKey = RuleKey.of(CssRulesDefinition.REPOSITORY_KEY, key);
+ }
+
+ @Override
+ public RuleKey ruleKey() {
+ return ruleKey;
+ }
+
+ @Override
+ public String severity() {
+ return null;
+ }
+
+ @Override
+ public String language() {
+ return null;
+ }
+
+ @CheckForNull
+ @Override
+ public String param(String key) {
+ return null;
+ }
+
+ @Override
+ public Map<String, String> params() {
+ return Collections.emptyMap();
+ }
+
+ @CheckForNull
+ @Override
+ public String internalKey() {
+ return null;
+ }
+
+ @CheckForNull
+ @Override
+ public String templateRuleKey() {
+ return null;
+ }
+ }
+}
diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/bundle/CssBundleHandlerTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/bundle/CssBundleHandlerTest.java
new file mode 100644
index 0000000..b55d050
--- /dev/null
+++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/bundle/CssBundleHandlerTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.bundle;
+
+import java.io.File;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CssBundleHandlerTest {
+
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private File DEPLOY_DESTINATION;
+
+ @Before
+ public void setUp() throws Exception {
+ DEPLOY_DESTINATION = temporaryFolder.newFolder("deployDestination");
+ }
+
+ @Test
+ public void test() throws Exception {
+ CssBundleHandler bundleHandler = new CssBundleHandler();
+ bundleHandler.bundleLocation = "/bundle/test-bundle.zip";
+ bundleHandler.deployBundle(DEPLOY_DESTINATION);
+
+ assertThat(new File(DEPLOY_DESTINATION, "test-bundle.js").exists()).isTrue();
+ }
+
+}
diff --git a/sonar-css-plugin/src/test/resources/.DS_Store b/sonar-css-plugin/src/test/resources/.DS_Store
new file mode 100644
index 0000000..63066fe
--- /dev/null
+++ b/sonar-css-plugin/src/test/resources/.DS_Store
Binary files differ
diff --git a/sonar-css-plugin/src/test/resources/bundle/.DS_Store b/sonar-css-plugin/src/test/resources/bundle/.DS_Store
new file mode 100644
index 0000000..5008ddf
--- /dev/null
+++ b/sonar-css-plugin/src/test/resources/bundle/.DS_Store
Binary files differ
diff --git a/sonar-css-plugin/src/test/resources/bundle/test-bundle.zip b/sonar-css-plugin/src/test/resources/bundle/test-bundle.zip
new file mode 100644
index 0000000..446e89f
--- /dev/null
+++ b/sonar-css-plugin/src/test/resources/bundle/test-bundle.zip
Binary files differ
diff --git a/sonar-css-plugin/src/test/resources/executables/mockStylelint.js b/sonar-css-plugin/src/test/resources/executables/mockStylelint.js
new file mode 100644
index 0000000..e8c1667
--- /dev/null
+++ b/sonar-css-plugin/src/test/resources/executables/mockStylelint.js
@@ -0,0 +1,19 @@
+#!/usr/bin/env node
+var testFile = process.argv[2];
+
+var result = [
+ {
+ source: testFile,
+
+ warnings: [
+ {
+ text: "some message",
+ line: 2,
+ rule: "color-no-invalid-hex"
+ }
+ ]
+ }
+];
+
+var json = JSON.stringify(result);
+console.log(json);