diff options
author | BenoƮt Gianinetti <benoit.gianinetti@sonarsource.com> | 2019-01-14 15:48:20 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-01-22 20:21:02 +0100 |
commit | d4dc6786fb05dec7e415af2c13ba27669d26d6b0 (patch) | |
tree | 885e6524a30f23a856e076f46e32d23ab155e1de | |
parent | 93ae9e4354431a59c44a26350b70691cb9a6de3d (diff) | |
download | sonarqube-d4dc6786fb05dec7e415af2c13ba27669d26d6b0.tar.gz sonarqube-d4dc6786fb05dec7e415af2c13ba27669d26d6b0.zip |
SONAR-11621 Implement IgnoreCommand in Xoo plugin
5 files changed, 155 insertions, 2 deletions
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java index 8817e5575ec..8378ddd5ab4 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java @@ -74,6 +74,7 @@ import org.sonar.xoo.rule.XooFakeImporterWithMessages; import org.sonar.xoo.rule.XooRulesDefinition; import org.sonar.xoo.rule.XooSonarWayProfile; import org.sonar.xoo.scm.XooBlameCommand; +import org.sonar.xoo.scm.XooIgnoreCommand; import org.sonar.xoo.scm.XooScmProvider; import org.sonar.xoo.test.CoveragePerTestSensor; import org.sonar.xoo.test.TestExecutionSensor; @@ -174,7 +175,9 @@ public class XooPlugin implements Plugin { context.addExtension(DeprecatedGlobalSensor.class); } if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 6))) { - context.addExtension(GlobalProjectSensor.class); + context.addExtensions( + GlobalProjectSensor.class, + XooIgnoreCommand.class); } if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 2))) { context.addExtensions( diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooIgnoreCommand.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooIgnoreCommand.java new file mode 100644 index 00000000000..3ee3f9707ca --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooIgnoreCommand.java @@ -0,0 +1,59 @@ +/* + * SonarQube + * Copyright (C) 2009-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.xoo.scm; + +import java.io.File; +import java.nio.file.Path; +import org.apache.commons.io.FilenameUtils; +import org.sonar.api.batch.scm.IgnoreCommand; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; + +/** + * To ignore a file simply add an empty file with the same name as the file to ignore with a .ignore suffix. + * E.g. to ignore src/foo.xoo create the file src/foo.xoo.ignore + */ +public class XooIgnoreCommand implements IgnoreCommand { + + static final String IGNORE_FILE_EXTENSION = ".ignore"; + private static final Logger LOG = Loggers.get(XooIgnoreCommand.class); + private boolean isInit; + + @Override + public boolean isIgnored(Path path) { + if (!isInit) { + throw new IllegalStateException("Called init() first"); + } + String fullPath = FilenameUtils.getFullPath(path.toAbsolutePath().toString()); + File scmIgnoreFile = new File(fullPath, path.getFileName() + IGNORE_FILE_EXTENSION); + return scmIgnoreFile.exists(); + } + + @Override + public void init(Path baseDir) { + isInit = true; + LOG.debug("Init IgnoreCommand on dir '{}'"); + } + + @Override + public void clean() { + LOG.debug("Clean IgnoreCommand"); + } +} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java index 3cf80922f97..856e6c829f9 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java @@ -20,6 +20,7 @@ package org.sonar.xoo.scm; import org.sonar.api.batch.scm.BlameCommand; +import org.sonar.api.batch.scm.IgnoreCommand; import org.sonar.api.batch.scm.ScmProvider; import java.io.File; @@ -27,9 +28,11 @@ import java.io.File; public class XooScmProvider extends ScmProvider { private final XooBlameCommand blame; + private XooIgnoreCommand ignore; - public XooScmProvider(XooBlameCommand blame) { + public XooScmProvider(XooBlameCommand blame, XooIgnoreCommand ignore) { this.blame = blame; + this.ignore = ignore; } @Override @@ -47,4 +50,8 @@ public class XooScmProvider extends ScmProvider { return blame; } + @Override + public IgnoreCommand ignoreCommand() { + return ignore; + } } diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java index 27bfe063894..aac7bbc2ef6 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java @@ -27,8 +27,11 @@ import org.sonar.api.SonarRuntime; import org.sonar.api.internal.PluginContextImpl; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.utils.Version; +import org.sonar.xoo.global.GlobalProjectSensor; import org.sonar.xoo.rule.OneExternalIssuePerLineSensor; import org.sonar.xoo.rule.XooBuiltInQualityProfilesDefinition; +import org.sonar.xoo.scm.XooBlameCommand; +import org.sonar.xoo.scm.XooIgnoreCommand; import static org.assertj.core.api.Assertions.assertThat; @@ -74,6 +77,17 @@ public class XooPluginTest { .contains(OneExternalIssuePerLineSensor.class); } + @Test + public void provide_extensions_for_7_6() { + SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("7.6"), SonarQubeSide.SCANNER); + Plugin.Context context = new PluginContextImpl.Builder().setSonarRuntime(runtime).build(); + new XooPlugin().define(context); + assertThat(getExtensions(context)) + .hasSize(58) + .contains(GlobalProjectSensor.class) + .contains(XooIgnoreCommand.class); + } + @SuppressWarnings("unchecked") private static List<Object> getExtensions(Plugin.Context context) { return (List<Object>) context.getExtensions(); diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/scm/XooIgnoreCommandTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/scm/XooIgnoreCommandTest.java new file mode 100644 index 00000000000..615c8b40fd4 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/scm/XooIgnoreCommandTest.java @@ -0,0 +1,70 @@ +/* + * SonarQube + * Copyright (C) 2009-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.xoo.scm; + +import java.io.File; +import java.io.IOException; +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.*; +import static org.sonar.xoo.scm.XooIgnoreCommand.IGNORE_FILE_EXTENSION; + +public class XooIgnoreCommandTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private File baseDir; + + @Before + public void prepare() throws IOException { + baseDir = temp.newFolder(); + } + + @Test + public void testBlame() throws IOException { + File source = newFile("foo.xoo", false); + File source1 = newFile("foo2.xoo", true); + + XooIgnoreCommand ignoreCommand = new XooIgnoreCommand(); + ignoreCommand.init(baseDir.toPath()); + + assertThat(ignoreCommand.isIgnored(source.toPath())).isFalse(); + assertThat(ignoreCommand.isIgnored(source1.toPath())).isTrue(); + } + + private File newFile(String name, boolean isIgnored) throws IOException { + File source = new File(baseDir, name); + source.createNewFile(); + if (isIgnored) { + File ignoredMetaFile = new File(baseDir, name + IGNORE_FILE_EXTENSION); + ignoredMetaFile.createNewFile(); + } + + return source; + } +} |