uartois / sonar-golang

Sonarqube plugin for the golang language.
GNU Lesser General Public License v3.0
245 stars 32 forks source link

Test files parsed as code #55

Closed kernle32dll closed 6 years ago

kernle32dll commented 6 years ago

Description

I realized today that the content of go test files is counted against the total project lines-of-code. I checked against SonarJava, as I was not sure if this might be desired behavior - but its not.

The inherit problem is that test files are parsed as regular source code. This also has the side effect that gometalinter issues for test files are also shown in SonarQube.

Steps to Reproduce

  1. Write some go code (e.g. foo.go)
  2. Write a test file (e.g. foo_test.go)
  3. Trigger the sonar scanner

Content of your sonar-project.properties

sonar.golint.reportPath=report.xml
sonar.coverage.reportPath=coverage.xml
sonar.test.reportPath=test.xml

sonar.sources=./
sonar.exclusions=sonar-project.properties,report.xml,test.xml,coverage.xml

Log of sonar-scanner related to the plugin

Nothing useful, as no errors happen.

Versions

SonarQube: 6.7.1 (build 35068) SonarScanner: 3.0.3.778 sonar-golang: sonar-golang-plugin-1.2.11-rc-reubentfix (also verified on rc7 and rc8)

Additional Information

I tried to add ./*_test.go to the sonar.exclusions and sonar.tests in sonar-project.properties. I thought, that maybe sonar-go just doesn't correctly pick up the test files (not sure if it can code-wise anyway?). However, that just resulted in a nasty stacktrace. This is a different issue, but this definitely prevents setting a correct expression for distinguishing between code and tests....

ERROR: Error during SonarQube Scanner execution
java.nio.file.InvalidPathException: Illegal char <*> at index 2: ./*_test.go
        at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
        at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
        at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
        at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
        at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
        at sun.nio.fs.AbstractPath.endsWith(AbstractPath.java:48)
        at fr.univartois.sonargo.coverage.CoverageSensor.isAnExcludedPath(CoverageSensor.java:86)
        at fr.univartois.sonargo.coverage.CoverageSensor.lambda$createStream$0(CoverageSensor.java:100)
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:174)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.Iterator.forEachRemaining(Iterator.java:116)
        at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
        at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
        at fr.univartois.sonargo.coverage.CoverageSensor.execute(CoverageSensor.java:108)
        at org.sonar.scanner.sensor.SensorWrapper.analyse(SensorWrapper.java:53)
        at org.sonar.scanner.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:88)
        at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:82)
        at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:68)
        at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:88)
        at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:180)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
        at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:288)
        at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:283)
        at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:261)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
        at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:48)
        at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:84)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
        at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:121)
        at org.sonar.batch.bootstrapper.Batch.doExecuteTask(Batch.java:116)
        at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:111)
        at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:63)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
        at com.sun.proxy.$Proxy0.execute(Unknown Source)
        at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:233)
        at org.sonarsource.scanner.api.EmbeddedScanner.runAnalysis(EmbeddedScanner.java:151)
        at org.sonarsource.scanner.cli.Main.runAnalysis(Main.java:123)
        at org.sonarsource.scanner.cli.Main.execute(Main.java:77)
        at org.sonarsource.scanner.cli.Main.main(Main.java:61)
danielleberre commented 6 years ago

Thanks for pointing that out.

It is indeed the case that we do not filter out files ending with "_test.go" for those metrics (see https://github.com/uartois/sonar-golang/blob/cb6767754e29338baaf816085b587dc5ab22c030/src/main/java/fr/univartois/sonargo/core/metrics/GoMetricSensor.java#L33)

We will fix that asap.

thibaultfalque commented 6 years ago

Please test https://github.com/uartois/sonar-golang/releases/tag/v1.2.11-rc9

kernle32dll commented 6 years ago

Fix confirmed - thanks for the quick response! :-)