From 6bc0f09f336347be7df38ab731ade36a34f08995 Mon Sep 17 00:00:00 2001 From: Elena Vilchik Date: Thu, 14 Nov 2019 15:14:12 +0100 Subject: [PATCH] Add sensor logging message that SonarTS does nothing (#885) --- .../plugin/typescript/EmptyTsSensor.java | 43 ++++++++++++++++ .../plugin/typescript/TypeScriptPlugin.java | 2 + .../plugin/typescript/EmptyTsSensorTest.java | 51 +++++++++++++++++++ .../typescript/TypeScriptPluginTest.java | 2 +- 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 sonarts-sq-plugin/sonar-typescript-plugin/src/main/java/org/sonar/plugin/typescript/EmptyTsSensor.java create mode 100644 sonarts-sq-plugin/sonar-typescript-plugin/src/test/java/org/sonar/plugin/typescript/EmptyTsSensorTest.java diff --git a/sonarts-sq-plugin/sonar-typescript-plugin/src/main/java/org/sonar/plugin/typescript/EmptyTsSensor.java b/sonarts-sq-plugin/sonar-typescript-plugin/src/main/java/org/sonar/plugin/typescript/EmptyTsSensor.java new file mode 100644 index 000000000..9bbf930eb --- /dev/null +++ b/sonarts-sq-plugin/sonar-typescript-plugin/src/main/java/org/sonar/plugin/typescript/EmptyTsSensor.java @@ -0,0 +1,43 @@ +/* + * SonarTS + * Copyright (C) 2017-2019 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.plugin.typescript; + +import org.sonar.api.batch.sensor.Sensor; +import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.batch.sensor.SensorDescriptor; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; + +public class EmptyTsSensor implements Sensor { + + private static final Logger LOG = Loggers.get(EmptyTsSensor.class); + + @Override + public void describe(SensorDescriptor sensorDescriptor) { + sensorDescriptor + .onlyOnLanguage("ts") + .name("SonarTS"); + } + + @Override + public void execute(SensorContext sensorContext) { + LOG.info("Since SonarTS v2.0, TypeScript analysis is performed by SonarJS analyzer v6.0 or later. No TypeScript analysis is performed by SonarTS."); + } +} diff --git a/sonarts-sq-plugin/sonar-typescript-plugin/src/main/java/org/sonar/plugin/typescript/TypeScriptPlugin.java b/sonarts-sq-plugin/sonar-typescript-plugin/src/main/java/org/sonar/plugin/typescript/TypeScriptPlugin.java index aba1a6ec0..b0e44fa67 100644 --- a/sonarts-sq-plugin/sonar-typescript-plugin/src/main/java/org/sonar/plugin/typescript/TypeScriptPlugin.java +++ b/sonarts-sq-plugin/sonar-typescript-plugin/src/main/java/org/sonar/plugin/typescript/TypeScriptPlugin.java @@ -26,5 +26,7 @@ public class TypeScriptPlugin implements Plugin { @Override public void define(Context context) { // This plugin doesn't define any extension, TypeScript analysis is migrated to SonarJS + // EmptyTsSensor is used to log message that this plugin does nothing + context.addExtension(EmptyTsSensor.class); } } diff --git a/sonarts-sq-plugin/sonar-typescript-plugin/src/test/java/org/sonar/plugin/typescript/EmptyTsSensorTest.java b/sonarts-sq-plugin/sonar-typescript-plugin/src/test/java/org/sonar/plugin/typescript/EmptyTsSensorTest.java new file mode 100644 index 000000000..10237470b --- /dev/null +++ b/sonarts-sq-plugin/sonar-typescript-plugin/src/test/java/org/sonar/plugin/typescript/EmptyTsSensorTest.java @@ -0,0 +1,51 @@ +/* + * SonarTS + * Copyright (C) 2017-2019 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.plugin.typescript; + +import java.io.File; +import org.junit.Test; +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.batch.sensor.internal.SensorContextTester; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; + +import static org.assertj.core.api.Assertions.assertThat; + +public class EmptyTsSensorTest { + + @org.junit.Rule + public LogTester logTester = new LogTester(); + + @Test + public void descriptor() { + DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor(); + new EmptyTsSensor().describe(sensorDescriptor); + + assertThat(sensorDescriptor.languages()).containsExactly("ts"); + assertThat(sensorDescriptor.name()).isEqualTo("SonarTS"); + } + + @Test + public void log_message() { + new EmptyTsSensor().execute(SensorContextTester.create(new File(""))); + assertThat(logTester.logs(LoggerLevel.INFO)) + .containsExactly("Since SonarTS v2.0, TypeScript analysis is performed by SonarJS analyzer v6.0 or later. No TypeScript analysis is performed by SonarTS."); + } +} diff --git a/sonarts-sq-plugin/sonar-typescript-plugin/src/test/java/org/sonar/plugin/typescript/TypeScriptPluginTest.java b/sonarts-sq-plugin/sonar-typescript-plugin/src/test/java/org/sonar/plugin/typescript/TypeScriptPluginTest.java index 4e218e7fa..a33978e89 100644 --- a/sonarts-sq-plugin/sonar-typescript-plugin/src/test/java/org/sonar/plugin/typescript/TypeScriptPluginTest.java +++ b/sonarts-sq-plugin/sonar-typescript-plugin/src/test/java/org/sonar/plugin/typescript/TypeScriptPluginTest.java @@ -37,6 +37,6 @@ public void count_extensions() { Plugin.Context context = new Plugin.Context(runtime); Plugin underTest = new TypeScriptPlugin(); underTest.define(context); - assertThat(context.getExtensions()).hasSize(0); + assertThat(context.getExtensions()).hasSize(1); } }