Browse Source

SONAR-11621 Implement IgnoreCommand in Xoo plugin

tags/7.7
Benoît Gianinetti 5 years ago
parent
commit
d4dc6786fb

+ 4
- 1
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java View File

@@ -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(

+ 59
- 0
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooIgnoreCommand.java View File

@@ -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");
}
}

+ 8
- 1
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java View File

@@ -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;
}
}

+ 14
- 0
plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java View File

@@ -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();

+ 70
- 0
plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/scm/XooIgnoreCommandTest.java View File

@@ -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;
}
}

Loading…
Cancel
Save