Uploaded image for project: 'SonarSlang'
  1. SonarSlang
  2. SONARSLANG-451

[Go] - Fix coverage to match "go test" (replaces sonar-go/issues/340)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.8
    • Component/s: Go
    • Labels:
      None

      Description

      SonarGo coverage and go test coverage show different results. We should match go test coverage to avoid confusion.

      Let's take the following code as example:

      // hello.go
      package hello
      
      import "rsc.io/quote"
      import "fmt"
      
      func Hello() string {
      
          fmt.Printf(
              "Hello")
      
          return quote.Hello()
      }
      
      func fn(myvar int) int {
          myvar =
              42
      	
          return myvar
      }
      
      // hello_test.go
      package hello
      
      import "testing"
      
      func TestHello(t *testing.T) {
          want := "Hello, world."
          if got := Hello(); got != want {
              t.Errorf("Hello() = %q, want %q", got, want)
          }
      }
      

      Let's first run go test

      $ go test -coverprofile=coverage.out
      HelloPASS
      coverage: 50.0% of statements
      ok  	example.com/hello	0.006s
      

      If use gocov and gocov-xml to convert the output (gocov convert coverage.out | gocov-xml > coverage.xml) we see that it counts 4 lines to cover and 2 lines uncovered (50% coverage) It counts only one line for statements split on multiple lines.

      SonarGo on the other hand will count 9 lines to cover and 4 lines uncovered (55.6% coverage):

      • including empty lines in Hello and fn.
      • counting multiple lines when a statement is split on multiple lines: fmt.Printf( \n "Hello") and myvar = \n 42

      This ticket replaces https://github.com/SonarSource/sonar-go/issues/340 from the old sonar-go repository

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              costin.zaharia Costin Zaharia
              Reporter:
              nicolas.harraudeau Nicolas Harraudeau (Inactive)
              Votes:
              4 Vote for this issue
              Watchers:
              8 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: