Skip to content

Commit

Permalink
SONARPY-189 Metrics "files" and "lines" should not be computed by the…
Browse files Browse the repository at this point in the history
… python plugin (#76)

* SONARPY-189 Metrics "files" and "lines" should not be computed by the python plugin

* SONARPY-189 Remove code which is now useless
  • Loading branch information
vilchik-elena authored and pynicolas committed Jan 10, 2017
1 parent 3708cfe commit 757e077
Show file tree
Hide file tree
Showing 9 changed files with 6 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.sonar.squidbridge.indexer.QueryByType;
import org.sonar.squidbridge.metrics.ComplexityVisitor;
import org.sonar.squidbridge.metrics.CounterVisitor;
import org.sonar.squidbridge.metrics.LinesVisitor;

public final class PythonAstScanner {

Expand Down Expand Up @@ -99,7 +98,6 @@ public static AstScanner<Grammar> create(PythonConfiguration conf, SquidAstVisit
}

private static void setMetrics(AstScanner.Builder<Grammar> builder) {
builder.withSquidAstVisitor(new LinesVisitor<Grammar>(PythonMetric.LINES));
AstNodeType[] complexityAstNodeType = new AstNodeType[]{
// Entry points
PythonGrammar.FUNCDEF,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

public enum PythonMetric implements MetricDef {
FILES,
LINES,
LINES_OF_CODE,
STATEMENTS,
FUNCTIONS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,11 @@ public void leaveFile(AstNode astNode) {
linesOfComments.add(line);
}

int fileLength = getContext().peekSourceCode().getInt(PythonMetric.LINES);
for (int line = 1; line <= fileLength; line++) {
if (linesOfCode.contains(line)) {
fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, 1);
}
if (linesOfComments.contains(line)) {
fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, 1);
}
for (int line : linesOfCode) {
fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, 1);
}
for (int line : linesOfComments) {
fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, 1);
}
fileLinesContext.save();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,14 @@
*/
package org.sonar.python;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.sonar.sslr.api.Grammar;
import java.io.File;
import org.junit.Test;
import org.sonar.python.api.PythonMetric;
import org.sonar.squidbridge.AstScanner;
import org.sonar.squidbridge.api.SourceFile;
import org.sonar.squidbridge.api.SourceProject;
import org.sonar.squidbridge.indexer.QueryByType;

import static org.assertj.core.api.Assertions.assertThat;

public class PythonAstScannerTest {

@Test
public void files() {
AstScanner<Grammar> scanner = PythonAstScanner.create(new PythonConfiguration(Charsets.UTF_8));
scanner.scanFiles(ImmutableList.of(new File("src/test/resources/metrics/lines.py"), new File("src/test/resources/metrics/lines_of_code.py")));
SourceProject project = (SourceProject) scanner.getIndex().search(new QueryByType(SourceProject.class)).iterator().next();
assertThat(project.getInt(PythonMetric.FILES)).isEqualTo(2);
}

@Test
public void lines() {
SourceFile file = PythonAstScanner.scanSingleFile("src/test/resources/metrics/lines.py");
assertThat(file.getInt(PythonMetric.LINES)).isEqualTo(6);
}

@Test
public void statements() {
SourceFile file = PythonAstScanner.scanSingleFile("src/test/resources/metrics/statements.py");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class PythonMetricTest {

@Test
public void test() {
assertThat(PythonMetric.values()).hasSize(8);
assertThat(PythonMetric.values()).hasSize(7);

for (PythonMetric metric : PythonMetric.values()) {
assertThat(metric.getName()).isEqualTo(metric.name());
Expand Down
5 changes: 0 additions & 5 deletions python-squid/src/test/resources/metrics/lines.py

This file was deleted.

5 changes: 0 additions & 5 deletions python-squid/src/test/resources/metrics/lines_of_code.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ private void save(Collection<SourceCode> squidSourceFiles) {
}

private void saveMeasures(InputFile inputFile, SourceFile squidFile) {
saveMetricOnFile(inputFile, CoreMetrics.FILES, squidFile.getInt(PythonMetric.FILES));
saveMetricOnFile(inputFile, CoreMetrics.LINES, squidFile.getInt(PythonMetric.LINES));
saveMetricOnFile(inputFile, CoreMetrics.NCLOC, squidFile.getInt(PythonMetric.LINES_OF_CODE));
saveMetricOnFile(inputFile, CoreMetrics.STATEMENTS, squidFile.getInt(PythonMetric.STATEMENTS));
saveMetricOnFile(inputFile, CoreMetrics.FUNCTIONS, squidFile.getInt(PythonMetric.FUNCTIONS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public void test_execute() {
sensor().execute(context);

String key = "moduleKey:file1.py";
assertThat(context.measure(key, CoreMetrics.FILES).value()).isEqualTo(1);
assertThat(context.measure(key, CoreMetrics.LINES).value()).isEqualTo(25);
assertThat(context.measure(key, CoreMetrics.NCLOC).value()).isEqualTo(22);
assertThat(context.measure(key, CoreMetrics.STATEMENTS).value()).isEqualTo(20);
assertThat(context.measure(key, CoreMetrics.FUNCTIONS).value()).isEqualTo(4);
Expand Down

0 comments on commit 757e077

Please sign in to comment.