uartois / sonar-golang

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

Fix issue in FunctionFinder #27

Closed Tvli closed 7 years ago

Tvli commented 7 years ago

PR Contribution

This PR fixes the FunctionFinder's problem so that it finds test names correctly for test suites.

Bug description

We use stretchr's testify suite in our go projects, so the test cases in our projects are like the example below:

type ExampleTestSuite struct {
    suite.Suite
    VariableThatShouldStartAtFive int
}

func (suite *ExampleTestSuite) TestExample() {
    suite.Equal(5, suite.VariableThatShouldStartAtFive)
}

The bug is when FunctionFinder scans test files to find all testName -> testFilePath mappings, it finds the wrong function names. For example, when scans the code above, the FunctionFinder thinks the test name is TestSuite) TestExample, but the correct function name is TestExample.

In later step, the GoJunitParser finds function names in the xml file. Then GoJunitParser uses the function name TestExample to find the filePath in the mapping that FunctionFinder provided. Due to the FunctionFinder found the wrong function name, so the GoJunitParser finds filePath to be null. This null breaks the GoTestReportSaver in line 47:

// Due to value.getFile() returns null,  this line throws error
InputFile file = context.fileSystem().inputFile(predicates.hasAbsolutePath(value.getFile()));

We currently work around this issue by renaming our suites to be ExampleSuite so that it doesn't include the Test keyword. However it would be much better to get this problem fixed so that we can still use generic TestSuite names.

Steps to Reproduce

  1. Use test suite in golang project and name the suite as TestSuite
  2. Generate junit test report.
  3. Run sonar-scanner on this project p.s (Only check test metric is enough, since coverage and linter metrics have no problems)

Content of your sonar-project.properties

All default settings

Versions

Server: 6.5 Sonar-Scanner: 3.0.3.778-macosx Go version: 1.8.3 OS: macOS Sierra 10.12.6 gometalinter: 1.2.1

thibaultfalque commented 7 years ago

Hi, Thanks for your PR.

Tvli commented 7 years ago

Thanks for merging.