Skip to content

Commit

Permalink
SONARPY-57 Migrate to last SQ LTS 4.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
vilchik-elena authored and Elena Vilchik committed Mar 5, 2015
1 parent d006763 commit d98787e
Show file tree
Hide file tree
Showing 33 changed files with 305 additions and 731 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

<maven.test.redirectTestOutputToFile>true</maven.test.redirectTestOutputToFile>

<sonar.version>3.7</sonar.version>
<sonar.version>4.5.2</sonar.version>
<sslr.version>1.20</sslr.version>

<sonar.pluginClass>org.sonar.plugins.python.PythonPlugin</sonar.pluginClass>
Expand All @@ -87,7 +87,7 @@
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-squid</artifactId>
<artifactId>sonar-deprecated</artifactId>
<version>${sonar.version}</version>
</dependency>
<dependency>
Expand All @@ -114,7 +114,7 @@
<dependency>
<groupId>org.codehaus.sonar.sslr-squid-bridge</groupId>
<artifactId>sslr-squid-bridge</artifactId>
<version>2.4</version>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
package org.sonar.python.checks;

import com.sonar.sslr.api.Grammar;
import org.sonar.check.Cardinality;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.squidbridge.checks.AbstractCommentRegularExpressionCheck;

@Rule(
key = "CommentRegularExpression",
priority = Priority.MAJOR,
cardinality = Cardinality.MULTIPLE)
priority = Priority.MAJOR)
public class CommentRegularExpressionCheck extends AbstractCommentRegularExpressionCheck<Grammar> {

private static final String DEFAULT_REGULAR_EXPRESSION = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;
import org.sonar.squidbridge.checks.AbstractXPathCheck;
import org.sonar.check.Cardinality;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;

@Rule(
key = "XPath",
priority = Priority.MAJOR,
cardinality = Cardinality.MULTIPLE)
priority = Priority.MAJOR)
public class XPathCheck extends AbstractXPathCheck<Grammar> {

private static final String DEFAULT_XPATH_QUERY = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
import com.google.common.collect.Sets;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.sonar.api.rules.AnnotationRuleParser;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.CheckFactory;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleParam;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -68,7 +71,14 @@ public void test() {
ResourceBundle resourceBundle = ResourceBundle.getBundle("org.sonar.l10n.python", Locale.ENGLISH);

Set<String> keys = Sets.newHashSet();
List<Rule> rules = new AnnotationRuleParser().parse("repositoryKey", checks);

ActiveRules activeRules = (new ActiveRulesBuilder())
.build();
CheckFactory checkFactory = new CheckFactory(activeRules);
Collection<Rule> rules = checkFactory
.<Rule>create("repositoryKey")
.addAnnotatedChecks(CheckList.getChecks())
.all();
for (Rule rule : rules) {
assertThat(keys).as("Duplicate key " + rule.getKey()).excludes(rule.getKey());
keys.add(rule.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@
package org.sonar.python.metrics;

import com.google.common.collect.Sets;
import com.sonar.sslr.api.*;
import org.sonar.squidbridge.SquidAstVisitor;
import com.sonar.sslr.api.AstAndTokenVisitor;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.GenericTokenType;
import com.sonar.sslr.api.Grammar;
import com.sonar.sslr.api.Token;
import com.sonar.sslr.api.Trivia;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.resources.File;
import org.sonar.api.resources.Project;
import org.sonar.python.api.PythonMetric;
import org.sonar.python.api.PythonTokenType;
import org.sonar.squidbridge.SquidAstVisitor;

import java.util.List;
import java.util.Set;
Expand All @@ -38,15 +43,15 @@
*/
public class FileLinesVisitor extends SquidAstVisitor<Grammar> implements AstAndTokenVisitor {

private final Project project;
private final FileLinesContextFactory fileLinesContextFactory;

private final Set<Integer> linesOfCode = Sets.newHashSet();
private final Set<Integer> linesOfComments = Sets.newHashSet();
private final FileSystem fileSystem;

public FileLinesVisitor(Project project, FileLinesContextFactory fileLinesContextFactory) {
this.project = project;
public FileLinesVisitor(FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem) {
this.fileLinesContextFactory = fileLinesContextFactory;
this.fileSystem = fileSystem;
}

public void visitToken(Token token) {
Expand All @@ -72,8 +77,8 @@ public void visitToken(Token token) {

@Override
public void leaveFile(AstNode astNode) {
File sonarFile = File.fromIOFile(getContext().getFile(), project);
FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(sonarFile);
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().is(getContext().getFile()));
FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(inputFile);

int fileLength = getContext().peekSourceCode().getInt(PythonMetric.LINES);
for (int line = 1; line <= fileLength; line++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@
*/
package org.sonar.python;

import com.google.common.collect.ImmutableList;
import com.sonar.sslr.api.Grammar;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.python.metrics.FileLinesVisitor;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.squidbridge.api.SourceFile;

import java.io.File;

Expand All @@ -45,30 +43,25 @@ public class FileLinesVisitorTest {

private static final File BASE_DIR = new File("src/test/resources/metrics");

@Mock
private Project project;
@Mock
private FileLinesContextFactory fileLinesContextFactory;
@Mock
private ProjectFileSystem fileSystem;

@Before
public void before() {
MockitoAnnotations.initMocks(this);
when(project.getFileSystem()).thenReturn(fileSystem);
when(fileSystem.getSourceDirs()).thenReturn(ImmutableList.of(BASE_DIR));
}

@Test
public void test() {
File file = new File(BASE_DIR, "file_lines.py");
org.sonar.api.resources.File resource = org.sonar.api.resources.File.fromIOFile(file, project);
FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
FileSystem fileSystem = new DefaultFileSystem();
FileLinesContext fileLinesContext = mock(FileLinesContext.class);
when(fileLinesContextFactory.createFor(resource)).thenReturn(fileLinesContext);

SquidAstVisitor<Grammar> visitor = new FileLinesVisitor(project, fileLinesContextFactory);
File file = new File(BASE_DIR, "file_lines.py");
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().is(file));

when(fileLinesContextFactory.createFor(inputFile)).thenReturn(fileLinesContext);

SquidAstVisitor<Grammar> visitor = new FileLinesVisitor(fileLinesContextFactory, fileSystem);

SourceFile sourceFile = PythonAstScanner.scanSingleFile(file, visitor);
PythonAstScanner.scanSingleFile(file, visitor);
verify(fileLinesContext).setIntValue(CoreMetrics.NCLOC_DATA_KEY, 1, 0);
verify(fileLinesContext).setIntValue(CoreMetrics.NCLOC_DATA_KEY, 2, 1);
verify(fileLinesContext).setIntValue(CoreMetrics.NCLOC_DATA_KEY, 3, 0);
Expand Down
6 changes: 6 additions & 0 deletions sonar-python-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<properties>
<sonar.pluginClass>org.sonar.plugins.python.PythonPlugin</sonar.pluginClass>
<sonar.pluginName>Python</sonar.pluginName>
<!--<sonar.skipDependenciesPackaging>true</sonar.skipDependenciesPackaging>-->
</properties>

<dependencies>
Expand All @@ -33,6 +34,11 @@
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-deprecated</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.sonar.plugins.python.pylint.PylintRuleRepository;
import org.sonar.plugins.python.pylint.PylintSensor;
import org.sonar.plugins.python.pylint.PylintImportSensor;
import org.sonar.plugins.python.xunit.PythonXunitSensor;
import org.sonar.plugins.python.xunit.PythonXUnitSensor;

import java.util.List;

Expand All @@ -50,7 +50,6 @@ public List getExtensions() {
.build(),

Python.class,
PythonSourceImporter.class,
PythonColorizer.class,
PythonCpdMapping.class,

Expand All @@ -66,7 +65,7 @@ public List getExtensions() {
PylintImportSensor.class,
PylintRuleRepository.class,

PythonXunitSensor.class,
PythonXUnitSensor.class,
PythonCoverageSensor.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.scan.filesystem.FileQuery;
import org.sonar.api.scan.filesystem.ModuleFileSystem;
import org.sonar.api.utils.SonarException;
import org.sonar.api.utils.WildcardPattern;

import java.io.File;
Expand All @@ -38,28 +36,29 @@ public abstract class PythonReportSensor implements Sensor {
protected static final Logger LOG = LoggerFactory.getLogger(PythonReportSensor.class);

protected Settings conf = null;
protected ModuleFileSystem fileSystem;
protected FileSystem fileSystem;

public PythonReportSensor(Settings conf, ModuleFileSystem fileSystem) {
public PythonReportSensor(Settings conf, FileSystem fileSystem) {
this.conf = conf;
this.fileSystem = fileSystem;
}

public boolean shouldExecuteOnProject(Project project) {
return !fileSystem.files(FileQuery.onSource().onLanguage(Python.KEY)).isEmpty();
return fileSystem.hasFiles(fileSystem.predicates().hasLanguage(Python.KEY));
}

@Override
public void analyse(Project project, SensorContext context) {
try {
List<File> reports = getReports(conf, fileSystem.baseDir().getPath(), reportPathKey(), defaultReportPath());
processReports(project, context, reports);
processReports(context, reports);
} catch (javax.xml.stream.XMLStreamException e) {
String msg = new StringBuilder()
.append("Cannot feed the data into sonar, details: '")
.append(e)
.append("'")
.toString();
throw new SonarException(msg, e);
.append("Cannot feed the data into sonar, details: '")
.append(e)
.append("'")
.toString();
throw new IllegalStateException(msg, e);
}
}

Expand All @@ -68,12 +67,9 @@ public String toString() {
return getClass().getSimpleName();
}

protected List<File> getReports(Settings conf,
String baseDirPath,
String reportPathPropertyKey,
String defaultReportPath) {
protected List<File> getReports(Settings conf, String baseDirPath, String reportPathPropertyKey, String defaultReportPath) {
String reportPath = conf.getString(reportPathPropertyKey);
if(reportPath == null){
if (reportPath == null) {
reportPath = defaultReportPath;
}

Expand All @@ -83,10 +79,7 @@ protected List<File> getReports(Settings conf,
return scanner.getIncludedFiles();
}

protected void processReports(Project project, SensorContext context, List<File> reports) throws javax.xml.stream.XMLStreamException {
}

protected void handleNoReportsCase(SensorContext context) {
protected void processReports(SensorContext context, List<File> reports) throws javax.xml.stream.XMLStreamException {
}

protected String reportPathKey() {
Expand Down

This file was deleted.

Loading

0 comments on commit d98787e

Please sign in to comment.